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 2087182272 (checkout: split options[] array in three pieces, 2019-03-29), and the same pattern in the multi-pack-index command, see 60ca94769ce (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>
Ævar Arnfjörð Bjarmason committed
Aug 23, 2021 at 14:30 UTC
84e4484f12895b6acd4047ba6776cede6a627f67
1 file changed
+21
-16
builtin/commit-graph.c
+21
-16
@@ -46,6 +46,20 @@ static struct opts_commit_graph {
46
int enable_changed_paths;
47
} opts;
48
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
+
63
static struct object_directory *find_odb(struct repository *r,
64
const char *obj_dir)
65
{
@@ -79,20 +93,17 @@ static int graph_verify(int argc, const char **argv)
93
int flags = 0;
94
95
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")),
96
OPT_BOOL(0, "shallow", &opts.shallow,
97
N_("if the commit-graph is split, only verify the tip file")),
87
- OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
98
OPT_END(),
99
};
100
+ struct option *options = add_common_options(builtin_commit_graph_verify_options);
101
102
trace2_cmd_mode("verify");
103
104
opts.progress = isatty(2);
105
argc = parse_options(argc, argv, NULL,
95
- builtin_commit_graph_verify_options,
106
+ options,
107
builtin_commit_graph_verify_usage, 0);
108
109
if (!opts.obj_dir)
@@ -109,6 +120,7 @@ static int graph_verify(int argc, const char **argv)
120
die_errno(_("Could not open commit-graph '%s'"), graph_name);
121
122
FREE_AND_NULL(graph_name);
123
+ FREE_AND_NULL(options);
124
125
if (open_ok)
126
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)
221
struct progress *progress = NULL;
222
223
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")),
224
OPT_BOOL(0, "reachable", &opts.reachable,
225
N_("start walk at all refs")),
226
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
@@ -222,7 +231,6 @@ static int graph_write(int argc, const char **argv)
231
N_("include all commits already in the commit-graph file")),
232
OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
233
N_("enable computation for changed paths")),
225
- OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
234
OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
235
N_("allow writing an incremental commit-graph file"),
236
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -238,6 +246,7 @@ static int graph_write(int argc, const char **argv)
246
0, write_option_max_new_filters),
247
OPT_END(),
248
};
249
+ struct option *options = add_common_options(builtin_commit_graph_write_options);
250
251
opts.progress = isatty(2);
252
opts.enable_changed_paths = -1;
@@ -251,7 +260,7 @@ static int graph_write(int argc, const char **argv)
260
git_config(git_commit_graph_write_config, &opts);
261
262
argc = parse_options(argc, argv, NULL,
254
- builtin_commit_graph_write_options,
263
+ options,
264
builtin_commit_graph_write_usage, 0);
265
266
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
@@ -307,6 +316,7 @@ static int graph_write(int argc, const char **argv)
316
result = 1;
317
318
cleanup:
319
+ FREE_AND_NULL(options);
320
string_list_clear(&pack_indexes, 0);
321
strbuf_release(&buf);
322
return result;
@@ -314,12 +324,7 @@ cleanup:
324
325
int cmd_commit_graph(int argc, const char **argv, const char *prefix)
326
{
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;
328
329
git_config(git_default_config, NULL);
330
argc = parse_options(argc, argv, prefix,