builtin/gc.c: let parse-options parse 'git maintenance's subcommands

'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>

SZEDER Gábor committed Aug 19, 2022 at 18:04 UTC 0350954482bca6accd7b4ad072a1bdb83651b376
3 files changed +28 -26
builtin/gc.c
+21 -21
@@ -1465,7 +1465,7 @@ static char *get_maintpath(void)
1465 return strbuf_detach(&sb, NULL);
1466 }
1467
1468 -static int maintenance_register(void)
1468 +static int maintenance_register(int argc, const char **argv, const char *prefix)
1469 {
1470 int rc;
1471 char *config_value;
@@ -1509,7 +1509,7 @@ done:
1509 return rc;
1510 }
1511
1512 -static int maintenance_unregister(void)
1512 +static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1513 {
1514 int rc;
1515 struct child_process config_unset = CHILD_PROCESS_INIT;
@@ -2505,34 +2505,34 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
2505 opts.scheduler = resolve_scheduler(opts.scheduler);
2506 validate_scheduler(opts.scheduler);
2507
2508 - if (maintenance_register())
2508 + if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
2509 warning(_("failed to add repo to global config"));
2510 return update_background_schedule(&opts, 1);
2511 }
2512
2513 -static int maintenance_stop(void)
2513 +static int maintenance_stop(int argc, const char **argv, const char *prefix)
2514 {
2515 return update_background_schedule(NULL, 0);
2516 }
2517
2518 -static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
2518 +static const char * const builtin_maintenance_usage[] = {
2519 + N_("git maintenance <subcommand> [<options>]"),
2520 + NULL,
2521 +};
2522
2523 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2524 {
2522 - if (argc < 2 ||
2523 - (argc == 2 && !strcmp(argv[1], "-h")))
2524 - usage(builtin_maintenance_usage);
2525 -
2526 - if (!strcmp(argv[1], "run"))
2527 - return maintenance_run(argc - 1, argv + 1, prefix);
2528 - if (!strcmp(argv[1], "start"))
2529 - return maintenance_start(argc - 1, argv + 1, prefix);
2530 - if (!strcmp(argv[1], "stop"))
2531 - return maintenance_stop();
2532 - if (!strcmp(argv[1], "register"))
2533 - return maintenance_register();
2534 - if (!strcmp(argv[1], "unregister"))
2535 - return maintenance_unregister();
2536 -
2537 - die(_("invalid subcommand: %s"), argv[1]);
2525 + parse_opt_subcommand_fn *fn = NULL;
2526 + struct option builtin_maintenance_options[] = {
2527 + OPT_SUBCOMMAND("run", &fn, maintenance_run),
2528 + OPT_SUBCOMMAND("start", &fn, maintenance_start),
2529 + OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2530 + OPT_SUBCOMMAND("register", &fn, maintenance_register),
2531 + OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2532 + OPT_END(),
2533 + };
2534 +
2535 + argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2536 + builtin_maintenance_usage, 0);
2537 + return fn(argc, argv, prefix);
2538 }
git.c
+1 -1
@@ -555,7 +555,7 @@ static struct cmd_struct commands[] = {
555 { "ls-tree", cmd_ls_tree, RUN_SETUP },
556 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY },
557 { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
558 - { "maintenance", cmd_maintenance, RUN_SETUP | NO_PARSEOPT },
558 + { "maintenance", cmd_maintenance, RUN_SETUP },
559 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
560 { "merge-base", cmd_merge_base, RUN_SETUP },
561 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
t/t7900-maintenance.sh
+6 -4
@@ -32,11 +32,13 @@ test_systemd_analyze_verify () {
32 }
33
34 test_expect_success 'help text' '
35 - test_expect_code 129 git maintenance -h 2>err &&
36 - test_i18ngrep "usage: git maintenance <subcommand>" err &&
37 - test_expect_code 128 git maintenance barf 2>err &&
38 - test_i18ngrep "invalid subcommand: barf" err &&
35 + test_expect_code 129 git maintenance -h >actual &&
36 + test_i18ngrep "usage: git maintenance <subcommand>" actual &&
37 + test_expect_code 129 git maintenance barf 2>err &&
38 + test_i18ngrep "unknown subcommand: \`barf'\''" err &&
39 + test_i18ngrep "usage: git maintenance" err &&
40 test_expect_code 129 git maintenance 2>err &&
41 + test_i18ngrep "error: need a subcommand" err &&
42 test_i18ngrep "usage: git maintenance" err
43 '
44