Skip to content

Commit 5105417

Browse files
peffgitster
authored andcommitted
index-pack: detect local corruption in collision check
When we notice that we have a local copy of an incoming object, we compare the two objects to make sure we haven't found a collision. Before we get to the actual object bytes, though, we compare the type and size from sha1_object_info(). If our local object is corrupted, then the type will be OBJ_BAD, which obviously will not match the incoming type, and we'll report "SHA1 COLLISION FOUND" (with capital letters and everything). This is confusing, as the problem is not a collision but rather local corruption. We should report that instead (just like we do if reading the rest of the object content fails a few lines later). Note that we _could_ just ignore the error and mark it as a non-collision. That would let you "git fetch" to replace a corrupted object. But it's not a very reliable method for repairing a repository. The earlier want/have negotiation tries to get the other side to omit objects we already have, and it would not realize that we are "missing" this corrupted object. So we're better off complaining loudly when we see corruption, and letting the user take more drastic measures to repair (like making a full clone elsewhere and copying the pack into place). Note that the test sets transfer.unpackLimit in the receiving repository so that we use index-pack (which is what does the collision check). Normally for such a small push we'd use unpack-objects, which would simply try to write the loose object, and discard the new one when we see that there's already an old one. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 93cff9a commit 5105417

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

builtin/index-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
809809
unsigned long has_size;
810810
read_lock();
811811
has_type = sha1_object_info(sha1, &has_size);
812+
if (has_type < 0)
813+
die(_("cannot read existing object info %s"), sha1_to_hex(sha1));
812814
if (has_type != type || has_size != size)
813815
die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1));
814816
has_data = read_sha1_file(sha1, &has_type, &has_size);

t/t1060-object-corruption.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ test_expect_success 'setup corrupt repo' '
2121
cd bit-error &&
2222
test_commit content &&
2323
corrupt_byte HEAD:content.t 10
24+
) &&
25+
git init no-bit-error &&
26+
(
27+
# distinct commit from bit-error, but containing a
28+
# non-corrupted version of the same blob
29+
cd no-bit-error &&
30+
test_tick &&
31+
test_commit content
2432
)
2533
'
2634

@@ -108,4 +116,13 @@ test_expect_failure 'clone --local detects misnamed objects' '
108116
test_must_fail git clone --local misnamed misnamed-checkout
109117
'
110118

119+
test_expect_success 'fetch into corrupted repo with index-pack' '
120+
(
121+
cd bit-error &&
122+
test_must_fail git -c transfer.unpackLimit=1 \
123+
fetch ../no-bit-error 2>stderr &&
124+
test_i18ngrep ! -i collision stderr
125+
)
126+
'
127+
111128
test_done

0 commit comments

Comments
 (0)