Skip to content

Commit a599e2c

Browse files
ttaylorrgitster
authored andcommitted
builtin/commit-graph.c: introduce '--input=<source>'
The 'write' mode of the 'commit-graph' supports input from a number of different sources: pack indexes over stdin, commits over stdin, commits reachable from all references, and so on. Each of these options are specified with a unique option: '--stdin-packs', '--stdin-commits', etc. Similar to our replacement of 'git config [--<type>]' with 'git config [--type=<type>]' (c.f., fb0dc3b (builtin/config.c: support `--type=<type>` as preferred alias for `--<type>`, 2018-04-18)), softly deprecate '[--<input>]' in favor of '[--input=<source>]'. This makes it more clear to implement new options that are combinations of other options (such as, for example, "none", a combination of the old "--append" and a new sentinel to specify to _not_ look in other packs, which we will implement in a future patch). Unfortunately, the new enumerated type is a bitfield, even though it makes much more sense as '0, 1, 2, ...'. Even though *almost* all options are pairwise exclusive, '--stdin-{packs,commits}' *is* compatible with '--append'. For this reason, use a bitfield. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d5916f commit a599e2c

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

Documentation/git-commit-graph.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ COMMANDS
4141

4242
Write a commit-graph file based on the commits found in packfiles.
4343
+
44-
With the `--stdin-packs` option, generate the new commit graph by
44+
With the `--input=stdin-packs` option, generate the new commit graph by
4545
walking objects only in the specified pack-indexes. (Cannot be combined
46-
with `--stdin-commits` or `--reachable`.)
46+
with `--input=stdin-commits` or `--input=reachable`.)
4747
+
48-
With the `--stdin-commits` option, generate the new commit graph by
49-
walking commits starting at the commits specified in stdin as a list
48+
With the `--input=stdin-commits` option, generate the new commit graph
49+
by walking commits starting at the commits specified in stdin as a list
5050
of OIDs in hex, one OID per line. (Cannot be combined with
51-
`--stdin-packs` or `--reachable`.)
51+
`--input=stdin-packs` or `--input=reachable`.)
5252
+
53-
With the `--reachable` option, generate the new commit graph by walking
54-
commits starting at all refs. (Cannot be combined with `--stdin-commits`
55-
or `--stdin-packs`.)
53+
With the `--input=reachable` option, generate the new commit graph by
54+
walking commits starting at all refs. (Cannot be combined with
55+
`--input=stdin-commits` or `--input=stdin-packs`.)
5656
+
57-
With the `--append` option, include all commits that are present in the
58-
existing commit-graph file.
57+
With the `--input=append` option, include all commits that are present
58+
in the existing commit-graph file.
5959
+
6060
With the `--split[=<strategy>]` option, write the commit-graph as a
6161
chain of multiple commit-graph files stored in
@@ -106,20 +106,20 @@ $ git commit-graph write
106106
using commits in `<pack-index>`.
107107
+
108108
------------------------------------------------
109-
$ echo <pack-index> | git commit-graph write --stdin-packs
109+
$ echo <pack-index> | git commit-graph write --input=stdin-packs
110110
------------------------------------------------
111111

112112
* Write a commit-graph file containing all reachable commits.
113113
+
114114
------------------------------------------------
115-
$ git show-ref -s | git commit-graph write --stdin-commits
115+
$ git show-ref -s | git commit-graph write --input=stdin-commits
116116
------------------------------------------------
117117

118118
* Write a commit-graph file containing all commits in the current
119119
commit-graph file along with those reachable from `HEAD`.
120120
+
121121
------------------------------------------------
122-
$ git rev-parse HEAD | git commit-graph write --stdin-commits --append
122+
$ git rev-parse HEAD | git commit-graph write --input=stdin-commits --input=append
123123
------------------------------------------------
124124

125125

builtin/commit-graph.c

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
static char const * const builtin_commit_graph_usage[] = {
1111
N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
12-
N_("git commit-graph write [--object-dir <objdir>] [--append] "
13-
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
12+
N_("git commit-graph write [--object-dir <objdir>] "
13+
"[--split[=<strategy>]] "
14+
"[--input=<reachable|stdin-packs|stdin-commits|append>] "
1415
"[--[no-]progress] <split options>"),
1516
NULL
1617
};
@@ -21,18 +22,23 @@ static const char * const builtin_commit_graph_verify_usage[] = {
2122
};
2223

2324
static const char * const builtin_commit_graph_write_usage[] = {
24-
N_("git commit-graph write [--object-dir <objdir>] [--append] "
25-
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
25+
N_("git commit-graph write [--object-dir <objdir>] "
26+
"[--split[=<strategy>]] "
27+
"[--input=<reachable|stdin-packs|stdin-commits|append>] "
2628
"[--[no-]progress] <split options>"),
2729
NULL
2830
};
2931

32+
enum commit_graph_input {
33+
COMMIT_GRAPH_INPUT_REACHABLE = (1 << 1),
34+
COMMIT_GRAPH_INPUT_STDIN_PACKS = (1 << 2),
35+
COMMIT_GRAPH_INPUT_STDIN_COMMITS = (1 << 3),
36+
COMMIT_GRAPH_INPUT_APPEND = (1 << 4)
37+
};
38+
3039
static struct opts_commit_graph {
3140
const char *obj_dir;
32-
int reachable;
33-
int stdin_packs;
34-
int stdin_commits;
35-
int append;
41+
enum commit_graph_input input;
3642
int split;
3743
int shallow;
3844
int progress;
@@ -57,6 +63,28 @@ static struct object_directory *find_odb(struct repository *r,
5763
return odb;
5864
}
5965

66+
static int option_parse_input(const struct option *opt, const char *arg,
67+
int unset)
68+
{
69+
enum commit_graph_input *to = opt->value;
70+
if (unset || !strcmp(arg, "packs")) {
71+
*to = 0;
72+
return 0;
73+
}
74+
75+
if (!strcmp(arg, "reachable"))
76+
*to |= COMMIT_GRAPH_INPUT_REACHABLE;
77+
else if (!strcmp(arg, "stdin-packs"))
78+
*to |= COMMIT_GRAPH_INPUT_STDIN_PACKS;
79+
else if (!strcmp(arg, "stdin-commits"))
80+
*to |= COMMIT_GRAPH_INPUT_STDIN_COMMITS;
81+
else if (!strcmp(arg, "append"))
82+
*to |= COMMIT_GRAPH_INPUT_APPEND;
83+
else
84+
die(_("unrecognized --input source, %s"), arg);
85+
return 0;
86+
}
87+
6088
static int graph_verify(int argc, const char **argv)
6189
{
6290
struct commit_graph *graph = NULL;
@@ -149,14 +177,21 @@ static int graph_write(int argc, const char **argv)
149177
OPT_STRING(0, "object-dir", &opts.obj_dir,
150178
N_("dir"),
151179
N_("The object directory to store the graph")),
152-
OPT_BOOL(0, "reachable", &opts.reachable,
153-
N_("start walk at all refs")),
154-
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
155-
N_("scan pack-indexes listed by stdin for commits")),
156-
OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
157-
N_("start walk at commits listed by stdin")),
158-
OPT_BOOL(0, "append", &opts.append,
159-
N_("include all commits already in the commit-graph file")),
180+
OPT_CALLBACK(0, "input", &opts.input, NULL,
181+
N_("include commits from this source in the graph"),
182+
option_parse_input),
183+
OPT_BIT(0, "reachable", &opts.input,
184+
N_("start walk at all refs"),
185+
COMMIT_GRAPH_INPUT_REACHABLE),
186+
OPT_BIT(0, "stdin-packs", &opts.input,
187+
N_("scan pack-indexes listed by stdin for commits"),
188+
COMMIT_GRAPH_INPUT_STDIN_PACKS),
189+
OPT_BIT(0, "stdin-commits", &opts.input,
190+
N_("start walk at commits listed by stdin"),
191+
COMMIT_GRAPH_INPUT_STDIN_COMMITS),
192+
OPT_BIT(0, "append", &opts.input,
193+
N_("include all commits already in the commit-graph file"),
194+
COMMIT_GRAPH_INPUT_APPEND),
160195
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
161196
OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
162197
N_("allow writing an incremental commit-graph file"),
@@ -182,11 +217,13 @@ static int graph_write(int argc, const char **argv)
182217
builtin_commit_graph_write_options,
183218
builtin_commit_graph_write_usage, 0);
184219

185-
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
186-
die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
220+
if ((!!(opts.input & COMMIT_GRAPH_INPUT_REACHABLE) +
221+
!!(opts.input & COMMIT_GRAPH_INPUT_STDIN_PACKS) +
222+
!!(opts.input & COMMIT_GRAPH_INPUT_STDIN_COMMITS)) > 1)
223+
die(_("use at most one of --input=reachable, --input=stdin-commits, or --input=stdin-packs"));
187224
if (!opts.obj_dir)
188225
opts.obj_dir = get_object_directory();
189-
if (opts.append)
226+
if (opts.input & COMMIT_GRAPH_INPUT_APPEND)
190227
flags |= COMMIT_GRAPH_WRITE_APPEND;
191228
if (opts.split)
192229
flags |= COMMIT_GRAPH_WRITE_SPLIT;
@@ -196,22 +233,22 @@ static int graph_write(int argc, const char **argv)
196233
read_replace_refs = 0;
197234
odb = find_odb(the_repository, opts.obj_dir);
198235

199-
if (opts.reachable) {
236+
if (opts.input & COMMIT_GRAPH_INPUT_REACHABLE) {
200237
if (write_commit_graph_reachable(odb, flags, &split_opts))
201238
return 1;
202239
return 0;
203240
}
204241

205242
string_list_init(&lines, 0);
206-
if (opts.stdin_packs || opts.stdin_commits) {
243+
if (opts.input & (COMMIT_GRAPH_INPUT_STDIN_PACKS | COMMIT_GRAPH_INPUT_STDIN_COMMITS)) {
207244
struct strbuf buf = STRBUF_INIT;
208245

209246
while (strbuf_getline(&buf, stdin) != EOF)
210247
string_list_append(&lines, strbuf_detach(&buf, NULL));
211248

212-
if (opts.stdin_packs)
249+
if (opts.input & COMMIT_GRAPH_INPUT_STDIN_PACKS)
213250
pack_indexes = &lines;
214-
if (opts.stdin_commits) {
251+
if (opts.input & COMMIT_GRAPH_INPUT_STDIN_COMMITS) {
215252
commit_hex = &lines;
216253
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
217254
}

t/t5318-commit-graph.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
227227

228228
test_expect_success 'build graph from latest pack with closure' '
229229
cd "$TRASH_DIRECTORY/full" &&
230-
cat new-idx | git commit-graph write --stdin-packs &&
230+
cat new-idx | git commit-graph write --input=stdin-packs &&
231231
test_path_is_file $objdir/info/commit-graph &&
232232
graph_read_expect "9" "extra_edges"
233233
'
@@ -240,7 +240,7 @@ test_expect_success 'build graph from commits with closure' '
240240
git tag -a -m "merge" tag/merge merge/2 &&
241241
git rev-parse tag/merge >commits-in &&
242242
git rev-parse merge/1 >>commits-in &&
243-
cat commits-in | git commit-graph write --stdin-commits &&
243+
cat commits-in | git commit-graph write --input=stdin-commits &&
244244
test_path_is_file $objdir/info/commit-graph &&
245245
graph_read_expect "6"
246246
'

t/t5324-split-commit-graph.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_expect_success 'create commits and write commit-graph' '
3535
test_commit $i &&
3636
git branch commits/$i || return 1
3737
done &&
38-
git commit-graph write --reachable &&
38+
git commit-graph write --input=reachable &&
3939
test_path_is_file $infodir/commit-graph &&
4040
graph_read_expect 3
4141
'

0 commit comments

Comments
 (0)