builtin-notes: Misc. refactoring of argc and exit value handling

This is in preparation of future patches that add additional subcommands.

Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Johan Herland 2010-02-13 22:28:37 +01:00 committed by Junio C Hamano
parent 0691cff7dc
commit 5848769f9d
1 changed files with 36 additions and 25 deletions

View File

@ -287,7 +287,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)


int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0, int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
prune = 0, force = 0; prune = 0, force = 0;
int given_object; int given_object = 0, i = 1, retval = 0;
struct msg_arg msg = { 0, 0, STRBUF_INIT }; struct msg_arg msg = { 0, 0, STRBUF_INIT };
struct option options[] = { struct option options[] = {
OPT_GROUP("Notes options"), OPT_GROUP("Notes options"),
@ -321,8 +321,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
remove = 1; remove = 1;
else if (argc && !strcmp(argv[0], "prune")) else if (argc && !strcmp(argv[0], "prune"))
prune = 1; prune = 1;
else if (!argc) else if (!argc) {
list = 1; /* Default to 'list' if no other subcommand given */ list = 1; /* Default to 'list' if no other subcommand given */
i = 0;
}


if (list + add + append + edit + show + remove + prune != 1) if (list + add + append + edit + show + remove + prune != 1)
usage_with_options(git_notes_usage, options); usage_with_options(git_notes_usage, options);
@ -344,9 +346,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
usage_with_options(git_notes_usage, options); usage_with_options(git_notes_usage, options);
} }


given_object = argc == 2; given_object = argc > i;
object_ref = given_object ? argv[1] : "HEAD"; object_ref = given_object ? argv[i++] : "HEAD";
if (argc > 2 || (prune && argc > 1)) {
if (argc > i || (prune && given_object)) {
error("too many parameters"); error("too many parameters");
usage_with_options(git_notes_usage, options); usage_with_options(git_notes_usage, options);
} }
@ -369,34 +372,38 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
if (given_object) { if (given_object) {
if (note) { if (note) {
puts(sha1_to_hex(note)); puts(sha1_to_hex(note));
return 0; goto end;
}
} else {
retval = for_each_note(t, 0, list_each_note, NULL);
goto end;
} }
} else
return for_each_note(t, 0, list_each_note, NULL);
} }


/* show command */ /* show command */


if ((list || show) && !note) { if ((list || show) && !note) {
error("No note found for object %s.", sha1_to_hex(object)); error("No note found for object %s.", sha1_to_hex(object));
return 1; retval = 1;
goto end;
} else if (show) { } else if (show) {
const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
return execv_git_cmd(show_args); retval = execv_git_cmd(show_args);
goto end;
} }


/* add/append/edit/remove/prune command */ /* add/append/edit/remove/prune command */


if (add && note) { if (add && note) {
if (force) if (!force) {
error("Cannot add notes. Found existing notes for object"
" %s. Use '-f' to overwrite existing notes",
sha1_to_hex(object));
retval = 1;
goto end;
}
fprintf(stderr, "Overwriting existing notes for object %s\n", fprintf(stderr, "Overwriting existing notes for object %s\n",
sha1_to_hex(object)); 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) { if (remove) {
@ -408,18 +415,22 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
if (prune) { if (prune) {
hashclr(new_note); hashclr(new_note);
prune_notes(t); prune_notes(t);
} else { goto commit;
} else
create_note(object, &msg, append, note, new_note); create_note(object, &msg, append, note, new_note);

if (is_null_sha1(new_note)) if (is_null_sha1(new_note))
remove_note(t, object); remove_note(t, object);
else else
add_note(t, object, new_note, combine_notes_overwrite); add_note(t, object, new_note, combine_notes_overwrite);
}
snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'", commit:
snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
is_null_sha1(new_note) ? "removed" : "added", argv[0]); is_null_sha1(new_note) ? "removed" : "added", argv[0]);
commit_notes(t, logmsg); commit_notes(t, logmsg);


end:
free_notes(t); free_notes(t);
strbuf_release(&(msg.buf)); strbuf_release(&(msg.buf));
return 0; return retval;
} }