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>
Johan Herland committed
Feb 13, 2010 at 22:28 UTC
5848769f9de68cfb0735710c6c4d2f08aa53f317
1 file changed
+36
-25
builtin-notes.c
+36
-25
@@ -287,7 +287,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
287
288
int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
289
prune = 0, force = 0;
290
- int given_object;
290
+ int given_object = 0, i = 1, retval = 0;
291
struct msg_arg msg = { 0, 0, STRBUF_INIT };
292
struct option options[] = {
293
OPT_GROUP("Notes options"),
@@ -321,8 +321,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
321
remove = 1;
322
else if (argc && !strcmp(argv[0], "prune"))
323
prune = 1;
324
- else if (!argc)
324
+ else if (!argc) {
325
list = 1; /* Default to 'list' if no other subcommand given */
326
+ i = 0;
327
+ }
328
329
if (list + add + append + edit + show + remove + prune != 1)
330
usage_with_options(git_notes_usage, options);
@@ -344,9 +346,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
346
usage_with_options(git_notes_usage, options);
347
}
348
347
- given_object = argc == 2;
348
- object_ref = given_object ? argv[1] : "HEAD";
349
- if (argc > 2 || (prune && argc > 1)) {
349
+ given_object = argc > i;
350
+ object_ref = given_object ? argv[i++] : "HEAD";
351
+
352
+ if (argc > i || (prune && given_object)) {
353
error("too many parameters");
354
usage_with_options(git_notes_usage, options);
355
}
@@ -369,34 +372,38 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
372
if (given_object) {
373
if (note) {
374
puts(sha1_to_hex(note));
372
- return 0;
375
+ goto end;
376
}
374
- } else
375
- return for_each_note(t, 0, list_each_note, NULL);
377
+ } else {
378
+ retval = for_each_note(t, 0, list_each_note, NULL);
379
+ goto end;
380
+ }
381
}
382
383
/* show command */
384
385
if ((list || show) && !note) {
386
error("No note found for object %s.", sha1_to_hex(object));
382
- return 1;
387
+ retval = 1;
388
+ goto end;
389
} else if (show) {
390
const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
385
- return execv_git_cmd(show_args);
391
+ retval = execv_git_cmd(show_args);
392
+ goto end;
393
}
394
395
/* add/append/edit/remove/prune command */
396
397
if (add && note) {
391
- if (force)
392
- fprintf(stderr, "Overwriting existing notes for object %s\n",
393
- sha1_to_hex(object));
394
- else {
395
- error("Cannot add notes. Found existing notes for object %s. "
396
- "Use '-f' to overwrite existing notes",
398
+ if (!force) {
399
+ error("Cannot add notes. Found existing notes for object"
400
+ " %s. Use '-f' to overwrite existing notes",
401
sha1_to_hex(object));
398
- return 1;
402
+ retval = 1;
403
+ goto end;
404
}
405
+ fprintf(stderr, "Overwriting existing notes for object %s\n",
406
+ sha1_to_hex(object));
407
}
408
409
if (remove) {
@@ -408,18 +415,22 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
415
if (prune) {
416
hashclr(new_note);
417
prune_notes(t);
411
- } else {
418
+ goto commit;
419
+ } else
420
create_note(object, &msg, append, note, new_note);
413
- if (is_null_sha1(new_note))
414
- remove_note(t, object);
415
- else
416
- add_note(t, object, new_note, combine_notes_overwrite);
417
- }
418
- snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
421
+
422
+ if (is_null_sha1(new_note))
423
+ remove_note(t, object);
424
+ else
425
+ add_note(t, object, new_note, combine_notes_overwrite);
426
+
427
+commit:
428
+ snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
429
is_null_sha1(new_note) ? "removed" : "added", argv[0]);
430
commit_notes(t, logmsg);
431
432
+end:
433
free_notes(t);
434
strbuf_release(&(msg.buf));
424
- return 0;
435
+ return retval;
436
}