Skip to content

Commit 2183baf

Browse files
committed
Merge branch 'tb/commit-graph-split-merge' into next
The code to write out the commit-graph has been taught a few options to control if the resulting graph chains should be merged or a single new incremental graph is created. * tb/commit-graph-split-merge: builtin/commit-graph.c: support '--input=graphed' builtin/commit-graph.c: introduce '--input=<source>' builtin/commit-graph.c: support '--split[=<strategy>]'
2 parents 9fadedd + 8db4663 commit 2183baf

File tree

6 files changed

+205
-56
lines changed

6 files changed

+205
-56
lines changed

Documentation/git-commit-graph.txt

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,42 @@ COMMANDS
3939
--------
4040
'write'::
4141

42-
Write a commit-graph file based on the commits found in packfiles.
42+
Write a commit-graph file based on the specified sources of input:
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
+
60-
With the `--split` option, write the commit-graph as a chain of multiple
61-
commit-graph files stored in `<dir>/info/commit-graphs`. The new commits
62-
not already in the commit-graph are added in a new "tip" file. This file
63-
is merged with the existing file if the following merge conditions are
64-
met:
60+
With the `--input=graphed` option, behave as if `--input=append` were
61+
given, but do not walk other packs to find additional commits.
62+
+
63+
If none of the above options are given, then generate the new
64+
commit-graph by walking over all pack-indexes.
65+
+
66+
With the `--split[=<strategy>]` option, write the commit-graph as a
67+
chain of multiple commit-graph files stored in
68+
`<dir>/info/commit-graphs`. Commit-graph layers are merged based on the
69+
strategy and other splitting options. The new commits not already in the
70+
commit-graph are added in a new "tip" file. This file is merged with the
71+
existing file if the following merge conditions are met:
72+
+
73+
* If `--split=merge-always` is specified, then a merge is always
74+
conducted, and the remaining options are ignored. Conversely, if
75+
`--split=no-merge` is specified, a merge is never performed, and the
76+
remaining options are ignored. A bare `--split` defers to the remaining
77+
options.
6578
+
6679
* If `--size-multiple=<X>` is not specified, let `X` equal 2. If the new
6780
tip file would have `N` commits and the previous tip has `M` commits and
@@ -99,20 +112,20 @@ $ git commit-graph write
99112
using commits in `<pack-index>`.
100113
+
101114
------------------------------------------------
102-
$ echo <pack-index> | git commit-graph write --stdin-packs
115+
$ echo <pack-index> | git commit-graph write --input=stdin-packs
103116
------------------------------------------------
104117

105118
* Write a commit-graph file containing all reachable commits.
106119
+
107120
------------------------------------------------
108-
$ git show-ref -s | git commit-graph write --stdin-commits
121+
$ git show-ref -s | git commit-graph write --input=stdin-commits
109122
------------------------------------------------
110123

111124
* Write a commit-graph file containing all commits in the current
112125
commit-graph file along with those reachable from `HEAD`.
113126
+
114127
------------------------------------------------
115-
$ git rev-parse HEAD | git commit-graph write --stdin-commits --append
128+
$ git rev-parse HEAD | git commit-graph write --input=stdin-commits --input=append
116129
------------------------------------------------
117130

118131

builtin/commit-graph.c

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
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|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
12+
N_("git commit-graph write [--object-dir <objdir>] "
13+
"[--split[=<strategy>]] "
14+
"[--input=<reachable|stdin-packs|stdin-commits|append|graphed>] "
15+
"[--[no-]progress] <split options>"),
1316
NULL
1417
};
1518

@@ -19,16 +22,24 @@ static const char * const builtin_commit_graph_verify_usage[] = {
1922
};
2023

2124
static const char * const builtin_commit_graph_write_usage[] = {
22-
N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
25+
N_("git commit-graph write [--object-dir <objdir>] "
26+
"[--split[=<strategy>]] "
27+
"[--input=<reachable|stdin-packs|stdin-commits|append|graphed>] "
28+
"[--[no-]progress] <split options>"),
2329
NULL
2430
};
2531

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+
COMMIT_GRAPH_INPUT_GRAPHED = (1 << 5)
38+
};
39+
2640
static struct opts_commit_graph {
2741
const char *obj_dir;
28-
int reachable;
29-
int stdin_packs;
30-
int stdin_commits;
31-
int append;
42+
enum commit_graph_input input;
3243
int split;
3344
int shallow;
3445
int progress;
@@ -56,6 +67,30 @@ static struct object_directory *find_odb(struct repository *r,
5667
return odb;
5768
}
5869

70+
static int option_parse_input(const struct option *opt, const char *arg,
71+
int unset)
72+
{
73+
enum commit_graph_input *to = opt->value;
74+
if (unset || !strcmp(arg, "packs")) {
75+
*to = 0;
76+
return 0;
77+
}
78+
79+
if (!strcmp(arg, "reachable"))
80+
*to |= COMMIT_GRAPH_INPUT_REACHABLE;
81+
else if (!strcmp(arg, "stdin-packs"))
82+
*to |= COMMIT_GRAPH_INPUT_STDIN_PACKS;
83+
else if (!strcmp(arg, "stdin-commits"))
84+
*to |= COMMIT_GRAPH_INPUT_STDIN_COMMITS;
85+
else if (!strcmp(arg, "append"))
86+
*to |= COMMIT_GRAPH_INPUT_APPEND;
87+
else if (!strcmp(arg, "graphed"))
88+
*to |= (COMMIT_GRAPH_INPUT_APPEND | COMMIT_GRAPH_INPUT_GRAPHED);
89+
else
90+
die(_("unrecognized --input source, %s"), arg);
91+
return 0;
92+
}
93+
5994
static int graph_verify(int argc, const char **argv)
6095
{
6196
struct commit_graph *graph = NULL;
@@ -114,6 +149,27 @@ static int graph_verify(int argc, const char **argv)
114149
extern int read_replace_refs;
115150
static struct split_commit_graph_opts split_opts;
116151

152+
static int write_option_parse_split(const struct option *opt, const char *arg,
153+
int unset)
154+
{
155+
enum commit_graph_split_flags *flags = opt->value;
156+
157+
opts.split = 1;
158+
if (!arg) {
159+
*flags = COMMIT_GRAPH_SPLIT_MERGE_AUTO;
160+
return 0;
161+
}
162+
163+
if (!strcmp(arg, "merge-all"))
164+
*flags = COMMIT_GRAPH_SPLIT_MERGE_REQUIRED;
165+
else if (!strcmp(arg, "no-merge"))
166+
*flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
167+
else
168+
die(_("unrecognized --split argument, %s"), arg);
169+
170+
return 0;
171+
}
172+
117173
static int graph_write(int argc, const char **argv)
118174
{
119175
struct string_list *pack_indexes = NULL;
@@ -127,17 +183,26 @@ static int graph_write(int argc, const char **argv)
127183
OPT_STRING(0, "object-dir", &opts.obj_dir,
128184
N_("dir"),
129185
N_("The object directory to store the graph")),
130-
OPT_BOOL(0, "reachable", &opts.reachable,
131-
N_("start walk at all refs")),
132-
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
133-
N_("scan pack-indexes listed by stdin for commits")),
134-
OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
135-
N_("start walk at commits listed by stdin")),
136-
OPT_BOOL(0, "append", &opts.append,
137-
N_("include all commits already in the commit-graph file")),
186+
OPT_CALLBACK(0, "input", &opts.input, NULL,
187+
N_("include commits from this source in the graph"),
188+
option_parse_input),
189+
OPT_BIT(0, "reachable", &opts.input,
190+
N_("start walk at all refs"),
191+
COMMIT_GRAPH_INPUT_REACHABLE),
192+
OPT_BIT(0, "stdin-packs", &opts.input,
193+
N_("scan pack-indexes listed by stdin for commits"),
194+
COMMIT_GRAPH_INPUT_STDIN_PACKS),
195+
OPT_BIT(0, "stdin-commits", &opts.input,
196+
N_("start walk at commits listed by stdin"),
197+
COMMIT_GRAPH_INPUT_STDIN_COMMITS),
198+
OPT_BIT(0, "append", &opts.input,
199+
N_("include all commits already in the commit-graph file"),
200+
COMMIT_GRAPH_INPUT_APPEND),
138201
OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
139-
OPT_BOOL(0, "split", &opts.split,
140-
N_("allow writing an incremental commit-graph file")),
202+
OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
203+
N_("allow writing an incremental commit-graph file"),
204+
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
205+
write_option_parse_split),
141206
OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
142207
N_("maximum number of commits in a non-base split commit-graph")),
143208
OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
@@ -158,12 +223,16 @@ static int graph_write(int argc, const char **argv)
158223
builtin_commit_graph_write_options,
159224
builtin_commit_graph_write_usage, 0);
160225

161-
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
162-
die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
226+
if ((!!(opts.input & COMMIT_GRAPH_INPUT_REACHABLE) +
227+
!!(opts.input & COMMIT_GRAPH_INPUT_STDIN_PACKS) +
228+
!!(opts.input & COMMIT_GRAPH_INPUT_STDIN_COMMITS)) > 1)
229+
die(_("use at most one of --input=reachable, --input=stdin-commits, or --input=stdin-packs"));
163230
if (!opts.obj_dir)
164231
opts.obj_dir = get_object_directory();
165-
if (opts.append)
232+
if (opts.input & COMMIT_GRAPH_INPUT_APPEND)
166233
flags |= COMMIT_GRAPH_WRITE_APPEND;
234+
if (opts.input & COMMIT_GRAPH_INPUT_GRAPHED)
235+
flags |= COMMIT_GRAPH_WRITE_NO_INPUT;
167236
if (opts.split)
168237
flags |= COMMIT_GRAPH_WRITE_SPLIT;
169238
if (opts.progress)
@@ -172,22 +241,22 @@ static int graph_write(int argc, const char **argv)
172241
read_replace_refs = 0;
173242
odb = find_odb(the_repository, opts.obj_dir);
174243

175-
if (opts.reachable) {
244+
if (opts.input & COMMIT_GRAPH_INPUT_REACHABLE) {
176245
if (write_commit_graph_reachable(odb, flags, &split_opts))
177246
return 1;
178247
return 0;
179248
}
180249

181250
string_list_init(&lines, 0);
182-
if (opts.stdin_packs || opts.stdin_commits) {
251+
if (opts.input & (COMMIT_GRAPH_INPUT_STDIN_PACKS | COMMIT_GRAPH_INPUT_STDIN_COMMITS)) {
183252
struct strbuf buf = STRBUF_INIT;
184253

185254
while (strbuf_getline(&buf, stdin) != EOF)
186255
string_list_append(&lines, strbuf_detach(&buf, NULL));
187256

188-
if (opts.stdin_packs)
257+
if (opts.input & COMMIT_GRAPH_INPUT_STDIN_PACKS)
189258
pack_indexes = &lines;
190-
if (opts.stdin_commits) {
259+
if (opts.input & COMMIT_GRAPH_INPUT_STDIN_COMMITS) {
191260
commit_hex = &lines;
192261
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
193262
}

commit-graph.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ struct write_commit_graph_context {
788788
unsigned append:1,
789789
report_progress:1,
790790
split:1,
791-
check_oids:1;
791+
check_oids:1,
792+
no_input:1;
792793

793794
const struct split_commit_graph_opts *split_opts;
794795
};
@@ -1533,27 +1534,33 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
15331534

15341535
int max_commits = 0;
15351536
int size_mult = 2;
1537+
enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_MERGE_AUTO;
15361538

15371539
if (ctx->split_opts) {
15381540
max_commits = ctx->split_opts->max_commits;
15391541

15401542
if (ctx->split_opts->size_multiple)
15411543
size_mult = ctx->split_opts->size_multiple;
1544+
1545+
flags = ctx->split_opts->flags;
15421546
}
15431547

15441548
g = ctx->r->objects->commit_graph;
15451549
num_commits = ctx->commits.nr;
15461550
ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
15471551

1548-
while (g && (g->num_commits <= size_mult * num_commits ||
1549-
(max_commits && num_commits > max_commits))) {
1550-
if (g->odb != ctx->odb)
1551-
break;
1552+
if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED) {
1553+
while (g && (g->num_commits <= size_mult * num_commits ||
1554+
(max_commits && num_commits > max_commits) ||
1555+
(flags == COMMIT_GRAPH_SPLIT_MERGE_REQUIRED))) {
1556+
if (g->odb != ctx->odb)
1557+
break;
15521558

1553-
num_commits += g->num_commits;
1554-
g = g->base_graph;
1559+
num_commits += g->num_commits;
1560+
g = g->base_graph;
15551561

1556-
ctx->num_commit_graphs_after--;
1562+
ctx->num_commit_graphs_after--;
1563+
}
15571564
}
15581565

15591566
ctx->new_base_graph = g;
@@ -1775,6 +1782,7 @@ int write_commit_graph(struct object_directory *odb,
17751782
ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
17761783
ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
17771784
ctx->split_opts = split_opts;
1785+
ctx->no_input = flags & COMMIT_GRAPH_WRITE_NO_INPUT ? 1 : 0;
17781786

17791787
if (ctx->split) {
17801788
struct commit_graph *g;
@@ -1833,7 +1841,7 @@ int write_commit_graph(struct object_directory *odb,
18331841
goto cleanup;
18341842
}
18351843

1836-
if (!pack_indexes && !commit_hex)
1844+
if (!ctx->no_input && !pack_indexes && !commit_hex)
18371845
fill_oids_from_all_packs(ctx);
18381846

18391847
close_reachable(ctx);
@@ -1857,7 +1865,7 @@ int write_commit_graph(struct object_directory *odb,
18571865
goto cleanup;
18581866
}
18591867

1860-
if (!ctx->commits.nr)
1868+
if (!ctx->commits.nr && (!ctx->split_opts || ctx->split_opts->flags != COMMIT_GRAPH_SPLIT_MERGE_REQUIRED))
18611869
goto cleanup;
18621870

18631871
if (ctx->split) {

commit-graph.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,21 @@ enum commit_graph_write_flags {
7979
COMMIT_GRAPH_WRITE_PROGRESS = (1 << 1),
8080
COMMIT_GRAPH_WRITE_SPLIT = (1 << 2),
8181
/* Make sure that each OID in the input is a valid commit OID. */
82-
COMMIT_GRAPH_WRITE_CHECK_OIDS = (1 << 3)
82+
COMMIT_GRAPH_WRITE_CHECK_OIDS = (1 << 3),
83+
COMMIT_GRAPH_WRITE_NO_INPUT = (1 << 4)
84+
};
85+
86+
enum commit_graph_split_flags {
87+
COMMIT_GRAPH_SPLIT_MERGE_AUTO = 0,
88+
COMMIT_GRAPH_SPLIT_MERGE_REQUIRED = 1,
89+
COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED = 2
8390
};
8491

8592
struct split_commit_graph_opts {
8693
int size_multiple;
8794
int max_commits;
8895
timestamp_t expire_time;
96+
enum commit_graph_split_flags flags;
8997
};
9098

9199
/*

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
'

0 commit comments

Comments
 (0)