object-file: extract function to read object info from path

Extract a new function that allows us to read object info for a specific loose object via a user-supplied path. This function will be used in a subsequent commit. Note that this also allows us to drop `stat_loose_object()`, which is a simple wrapper around `odb_loose_path()` plus lstat(3p). Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jan 26, 2026 at 10:51 UTC 6ecab3cdf67012592734ed9493db634d39326d43
1 file changed +16 -23
object-file.c
+16 -23
@@ -165,30 +165,13 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
165 }
166
167 /*
168 - * Find "oid" as a loose object in given source.
169 - * Returns 0 on success, negative on failure.
168 + * Find "oid" as a loose object in given source, open the object and return its
169 + * file descriptor. Returns the file descriptor on success, negative on failure.
170 *
171 * The "path" out-parameter will give the path of the object we found (if any).
172 * Note that it may point to static storage and is only valid until another
173 * call to stat_loose_object().
174 */
175 -static int stat_loose_object(struct odb_source_loose *loose,
176 - const struct object_id *oid,
177 - struct stat *st, const char **path)
178 -{
179 - static struct strbuf buf = STRBUF_INIT;
180 -
181 - *path = odb_loose_path(loose->source, &buf, oid);
182 - if (!lstat(*path, st))
183 - return 0;
184 -
185 - return -1;
186 -}
187 -
188 -/*
189 - * Like stat_loose_object(), but actually open the object and return the
190 - * descriptor. See the caveats on the "path" parameter above.
191 - */
175 static int open_loose_object(struct odb_source_loose *loose,
176 const struct object_id *oid, const char **path)
177 {
@@ -412,7 +395,8 @@ static int parse_loose_header(const char *hdr, struct object_info *oi)
395 return 0;
396 }
397
415 -int odb_source_loose_read_object_info(struct odb_source *source,
398 +static int read_object_info_from_path(struct odb_source *source,
399 + const char *path,
400 const struct object_id *oid,
401 struct object_info *oi,
402 unsigned flags)
@@ -420,7 +404,6 @@ int odb_source_loose_read_object_info(struct odb_source *source,
404 int ret;
405 int fd;
406 unsigned long mapsize;
423 - const char *path;
407 void *map = NULL;
408 git_zstream stream, *stream_to_end = NULL;
409 char hdr[MAX_HEADER_LEN];
@@ -443,7 +426,7 @@ int odb_source_loose_read_object_info(struct odb_source *source,
426 goto out;
427 }
428
446 - if (stat_loose_object(source->loose, oid, &st, &path) < 0) {
429 + if (lstat(path, &st) < 0) {
430 ret = -1;
431 goto out;
432 }
@@ -455,7 +438,7 @@ int odb_source_loose_read_object_info(struct odb_source *source,
438 goto out;
439 }
440
458 - fd = open_loose_object(source->loose, oid, &path);
441 + fd = git_open(path);
442 if (fd < 0) {
443 if (errno != ENOENT)
444 error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
@@ -534,6 +517,16 @@ out:
517 return ret;
518 }
519
520 +int odb_source_loose_read_object_info(struct odb_source *source,
521 + const struct object_id *oid,
522 + struct object_info *oi,
523 + unsigned flags)
524 +{
525 + static struct strbuf buf = STRBUF_INIT;
526 + odb_loose_path(source, &buf, oid);
527 + return read_object_info_from_path(source, buf.buf, oid, oi, flags);
528 +}
529 +
530 static void hash_object_body(const struct git_hash_algo *algo, struct git_hash_ctx *c,
531 const void *buf, unsigned long len,
532 struct object_id *oid,