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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
ecd2d3efe0
commit
0d330a53f3
43
builtin/gc.c
43
builtin/gc.c
@ -1465,14 +1465,28 @@ static char *get_maintpath(void)
|
|||||||
return strbuf_detach(&sb, NULL);
|
return strbuf_detach(&sb, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char const * const builtin_maintenance_register_usage[] = {
|
||||||
|
N_("git maintenance register"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
static int maintenance_register(int argc, const char **argv, const char *prefix)
|
static int maintenance_register(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END(),
|
||||||
|
};
|
||||||
int rc;
|
int rc;
|
||||||
char *config_value;
|
char *config_value;
|
||||||
struct child_process config_set = CHILD_PROCESS_INIT;
|
struct child_process config_set = CHILD_PROCESS_INIT;
|
||||||
struct child_process config_get = CHILD_PROCESS_INIT;
|
struct child_process config_get = CHILD_PROCESS_INIT;
|
||||||
char *maintpath = get_maintpath();
|
char *maintpath = get_maintpath();
|
||||||
|
|
||||||
|
argc = parse_options(argc, argv, prefix, options,
|
||||||
|
builtin_maintenance_register_usage, 0);
|
||||||
|
if (argc)
|
||||||
|
usage_with_options(builtin_maintenance_register_usage,
|
||||||
|
options);
|
||||||
|
|
||||||
/* Disable foreground maintenance */
|
/* Disable foreground maintenance */
|
||||||
git_config_set("maintenance.auto", "false");
|
git_config_set("maintenance.auto", "false");
|
||||||
|
|
||||||
@ -1509,12 +1523,26 @@ done:
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char const * const builtin_maintenance_unregister_usage[] = {
|
||||||
|
N_("git maintenance unregister"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
|
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END(),
|
||||||
|
};
|
||||||
int rc;
|
int rc;
|
||||||
struct child_process config_unset = CHILD_PROCESS_INIT;
|
struct child_process config_unset = CHILD_PROCESS_INIT;
|
||||||
char *maintpath = get_maintpath();
|
char *maintpath = get_maintpath();
|
||||||
|
|
||||||
|
argc = parse_options(argc, argv, prefix, options,
|
||||||
|
builtin_maintenance_unregister_usage, 0);
|
||||||
|
if (argc)
|
||||||
|
usage_with_options(builtin_maintenance_unregister_usage,
|
||||||
|
options);
|
||||||
|
|
||||||
config_unset.git_cmd = 1;
|
config_unset.git_cmd = 1;
|
||||||
strvec_pushl(&config_unset.args, "config", "--global", "--unset",
|
strvec_pushl(&config_unset.args, "config", "--global", "--unset",
|
||||||
"--fixed-value", "maintenance.repo", maintpath, NULL);
|
"--fixed-value", "maintenance.repo", maintpath, NULL);
|
||||||
@ -2496,6 +2524,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
|
|||||||
PARSE_OPT_NONEG, maintenance_opt_scheduler),
|
PARSE_OPT_NONEG, maintenance_opt_scheduler),
|
||||||
OPT_END()
|
OPT_END()
|
||||||
};
|
};
|
||||||
|
const char *register_args[] = { "register", NULL };
|
||||||
|
|
||||||
argc = parse_options(argc, argv, prefix, options,
|
argc = parse_options(argc, argv, prefix, options,
|
||||||
builtin_maintenance_start_usage, 0);
|
builtin_maintenance_start_usage, 0);
|
||||||
@ -2505,13 +2534,25 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
|
|||||||
opts.scheduler = resolve_scheduler(opts.scheduler);
|
opts.scheduler = resolve_scheduler(opts.scheduler);
|
||||||
validate_scheduler(opts.scheduler);
|
validate_scheduler(opts.scheduler);
|
||||||
|
|
||||||
if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
|
if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
|
||||||
warning(_("failed to add repo to global config"));
|
warning(_("failed to add repo to global config"));
|
||||||
return update_background_schedule(&opts, 1);
|
return update_background_schedule(&opts, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *const builtin_maintenance_stop_usage[] = {
|
||||||
|
N_("git maintenance stop"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
static int maintenance_stop(int argc, const char **argv, const char *prefix)
|
static int maintenance_stop(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
argc = parse_options(argc, argv, prefix, options,
|
||||||
|
builtin_maintenance_stop_usage, 0);
|
||||||
|
if (argc)
|
||||||
|
usage_with_options(builtin_maintenance_stop_usage, options);
|
||||||
return update_background_schedule(NULL, 0);
|
return update_background_schedule(NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user