Skip to content

Commit 1396a95

Browse files
committed
Merge branch 'ab/commit-graph-usage'
Fixes on usage message from "git commit-graph". * ab/commit-graph-usage: commit-graph: show "unexpected subcommand" error commit-graph: show usage on "commit-graph [write|verify] garbage" commit-graph: early exit to "usage" on !argc multi-pack-index: refactor "goto usage" pattern commit-graph: use parse_options_concat() commit-graph: remove redundant handling of -h commit-graph: define common usage with a macro
2 parents bd29bcf + 367c5f3 commit 1396a95

File tree

3 files changed

+74
-46
lines changed

3 files changed

+74
-46
lines changed

builtin/commit-graph.c

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@
99
#include "progress.h"
1010
#include "tag.h"
1111

12-
static char const * const builtin_commit_graph_usage[] = {
13-
N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
14-
N_("git commit-graph write [--object-dir <objdir>] [--append] "
15-
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
16-
"[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
17-
"<split options>"),
12+
#define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
13+
N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]")
14+
15+
#define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
16+
N_("git commit-graph write [--object-dir <objdir>] [--append] " \
17+
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " \
18+
"[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] " \
19+
"<split options>")
20+
21+
static const char * builtin_commit_graph_verify_usage[] = {
22+
BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
1823
NULL
1924
};
2025

21-
static const char * const builtin_commit_graph_verify_usage[] = {
22-
N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
26+
static const char * builtin_commit_graph_write_usage[] = {
27+
BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
2328
NULL
2429
};
2530

26-
static const char * const builtin_commit_graph_write_usage[] = {
27-
N_("git commit-graph write [--object-dir <objdir>] [--append] "
28-
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
29-
"[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
30-
"<split options>"),
31-
NULL
31+
static char const * const builtin_commit_graph_usage[] = {
32+
BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
33+
BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
34+
NULL,
3235
};
3336

3437
static struct opts_commit_graph {
@@ -43,6 +46,20 @@ static struct opts_commit_graph {
4346
int enable_changed_paths;
4447
} opts;
4548

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+
4663
static struct object_directory *find_odb(struct repository *r,
4764
const char *obj_dir)
4865
{
@@ -76,21 +93,20 @@ static int graph_verify(int argc, const char **argv)
7693
int flags = 0;
7794

7895
static struct option builtin_commit_graph_verify_options[] = {
79-
OPT_STRING(0, "object-dir", &opts.obj_dir,
80-
N_("dir"),
81-
N_("the object directory to store the graph")),
8296
OPT_BOOL(0, "shallow", &opts.shallow,
8397
N_("if the commit-graph is split, only verify the tip file")),
84-
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
8598
OPT_END(),
8699
};
100+
struct option *options = add_common_options(builtin_commit_graph_verify_options);
87101

88102
trace2_cmd_mode("verify");
89103

90104
opts.progress = isatty(2);
91105
argc = parse_options(argc, argv, NULL,
92-
builtin_commit_graph_verify_options,
106+
options,
93107
builtin_commit_graph_verify_usage, 0);
108+
if (argc)
109+
usage_with_options(builtin_commit_graph_verify_usage, options);
94110

95111
if (!opts.obj_dir)
96112
opts.obj_dir = get_object_directory();
@@ -106,6 +122,7 @@ static int graph_verify(int argc, const char **argv)
106122
die_errno(_("Could not open commit-graph '%s'"), graph_name);
107123

108124
FREE_AND_NULL(graph_name);
125+
FREE_AND_NULL(options);
109126

110127
if (open_ok)
111128
graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
@@ -206,9 +223,6 @@ static int graph_write(int argc, const char **argv)
206223
struct progress *progress = NULL;
207224

208225
static struct option builtin_commit_graph_write_options[] = {
209-
OPT_STRING(0, "object-dir", &opts.obj_dir,
210-
N_("dir"),
211-
N_("the object directory to store the graph")),
212226
OPT_BOOL(0, "reachable", &opts.reachable,
213227
N_("start walk at all refs")),
214228
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
@@ -219,7 +233,6 @@ static int graph_write(int argc, const char **argv)
219233
N_("include all commits already in the commit-graph file")),
220234
OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
221235
N_("enable computation for changed paths")),
222-
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
223236
OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
224237
N_("allow writing an incremental commit-graph file"),
225238
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -235,6 +248,7 @@ static int graph_write(int argc, const char **argv)
235248
0, write_option_max_new_filters),
236249
OPT_END(),
237250
};
251+
struct option *options = add_common_options(builtin_commit_graph_write_options);
238252

239253
opts.progress = isatty(2);
240254
opts.enable_changed_paths = -1;
@@ -248,8 +262,10 @@ static int graph_write(int argc, const char **argv)
248262
git_config(git_commit_graph_write_config, &opts);
249263

250264
argc = parse_options(argc, argv, NULL,
251-
builtin_commit_graph_write_options,
265+
options,
252266
builtin_commit_graph_write_usage, 0);
267+
if (argc)
268+
usage_with_options(builtin_commit_graph_write_usage, options);
253269

254270
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
255271
die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
@@ -304,39 +320,33 @@ static int graph_write(int argc, const char **argv)
304320
result = 1;
305321

306322
cleanup:
323+
FREE_AND_NULL(options);
307324
string_list_clear(&pack_indexes, 0);
308325
strbuf_release(&buf);
309326
return result;
310327
}
311328

312329
int cmd_commit_graph(int argc, const char **argv, const char *prefix)
313330
{
314-
static struct option builtin_commit_graph_options[] = {
315-
OPT_STRING(0, "object-dir", &opts.obj_dir,
316-
N_("dir"),
317-
N_("the object directory to store the graph")),
318-
OPT_END(),
319-
};
320-
321-
if (argc == 2 && !strcmp(argv[1], "-h"))
322-
usage_with_options(builtin_commit_graph_usage,
323-
builtin_commit_graph_options);
331+
struct option *builtin_commit_graph_options = common_opts;
324332

325333
git_config(git_default_config, NULL);
326334
argc = parse_options(argc, argv, prefix,
327335
builtin_commit_graph_options,
328336
builtin_commit_graph_usage,
329337
PARSE_OPT_STOP_AT_NON_OPTION);
338+
if (!argc)
339+
goto usage;
330340

331341
save_commit_buffer = 0;
332342

333-
if (argc > 0) {
334-
if (!strcmp(argv[0], "verify"))
335-
return graph_verify(argc, argv);
336-
if (!strcmp(argv[0], "write"))
337-
return graph_write(argc, argv);
338-
}
343+
if (!strcmp(argv[0], "verify"))
344+
return graph_verify(argc, argv);
345+
else if (argc && !strcmp(argv[0], "write"))
346+
return graph_write(argc, argv);
339347

348+
error(_("unrecognized subcommand: %s"), argv[0]);
349+
usage:
340350
usage_with_options(builtin_commit_graph_usage,
341351
builtin_commit_graph_options);
342352
}

builtin/multi-pack-index.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ int cmd_multi_pack_index(int argc, const char **argv,
164164
if (!opts.object_dir)
165165
opts.object_dir = get_object_directory();
166166

167-
if (argc == 0)
167+
if (!argc)
168168
goto usage;
169169

170170
if (!strcmp(argv[0], "repack"))
@@ -175,10 +175,9 @@ int cmd_multi_pack_index(int argc, const char **argv,
175175
return cmd_multi_pack_index_verify(argc, argv);
176176
else if (!strcmp(argv[0], "expire"))
177177
return cmd_multi_pack_index_expire(argc, argv);
178-
else {
179-
error(_("unrecognized subcommand: %s"), argv[0]);
178+
179+
error(_("unrecognized subcommand: %s"), argv[0]);
180180
usage:
181-
usage_with_options(builtin_multi_pack_index_usage,
182-
builtin_multi_pack_index_options);
183-
}
181+
usage_with_options(builtin_multi_pack_index_usage,
182+
builtin_multi_pack_index_options);
184183
}

t/t5318-commit-graph.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ test_description='commit graph'
55

66
GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
77

8+
test_expect_success 'usage' '
9+
test_expect_code 129 git commit-graph write blah 2>err &&
10+
test_expect_code 129 git commit-graph write verify
11+
'
12+
13+
test_expect_success 'usage shown without sub-command' '
14+
test_expect_code 129 git commit-graph 2>err &&
15+
! grep error: err
16+
'
17+
18+
test_expect_success 'usage shown with an error on unknown sub-command' '
19+
cat >expect <<-\EOF &&
20+
error: unrecognized subcommand: unknown
21+
EOF
22+
test_expect_code 129 git commit-graph unknown 2>stderr &&
23+
grep error stderr >actual &&
24+
test_cmp expect actual
25+
'
26+
827
test_expect_success 'setup full repo' '
928
mkdir full &&
1029
cd "$TRASH_DIRECTORY/full" &&

0 commit comments

Comments
 (0)