Skip to content

Commit 3298616

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 82d8475 commit 3298616

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
@@ -267,6 +267,42 @@ The merge strategy values (2 for the size multiple, 64,000 for the maximum
267267
number of commits) could be extracted into config settings for full
268268
flexibility.
269269

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

294330
[4] https://public-inbox.org/git/[email protected]/T/#u
295331
A patch to remove the ahead-behind calculation from 'status'.
332+
333+
[5] https://public-inbox.org/git/[email protected]/
334+
A discussion of a "two-dimensional graph position" that can allow reading
335+
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 int prepare_commit_graph_v1(struct repository *r, const char *obj_dir)
320320
r->objects->commit_graph = load_commit_graph_one(graph_name);
321321
free(graph_name);
322322

323+
if (r->objects->commit_graph)
324+
r->objects->commit_graph->obj_dir = obj_dir;
325+
323326
return r->objects->commit_graph ? 0 : -1;
324327
}
325328

@@ -380,8 +383,7 @@ static void prepare_commit_graph_chain(struct repository *r, const char *obj_dir
380383
oids = xcalloc(st.st_size / (the_hash_algo->hexsz + 1), sizeof(struct object_id));
381384

382385
while (strbuf_getline_lf(&line, fp) != EOF && valid) {
383-
char *graph_name;
384-
struct commit_graph *g;
386+
struct object_directory *odb;
385387

386388
if (get_oid_hex(line.buf, &oids[i])) {
387389
warning(_("invalid commit-graph chain: line '%s' not a hash"),
@@ -390,14 +392,23 @@ static void prepare_commit_graph_chain(struct repository *r, const char *obj_dir
390392
break;
391393
}
392394

393-
graph_name = get_split_graph_filename(obj_dir, line.buf);
394-
g = load_commit_graph_one(graph_name);
395-
free(graph_name);
395+
for (odb = r->objects->odb; odb; odb = odb->next) {
396+
char *graph_name = get_split_graph_filename(odb->path, line.buf);
397+
struct commit_graph *g = load_commit_graph_one(graph_name);
396398

397-
if (g && add_graph_to_chain(g, r->objects->commit_graph, oids, i))
398-
r->objects->commit_graph = g;
399-
else
400-
valid = 0;
399+
free(graph_name);
400+
401+
if (g) {
402+
g->obj_dir = odb->path;
403+
404+
if (add_graph_to_chain(g, r->objects->commit_graph, oids, i))
405+
r->objects->commit_graph = g;
406+
else
407+
valid = 0;
408+
409+
break;
410+
}
411+
}
401412
}
402413

403414
free(oids);
@@ -1397,7 +1408,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
13971408

13981409
if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
13991410
char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1400-
char *new_base_name = get_split_graph_filename(ctx->obj_dir, new_base_hash);
1411+
char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
14011412

14021413
free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
14031414
free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
@@ -1468,6 +1479,9 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
14681479

14691480
while (g && (g->num_commits <= split_strategy_size_mult * num_commits ||
14701481
num_commits > split_strategy_max_commits)) {
1482+
if (strcmp(g->obj_dir, ctx->obj_dir))
1483+
break;
1484+
14711485
num_commits += g->num_commits;
14721486
g = g->base_graph;
14731487

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

14771491
ctx->new_base_graph = g;
14781492

1493+
if (ctx->num_commit_graphs_after == 2) {
1494+
char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1495+
1496+
if (!strcmp(g->filename, old_graph_name) &&
1497+
strcmp(g->obj_dir, ctx->obj_dir)) {
1498+
ctx->num_commit_graphs_after = 1;
1499+
ctx->new_base_graph = NULL;
1500+
}
1501+
1502+
free(old_graph_name);
1503+
}
1504+
14791505
ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
14801506
ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
14811507

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
@@ -88,6 +88,21 @@ test_expect_success 'add more commits, and write a new base graph' '
8888
graph_read_expect 12
8989
'
9090

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

131146
graph_git_behavior 'merged commit-graph: commit 12 vs 6' commits/12 commits/6
132147

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

0 commit comments

Comments
 (0)