Skip to content

Commit 0350954

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c3b051 commit 0350954

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

builtin/gc.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ static char *get_maintpath(void)
14651465
return strbuf_detach(&sb, NULL);
14661466
}
14671467

1468-
static int maintenance_register(void)
1468+
static int maintenance_register(int argc, const char **argv, const char *prefix)
14691469
{
14701470
int rc;
14711471
char *config_value;
@@ -1509,7 +1509,7 @@ static int maintenance_register(void)
15091509
return rc;
15101510
}
15111511

1512-
static int maintenance_unregister(void)
1512+
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
15131513
{
15141514
int rc;
15151515
struct child_process config_unset = CHILD_PROCESS_INIT;
@@ -2505,34 +2505,34 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
25052505
opts.scheduler = resolve_scheduler(opts.scheduler);
25062506
validate_scheduler(opts.scheduler);
25072507

2508-
if (maintenance_register())
2508+
if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
25092509
warning(_("failed to add repo to global config"));
25102510
return update_background_schedule(&opts, 1);
25112511
}
25122512

2513-
static int maintenance_stop(void)
2513+
static int maintenance_stop(int argc, const char **argv, const char *prefix)
25142514
{
25152515
return update_background_schedule(NULL, 0);
25162516
}
25172517

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+
};
25192522

25202523
int cmd_maintenance(int argc, const char **argv, const char *prefix)
25212524
{
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);
25382538
}

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ static struct cmd_struct commands[] = {
555555
{ "ls-tree", cmd_ls_tree, RUN_SETUP },
556556
{ "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY },
557557
{ "mailsplit", cmd_mailsplit, NO_PARSEOPT },
558-
{ "maintenance", cmd_maintenance, RUN_SETUP | NO_PARSEOPT },
558+
{ "maintenance", cmd_maintenance, RUN_SETUP },
559559
{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
560560
{ "merge-base", cmd_merge_base, RUN_SETUP },
561561
{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },

t/t7900-maintenance.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ test_systemd_analyze_verify () {
3232
}
3333

3434
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 &&
3940
test_expect_code 129 git maintenance 2>err &&
41+
test_i18ngrep "error: need a subcommand" err &&
4042
test_i18ngrep "usage: git maintenance" err
4143
'
4244

0 commit comments

Comments
 (0)