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 <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Aug 19, 2022 at 18:04 UTC 729b97332b05564133e4039fbab9f694c25097a2
1 file changed +11 -30
builtin/reflog.c
+11 -30
@@ -404,40 +404,21 @@ static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
404
405 int cmd_reflog(int argc, const char **argv, const char *prefix)
406 {
407 + parse_opt_subcommand_fn *fn = NULL;
408 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),
413 OPT_END()
414 };
415
416 argc = parse_options(argc, argv, prefix, options, reflog_usage,
417 + PARSE_OPT_SUBCOMMAND_OPTIONAL |
418 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);
424 }