commit-graph: stop using `the_hash_algo`

Stop using `the_hash_algo` as it implicitly relies on `the_repository`. Instead, we either use the hash algo provided via the context or, if there is no such hash algo, we use `the_repository` explicitly. Such uses will be removed in subsequent commits. 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 89cc9b9adf31729c91aa94c178b44b45febd260f
3 files changed +18 -15
builtin/commit-graph.c
+2 -1
@@ -109,7 +109,8 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
109 opened = OPENED_GRAPH;
110 else if (errno != ENOENT)
111 die_errno(_("Could not open commit-graph '%s'"), graph_name);
112 - else if (open_commit_graph_chain(chain_name, &fd, &st))
112 + else if (open_commit_graph_chain(chain_name, &fd, &st,
113 + the_repository->hash_algo))
114 opened = OPENED_CHAIN;
115 else if (errno != ENOENT)
116 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
commit-graph.c
+14 -13
@@ -265,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
268 - if (graph_size < graph_min_size(the_hash_algo)) {
268 + if (graph_size < graph_min_size(r->hash_algo)) {
269 close(fd);
270 error(_("commit-graph file is too small"));
271 return NULL;
@@ -320,7 +320,7 @@ static int graph_read_commit_data(const unsigned char *chunk_start,
320 size_t chunk_size, void *data)
321 {
322 struct commit_graph *g = data;
323 - if (chunk_size / graph_data_width(the_hash_algo) != g->num_commits)
323 + if (chunk_size / graph_data_width(g->hash_algo) != g->num_commits)
324 return error(_("commit-graph commit data chunk is wrong size"));
325 g->chunk_commit_data = chunk_start;
326 return 0;
@@ -621,7 +621,8 @@ static int add_graph_to_chain(struct commit_graph *g,
621 }
622
623 int open_commit_graph_chain(const char *chain_file,
624 - int *fd, struct stat *st)
624 + int *fd, struct stat *st,
625 + const struct git_hash_algo *hash_algo)
626 {
627 *fd = git_open(chain_file);
628 if (*fd < 0)
@@ -630,7 +631,7 @@ int open_commit_graph_chain(const char *chain_file,
631 close(*fd);
632 return 0;
633 }
633 - if (st->st_size < the_hash_algo->hexsz) {
634 + if (st->st_size < hash_algo->hexsz) {
635 close(*fd);
636 if (!st->st_size) {
637 /* treat empty files the same as missing */
@@ -654,7 +655,7 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
655 int i = 0, valid = 1, count;
656 FILE *fp = xfdopen(fd, "r");
657
657 - count = st->st_size / (the_hash_algo->hexsz + 1);
658 + count = st->st_size / (r->hash_algo->hexsz + 1);
659 CALLOC_ARRAY(oids, count);
660
661 odb_prepare_alternates(r->objects);
@@ -716,7 +717,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
717 int fd;
718 struct commit_graph *g = NULL;
719
719 - if (open_commit_graph_chain(chain_file, &fd, &st)) {
720 + if (open_commit_graph_chain(chain_file, &fd, &st, r->hash_algo)) {
721 int incomplete;
722 /* ownership of fd is taken over by load function */
723 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
@@ -908,7 +909,7 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
909 die(_("invalid commit position. commit-graph is likely corrupt"));
910
911 lex_index = pos - g->num_commits_in_base;
911 - commit_data = g->chunk_commit_data + st_mult(graph_data_width(the_hash_algo), lex_index);
912 + commit_data = g->chunk_commit_data + st_mult(graph_data_width(g->hash_algo), lex_index);
913
914 graph_data = commit_graph_data_at(item);
915 graph_data->graph_pos = pos;
@@ -1112,7 +1113,7 @@ static struct tree *load_tree_for_commit(struct repository *r,
1113 g = g->base_graph;
1114
1115 commit_data = g->chunk_commit_data +
1115 - st_mult(graph_data_width(the_hash_algo),
1116 + st_mult(graph_data_width(g->hash_algo),
1117 graph_pos - g->num_commits_in_base);
1118
1119 oidread(&oid, commit_data, the_repository->hash_algo);
@@ -1221,7 +1222,7 @@ static int write_graph_chunk_oids(struct hashfile *f,
1222 int count;
1223 for (count = 0; count < ctx->commits.nr; count++, list++) {
1224 display_progress(ctx->progress, ++ctx->progress_cnt);
1224 - hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1225 + hashwrite(f, (*list)->object.oid.hash, f->algop->rawsz);
1226 }
1227
1228 return 0;
@@ -1252,7 +1253,7 @@ static int write_graph_chunk_data(struct hashfile *f,
1253 die(_("unable to parse commit %s"),
1254 oid_to_hex(&(*list)->object.oid));
1255 tree = get_commit_tree_oid(*list);
1255 - hashwrite(f, tree->hash, the_hash_algo->rawsz);
1256 + hashwrite(f, tree->hash, ctx->r->hash_algo->rawsz);
1257
1258 parent = (*list)->parents;
1259
@@ -2035,7 +2036,7 @@ static int write_graph_chunk_base_1(struct hashfile *f,
2036 return 0;
2037
2038 num = write_graph_chunk_base_1(f, g->base_graph);
2038 - hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
2039 + hashwrite(f, g->oid.hash, g->hash_algo->rawsz);
2040 return num + 1;
2041 }
2042
@@ -2059,7 +2060,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2060 struct hashfile *f;
2061 struct tempfile *graph_layer; /* when ctx->split is non-zero */
2062 struct lock_file lk = LOCK_INIT;
2062 - const unsigned hashsz = the_hash_algo->rawsz;
2063 + const unsigned hashsz = ctx->r->hash_algo->rawsz;
2064 struct strbuf progress_title = STRBUF_INIT;
2065 struct chunkfile *cf;
2066 unsigned char file_hash[GIT_MAX_RAWSZ];
@@ -2147,7 +2148,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2148 hashwrite_be32(f, GRAPH_SIGNATURE);
2149
2150 hashwrite_u8(f, GRAPH_VERSION);
2150 - hashwrite_u8(f, oid_version(the_hash_algo));
2151 + hashwrite_u8(f, oid_version(ctx->r->hash_algo));
2152 hashwrite_u8(f, get_num_chunks(cf));
2153 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2154
commit-graph.h
+2 -1
@@ -32,7 +32,8 @@ struct string_list;
32 char *get_commit_graph_filename(struct odb_source *source);
33 char *get_commit_graph_chain_filename(struct odb_source *source);
34 int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
35 -int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st);
35 +int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st,
36 + const struct git_hash_algo *hash_algo);
37
38 /*
39 * Given a commit struct, try to fill the commit struct info, including: