object-file: get rid of `the_repository` in `read_loose_object()`

The function `read_loose_object()` takes a path to an object file and tries to parse it. As such, the function does not depend on any specific object database but instead acts as an ODB-independent way to read a specific file. As such, all it needs as input is a repository so that we can derive repo settings and the hash algorithm. That repository isn't passed in as a parameter though, as we implicitly depend on the global `the_repository`. Refactor the function so that we pass in the repository as a parameter. 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 0df005353aca4e490478a4e8c2d090728599868e
3 files changed +8 -6
builtin/fsck.c
+1 -1
@@ -633,7 +633,7 @@ static int fsck_loose(const struct object_id *oid, const char *path,
633 oi.sizep = &size;
634 oi.typep = &type;
635
636 - if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
636 + if (read_loose_object(the_repository, path, oid, &real_oid, &contents, &oi) < 0) {
637 if (contents && !oideq(&real_oid, oid))
638 err = error(_("%s: hash-path mismatch, found at: %s"),
639 oid_to_hex(&real_oid), path);
object-file.c
+5 -4
@@ -1535,7 +1535,8 @@ static int check_stream_oid(git_zstream *stream,
1535 return 0;
1536 }
1537
1538 -int read_loose_object(const char *path,
1538 +int read_loose_object(struct repository *repo,
1539 + const char *path,
1540 const struct object_id *expected_oid,
1541 struct object_id *real_oid,
1542 void **contents,
@@ -1574,9 +1575,9 @@ int read_loose_object(const char *path,
1575 }
1576
1577 if (*oi->typep == OBJ_BLOB &&
1577 - *size > repo_settings_get_big_file_threshold(the_repository)) {
1578 + *size > repo_settings_get_big_file_threshold(repo)) {
1579 if (check_stream_oid(&stream, hdr, *size, path, expected_oid,
1579 - the_repository->hash_algo) < 0)
1580 + repo->hash_algo) < 0)
1581 goto out_inflate;
1582 } else {
1583 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
@@ -1584,7 +1585,7 @@ int read_loose_object(const char *path,
1585 error(_("unable to unpack contents of %s"), path);
1586 goto out_inflate;
1587 }
1587 - hash_object_file(the_repository->hash_algo,
1588 + hash_object_file(repo->hash_algo,
1589 *contents, *size,
1590 *oi->typep, real_oid);
1591 if (!oideq(expected_oid, real_oid))
object-file.h
+2 -1
@@ -210,7 +210,8 @@ int check_and_freshen_file(const char *fn, int freshen);
210 *
211 * Returns 0 on success, negative on error (details may be written to stderr).
212 */
213 -int read_loose_object(const char *path,
213 +int read_loose_object(struct repository *repo,
214 + const char *path,
215 const struct object_id *expected_oid,
216 struct object_id *real_oid,
217 void **contents,