Skip to content

Commit

Permalink
builtin/hook.c: let parse-options parse subcommands
Browse files Browse the repository at this point in the history
'git hook' parses its currently only subcommand with an if statement.
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.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
szeder authored and gitster committed Aug 19, 2022
1 parent 0350954 commit f83736c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions builtin/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,14 @@ static int run(int argc, const char **argv, const char *prefix)

int cmd_hook(int argc, const char **argv, const char *prefix)
{
parse_opt_subcommand_fn *fn = NULL;
struct option builtin_hook_options[] = {
OPT_SUBCOMMAND("run", &fn, run),
OPT_END(),
};

argc = parse_options(argc, argv, NULL, builtin_hook_options,
builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
if (!argc)
goto usage;
builtin_hook_usage, 0);

if (!strcmp(argv[0], "run"))
return run(argc, argv, prefix);

usage:
usage_with_options(builtin_hook_usage, builtin_hook_options);
return fn(argc, argv, prefix);
}

0 comments on commit f83736c

Please sign in to comment.