Commit

63a3257352868dae9e842f2aa9637ee36651ad38

odb/source: allow `read_object_info()` to bubble up error messages

When reading an object fails even though it exists, the sources know
best what exactly went wrong and where the corrupt object is located.
This information is lost though when bubbling up the error to the object
database layer, which forces that layer to reconstruct it after the
fact. This is exactly what `do_oid_object_info_extended()` does via
`has_packed_and_bad()`, but that function only really knows to handle
the "files" backend by reaching into its internals.

Introduce a new `errmsg` parameter for the `read_object_info()` callback
that sources are expected to populate with a human-readable message in
case reading the object has failed. Adapt the packed and loose sources
to populate the buffer with the messages that we ultimately want to
surface to the user.

For now, all callers are adapted to pass a `NULL` pointer. We will add a
user of this new infrastructure in a subsequent commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-08-19 12:17 UTC · committed by Junio C Hamano

Browse files
9 files changed, +73 −33
modified builtin/pack-objects.c
@@ -1759,7 +1759,7 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1759 1759 struct odb_source *source = the_repository->objects->sources->next;
1760 1760 for (; source; source = source->next) {
1761 1761 struct odb_source_files *files = odb_source_files_downcast(source);
1762 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
1762 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
1763 1763 return 0;
1764 1764 }
1765 1765 }
@@ -4171,7 +4171,7 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
4171 4171
4172 4172 for (; !found && source; source = source->next) {
4173 4173 struct odb_source_files *files = odb_source_files_downcast(source);
4174 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4174 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4175 4175 found = 1;
4176 4176 }
4177 4177
@@ -4637,7 +4637,7 @@ static int force_object_loose(struct odb_source *source,
4637 4637
4638 4638 for (struct odb_source *s = source->odb->sources; s; s = s->next) {
4639 4639 struct odb_source_files *files = odb_source_files_downcast(s);
4640 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4640 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4641 4641 return 0;
4642 4642 }
4643 4643
modified odb.c
@@ -560,7 +560,7 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
560 560 if (is_null_oid(real))
561 561 return -1;
562 562
563 - if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
563 + if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL))
564 564 return 0;
565 565
566 566 odb_prepare_alternates(odb);
@@ -569,7 +569,7 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
569 569 struct odb_source *source;
570 570
571 571 for (source = odb->sources; source; source = source->next)
572 - if (!odb_source_read_object_info(source, real, oi, flags))
572 + if (!odb_source_read_object_info(source, real, oi, flags, NULL))
573 573 return 0;
574 574
575 575 /*
@@ -580,7 +580,8 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
580 580 if (!(flags & OBJECT_INFO_QUICK)) {
581 581 for (source = odb->sources; source; source = source->next)
582 582 if (!odb_source_read_object_info(source, real, oi,
583 - flags | OBJECT_INFO_SECOND_READ))
583 + flags | OBJECT_INFO_SECOND_READ,
584 + NULL))
584 585 return 0;
585 586 }
586 587
modified odb/source-files.c
@@ -62,16 +62,19 @@ static void odb_source_files_prepare(struct odb_source *source,
62 62 static enum odb_read_status odb_source_files_read_object_info(struct odb_source *source,
63 63 const struct object_id *oid,
64 64 struct object_info *oi,
65 - enum object_info_flags flags)
65 + enum object_info_flags flags,
66 + struct strbuf *errmsg)
66 67 {
67 68 struct odb_source_files *files = odb_source_files_downcast(source);
68 69 enum odb_read_status ret_packed, ret_loose;
69 70
70 - ret_packed = odb_source_read_object_info(&files->packed->base, oid, oi, flags);
71 + ret_packed = odb_source_read_object_info(&files->packed->base, oid, oi,
72 + flags, errmsg);
71 73 if (!ret_packed)
72 74 return 0;
73 75
74 - ret_loose = odb_source_read_object_info(&files->loose->base, oid, oi, flags);
76 + ret_loose = odb_source_read_object_info(&files->loose->base, oid, oi, flags,
77 + ret_packed == ODB_READ_NOT_FOUND ? errmsg : NULL);
75 78 if (!ret_loose)
76 79 return 0;
77 80
modified odb/source-inmemory.c
@@ -59,7 +59,8 @@ static void populate_object_info(struct odb_source_inmemory *source,
59 59 static enum odb_read_status odb_source_inmemory_read_object_info(struct odb_source *source,
60 60 const struct object_id *oid,
61 61 struct object_info *oi,
62 - enum object_info_flags flags UNUSED)
62 + enum object_info_flags flags UNUSED,
63 + struct strbuf *errmsg UNUSED)
63 64 {
64 65 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
65 66 const struct inmemory_object *object;
modified odb/source-loose.c
@@ -67,7 +67,8 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
67 67 const char *path,
68 68 const struct object_id *oid,
69 69 struct object_info *oi,
70 - enum object_info_flags flags)
70 + enum object_info_flags flags,
71 + struct strbuf *errmsg)
71 72 {
72 73 int ret;
73 74 int fd;
@@ -191,9 +192,14 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
191 192 ret = 0;
192 193
193 194 out:
194 - if (ret && ret != ODB_READ_NOT_FOUND && (flags & OBJECT_INFO_DIE_IF_CORRUPT))
195 - die(_("loose object %s (stored in %s) is corrupt"),
196 - oid_to_hex(oid), path);
195 + if (ret && ret != ODB_READ_NOT_FOUND) {
196 + if ((flags & OBJECT_INFO_DIE_IF_CORRUPT))
197 + die(_("loose object %s (stored in %s) is corrupt"),
198 + oid_to_hex(oid), path);
199 + if (errmsg)
200 + strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"),
201 + oid_to_hex(oid), path);
202 + }
197 203
198 204 if (stream_to_end)
199 205 git_inflate_end(stream_to_end);
@@ -216,7 +222,8 @@ out:
216 222 static enum odb_read_status odb_source_loose_read_object_info(struct odb_source *source,
217 223 const struct object_id *oid,
218 224 struct object_info *oi,
219 - enum object_info_flags flags)
225 + enum object_info_flags flags,
226 + struct strbuf *errmsg)
220 227 {
221 228 struct odb_source_loose *loose = odb_source_loose_downcast(source);
222 229 static struct strbuf buf = STRBUF_INIT;
@@ -231,7 +238,7 @@ static enum odb_read_status odb_source_loose_read_object_info(struct odb_source
231 238 return ODB_READ_NOT_FOUND;
232 239
233 240 odb_loose_path(loose, &buf, oid);
234 - return read_object_info_from_path(loose, buf.buf, oid, oi, flags);
241 + return read_object_info_from_path(loose, buf.buf, oid, oi, flags, errmsg);
235 242 }
236 243
237 244 /*
@@ -428,7 +435,7 @@ static int for_each_object_wrapper_cb(const struct object_id *oid,
428 435 if (data->request) {
429 436 struct object_info oi = *data->request;
430 437
431 - if (read_object_info_from_path(data->loose, path, oid, &oi, 0) < 0)
438 + if (read_object_info_from_path(data->loose, path, oid, &oi, 0, NULL) < 0)
432 439 return -1;
433 440
434 441 return data->cb(oid, &oi, data->cb_data);
@@ -446,7 +453,7 @@ static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
446 453 struct object_info oi = *data->request;
447 454
448 455 if (odb_source_read_object_info(&data->loose->base,
449 - oid, &oi, 0) < 0)
456 + oid, &oi, 0, NULL) < 0)
450 457 return -1;
451 458
452 459 return data->cb(oid, &oi, data->cb_data);
modified odb/source-packed.c
@@ -2,7 +2,9 @@
2 2 #include "abspath.h"
3 3 #include "chdir-notify.h"
4 4 #include "dir.h"
5 + #include "gettext.h"
5 6 #include "git-zlib.h"
7 + #include "hex.h"
6 8 #include "list-objects-filter-options.h"
7 9 #include "mergesort.h"
8 10 #include "midx.h"
@@ -10,6 +12,7 @@
10 12 #include "odb/streaming.h"
11 13 #include "packfile.h"
12 14 #include "pack-bitmap.h"
15 + #include "strbuf.h"
13 16
14 17 static int find_pack_entry(struct odb_source_packed *store,
15 18 const struct object_id *oid,
@@ -38,7 +41,8 @@ static int find_pack_entry(struct odb_source_packed *store,
38 41 static enum odb_read_status odb_source_packed_read_object_info(struct odb_source *source,
39 42 const struct object_id *oid,
40 43 struct object_info *oi,
41 - enum object_info_flags flags)
44 + enum object_info_flags flags,
45 + struct strbuf *errmsg)
42 46 {
43 47 struct odb_source_packed *packed = odb_source_packed_downcast(source);
44 48 struct packed_git *bad_pack = NULL;
@@ -59,25 +63,39 @@ static enum odb_read_status odb_source_packed_read_object_info(struct odb_source
59 63 * corrupt in one of the packfiles. Report the object as
60 64 * corrupt instead of missing in that case.
61 65 */
62 - if (bad_pack)
63 - return -1;
64 - return ODB_READ_NOT_FOUND;
66 + if (bad_pack) {
67 + ret = -1;
68 + goto out;
69 + }
70 +
71 + ret = ODB_READ_NOT_FOUND;
72 + goto out;
65 73 }
66 74
67 75 /*
68 76 * We know that the caller doesn't actually need the
69 77 * information below, so return early.
70 78 */
71 - if (!oi)
72 - return 0;
79 + if (!oi) {
80 + ret = 0;
81 + goto out;
82 + }
73 83
74 84 ret = packed_object_info(packed, e.p, e.offset, oi);
75 85 if (ret < 0) {
86 + bad_pack = e.p;
76 87 mark_bad_packed_object(e.p, oid);
77 - return -1;
88 + goto out;
78 89 }
79 90
80 - return 0;
91 + ret = 0;
92 +
93 + out:
94 + if (ret < 0 && bad_pack && errmsg)
95 + strbuf_addf(errmsg, _("packed object %s (stored in %s) is corrupt"),
96 + oid_to_hex(oid), bad_pack->pack_name);
97 +
98 + return ret;
81 99 }
82 100
83 101 static int odb_source_packed_read_object_stream(struct odb_read_stream **out,
modified odb/source.h
@@ -27,6 +27,7 @@ enum odb_source_type {
27 27
28 28 struct object_id;
29 29 struct odb_read_stream;
30 + struct strbuf;
30 31 struct strvec;
31 32
32 33 /*
@@ -111,12 +112,16 @@ struct odb_source {
111 112 * already surfaced the object without reloading any on-disk state.
112 113 *
113 114 * The callback is expected to return an `enum odb_read_status`. Please
114 - * refer to the individual values that can be returned.
115 + * refer to the individual values that can be returned. In case reading
116 + * the object has failed with a generic error and `errmsg` is non-NULL,
117 + * the callback is expected to populate it with a human-readable
118 + * message that describes the failure.
115 119 */
116 120 enum odb_read_status (*read_object_info)(struct odb_source *source,
117 121 const struct object_id *oid,
118 122 struct object_info *oi,
119 - enum object_info_flags flags);
123 + enum object_info_flags flags,
124 + struct strbuf *errmsg);
120 125
121 126 /*
122 127 * This callback is expected to create a new read stream that can be
@@ -341,13 +346,18 @@ static inline void odb_source_prepare(struct odb_source *source,
341 346 /*
342 347 * Read an object from the object database source identified by its object ID.
343 348 * Please refer to `enum odb_read_status` for the individual error codes.
349 + *
350 + * In case reading the object has failed with a generic error and `errmsg` is
351 + * non-NULL it will be populated with a human-readable message that describes
352 + * the failure.
344 353 */
345 354 static inline enum odb_read_status odb_source_read_object_info(struct odb_source *source,
346 355 const struct object_id *oid,
347 356 struct object_info *oi,
348 - enum object_info_flags flags)
357 + enum object_info_flags flags,
358 + struct strbuf *errmsg)
349 359 {
350 - return source->read_object_info(source, oid, oi, flags);
360 + return source->read_object_info(source, oid, oi, flags, errmsg);
351 361 }
352 362
353 363 /*
modified packfile.c
@@ -1945,7 +1945,7 @@ int has_object_pack(struct repository *r, const struct object_id *oid)
1945 1945 odb_prepare_alternates(r->objects);
1946 1946 for (source = r->objects->sources; source; source = source->next) {
1947 1947 struct odb_source_files *files = odb_source_files_downcast(source);
1948 - if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0))
1948 + if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0, NULL))
1949 1949 return 1;
1950 1950 }
1951 1951
modified t/unit-tests/u-odb-inmemory.c
@@ -29,7 +29,7 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
29 29 .contentp = &actual_content,
30 30 };
31 31
32 - cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0));
32 + cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0, NULL));
33 33 cl_assert_equal_u(actual_size, strlen(expected_content));
34 34 cl_assert_equal_u(actual_type, expected_type);
35 35 cl_assert_equal_s((char *) actual_content, expected_content);
@@ -72,7 +72,7 @@ void test_odb_inmemory__read_missing_object(void)
72 72 const char *end;
73 73
74 74 cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo));
75 - cl_assert_equal_i(odb_source_read_object_info(&source->base, &oid, NULL, 0),
75 + cl_assert_equal_i(odb_source_read_object_info(&source->base, &oid, NULL, 0, NULL),
76 76 ODB_READ_NOT_FOUND);
77 77
78 78 odb_source_free(&source->base);