commit-graph: stop using `the_hash_algo` via macros

We have two macros `GRAPH_DATA_WIDTH` and `GRAPH_MIN_SIZE` that compute hash-dependent sizes. They do so by using the global `the_hash_algo` variable though, which we want to get rid of over time. Convert these macros into functions that accept the hash algorithm as input parameter. Adapt callers accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 15, 2025 at 07:49 UTC 3481cb7dfd4407d2dae411662e978011250ec2b2
1 file changed +16 -9
commit-graph.c
+16 -9
@@ -54,8 +54,6 @@ void git_test_write_commit_graph_or_die(void)
54 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
55 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56
57 -#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
58 -
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
59
@@ -67,8 +65,6 @@ void git_test_write_commit_graph_or_die(void)
65
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
70 -#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
71 - + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
68
69 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
70
@@ -81,6 +77,16 @@ define_commit_slab(topo_level_slab, uint32_t);
77 define_commit_slab(commit_pos, int);
78 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
79
80 +static size_t graph_data_width(const struct git_hash_algo *algop)
81 +{
82 + return algop->rawsz + 16;
83 +}
84 +
85 +static size_t graph_min_size(const struct git_hash_algo *algop)
86 +{
87 + return GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE + GRAPH_FANOUT_SIZE + algop->rawsz;
88 +}
89 +
90 static void set_commit_pos(struct repository *r, const struct object_id *oid)
91 {
92 static int32_t max_pos;
@@ -259,7 +265,7 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
265
266 graph_size = xsize_t(st->st_size);
267
262 - if (graph_size < GRAPH_MIN_SIZE) {
268 + if (graph_size < graph_min_size(the_hash_algo)) {
269 close(fd);
270 error(_("commit-graph file is too small"));
271 return NULL;
@@ -315,7 +321,7 @@ static int graph_read_commit_data(const unsigned char *chunk_start,
321 size_t chunk_size, void *data)
322 {
323 struct commit_graph *g = data;
318 - if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
324 + if (chunk_size / graph_data_width(the_hash_algo) != g->num_commits)
325 return error(_("commit-graph commit data chunk is wrong size"));
326 g->chunk_commit_data = chunk_start;
327 return 0;
@@ -380,7 +386,7 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
386 if (!graph_map)
387 return NULL;
388
383 - if (graph_size < GRAPH_MIN_SIZE)
389 + if (graph_size < graph_min_size(the_hash_algo))
390 return NULL;
391
392 data = (const unsigned char *)graph_map;
@@ -901,7 +907,7 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
907 die(_("invalid commit position. commit-graph is likely corrupt"));
908
909 lex_index = pos - g->num_commits_in_base;
904 - commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
910 + commit_data = g->chunk_commit_data + st_mult(graph_data_width(the_hash_algo), lex_index);
911
912 graph_data = commit_graph_data_at(item);
913 graph_data->graph_pos = pos;
@@ -1105,7 +1111,8 @@ static struct tree *load_tree_for_commit(struct repository *r,
1111 g = g->base_graph;
1112
1113 commit_data = g->chunk_commit_data +
1108 - st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1114 + st_mult(graph_data_width(the_hash_algo),
1115 + graph_pos - g->num_commits_in_base);
1116
1117 oidread(&oid, commit_data, the_repository->hash_algo);
1118 set_commit_tree(c, lookup_tree(r, &oid));