builtin-notes: Add "add" subcommand for adding notes to objects

"git notes add" is identical to "git notes edit" except that instead of
editing existing notes for a given object, you can only add notes to an
object that currently has none. If "git notes add" finds existing notes
for the given object, the addition is aborted. However, if the new
-f/--force option is used, "git notes add" will _overwrite_ the existing
notes with the new notes contents.

If there is no existing notes for the given object. "git notes add" is
identical to "git notes edit" (i.e. it adds a new note).

The patch includes tests verifying correct behaviour of the new subcommand.

Suggested-by: Joey Hess <joey@kitenet.net>
Improved-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johan Herland
2010-02-13 22:28:32 +01:00
committed by Junio C Hamano
parent ba20f15e0a
commit 7aa4754e55
3 changed files with 74 additions and 22 deletions

View File

@ -19,6 +19,7 @@
static const char * const git_notes_usage[] = {
"git notes [list [<object>]]",
"git notes add [-f] [-m <msg> | -F <file>] [<object>]",
"git notes edit [-m <msg> | -F <file>] [<object>]",
"git notes show [<object>]",
"git notes remove [<object>]",
@ -211,7 +212,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
const char *object_ref;
char logmsg[100];
int list = 0, edit = 0, show = 0, remove = 0, prune = 0;
int list = 0, add = 0, edit = 0, show = 0, remove = 0, prune = 0,
force = 0;
int given_object;
const char *msgfile = NULL;
struct msg_arg msg = { 0, STRBUF_INIT };
@ -220,6 +222,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
OPT_CALLBACK('m', "message", &msg, "msg",
"note contents as a string", parse_msg_arg),
OPT_FILENAME('F', "file", &msgfile, "note contents in a file"),
OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
OPT_END()
};
@ -229,6 +232,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
if (argc && !strcmp(argv[0], "list"))
list = 1;
else if (argc && !strcmp(argv[0], "add"))
add = 1;
else if (argc && !strcmp(argv[0], "edit"))
edit = 1;
else if (argc && !strcmp(argv[0], "show"))
@ -240,10 +245,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
else if (!argc)
list = 1; /* Default to 'list' if no other subcommand given */
if (list + edit + show + remove + prune != 1)
if (list + add + edit + show + remove + prune != 1)
usage_with_options(git_notes_usage, options);
if ((msg.given || msgfile) && !edit) {
if ((msg.given || msgfile) && !(add || edit)) {
error("cannot use -m/-F options with %s subcommand.", argv[0]);
usage_with_options(git_notes_usage, options);
}
@ -253,6 +258,11 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
usage_with_options(git_notes_usage, options);
}
if (force && !add) {
error("cannot use -f option with %s subcommand.", argv[0]);
usage_with_options(git_notes_usage, options);
}
given_object = argc == 2;
object_ref = given_object ? argv[1] : "HEAD";
if (argc > 2 || (prune && argc > 1)) {
@ -294,7 +304,19 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
return execv_git_cmd(show_args);
}
/* edit/remove/prune command */
/* add/edit/remove/prune command */
if (add && note) {
if (force)
fprintf(stderr, "Overwriting existing notes for object %s\n",
sha1_to_hex(object));
else {
error("Cannot add notes. Found existing notes for object %s. "
"Use '-f' to overwrite existing notes",
sha1_to_hex(object));
return 1;
}
}
if (remove)
strbuf_reset(&buf);