]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/gc.c: let parse-options parse 'git maintenance's subcommands
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 19 Aug 2022 16:04:03 +0000 (18:04 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Aug 2022 18:13:15 +0000 (11:13 -0700)
'git maintenanze' parses its subcommands with a couple of if
statements.  parse-options has just learned to parse subcommands, so
let's use that facility instead, with the benefits of shorter code,
handling missing or unknown subcommands, and listing subcommands for
Bash completion.

This change makes 'git maintenance' consistent with other commands in
that the help text shown for '-h' goes to standard output, not error,
in the exit code and error message on unknown subcommand, and the
error message on missing subcommand.  There is a test checking these,
which is now updated accordingly.

Note that some of the functions implementing each subcommand don't
accept any parameters, so add the (unused) 'argc', '**argv' and
'*prefix' parameters to make them match the type expected by
parse-options, and thus avoid casting function pointers.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/gc.c
git.c
t/t7900-maintenance.sh

index eeff2b760e0cc58978618c791a195df0bca6627b..19d6b3b5581bfb0ab24040cfde8b0023618e1ddb 100644 (file)
@@ -1465,7 +1465,7 @@ static char *get_maintpath(void)
        return strbuf_detach(&sb, NULL);
 }
 
-static int maintenance_register(void)
+static int maintenance_register(int argc, const char **argv, const char *prefix)
 {
        int rc;
        char *config_value;
@@ -1509,7 +1509,7 @@ done:
        return rc;
 }
 
-static int maintenance_unregister(void)
+static int maintenance_unregister(int argc, const char **argv, const char *prefix)
 {
        int rc;
        struct child_process config_unset = CHILD_PROCESS_INIT;
@@ -2505,34 +2505,34 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
        opts.scheduler = resolve_scheduler(opts.scheduler);
        validate_scheduler(opts.scheduler);
 
-       if (maintenance_register())
+       if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
                warning(_("failed to add repo to global config"));
        return update_background_schedule(&opts, 1);
 }
 
-static int maintenance_stop(void)
+static int maintenance_stop(int argc, const char **argv, const char *prefix)
 {
        return update_background_schedule(NULL, 0);
 }
 
-static const char builtin_maintenance_usage[] =        N_("git maintenance <subcommand> [<options>]");
+static const char * const builtin_maintenance_usage[] = {
+       N_("git maintenance <subcommand> [<options>]"),
+       NULL,
+};
 
 int cmd_maintenance(int argc, const char **argv, const char *prefix)
 {
-       if (argc < 2 ||
-           (argc == 2 && !strcmp(argv[1], "-h")))
-               usage(builtin_maintenance_usage);
-
-       if (!strcmp(argv[1], "run"))
-               return maintenance_run(argc - 1, argv + 1, prefix);
-       if (!strcmp(argv[1], "start"))
-               return maintenance_start(argc - 1, argv + 1, prefix);
-       if (!strcmp(argv[1], "stop"))
-               return maintenance_stop();
-       if (!strcmp(argv[1], "register"))
-               return maintenance_register();
-       if (!strcmp(argv[1], "unregister"))
-               return maintenance_unregister();
-
-       die(_("invalid subcommand: %s"), argv[1]);
+       parse_opt_subcommand_fn *fn = NULL;
+       struct option builtin_maintenance_options[] = {
+               OPT_SUBCOMMAND("run", &fn, maintenance_run),
+               OPT_SUBCOMMAND("start", &fn, maintenance_start),
+               OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
+               OPT_SUBCOMMAND("register", &fn, maintenance_register),
+               OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
+               OPT_END(),
+       };
+
+       argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
+                            builtin_maintenance_usage, 0);
+       return fn(argc, argv, prefix);
 }
diff --git a/git.c b/git.c
index 09c126c33da8a9af3a68de4c1f3aa55c5e7027d8..73ddf0f452c3c1ef4dc572605bebd817b6129b8b 100644 (file)
--- a/git.c
+++ b/git.c
@@ -555,7 +555,7 @@ static struct cmd_struct commands[] = {
        { "ls-tree", cmd_ls_tree, RUN_SETUP },
        { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY },
        { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
-       { "maintenance", cmd_maintenance, RUN_SETUP | NO_PARSEOPT },
+       { "maintenance", cmd_maintenance, RUN_SETUP },
        { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
        { "merge-base", cmd_merge_base, RUN_SETUP },
        { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
index 74aa6384755ec6d53cf061a48dbe2cdfc93725f9..852498ff06acd37a8b096b3e1e3273dabd30d012 100755 (executable)
@@ -32,11 +32,13 @@ test_systemd_analyze_verify () {
 }
 
 test_expect_success 'help text' '
-       test_expect_code 129 git maintenance -h 2>err &&
-       test_i18ngrep "usage: git maintenance <subcommand>" err &&
-       test_expect_code 128 git maintenance barf 2>err &&
-       test_i18ngrep "invalid subcommand: barf" err &&
+       test_expect_code 129 git maintenance -h >actual &&
+       test_i18ngrep "usage: git maintenance <subcommand>" actual &&
+       test_expect_code 129 git maintenance barf 2>err &&
+       test_i18ngrep "unknown subcommand: \`barf'\''" err &&
+       test_i18ngrep "usage: git maintenance" err &&
        test_expect_code 129 git maintenance 2>err &&
+       test_i18ngrep "error: need a subcommand" err &&
        test_i18ngrep "usage: git maintenance" err
 '