Skip to content

Commit 84e4484

Browse files
avarttaylorr
authored andcommitted
commit-graph: use parse_options_concat()
Make use of the parse_options_concat() so we don't need to copy/paste common options like --object-dir. This is inspired by a similar change to "checkout" in 2087182 (checkout: split options[] array in three pieces, 2019-03-29), and the same pattern in the multi-pack-index command, see 60ca947 (builtin/multi-pack-index.c: split sub-commands, 2021-03-30). A minor behavior change here is that now we're going to list both --object-dir and --progress first, before we'd list --progress along with other options. Co-authored-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8722f9f commit 84e4484

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

builtin/commit-graph.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ static struct opts_commit_graph {
4646
int enable_changed_paths;
4747
} opts;
4848

49+
static struct option common_opts[] = {
50+
OPT_STRING(0, "object-dir", &opts.obj_dir,
51+
N_("dir"),
52+
N_("the object directory to store the graph")),
53+
OPT_BOOL(0, "progress", &opts.progress,
54+
N_("force progress reporting")),
55+
OPT_END()
56+
};
57+
58+
static struct option *add_common_options(struct option *to)
59+
{
60+
return parse_options_concat(common_opts, to);
61+
}
62+
4963
static struct object_directory *find_odb(struct repository *r,
5064
const char *obj_dir)
5165
{
@@ -79,20 +93,17 @@ static int graph_verify(int argc, const char **argv)
7993
int flags = 0;
8094

8195
static struct option builtin_commit_graph_verify_options[] = {
82-
OPT_STRING(0, "object-dir", &opts.obj_dir,
83-
N_("dir"),
84-
N_("the object directory to store the graph")),
8596
OPT_BOOL(0, "shallow", &opts.shallow,
8697
N_("if the commit-graph is split, only verify the tip file")),
87-
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
8898
OPT_END(),
8999
};
100+
struct option *options = add_common_options(builtin_commit_graph_verify_options);
90101

91102
trace2_cmd_mode("verify");
92103

93104
opts.progress = isatty(2);
94105
argc = parse_options(argc, argv, NULL,
95-
builtin_commit_graph_verify_options,
106+
options,
96107
builtin_commit_graph_verify_usage, 0);
97108

98109
if (!opts.obj_dir)
@@ -109,6 +120,7 @@ static int graph_verify(int argc, const char **argv)
109120
die_errno(_("Could not open commit-graph '%s'"), graph_name);
110121

111122
FREE_AND_NULL(graph_name);
123+
FREE_AND_NULL(options);
112124

113125
if (open_ok)
114126
graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
@@ -209,9 +221,6 @@ static int graph_write(int argc, const char **argv)
209221
struct progress *progress = NULL;
210222

211223
static struct option builtin_commit_graph_write_options[] = {
212-
OPT_STRING(0, "object-dir", &opts.obj_dir,
213-
N_("dir"),
214-
N_("the object directory to store the graph")),
215224
OPT_BOOL(0, "reachable", &opts.reachable,
216225
N_("start walk at all refs")),
217226
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
@@ -222,7 +231,6 @@ static int graph_write(int argc, const char **argv)
222231
N_("include all commits already in the commit-graph file")),
223232
OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
224233
N_("enable computation for changed paths")),
225-
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
226234
OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
227235
N_("allow writing an incremental commit-graph file"),
228236
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -238,6 +246,7 @@ static int graph_write(int argc, const char **argv)
238246
0, write_option_max_new_filters),
239247
OPT_END(),
240248
};
249+
struct option *options = add_common_options(builtin_commit_graph_write_options);
241250

242251
opts.progress = isatty(2);
243252
opts.enable_changed_paths = -1;
@@ -251,7 +260,7 @@ static int graph_write(int argc, const char **argv)
251260
git_config(git_commit_graph_write_config, &opts);
252261

253262
argc = parse_options(argc, argv, NULL,
254-
builtin_commit_graph_write_options,
263+
options,
255264
builtin_commit_graph_write_usage, 0);
256265

257266
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
@@ -307,19 +316,15 @@ static int graph_write(int argc, const char **argv)
307316
result = 1;
308317

309318
cleanup:
319+
FREE_AND_NULL(options);
310320
string_list_clear(&pack_indexes, 0);
311321
strbuf_release(&buf);
312322
return result;
313323
}
314324

315325
int cmd_commit_graph(int argc, const char **argv, const char *prefix)
316326
{
317-
static struct option builtin_commit_graph_options[] = {
318-
OPT_STRING(0, "object-dir", &opts.obj_dir,
319-
N_("dir"),
320-
N_("the object directory to store the graph")),
321-
OPT_END(),
322-
};
327+
struct option *builtin_commit_graph_options = common_opts;
323328

324329
git_config(git_default_config, NULL);
325330
argc = parse_options(argc, argv, prefix,

0 commit comments

Comments
 (0)