Skip to content

Commit 729b973

Browse files
szedergitster
authored andcommitted
builtin/reflog.c: let parse-options parse subcommands
'git reflog' parses its subcommands with a couple of if-else if statements. parse-options has just learned to parse subcommands, so let's use that facility instead, with the benefits of shorter code, and listing subcommands for Bash completion. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54ef767 commit 729b973

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

builtin/reflog.c

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -404,40 +404,21 @@ static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
404404

405405
int cmd_reflog(int argc, const char **argv, const char *prefix)
406406
{
407+
parse_opt_subcommand_fn *fn = NULL;
407408
struct option options[] = {
409+
OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
410+
OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
411+
OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
412+
OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
408413
OPT_END()
409414
};
410415

411416
argc = parse_options(argc, argv, prefix, options, reflog_usage,
417+
PARSE_OPT_SUBCOMMAND_OPTIONAL |
412418
PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
413-
PARSE_OPT_KEEP_UNKNOWN_OPT |
414-
PARSE_OPT_NO_INTERNAL_HELP);
415-
416-
/*
417-
* With "git reflog" we default to showing it. !argc is
418-
* impossible with PARSE_OPT_KEEP_ARGV0.
419-
*/
420-
if (argc == 1)
421-
goto log_reflog;
422-
423-
if (!strcmp(argv[1], "-h"))
424-
usage_with_options(reflog_usage, options);
425-
else if (*argv[1] == '-')
426-
goto log_reflog;
427-
428-
if (!strcmp(argv[1], "show"))
429-
return cmd_reflog_show(argc - 1, argv + 1, prefix);
430-
else if (!strcmp(argv[1], "expire"))
431-
return cmd_reflog_expire(argc - 1, argv + 1, prefix);
432-
else if (!strcmp(argv[1], "delete"))
433-
return cmd_reflog_delete(argc - 1, argv + 1, prefix);
434-
else if (!strcmp(argv[1], "exists"))
435-
return cmd_reflog_exists(argc - 1, argv + 1, prefix);
436-
437-
/*
438-
* Fall-through for e.g. "git reflog -1", "git reflog master",
439-
* as well as the plain "git reflog" above goto above.
440-
*/
441-
log_reflog:
442-
return cmd_log_reflog(argc, argv, prefix);
419+
PARSE_OPT_KEEP_UNKNOWN_OPT);
420+
if (fn)
421+
return fn(argc - 1, argv + 1, prefix);
422+
else
423+
return cmd_log_reflog(argc, argv, prefix);
443424
}

0 commit comments

Comments
 (0)