]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/reflog.c: let parse-options parse subcommands
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 19 Aug 2022 16:04:07 +0000 (18:04 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Aug 2022 18:13:16 +0000 (11:13 -0700)
'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>
builtin/reflog.c

index b8b1f4f8ea84d279a7fc102d0bf962f43681db4a..d3f6d903fb29a2e5c79131579ba97687a944cf84 100644 (file)
@@ -404,40 +404,21 @@ static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
 
 int cmd_reflog(int argc, const char **argv, const char *prefix)
 {
+       parse_opt_subcommand_fn *fn = NULL;
        struct option options[] = {
+               OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
+               OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
+               OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
+               OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
                OPT_END()
        };
 
        argc = parse_options(argc, argv, prefix, options, reflog_usage,
+                            PARSE_OPT_SUBCOMMAND_OPTIONAL |
                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
-                            PARSE_OPT_KEEP_UNKNOWN_OPT |
-                            PARSE_OPT_NO_INTERNAL_HELP);
-
-       /*
-        * With "git reflog" we default to showing it. !argc is
-        * impossible with PARSE_OPT_KEEP_ARGV0.
-        */
-       if (argc == 1)
-               goto log_reflog;
-
-       if (!strcmp(argv[1], "-h"))
-               usage_with_options(reflog_usage, options);
-       else if (*argv[1] == '-')
-               goto log_reflog;
-
-       if (!strcmp(argv[1], "show"))
-               return cmd_reflog_show(argc - 1, argv + 1, prefix);
-       else if (!strcmp(argv[1], "expire"))
-               return cmd_reflog_expire(argc - 1, argv + 1, prefix);
-       else if (!strcmp(argv[1], "delete"))
-               return cmd_reflog_delete(argc - 1, argv + 1, prefix);
-       else if (!strcmp(argv[1], "exists"))
-               return cmd_reflog_exists(argc - 1, argv + 1, prefix);
-
-       /*
-        * Fall-through for e.g. "git reflog -1", "git reflog master",
-        * as well as the plain "git reflog" above goto above.
-        */
-log_reflog:
-       return cmd_log_reflog(argc, argv, prefix);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
+       if (fn)
+               return fn(argc - 1, argv + 1, prefix);
+       else
+               return cmd_log_reflog(argc, argv, prefix);
 }