Skip to content

Commit 0d330a5

Browse files
peffgitster
authored andcommitted
maintenance: add parse-options boilerplate for subcommands
Several of the git-maintenance subcommands don't take any options, so they don't bother looking at argv at all. This means they'll silently accept garbage, like: $ git maintenance register --foo [no output] $ git maintenance stop bar [no output] Let's give them the basic boilerplate to detect and handle these cases: $ git maintenance register --foo error: unknown option `foo' usage: git maintenance register $ git maintenance stop bar usage: git maintenance stop We could reduce the number of lines of code here a bit with a shared helper function. But it's worth building out the boilerplate, as it may serve as the base for adding options later. Note one complication: maintenance_start() calls directly into maintenance_register(), so it now needs to pass a plausible argv (we don't care, but parse_options() is expecting there to at least be an argv[0] program name). This is an extra line of code, but it eliminates the need for an explanatory comment. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ecd2d3e commit 0d330a5

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

builtin/gc.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,14 +1465,28 @@ static char *get_maintpath(void)
14651465
return strbuf_detach(&sb, NULL);
14661466
}
14671467

1468+
static char const * const builtin_maintenance_register_usage[] = {
1469+
N_("git maintenance register"),
1470+
NULL
1471+
};
1472+
14681473
static int maintenance_register(int argc, const char **argv, const char *prefix)
14691474
{
1475+
struct option options[] = {
1476+
OPT_END(),
1477+
};
14701478
int rc;
14711479
char *config_value;
14721480
struct child_process config_set = CHILD_PROCESS_INIT;
14731481
struct child_process config_get = CHILD_PROCESS_INIT;
14741482
char *maintpath = get_maintpath();
14751483

1484+
argc = parse_options(argc, argv, prefix, options,
1485+
builtin_maintenance_register_usage, 0);
1486+
if (argc)
1487+
usage_with_options(builtin_maintenance_register_usage,
1488+
options);
1489+
14761490
/* Disable foreground maintenance */
14771491
git_config_set("maintenance.auto", "false");
14781492

@@ -1509,12 +1523,26 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
15091523
return rc;
15101524
}
15111525

1526+
static char const * const builtin_maintenance_unregister_usage[] = {
1527+
N_("git maintenance unregister"),
1528+
NULL
1529+
};
1530+
15121531
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
15131532
{
1533+
struct option options[] = {
1534+
OPT_END(),
1535+
};
15141536
int rc;
15151537
struct child_process config_unset = CHILD_PROCESS_INIT;
15161538
char *maintpath = get_maintpath();
15171539

1540+
argc = parse_options(argc, argv, prefix, options,
1541+
builtin_maintenance_unregister_usage, 0);
1542+
if (argc)
1543+
usage_with_options(builtin_maintenance_unregister_usage,
1544+
options);
1545+
15181546
config_unset.git_cmd = 1;
15191547
strvec_pushl(&config_unset.args, "config", "--global", "--unset",
15201548
"--fixed-value", "maintenance.repo", maintpath, NULL);
@@ -2496,6 +2524,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
24962524
PARSE_OPT_NONEG, maintenance_opt_scheduler),
24972525
OPT_END()
24982526
};
2527+
const char *register_args[] = { "register", NULL };
24992528

25002529
argc = parse_options(argc, argv, prefix, options,
25012530
builtin_maintenance_start_usage, 0);
@@ -2505,13 +2534,25 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
25052534
opts.scheduler = resolve_scheduler(opts.scheduler);
25062535
validate_scheduler(opts.scheduler);
25072536

2508-
if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
2537+
if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
25092538
warning(_("failed to add repo to global config"));
25102539
return update_background_schedule(&opts, 1);
25112540
}
25122541

2542+
static const char *const builtin_maintenance_stop_usage[] = {
2543+
N_("git maintenance stop"),
2544+
NULL
2545+
};
2546+
25132547
static int maintenance_stop(int argc, const char **argv, const char *prefix)
25142548
{
2549+
struct option options[] = {
2550+
OPT_END()
2551+
};
2552+
argc = parse_options(argc, argv, prefix, options,
2553+
builtin_maintenance_stop_usage, 0);
2554+
if (argc)
2555+
usage_with_options(builtin_maintenance_stop_usage, options);
25152556
return update_background_schedule(NULL, 0);
25162557
}
25172558

0 commit comments

Comments
 (0)