object-file: stop using `the_hash_algo`

There are a couple of users of the `the_hash_algo` macro, which implicitly depends on `the_repository`. Adapt these callers to not do so anymore, either by deriving it from already-available context or by using `the_repository->hash_algo`. The latter variant doesn't yet help to remove the global dependency, but such users will be adapted in the following commits to not use `the_repository` anymore. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2025 at 06:56 UTC 18323f5b485f5c484622e18c6bfd167fdf5ca101
2 files changed +25 -16
object-file.c
+24 -16
@@ -25,6 +25,7 @@
25 #include "pack.h"
26 #include "packfile.h"
27 #include "path.h"
28 +#include "read-cache-ll.h"
29 #include "setup.h"
30 #include "streaming.h"
31
@@ -41,9 +42,11 @@ static int get_conv_flags(unsigned flags)
42 return 0;
43 }
44
44 -static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
45 +static void fill_loose_path(struct strbuf *buf,
46 + const struct object_id *oid,
47 + const struct git_hash_algo *algop)
48 {
46 - for (size_t i = 0; i < the_hash_algo->rawsz; i++) {
49 + for (size_t i = 0; i < algop->rawsz; i++) {
50 static char hex[] = "0123456789abcdef";
51 unsigned int val = oid->hash[i];
52 strbuf_addch(buf, hex[val >> 4]);
@@ -60,7 +63,7 @@ const char *odb_loose_path(struct odb_source *source,
63 strbuf_reset(buf);
64 strbuf_addstr(buf, source->path);
65 strbuf_addch(buf, '/');
63 - fill_loose_path(buf, oid);
66 + fill_loose_path(buf, oid, source->odb->repo->hash_algo);
67 return buf->buf;
68 }
69
@@ -1165,7 +1168,7 @@ static int index_mem(struct index_state *istate,
1168
1169 opts.strict = 1;
1170 opts.error_func = hash_format_check_report;
1168 - if (fsck_buffer(null_oid(the_hash_algo), type, buf, size, &opts))
1171 + if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))
1172 die(_("refusing to create malformed object"));
1173 fsck_finish(&opts);
1174 }
@@ -1173,7 +1176,7 @@ static int index_mem(struct index_state *istate,
1176 if (write_object)
1177 ret = write_object_file(buf, size, type, oid);
1178 else
1176 - hash_object_file(the_hash_algo, buf, size, type, oid);
1179 + hash_object_file(istate->repo->hash_algo, buf, size, type, oid);
1180
1181 strbuf_release(&nbuf);
1182 return ret;
@@ -1199,7 +1202,7 @@ static int index_stream_convert_blob(struct index_state *istate,
1202 ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
1203 oid);
1204 else
1202 - hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
1205 + hash_object_file(istate->repo->hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
1206 oid);
1207 strbuf_release(&sbuf);
1208 return ret;
@@ -1297,7 +1300,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
1300 if (strbuf_readlink(&sb, path, st->st_size))
1301 return error_errno("readlink(\"%s\")", path);
1302 if (!(flags & INDEX_WRITE_OBJECT))
1300 - hash_object_file(the_hash_algo, sb.buf, sb.len,
1303 + hash_object_file(istate->repo->hash_algo, sb.buf, sb.len,
1304 OBJ_BLOB, oid);
1305 else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
1306 rc = error(_("%s: failed to insert into database"), path);
@@ -1328,6 +1331,7 @@ int read_pack_header(int fd, struct pack_header *header)
1331
1332 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1333 struct strbuf *path,
1334 + const struct git_hash_algo *algop,
1335 each_loose_object_fn obj_cb,
1336 each_loose_cruft_fn cruft_cb,
1337 each_loose_subdir_fn subdir_cb,
@@ -1364,12 +1368,12 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1368 namelen = strlen(de->d_name);
1369 strbuf_setlen(path, baselen);
1370 strbuf_add(path, de->d_name, namelen);
1367 - if (namelen == the_hash_algo->hexsz - 2 &&
1371 + if (namelen == algop->hexsz - 2 &&
1372 !hex_to_bytes(oid.hash + 1, de->d_name,
1369 - the_hash_algo->rawsz - 1)) {
1370 - oid_set_algo(&oid, the_hash_algo);
1371 - memset(oid.hash + the_hash_algo->rawsz, 0,
1372 - GIT_MAX_RAWSZ - the_hash_algo->rawsz);
1373 + algop->rawsz - 1)) {
1374 + oid_set_algo(&oid, algop);
1375 + memset(oid.hash + algop->rawsz, 0,
1376 + GIT_MAX_RAWSZ - algop->rawsz);
1377 if (obj_cb) {
1378 r = obj_cb(&oid, path->buf, data);
1379 if (r)
@@ -1405,7 +1409,8 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1409 int i;
1410
1411 for (i = 0; i < 256; i++) {
1408 - r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
1412 + r = for_each_file_in_obj_subdir(i, path, the_repository->hash_algo,
1413 + obj_cb, cruft_cb,
1414 subdir_cb, data);
1415 if (r)
1416 break;
@@ -1481,6 +1486,7 @@ struct oidtree *odb_loose_cache(struct odb_source *source,
1486 }
1487 strbuf_addstr(&buf, source->path);
1488 for_each_file_in_obj_subdir(subdir_nr, &buf,
1489 + source->odb->repo->hash_algo,
1490 append_loose_object,
1491 NULL, NULL,
1492 source->loose_objects_cache);
@@ -1501,7 +1507,8 @@ static int check_stream_oid(git_zstream *stream,
1507 const char *hdr,
1508 unsigned long size,
1509 const char *path,
1504 - const struct object_id *expected_oid)
1510 + const struct object_id *expected_oid,
1511 + const struct git_hash_algo *algop)
1512 {
1513 struct git_hash_ctx c;
1514 struct object_id real_oid;
@@ -1509,7 +1516,7 @@ static int check_stream_oid(git_zstream *stream,
1516 unsigned long total_read;
1517 int status = Z_OK;
1518
1512 - the_hash_algo->init_fn(&c);
1519 + algop->init_fn(&c);
1520 git_hash_update(&c, hdr, stream->total_out);
1521
1522 /*
@@ -1594,7 +1601,8 @@ int read_loose_object(const char *path,
1601
1602 if (*oi->typep == OBJ_BLOB &&
1603 *size > repo_settings_get_big_file_threshold(the_repository)) {
1597 - if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
1604 + if (check_stream_oid(&stream, hdr, *size, path, expected_oid,
1605 + the_repository->hash_algo) < 0)
1606 goto out_inflate;
1607 } else {
1608 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
object-file.h
+1
@@ -89,6 +89,7 @@ typedef int each_loose_subdir_fn(unsigned int nr,
89 void *data);
90 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
91 struct strbuf *path,
92 + const struct git_hash_algo *algo,
93 each_loose_object_fn obj_cb,
94 each_loose_cruft_fn cruft_cb,
95 each_loose_subdir_fn subdir_cb,