From: Patrick Steinhardt Date: Fri, 20 Nov 2026 12:22:50 GMT Subject: [PATCH v2 2/7] commit-graph: simplify linking commit graph chains Message-ID: <20261120-pks-odb-pluggable-commit-graphs-v2-2-31f2341a9932@pks.im> In-Reply-To: <20261120-pks-odb-pluggable-commit-graphs-v2-0-31f2341a9932@pks.im> When we load a commit graph chain we verify for every layer that the hash of the base commit graph matches what the current tip has in its base graphs chunk. To do so we pass both the chain itself as well as the array of object IDs that we have read from the commit-graph chain file to `add_graph_to_chain()`. But the array of object IDs is redundant information: we have already loaded and linked the base layers, and as part of that we have stored their respective hashes in the structure itself. Consequently, we can already derive the hashes by walking the chain of base layers. Refactor the function to only take the new tip and base layers as input and derive all the other information ad-hoc and make the function public. This prepares for a subsequent commit, where we are about to move the logic that creates commit graph chains into the object database backend. While at it, align on error semantics so that we return 0 on success, or a negative error code otherwise. Signed-off-by: Patrick Steinhardt --- commit-graph.c | 50 ++++++++++++++++++++++++++------------------------ commit-graph.h | 8 ++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/commit-graph.c b/commit-graph.c index 0c75939910d..9e5656992eb 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -575,44 +575,48 @@ static void validate_mixed_bloom_settings(struct commit_graph *g) } } -static int add_graph_to_chain(struct commit_graph *g, - struct commit_graph *chain, - struct object_id *oids, - int n) +int commit_graph_link_base(struct commit_graph *g, + struct commit_graph *base) { + struct commit_graph *layer; + size_t n = 0; + + for (layer = base; layer; layer = layer->base_graph) + n++; + if (n && !g->chunk_base_graphs) { warning(_("commit-graph has no base graphs chunk")); - return 0; + return -1; } if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) { warning(_("commit-graph base graphs chunk is too small")); - return 0; + return -1; } - while (n) { + for (layer = base; layer; layer = layer->base_graph) { n--; - - if (!hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n), + if (!hasheq(layer->oid.hash, + g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n), g->hash_algo)) { warning(_("commit-graph chain does not match")); - return 0; + return -1; } } - if (chain) { - if (unsigned_add_overflows(chain->num_commits, - chain->num_commits_in_base)) { + if (base) { + if (unsigned_add_overflows(base->num_commits, + base->num_commits_in_base)) { warning(_("commit count in base graph too high: %"PRIuMAX), - (uintmax_t)chain->num_commits_in_base); - return 0; + (uintmax_t)base->num_commits_in_base); + return -1; } - g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base; + g->num_commits_in_base = base->num_commits + base->num_commits_in_base; } - g->base_graph = chain; + g->base_graph = base; - return 1; + return 0; } int open_commit_graph_chain(const char *chain_file, @@ -646,20 +650,19 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, { struct commit_graph *graph_chain = NULL; struct strbuf line = STRBUF_INIT; - struct object_id *oids; int i = 0, valid = 1, count; FILE *fp = xfdopen(fd, "r"); count = st->st_size / (odb->repo->hash_algo->hexsz + 1); - CALLOC_ARRAY(oids, count); for (i = 0; i < count; i++) { struct odb_source_files *files; + struct object_id oid; if (strbuf_getline_lf(&line, fp) == EOF) break; - if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) { + if (get_oid_hex_algop(line.buf, &oid, odb->repo->hash_algo)) { warning(_("invalid commit-graph chain: line '%s' not a hash"), line.buf); valid = 0; @@ -680,9 +683,9 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, * Verify that the loaded commit graph's * trailer matches the chain file. */ - if (!oideq(&g->oid, &oids[i])) { + if (!oideq(&g->oid, &oid)) { warning(_("commit-graph chain does not match")); - } else if (add_graph_to_chain(g, graph_chain, oids, i)) { + } else if (!commit_graph_link_base(g, graph_chain)) { graph_chain = g; valid = 1; } @@ -702,7 +705,6 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, validate_mixed_generation_chain(graph_chain); validate_mixed_bloom_settings(graph_chain); - free(oids); fclose(fp); strbuf_release(&line); diff --git a/commit-graph.h b/commit-graph.h index 2156499a688..f0e7cc93461 100644 --- a/commit-graph.h +++ b/commit-graph.h @@ -128,6 +128,14 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo, struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, int fd, struct stat *st, int *incomplete_chain); + +/* + * Link the commit graph layer `g` on top of the chain `base`. `base` may be + * NULL if `g` is the root of a new chain. Returns 0 on success, a negative + * error code otherwise. + */ +int commit_graph_link_base(struct commit_graph *g, + struct commit_graph *base); struct commit_graph *read_commit_graph_one(struct repository *repo, const char *dir); -- 2.56.1.831.g720f3f5937.dirty