commit-graph: return commit graph from `repo_find_commit_pos_in_graph()`

The function `repo_find_commit_pos_in_graph()` takes a commit as input and tries to figure out whether the given repository has a commit graph that contains that specific commit. If so, it returns the corresponding position of that commit inside the graph. Right now though we only return the position, but not the actual graph that the commit has been found in. This is sensible as repositories always have the graph in `struct repository::objects::commit_graph`. Consequently, the caller always knows where to find it. But in a subsequent change we're going to move the graph into the object sources. This would require callers of the function to loop through all sources to find the relevant commit graph. Refactor the code so that we instead return the commit-graph that the commit has been found with. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 4, 2025 at 14:49 UTC 88bc3500e5be888c13757d12c4a5cb16e39ec673
3 files changed +23 -15
bloom.c
+5 -3
@@ -452,10 +452,12 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
452 filter = bloom_filter_slab_at(&bloom_filters, c);
453
454 if (!filter->data) {
455 + struct commit_graph *g;
456 uint32_t graph_pos;
456 - if (repo_find_commit_pos_in_graph(r, c, &graph_pos))
457 - load_bloom_filter_from_graph(r->objects->commit_graph,
458 - filter, graph_pos);
457 +
458 + g = repo_find_commit_pos_in_graph(r, c, &graph_pos);
459 + if (g)
460 + load_bloom_filter_from_graph(g, filter, graph_pos);
461 }
462
463 if (filter->data && filter->len) {
commit-graph.c
+12 -6
@@ -1003,13 +1003,16 @@ static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g,
1003 }
1004 }
1005
1006 -int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
1007 - uint32_t *pos)
1006 +struct commit_graph *repo_find_commit_pos_in_graph(struct repository *r,
1007 + struct commit *c,
1008 + uint32_t *pos)
1009 {
1010 struct commit_graph *g = prepare_commit_graph(r);
1011 if (!g)
1011 - return 0;
1012 - return find_commit_pos_in_graph(c, g, pos);
1012 + return NULL;
1013 + if (!find_commit_pos_in_graph(c, g, pos))
1014 + return NULL;
1015 + return g;
1016 }
1017
1018 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
@@ -1075,9 +1078,12 @@ int parse_commit_in_graph(struct repository *r, struct commit *item)
1078
1079 void load_commit_graph_info(struct repository *r, struct commit *item)
1080 {
1081 + struct commit_graph *g;
1082 uint32_t pos;
1079 - if (repo_find_commit_pos_in_graph(r, item, &pos))
1080 - fill_commit_graph_info(item, r->objects->commit_graph, pos);
1083 +
1084 + g = repo_find_commit_pos_in_graph(r, item, &pos);
1085 + if (g)
1086 + fill_commit_graph_info(item, g, pos);
1087 }
1088
1089 static struct tree *load_tree_for_commit(struct commit_graph *g,
commit-graph.h
+6 -6
@@ -48,10 +48,9 @@ int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st,
48 int parse_commit_in_graph(struct repository *r, struct commit *item);
49
50 /*
51 - * Fills `*pos` with the graph position of `c`, and returns 1 if `c` is
52 - * found in the commit-graph belonging to `r`, or 0 otherwise.
53 - * Initializes the commit-graph belonging to `r` if it hasn't been
54 - * already.
51 + * Fills `*pos` with the graph position of `c`, and returns the graph `c` is
52 + * found in, or NULL otherwise. Initializes the commit-graphs belonging to
53 + * `r` if it hasn't been already.
54 *
55 * Note: this is a low-level helper that does not alter any slab data
56 * associated with `c`. Useful in circumstances where the slab data is
@@ -59,8 +58,9 @@ int parse_commit_in_graph(struct repository *r, struct commit *item);
58 *
59 * In most cases, callers should use `parse_commit_in_graph()` instead.
60 */
62 -int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
63 - uint32_t *pos);
61 +struct commit_graph *repo_find_commit_pos_in_graph(struct repository *r,
62 + struct commit *c,
63 + uint32_t *pos);
64
65 /*
66 * Look up the given commit ID in the commit-graph. This will only return a