Skip to content

Commit 5cfd653

Browse files
committed
commit-graph: allow cross-alternate chains
In an environment like a fork network, it is helpful to have a commit-graph chain that spans both the base repo and the fork repo. The fork is usually a small set of data on top of the large repo, but sometimes the fork is much larger. For example, git-for-windows/git has almost double the number of commits as git/git because it rebases its commits on every major version update. To allow cross-alternate commit-graph chains, we need a few pieces: 1. When looking for a graph-{hash}.graph file, check all alternates. 2. When merging commit-graph chains, do not merge across alternates. 3. When writing a new commit-graph chain based on a commit-graph file in another object directory, do not allow success if the base file has of the name "commit-graph" instead of "commit-graphs/graoh-{hash}.graph". Signed-off-by: Derrick Stolee <[email protected]>
1 parent 87fb895 commit 5cfd653

File tree

4 files changed

+114
-10
lines changed

4 files changed

+114
-10
lines changed

Documentation/technical/commit-graph.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,42 @@ The merge strategy values (2 for the size multiple, 64,000 for the maximum
266266
number of commits) could be extracted into config settings for full
267267
flexibility.
268268

269+
## Chains across multiple object directories
270+
271+
In a repo with alternates, we look for the `commit-graph-chain` file starting
272+
in the local object directory and then in each alternate. The first file that
273+
exists defines our chain. As we look for the `graph-{hash}` files for
274+
each `{hash}` in the chain file, we follow the same pattern for the host
275+
directories.
276+
277+
This allows commit-graphs to be split across multiple forks in a fork network.
278+
The typical case is a large "base" repo with many smaller forks.
279+
280+
As the base repo advances, it will likely update and merge its commit-graph
281+
chain more frequently than the forks. If a fork updates their commit-graph after
282+
the base repo, then it should "reparent" the commit-graph chain onto the new
283+
chain in the base repo. When reading each `graph-{hash}` file, we track
284+
the object directory containing it. During a write of a new commit-graph file,
285+
we check for any changes in the source object directory and read the
286+
`commit-graph-chain` file for that source and create a new file based on those
287+
files. During this "reparent" operation, we necessarily need to collapse all
288+
levels in the fork, as all of the files are invalid against the new base file.
289+
290+
It is crucial to be careful when cleaning up "unreferenced" `graph-{hash}.graph`
291+
files in this scenario. It falls to the user to define the proper settings for
292+
their custom environment:
293+
294+
1. When merging levels in the base repo, the unreferenced files may still be
295+
referenced by chains from fork repos.
296+
297+
2. The expiry time should be set to a length of time such that every fork has
298+
time to recompute their commit-graph chain to "reparent" onto the new base
299+
file(s).
300+
301+
3. If the commit-graph chain is updated in the base, the fork will not have
302+
access to the new chain until its chain is updated to reference those files.
303+
(This may change in the future [5].)
304+
269305
Related Links
270306
-------------
271307
[0] https://bugs.chromium.org/p/git/issues/detail?id=8
@@ -292,3 +328,7 @@ Related Links
292328

293329
[4] https://public-inbox.org/git/[email protected]/T/#u
294330
A patch to remove the ahead-behind calculation from 'status'.
331+
332+
[5] https://public-inbox.org/git/[email protected]/
333+
A discussion of a "two-dimensional graph position" that can allow reading
334+
multiple commit-graph chains at the same time.

commit-graph.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ static struct commit_graph *load_commit_graph_v1(struct repository *r, const cha
320320
struct commit_graph *g = load_commit_graph_one(graph_name);
321321
free(graph_name);
322322

323+
if (g)
324+
g->obj_dir = obj_dir;
325+
323326
return g;
324327
}
325328

@@ -385,8 +388,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
385388
oids = xcalloc(st.st_size / (the_hash_algo->hexsz + 1), sizeof(struct object_id));
386389

387390
while (strbuf_getline_lf(&line, fp) != EOF && valid) {
388-
char *graph_name;
389-
struct commit_graph *g;
391+
struct object_directory *odb;
390392

391393
if (get_oid_hex(line.buf, &oids[i])) {
392394
warning(_("invalid commit-graph chain: line '%s' not a hash"),
@@ -395,14 +397,23 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
395397
break;
396398
}
397399

398-
graph_name = get_split_graph_filename(obj_dir, line.buf);
399-
g = load_commit_graph_one(graph_name);
400-
free(graph_name);
400+
for (odb = r->objects->odb; odb; odb = odb->next) {
401+
char *graph_name = get_split_graph_filename(odb->path, line.buf);
402+
struct commit_graph *g = load_commit_graph_one(graph_name);
401403

402-
if (g && add_graph_to_chain(g, graph_chain, oids, i))
403-
graph_chain = g;
404-
else
405-
valid = 0;
404+
free(graph_name);
405+
406+
if (g) {
407+
g->obj_dir = odb->path;
408+
409+
if (add_graph_to_chain(g, graph_chain, oids, i))
410+
graph_chain = g;
411+
else
412+
valid = 0;
413+
414+
break;
415+
}
416+
}
406417
}
407418

408419
free(oids);
@@ -1411,7 +1422,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
14111422

14121423
if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
14131424
char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1414-
char *new_base_name = get_split_graph_filename(ctx->obj_dir, new_base_hash);
1425+
char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
14151426

14161427
free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
14171428
free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
@@ -1482,6 +1493,9 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
14821493

14831494
while (g && (g->num_commits <= split_strategy_size_mult * num_commits ||
14841495
num_commits > split_strategy_max_commits)) {
1496+
if (strcmp(g->obj_dir, ctx->obj_dir))
1497+
break;
1498+
14851499
num_commits += g->num_commits;
14861500
g = g->base_graph;
14871501

@@ -1490,6 +1504,18 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
14901504

14911505
ctx->new_base_graph = g;
14921506

1507+
if (ctx->num_commit_graphs_after == 2) {
1508+
char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1509+
1510+
if (!strcmp(g->filename, old_graph_name) &&
1511+
strcmp(g->obj_dir, ctx->obj_dir)) {
1512+
ctx->num_commit_graphs_after = 1;
1513+
ctx->new_base_graph = NULL;
1514+
}
1515+
1516+
free(old_graph_name);
1517+
}
1518+
14931519
ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
14941520
ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
14951521

commit-graph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct commit_graph {
4848
uint32_t num_commits;
4949
struct object_id oid;
5050
char *filename;
51+
const char *obj_dir;
5152

5253
uint32_t num_commits_in_base;
5354
struct commit_graph *base_graph;

t/t5323-split-commit-graph.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ test_expect_success 'add more commits, and write a new base graph' '
9090
graph_read_expect 12
9191
'
9292

93+
test_expect_success 'fork and fail to base a chain on a commit-graph file' '
94+
test_when_finished rm -rf fork &&
95+
git clone . fork &&
96+
(
97+
cd fork &&
98+
rm .git/objects/info/commit-graph &&
99+
echo "$TRASH_DIRECTORY/.git/objects" >.git/objects/info/alternates &&
100+
test_commit new-commit &&
101+
git commit-graph write --reachable --split &&
102+
test_path_is_file $graphdir/commit-graph-chain &&
103+
test_line_count = 1 $graphdir/commit-graph-chain &&
104+
verify_chain_files_exist $graphdir
105+
)
106+
'
107+
93108
test_expect_success 'add three more commits, write a tip graph' '
94109
git reset --hard commits/3 &&
95110
git merge merge/1 &&
@@ -132,4 +147,26 @@ test_expect_success 'add one commit, write a merged graph' '
132147

133148
graph_git_behavior 'merged commit-graph: commit 12 vs 6' commits/12 commits/6
134149

150+
test_expect_success 'create fork and chain across alternate' '
151+
git clone . fork &&
152+
(
153+
cd fork &&
154+
git config core.commitGraph true &&
155+
rm -rf $graphdir &&
156+
echo "$TRASH_DIRECTORY/.git/objects" >.git/objects/info/alternates &&
157+
test_commit 13 &&
158+
git branch commits/13 &&
159+
git commit-graph write --reachable --split &&
160+
test_path_is_file $graphdir/commit-graph-chain &&
161+
test_line_count = 3 $graphdir/commit-graph-chain &&
162+
ls $graphdir/graph-*.graph >graph-files &&
163+
test_line_count = 1 graph-files &&
164+
git -c core.commitGraph=true rev-list HEAD >expect &&
165+
git -c core.commitGraph=false rev-list HEAD >actual &&
166+
test_cmp expect actual
167+
)
168+
'
169+
170+
graph_git_behavior 'alternate: commit 13 vs 6' commits/13 commits/6
171+
135172
test_done

0 commit comments

Comments
 (0)