Commit

2135b14863642bbcec02996e7f5e54ac1f77b03a

odb: handle `OBJECT_INFO_DIE_IF_CORRUPT` generically

When a lookup with `OBJECT_INFO_DIE_IF_CORRUPT` fails we want to die in
case the object exists, but cannot be read. This flag is handled in two
different spots right now:

  - `do_oid_object_info_extended()` calls `has_packed_and_bad()` to
    check whether the object is known to be corrupt in any packfile.
    This function reaches into the internals of the packed source and
    thus breaks the abstraction provided by our object sources.

  - The loose source handles the flag itself and dies directly in
    `read_object_info_from_path()`, which means that we die even in
    cases where another source may still have a good copy of the
    object.

Besides being inconsistent, it also ties us to the specific backend used
by the database sources because `has_packed_and_bad()` assumes that they
use the "files" backend. Any other backend will instead cause us to die
when calling `odb_source_files_downcast()`, even if the object was
simply nonexistent.

In the preceding commits we've carved out the infrastructure to make
this mechanism fully generic. On the one hand, all backends now tell us
whether the object is missing or corrupt via their return values. And
on the other hand, they have been taught to provide a readable error
message to the caller.

Adapt `do_oid_object_info_extended()` to use those new mechanisms. This
means that we won't die immediately anymore when a loose object is
corrupt, and we properly handle backends other than the "files" backend.

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
5 files changed, +52 −40
modified odb.c
@@ -15,7 +15,6 @@
15 15 #include "object-name.h"
16 16 #include "odb.h"
17 17 #include "odb/source-inmemory.h"
18 - #include "packfile.h"
19 18 #include "path.h"
20 19 #include "promisor-remote.h"
21 20 #include "quote.h"
@@ -551,8 +550,11 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
551 550 const struct object_id *oid,
552 551 struct object_info *oi, unsigned flags)
553 552 {
553 + struct strbuf corrupt_err = STRBUF_INIT;
554 554 const struct object_id *real = oid;
555 + enum odb_read_status ret;
555 556 int already_retried = 0;
557 + bool corrupt = false;
556 558
557 559 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
558 560 real = lookup_replace_object(odb->repo, oid);
@@ -568,9 +570,14 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
568 570 while (1) {
569 571 struct odb_source *source;
570 572
571 - for (source = odb->sources; source; source = source->next)
572 - if (!odb_source_read_object_info(source, real, oi, flags, NULL))
573 - return 0;
573 + for (source = odb->sources; source; source = source->next) {
574 + ret = odb_source_read_object_info(source, real, oi, flags,
575 + corrupt_err.len ? NULL : &corrupt_err);
576 + if (!ret)
577 + goto out;
578 + if (ret != ODB_READ_NOT_FOUND)
579 + corrupt = true;
580 + }
574 581
575 582 /*
576 583 * When the object hasn't been found we try a second read and
@@ -578,11 +585,15 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
578 585 * caches or reload on-disk state.
579 586 */
580 587 if (!(flags & OBJECT_INFO_QUICK)) {
581 - for (source = odb->sources; source; source = source->next)
582 - if (!odb_source_read_object_info(source, real, oi,
583 - flags | OBJECT_INFO_SECOND_READ,
584 - NULL))
585 - return 0;
588 + for (source = odb->sources; source; source = source->next) {
589 + ret = odb_source_read_object_info(source, real, oi,
590 + flags | OBJECT_INFO_SECOND_READ,
591 + corrupt_err.len ? NULL : &corrupt_err);
592 + if (!ret)
593 + goto out;
594 + if (ret != ODB_READ_NOT_FOUND)
595 + corrupt = true;
596 + }
586 597 }
587 598
588 599 /*
@@ -605,16 +616,23 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
605 616 }
606 617
607 618 if (flags & OBJECT_INFO_DIE_IF_CORRUPT) {
608 - const struct packed_git *p;
609 619 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
610 620 die(_("replacement %s not found for %s"),
611 621 oid_to_hex(real), oid_to_hex(oid));
612 - if ((p = has_packed_and_bad(odb->repo, real)))
613 - die(_("packed object %s (stored in %s) is corrupt"),
614 - oid_to_hex(real), p->pack_name);
622 + if (corrupt) {
623 + if (corrupt_err.len)
624 + die("%s", corrupt_err.buf);
625 + die(_("object %s is corrupt"), oid_to_hex(real));
626 + }
615 627 }
616 - return -1;
628 +
629 + ret = corrupt ? ODB_READ_ERROR : ODB_READ_NOT_FOUND;
630 + goto out;
617 631 }
632 +
633 + out:
634 + strbuf_release(&corrupt_err);
635 + return ret;
618 636 }
619 637
620 638 static int oid_object_info_convert(struct repository *r,
modified odb/source-loose.c
@@ -192,15 +192,9 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
192 192 ret = 0;
193 193
194 194 out:
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"),
195 + if (ret && ret != ODB_READ_NOT_FOUND && errmsg)
196 + strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"),
198 197 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 - }
203 -
204 198 if (stream_to_end)
205 199 git_inflate_end(stream_to_end);
206 200 if (map)
modified packfile.c
@@ -985,23 +985,6 @@ void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
985 985 oidset_insert(&p->bad_objects, oid);
986 986 }
987 987
988 - const struct packed_git *has_packed_and_bad(struct repository *r,
989 - const struct object_id *oid)
990 - {
991 - struct odb_source *source;
992 -
993 - for (source = r->objects->sources; source; source = source->next) {
994 - struct odb_source_files *files = odb_source_files_downcast(source);
995 - struct packfile_list_entry *e;
996 -
997 - for (e = files->packed->packs.head; e; e = e->next)
998 - if (oidset_contains(&e->pack->bad_objects, oid))
999 - return e->pack;
1000 - }
1001 -
1002 - return NULL;
1003 - }
1004 -
1005 988 off_t get_delta_base(struct packed_git *p,
1006 989 struct pack_window **w_curs,
1007 990 off_t *curpos,
modified packfile.h
@@ -329,7 +329,6 @@ int packed_object_info_with_index_pos(struct odb_source_packed *source,
329 329 uint32_t *maybe_index_pos, struct object_info *oi);
330 330
331 331 void mark_bad_packed_object(struct packed_git *, const struct object_id *);
332 - const struct packed_git *has_packed_and_bad(struct repository *, const struct object_id *);
333 332
334 333 int has_object_pack(struct repository *r, const struct object_id *oid);
335 334 int has_object_kept_pack(struct repository *r, const struct object_id *oid,
modified t/t1060-object-corruption.sh
@@ -145,4 +145,22 @@ test_expect_success 'partial clone of corrupted repository' '
145 145 test_must_fail git -C corrupt-partial checkout --force
146 146 '
147 147
148 + test_expect_success 'corrupted loose commit can be read from alternate' '
149 + git init repo-a &&
150 + tree=$(git -C repo-a write-tree) &&
151 + commit=$(git -C repo-a commit-tree $tree </dev/null) &&
152 +
153 + cp -r repo-a repo-b &&
154 + (
155 + cd repo-b &&
156 + echo ../../../repo-a/.git/objects >.git/objects/info/alternates &&
157 + corrupt_byte "$commit" 1
158 + ) &&
159 +
160 + git -C repo-a cat-file -p "$commit" >expect &&
161 + git -C repo-b cat-file -p "$commit" >actual 2>err &&
162 + test_cmp expect actual &&
163 + test_grep "inflate: data stream error" err
164 + '
165 +
148 166 test_done