Skip to content

Commit 1c3b051

Browse files
szedergitster
authored andcommitted
builtin/commit-graph.c: let parse-options parse subcommands
'git commit-graph' parses its subcommands with an if-else 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. Note that the functions implementing each subcommand only accept the 'argc' and '**argv' parameters, so add a (unused) '*prefix' parameter 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 aef7d75 commit 1c3b051

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

builtin/commit-graph.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static struct option *add_common_options(struct option *to)
5858
return parse_options_concat(common_opts, to);
5959
}
6060

61-
static int graph_verify(int argc, const char **argv)
61+
static int graph_verify(int argc, const char **argv, const char *prefix)
6262
{
6363
struct commit_graph *graph = NULL;
6464
struct object_directory *odb = NULL;
@@ -190,7 +190,7 @@ static int git_commit_graph_write_config(const char *var, const char *value,
190190
return 0;
191191
}
192192

193-
static int graph_write(int argc, const char **argv)
193+
static int graph_write(int argc, const char **argv, const char *prefix)
194194
{
195195
struct string_list pack_indexes = STRING_LIST_INIT_DUP;
196196
struct strbuf buf = STRBUF_INIT;
@@ -307,26 +307,22 @@ static int graph_write(int argc, const char **argv)
307307

308308
int cmd_commit_graph(int argc, const char **argv, const char *prefix)
309309
{
310-
struct option *builtin_commit_graph_options = common_opts;
310+
parse_opt_subcommand_fn *fn = NULL;
311+
struct option builtin_commit_graph_options[] = {
312+
OPT_SUBCOMMAND("verify", &fn, graph_verify),
313+
OPT_SUBCOMMAND("write", &fn, graph_write),
314+
OPT_END(),
315+
};
316+
struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
311317

312318
git_config(git_default_config, NULL);
313-
argc = parse_options(argc, argv, prefix,
314-
builtin_commit_graph_options,
315-
builtin_commit_graph_usage,
316-
PARSE_OPT_STOP_AT_NON_OPTION);
317-
if (!argc)
318-
goto usage;
319319

320320
read_replace_refs = 0;
321321
save_commit_buffer = 0;
322322

323-
if (!strcmp(argv[0], "verify"))
324-
return graph_verify(argc, argv);
325-
else if (argc && !strcmp(argv[0], "write"))
326-
return graph_write(argc, argv);
323+
argc = parse_options(argc, argv, prefix, options,
324+
builtin_commit_graph_usage, 0);
325+
FREE_AND_NULL(options);
327326

328-
error(_("unrecognized subcommand: %s"), argv[0]);
329-
usage:
330-
usage_with_options(builtin_commit_graph_usage,
331-
builtin_commit_graph_options);
327+
return fn(argc, argv, prefix);
332328
}

t/t5318-commit-graph.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ test_expect_success 'usage' '
1212

1313
test_expect_success 'usage shown without sub-command' '
1414
test_expect_code 129 git commit-graph 2>err &&
15-
! grep error: err
15+
grep usage: err
1616
'
1717

1818
test_expect_success 'usage shown with an error on unknown sub-command' '
1919
cat >expect <<-\EOF &&
20-
error: unrecognized subcommand: unknown
20+
error: unknown subcommand: `unknown'\''
2121
EOF
2222
test_expect_code 129 git commit-graph unknown 2>stderr &&
2323
grep error stderr >actual &&

0 commit comments

Comments
 (0)