]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1450-fsck.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t1450-fsck.sh
CommitLineData
469e2ebf
JH
1#!/bin/sh
2
dbedf8bf
JN
3test_description='git fsck random collection of tests
4
5* (HEAD) B
06d53148 6* (main) A
dbedf8bf 7'
469e2ebf
JH
8
9. ./test-lib.sh
10
11test_expect_success setup '
dbedf8bf 12 git config gc.auto 0 &&
0adc6a3d 13 git config i18n.commitencoding ISO-8859-1 &&
469e2ebf 14 test_commit A fileA one &&
0adc6a3d 15 git config --unset i18n.commitencoding &&
469e2ebf
JH
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
1c6667cb 18 orig_head=$(git rev-parse HEAD) &&
469e2ebf 19 git tag -d A B &&
1c5e94f4 20 git reflog expire --expire=now --all
469e2ebf
JH
21'
22
e15ef669
JH
23test_expect_success 'loose objects borrowed from alternate are not missing' '
24 mkdir another &&
25 (
26 cd another &&
27 git init &&
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
c6a13b2c 30 git fsck --no-dangling >../actual 2>&1
dbedf8bf 31 ) &&
1c5e94f4 32 test_must_be_empty actual
e15ef669
JH
33'
34
dbedf8bf
JN
35test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
1c5e94f4 37 test_must_be_empty actual
0adc6a3d
JN
38'
39
02a6552c
TR
40# Corruption tests follow. Make sure to remove all traces of the
41# specific corruption you test afterwards, lest a later test trip over
42# it.
43
8c891eed
JS
44sha1_file () {
45 git rev-parse --git-path objects/$(test_oid_to_path "$1")
46}
dbedf8bf 47
8c891eed
JS
48remove_object () {
49 rm "$(sha1_file "$1")"
50}
dbedf8bf 51
f7a0dba7
ÆAB
52test_expect_success 'object with hash mismatch' '
53 git init --bare hash-mismatch &&
54 (
55 cd hash-mismatch &&
dbedf8bf 56
f7a0dba7 57 oid=$(echo blob | git hash-object -w --stdin) &&
96e41f58 58 oldoid=$oid &&
f7a0dba7
ÆAB
59 old=$(test_oid_to_path "$oid") &&
60 new=$(dirname $old)/$(test_oid ff_2) &&
61 oid="$(dirname $new)$(basename $new)" &&
62
63 mv objects/$old objects/$new &&
64 git update-index --add --cacheinfo 100644 $oid foo &&
65 tree=$(git write-tree) &&
66 cmt=$(echo bogus | git commit-tree $tree) &&
67 git update-ref refs/heads/bogus $cmt &&
68
69 test_must_fail git fsck 2>out &&
96e41f58 70 grep "$oldoid: hash-path mismatch, found at: .*$new" out
f7a0dba7 71 )
02a6552c
TR
72'
73
42cd635b
ÆAB
74test_expect_success 'object with hash and type mismatch' '
75 git init --bare hash-type-mismatch &&
76 (
77 cd hash-type-mismatch &&
78
79 oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) &&
96e41f58 80 oldoid=$oid &&
42cd635b
ÆAB
81 old=$(test_oid_to_path "$oid") &&
82 new=$(dirname $old)/$(test_oid ff_2) &&
83 oid="$(dirname $new)$(basename $new)" &&
84
85 mv objects/$old objects/$new &&
86 git update-index --add --cacheinfo 100644 $oid foo &&
87 tree=$(git write-tree) &&
88 cmt=$(echo bogus | git commit-tree $tree) &&
89 git update-ref refs/heads/bogus $cmt &&
90
31deb28f
ÆAB
91
92 test_must_fail git fsck 2>out &&
96e41f58
ÆAB
93 grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
94 grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
42cd635b
ÆAB
95 )
96'
97
59069107 98test_expect_success 'zlib corrupt loose object output ' '
a5ed3331
ÆAB
99 git init --bare corrupt-loose-output &&
100 (
101 cd corrupt-loose-output &&
102 oid=$(git hash-object -w --stdin --literally </dev/null) &&
103 oidf=objects/$(test_oid_to_path "$oid") &&
59069107 104 chmod +w $oidf &&
a5ed3331
ÆAB
105 echo extra garbage >>$oidf &&
106
107 cat >expect.error <<-EOF &&
108 error: garbage at end of loose object '\''$oid'\''
109 error: unable to unpack contents of ./$oidf
110 error: $oid: object corrupt or missing: ./$oidf
111 EOF
112 test_must_fail git fsck 2>actual &&
113 grep ^error: actual >error &&
114 test_cmp expect.error error
115 )
116'
117
02a6552c 118test_expect_success 'branch pointing to non-commit' '
1c6667cb 119 tree_oid=$(git rev-parse --verify HEAD^{tree}) &&
dbedf8bf 120 test_when_finished "git update-ref -d refs/heads/invalid" &&
1c6667cb 121 test-tool ref-store main update-ref msg refs/heads/invalid $tree_oid $ZERO_OID REF_SKIP_OID_VERIFICATION &&
122f76f5 122 test_must_fail git fsck 2>out &&
6789275d 123 test_grep "not a commit" out
02a6552c
TR
124'
125
a6f43364 126test_expect_success REFFILES 'HEAD link pointing at a funny object' '
1c6667cb 127 test_when_finished "git update-ref HEAD $orig_head" &&
8de6b383 128 echo $ZERO_OID >.git/HEAD &&
122f76f5
JH
129 # avoid corrupt/broken HEAD from interfering with repo discovery
130 test_must_fail env GIT_DIR=.git git fsck 2>out &&
6789275d 131 test_grep "detached HEAD points" out
122f76f5
JH
132'
133
134test_expect_success 'HEAD link pointing at a funny place' '
1c6667cb 135 test_when_finished "git update-ref --no-deref HEAD $orig_head" &&
23937116 136 test-tool ref-store main create-symref HEAD refs/funny/place &&
122f76f5
JH
137 # avoid corrupt/broken HEAD from interfering with repo discovery
138 test_must_fail env GIT_DIR=.git git fsck 2>out &&
6789275d 139 test_grep "HEAD points to something strange" out
122f76f5
JH
140'
141
a6f43364 142test_expect_success REFFILES 'HEAD link pointing at a funny object (from different wt)' '
1c6667cb 143 test_when_finished "git update-ref HEAD $orig_head" &&
c603138e 144 test_when_finished "git worktree remove -f wt" &&
b29759d8 145 git worktree add wt &&
b29759d8
NTND
146 echo $ZERO_OID >.git/HEAD &&
147 # avoid corrupt/broken HEAD from interfering with repo discovery
148 test_must_fail git -C wt fsck 2>out &&
6789275d 149 test_grep "main-worktree/HEAD: detached HEAD points" out
b29759d8
NTND
150'
151
a6f43364 152test_expect_success REFFILES 'other worktree HEAD link pointing at a funny object' '
c603138e 153 test_when_finished "git worktree remove -f other" &&
b29759d8
NTND
154 git worktree add other &&
155 echo $ZERO_OID >.git/worktrees/other/HEAD &&
156 test_must_fail git fsck 2>out &&
6789275d 157 test_grep "worktrees/other/HEAD: detached HEAD points" out
b29759d8
NTND
158'
159
160test_expect_success 'other worktree HEAD link pointing at missing object' '
c603138e 161 test_when_finished "git worktree remove -f other" &&
b29759d8 162 git worktree add other &&
1c6667cb
PS
163 object_id=$(echo "Contents missing from repo" | git hash-object --stdin) &&
164 test-tool -C other ref-store main update-ref msg HEAD $object_id "" REF_NO_DEREF,REF_SKIP_OID_VERIFICATION &&
b29759d8 165 test_must_fail git fsck 2>out &&
6789275d 166 test_grep "worktrees/other/HEAD: invalid sha1 pointer" out
b29759d8
NTND
167'
168
169test_expect_success 'other worktree HEAD link pointing at a funny place' '
c603138e 170 test_when_finished "git worktree remove -f other" &&
b29759d8 171 git worktree add other &&
23937116 172 git -C other symbolic-ref HEAD refs/funny/place &&
b29759d8 173 test_must_fail git fsck 2>out &&
6789275d 174 test_grep "worktrees/other/HEAD points to something strange" out
b29759d8
NTND
175'
176
42d4e1d1 177test_expect_success 'commit with multiple signatures is okay' '
178 git cat-file commit HEAD >basis &&
179 cat >sigs <<-EOF &&
180 gpgsig -----BEGIN PGP SIGNATURE-----
181 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
182 -----END PGP SIGNATURE-----
183 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
184 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
185 -----END PGP SIGNATURE-----
186 EOF
187 sed -e "/^committer/q" basis >okay &&
188 cat sigs >>okay &&
189 echo >>okay &&
190 sed -e "1,/^$/d" basis >>okay &&
191 cat okay &&
192 new=$(git hash-object -t commit -w --stdin <okay) &&
193 test_when_finished "remove_object $new" &&
194 git update-ref refs/heads/bogus "$new" &&
195 test_when_finished "git update-ref -d refs/heads/bogus" &&
196 git fsck 2>out &&
197 cat out &&
198 ! grep "commit $new" out
199'
200
daae1922
JN
201test_expect_success 'email without @ is okay' '
202 git cat-file commit HEAD >basis &&
203 sed "s/@/AT/" basis >okay &&
204 new=$(git hash-object -t commit -w --stdin <okay) &&
dbedf8bf 205 test_when_finished "remove_object $new" &&
daae1922 206 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 207 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922 208 git fsck 2>out &&
dbedf8bf 209 ! grep "commit $new" out
daae1922 210'
daae1922 211
daae1922
JN
212test_expect_success 'email with embedded > is not okay' '
213 git cat-file commit HEAD >basis &&
214 sed "s/@[a-z]/&>/" basis >bad-email &&
34959d80 215 new=$(git hash-object --literally -t commit -w --stdin <bad-email) &&
dbedf8bf 216 test_when_finished "remove_object $new" &&
daae1922 217 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 218 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 219 test_must_fail git fsck 2>out &&
6789275d 220 test_grep "error in commit $new" out
daae1922 221'
02a6552c 222
53f53cff 223test_expect_success 'missing < email delimiter is reported nicely' '
e3c98120
DI
224 git cat-file commit HEAD >basis &&
225 sed "s/<//" basis >bad-email-2 &&
34959d80 226 new=$(git hash-object --literally -t commit -w --stdin <bad-email-2) &&
e3c98120
DI
227 test_when_finished "remove_object $new" &&
228 git update-ref refs/heads/bogus "$new" &&
229 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 230 test_must_fail git fsck 2>out &&
6789275d 231 test_grep "error in commit $new.* - bad name" out
e3c98120
DI
232'
233
53f53cff 234test_expect_success 'missing email is reported nicely' '
e3c98120
DI
235 git cat-file commit HEAD >basis &&
236 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
34959d80 237 new=$(git hash-object --literally -t commit -w --stdin <bad-email-3) &&
e3c98120
DI
238 test_when_finished "remove_object $new" &&
239 git update-ref refs/heads/bogus "$new" &&
240 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 241 test_must_fail git fsck 2>out &&
6789275d 242 test_grep "error in commit $new.* - missing email" out
e3c98120
DI
243'
244
53f53cff 245test_expect_success '> in name is reported' '
e3c98120
DI
246 git cat-file commit HEAD >basis &&
247 sed "s/ </> </" basis >bad-email-4 &&
34959d80 248 new=$(git hash-object --literally -t commit -w --stdin <bad-email-4) &&
e3c98120
DI
249 test_when_finished "remove_object $new" &&
250 git update-ref refs/heads/bogus "$new" &&
251 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 252 test_must_fail git fsck 2>out &&
6789275d 253 test_grep "error in commit $new" out
e3c98120
DI
254'
255
d4b8de04
JK
256# date is 2^64 + 1
257test_expect_success 'integer overflow in timestamps is reported' '
258 git cat-file commit HEAD >basis &&
259 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
260 <basis >bad-timestamp &&
34959d80 261 new=$(git hash-object --literally -t commit -w --stdin <bad-timestamp) &&
d4b8de04
JK
262 test_when_finished "remove_object $new" &&
263 git update-ref refs/heads/bogus "$new" &&
264 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 265 test_must_fail git fsck 2>out &&
6789275d 266 test_grep "error in commit $new.*integer overflow" out
d4b8de04
JK
267'
268
80c7f5a0
RS
269test_expect_success 'commit with NUL in header' '
270 git cat-file commit HEAD >basis &&
271 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
34959d80 272 new=$(git hash-object --literally -t commit -w --stdin <commit-NUL-header) &&
80c7f5a0
RS
273 test_when_finished "remove_object $new" &&
274 git update-ref refs/heads/bogus "$new" &&
275 test_when_finished "git update-ref -d refs/heads/bogus" &&
276 test_must_fail git fsck 2>out &&
6789275d 277 test_grep "error in commit $new.*unterminated header: NUL at offset" out
80c7f5a0
RS
278'
279
8354fa3d 280test_expect_success 'tree object with duplicate entries' '
1ada11ee 281 test_when_finished "for i in \$T; do remove_object \$i; done" &&
2e770fe4
JK
282 T=$(
283 GIT_INDEX_FILE=test-index &&
284 export GIT_INDEX_FILE &&
285 rm -f test-index &&
286 >x &&
287 git add x &&
1ada11ee 288 git rev-parse :x &&
2e770fe4 289 T=$(git write-tree) &&
1ada11ee 290 echo $T &&
2e770fe4
JK
291 (
292 git cat-file tree $T &&
293 git cat-file tree $T
294 ) |
34959d80 295 git hash-object --literally -w -t tree --stdin
2e770fe4
JK
296 ) &&
297 test_must_fail git fsck 2>out &&
6789275d 298 test_grep "error in tree .*contains duplicate file entries" out
2e770fe4
JK
299'
300
fc12aa7b
RS
301check_duplicate_names () {
302 expect=$1 &&
303 shift &&
304 names=$@ &&
305 test_expect_$expect "tree object with duplicate names: $names" '
306 test_when_finished "remove_object \$blob" &&
307 test_when_finished "remove_object \$tree" &&
308 test_when_finished "remove_object \$badtree" &&
309 blob=$(echo blob | git hash-object -w --stdin) &&
310 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
311 tree=$(git mktree <tree) &&
312 for name in $names
313 do
314 case "$name" in
315 */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
316 *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
317 esac
318 done >badtree &&
319 badtree=$(git mktree <badtree) &&
320 test_must_fail git fsck 2>out &&
6789275d
JH
321 test_grep "$badtree" out &&
322 test_grep "error in tree .*contains duplicate file entries" out
fc12aa7b
RS
323 '
324}
325
326check_duplicate_names success x x.1 x/
327check_duplicate_names success x x.1.2 x.1/ x/
fe747043 328check_duplicate_names success x x.1 x.1.2 x/
9068cfb2 329
8354fa3d 330test_expect_success 'unparseable tree object' '
8de6b383 331 test_oid_cache <<-\EOF &&
332 junk sha1:twenty-bytes-of-junk
333 junk sha256:twenty-bytes-of-junk-twelve-more
334 EOF
335
8354fa3d
DT
336 test_when_finished "git update-ref -d refs/heads/wrong" &&
337 test_when_finished "remove_object \$tree_sha1" &&
338 test_when_finished "remove_object \$commit_sha1" &&
8de6b383 339 junk=$(test_oid junk) &&
340 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
8354fa3d
DT
341 commit_sha1=$(git commit-tree $tree_sha1) &&
342 git update-ref refs/heads/wrong $commit_sha1 &&
343 test_must_fail git fsck 2>out &&
6789275d
JH
344 test_grep "error: empty filename in tree entry" out &&
345 test_grep "$tree_sha1" out &&
346 test_grep ! "fatal: empty filename in tree entry" out
8354fa3d
DT
347'
348
2720f6db
RS
349test_expect_success 'tree entry with type mismatch' '
350 test_when_finished "remove_object \$blob" &&
351 test_when_finished "remove_object \$tree" &&
352 test_when_finished "remove_object \$commit" &&
353 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
354 blob=$(echo blob | git hash-object -w --stdin) &&
355 blob_bin=$(echo $blob | hex2oct) &&
356 tree=$(
357 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
358 git hash-object -t tree --stdin -w --literally
359 ) &&
360 commit=$(git commit-tree $tree) &&
361 git update-ref refs/heads/type_mismatch $commit &&
362 test_must_fail git fsck >out 2>&1 &&
6789275d
JH
363 test_grep "is a blob, not a tree" out &&
364 test_grep ! "dangling blob" out
2720f6db
RS
365'
366
53602a93
JK
367test_expect_success 'tree entry with bogus mode' '
368 test_when_finished "remove_object \$blob" &&
369 test_when_finished "remove_object \$tree" &&
370 blob=$(echo blob | git hash-object -w --stdin) &&
371 blob_oct=$(echo $blob | hex2oct) &&
372 tree=$(printf "100000 foo\0${blob_oct}" |
373 git hash-object -t tree --stdin -w --literally) &&
374 git fsck 2>err &&
375 cat >expect <<-EOF &&
376 warning in tree $tree: badFilemode: contains bad file modes
377 EOF
378 test_cmp expect err
379'
380
4551d035 381test_expect_success 'tag pointing to nonexistent' '
8de6b383 382 badoid=$(test_oid deadbeef) &&
383 cat >invalid-tag <<-EOF &&
384 object $badoid
dbedf8bf
JN
385 type commit
386 tag invalid
387 tagger T A Gger <tagger@example.com> 1234567890 -0000
388
389 This is an invalid tag.
390 EOF
391
392 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
393 test_when_finished "remove_object $tag" &&
1c6667cb 394 git update-ref refs/tags/invalid $tag &&
dbedf8bf 395 test_when_finished "git update-ref -d refs/tags/invalid" &&
4551d035 396 test_must_fail git fsck --tags >out &&
6789275d 397 test_grep "broken link" out
02a6552c
TR
398'
399
4551d035 400test_expect_success 'tag pointing to something else than its type' '
dbedf8bf
JN
401 sha=$(echo blob | git hash-object -w --stdin) &&
402 test_when_finished "remove_object $sha" &&
403 cat >wrong-tag <<-EOF &&
404 object $sha
405 type commit
406 tag wrong
407 tagger T A Gger <tagger@example.com> 1234567890 -0000
408
409 This is an invalid tag.
410 EOF
411
412 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
413 test_when_finished "remove_object $tag" &&
1c6667cb 414 git update-ref refs/tags/wrong $tag &&
dbedf8bf 415 test_when_finished "git update-ref -d refs/tags/wrong" &&
9dad83be 416 test_must_fail git fsck --tags
02a6552c
TR
417'
418
90e3e5f0
JS
419test_expect_success 'tag with incorrect tag name & missing tagger' '
420 sha=$(git rev-parse HEAD) &&
421 cat >wrong-tag <<-EOF &&
422 object $sha
423 type commit
424 tag wrong name format
425
426 This is an invalid tag.
427 EOF
428
34959d80 429 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
90e3e5f0 430 test_when_finished "remove_object $tag" &&
1c6667cb 431 git update-ref refs/tags/wrong $tag &&
90e3e5f0
JS
432 test_when_finished "git update-ref -d refs/tags/wrong" &&
433 git fsck --tags 2>out &&
7add4419
JK
434
435 cat >expect <<-EOF &&
71ab8fa8
JS
436 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
437 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
7add4419 438 EOF
1108cea7 439 test_cmp expect out
90e3e5f0
JS
440'
441
b659605d
JH
442test_expect_success 'tag with bad tagger' '
443 sha=$(git rev-parse HEAD) &&
444 cat >wrong-tag <<-EOF &&
445 object $sha
446 type commit
447 tag not-quite-wrong
448 tagger Bad Tagger Name
449
450 This is an invalid tag.
451 EOF
452
453 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
454 test_when_finished "remove_object $tag" &&
1c6667cb 455 git update-ref refs/tags/wrong $tag &&
b659605d
JH
456 test_when_finished "git update-ref -d refs/tags/wrong" &&
457 test_must_fail git fsck --tags 2>out &&
6789275d 458 test_grep "error in tag .*: invalid author/committer" out
b659605d
JH
459'
460
8a272f29 461test_expect_success 'tag with NUL in header' '
80c7f5a0
RS
462 sha=$(git rev-parse HEAD) &&
463 q_to_nul >tag-NUL-header <<-EOF &&
464 object $sha
465 type commit
466 tag contains-Q-in-header
467 tagger T A Gger <tagger@example.com> 1234567890 -0000
468
469 This is an invalid tag.
470 EOF
471
472 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
473 test_when_finished "remove_object $tag" &&
1c6667cb 474 git update-ref refs/tags/wrong $tag &&
80c7f5a0
RS
475 test_when_finished "git update-ref -d refs/tags/wrong" &&
476 test_must_fail git fsck --tags 2>out &&
6789275d 477 test_grep "error in tag $tag.*unterminated header: NUL at offset" out
80c7f5a0
RS
478'
479
dbedf8bf
JN
480test_expect_success 'cleaned up' '
481 git fsck >actual 2>&1 &&
1c5e94f4 482 test_must_be_empty actual
dbedf8bf 483'
02a6552c 484
cb8da705
CB
485test_expect_success 'rev-list --verify-objects' '
486 git rev-list --verify-objects --all >/dev/null 2>out &&
1c5e94f4 487 test_must_be_empty out
cb8da705
CB
488'
489
490test_expect_success 'rev-list --verify-objects with bad sha1' '
491 sha=$(echo blob | git hash-object -w --stdin) &&
8de6b383 492 old=$(test_oid_to_path $sha) &&
493 new=$(dirname $old)/$(test_oid ff_2) &&
cb8da705
CB
494 sha="$(dirname $new)$(basename $new)" &&
495 mv .git/objects/$old .git/objects/$new &&
496 test_when_finished "remove_object $sha" &&
497 git update-index --add --cacheinfo 100644 $sha foo &&
498 test_when_finished "git read-tree -u --reset HEAD" &&
499 tree=$(git write-tree) &&
500 test_when_finished "remove_object $tree" &&
501 cmt=$(echo bogus | git commit-tree $tree) &&
502 test_when_finished "remove_object $cmt" &&
503 git update-ref refs/heads/bogus $cmt &&
504 test_when_finished "git update-ref -d refs/heads/bogus" &&
505
506 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
6789275d 507 test_grep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
cb8da705
CB
508'
509
0bc25579
JK
510# An actual bit corruption is more likely than swapped commits, but
511# this provides an easy way to have commits which don't match their purported
512# hashes, but which aren't so broken we can't read them at all.
513test_expect_success 'rev-list --verify-objects notices swapped commits' '
514 git init swapped-commits &&
515 (
516 cd swapped-commits &&
517 test_commit one &&
518 test_commit two &&
519 one_oid=$(git rev-parse HEAD) &&
520 two_oid=$(git rev-parse HEAD^) &&
521 one=.git/objects/$(test_oid_to_path $one_oid) &&
522 two=.git/objects/$(test_oid_to_path $two_oid) &&
523 mv $one tmp &&
524 mv $two $one &&
525 mv tmp $two &&
526 test_must_fail git rev-list --verify-objects HEAD
527 )
528'
529
b27ccae3
JK
530test_expect_success 'set up repository with commit-graph' '
531 git init corrupt-graph &&
532 (
533 cd corrupt-graph &&
534 test_commit one &&
535 test_commit two &&
536 git commit-graph write --reachable
537 )
538'
539
540corrupt_graph_obj () {
541 oid=$(git -C corrupt-graph rev-parse "$1") &&
542 obj=corrupt-graph/.git/objects/$(test_oid_to_path $oid) &&
543 test_when_finished 'mv backup $obj' &&
544 mv $obj backup &&
545 echo garbage >$obj
546}
547
548test_expect_success 'rev-list --verify-objects with commit graph (tip)' '
549 corrupt_graph_obj HEAD &&
550 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
551'
552
553test_expect_success 'rev-list --verify-objects with commit graph (parent)' '
554 corrupt_graph_obj HEAD^ &&
555 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
556'
557
2becf00f
JS
558test_expect_success 'force fsck to ignore double author' '
559 git cat-file commit HEAD >basis &&
560 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
34959d80 561 new=$(git hash-object --literally -t commit -w --stdin <multiple-authors) &&
2becf00f
JS
562 test_when_finished "remove_object $new" &&
563 git update-ref refs/heads/bogus "$new" &&
564 test_when_finished "git update-ref -d refs/heads/bogus" &&
565 test_must_fail git fsck &&
566 git -c fsck.multipleAuthors=ignore fsck
567'
568
c479d14a 569_bz='\0'
8de6b383 570_bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
c479d14a
JK
571
572test_expect_success 'fsck notices blob entry pointing to null sha1' '
573 (git init null-blob &&
574 cd null-blob &&
8de6b383 575 sha=$(printf "100644 file$_bz$_bzoid" |
34959d80 576 git hash-object --literally -w --stdin -t tree) &&
c479d14a 577 git fsck 2>out &&
6789275d 578 test_grep "warning.*null sha1" out
c479d14a
JK
579 )
580'
581
582test_expect_success 'fsck notices submodule entry pointing to null sha1' '
583 (git init null-commit &&
584 cd null-commit &&
8de6b383 585 sha=$(printf "160000 submodule$_bz$_bzoid" |
34959d80 586 git hash-object --literally -w --stdin -t tree) &&
c479d14a 587 git fsck 2>out &&
6789275d 588 test_grep "warning.*null sha1" out
c479d14a
JK
589 )
590'
591
0fbcaef6
JK
592test_expect_success 'fsck notices excessively large tree entry name' '
593 git init large-name &&
594 (
595 cd large-name &&
596 test_commit a-long-name &&
597 git -c fsck.largePathname=warn:10 fsck 2>out &&
598 grep "warning.*large pathname" out
599 )
600'
601
a18fcc9f 602while read name path pretty; do
450870cb 603 while read mode type; do
a18fcc9f
JK
604 : ${pretty:=$path}
605 test_expect_success "fsck notices $pretty as $type" '
450870cb
JK
606 (
607 git init $name-$type &&
608 cd $name-$type &&
e1d911dd 609 git config core.protectNTFS false &&
450870cb
JK
610 echo content >file &&
611 git add file &&
612 git commit -m base &&
613 blob=$(git rev-parse :file) &&
614 tree=$(git rev-parse HEAD^{tree}) &&
615 value=$(eval "echo \$$type") &&
616 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
d08c13b9 617 bad_tree=$(git mktree <bad) &&
450870cb 618 git fsck 2>out &&
6789275d 619 test_grep "warning.*tree $bad_tree" out
450870cb
JK
620 )'
621 done <<-\EOF
622 100644 blob
623 040000 tree
624 EOF
a18fcc9f 625done <<-EOF
450870cb
JK
626dot .
627dotdot ..
628dotgit .git
76e86fc6 629dotgit-case .GIT
a18fcc9f 630dotgit-unicode .gI${u200c}T .gI{u200c}T
d08c13b9
JS
631dotgit-case2 .Git
632git-tilde1 git~1
633dotgitdot .git.
634dot-backslash-case .\\\\.GIT\\\\foobar
635dotgit-case-backslash .git\\\\foobar
450870cb 636EOF
5c17f512 637
6aaf956b
JK
638test_expect_success 'fsck allows .Ňit' '
639 (
640 git init not-dotgit &&
641 cd not-dotgit &&
642 echo content >file &&
643 git add file &&
644 git commit -m base &&
645 blob=$(git rev-parse :file) &&
646 printf "100644 blob $blob\t.\\305\\207it" >tree &&
647 tree=$(git mktree <tree) &&
648 git fsck 2>err &&
649 test_line_count = 0 err
650 )
651'
652
6d2d780f
JH
653test_expect_success 'NUL in commit' '
654 rm -fr nul-in-commit &&
655 git init nul-in-commit &&
656 (
657 cd nul-in-commit &&
658 git commit --allow-empty -m "initial commitQNUL after message" &&
659 git cat-file commit HEAD >original &&
660 q_to_nul <original >munged &&
34959d80 661 git hash-object --literally -w -t commit --stdin <munged >name &&
6d2d780f
JH
662 git branch bad $(cat name) &&
663
664 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
6789275d 665 test_grep nulInCommit warn.1 &&
6d2d780f 666 git fsck 2>warn.2 &&
6789275d 667 test_grep nulInCommit warn.2
6d2d780f
JH
668 )
669'
670
30d1038d
JK
671# create a static test repo which is broken by omitting
672# one particular object ($1, which is looked up via rev-parse
673# in the new repository).
674create_repo_missing () {
675 rm -rf missing &&
676 git init missing &&
677 (
678 cd missing &&
679 git commit -m one --allow-empty &&
680 mkdir subdir &&
681 echo content >subdir/file &&
682 git add subdir/file &&
683 git commit -m two &&
684 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
685 git tag -m foo tag $unrelated &&
686 sha1=$(git rev-parse --verify "$1") &&
687 path=$(echo $sha1 | sed 's|..|&/|') &&
688 rm .git/objects/$path
689 )
690}
691
692test_expect_success 'fsck notices missing blob' '
693 create_repo_missing HEAD:subdir/file &&
694 test_must_fail git -C missing fsck
695'
696
697test_expect_success 'fsck notices missing subtree' '
698 create_repo_missing HEAD:subdir &&
699 test_must_fail git -C missing fsck
700'
701
702test_expect_success 'fsck notices missing root tree' '
703 create_repo_missing HEAD^{tree} &&
704 test_must_fail git -C missing fsck
705'
706
707test_expect_success 'fsck notices missing parent' '
708 create_repo_missing HEAD^ &&
709 test_must_fail git -C missing fsck
710'
711
712test_expect_success 'fsck notices missing tagged object' '
713 create_repo_missing tag^{blob} &&
714 test_must_fail git -C missing fsck
715'
716
717test_expect_success 'fsck notices ref pointing to missing commit' '
718 create_repo_missing HEAD &&
719 test_must_fail git -C missing fsck
720'
721
722test_expect_success 'fsck notices ref pointing to missing tag' '
723 create_repo_missing tag &&
724 test_must_fail git -C missing fsck
725'
726
02976bf8
JS
727test_expect_success 'fsck --connectivity-only' '
728 rm -rf connectivity-only &&
729 git init connectivity-only &&
730 (
731 cd connectivity-only &&
732 touch empty &&
733 git add empty &&
734 test_commit empty &&
3e3f8bd6
JK
735
736 # Drop the index now; we want to be sure that we
737 # recursively notice the broken objects
738 # because they are reachable from refs, not because
739 # they are in the index.
740 rm -f .git/index &&
741
742 # corrupt the blob, but in a way that we can still identify
743 # its type. That lets us see that --connectivity-only is
744 # not actually looking at the contents, but leaves it
745 # free to examine the type if it chooses.
8de6b383 746 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
3e3f8bd6 747 blob=$(echo unrelated | git hash-object -w --stdin) &&
c20d4d70 748 mv -f $(sha1_file $blob) $empty &&
3e3f8bd6 749
02976bf8
JS
750 test_must_fail git fsck --strict &&
751 git fsck --strict --connectivity-only &&
752 tree=$(git rev-parse HEAD:) &&
753 suffix=${tree#??} &&
754 tree=.git/objects/${tree%$suffix}/$suffix &&
755 rm -f $tree &&
756 echo invalid >$tree &&
757 test_must_fail git fsck --strict --connectivity-only
758 )
759'
760
3e3f8bd6
JK
761test_expect_success 'fsck --connectivity-only with explicit head' '
762 rm -rf connectivity-only &&
763 git init connectivity-only &&
764 (
765 cd connectivity-only &&
766 test_commit foo &&
767 rm -f .git/index &&
768 tree=$(git rev-parse HEAD^{tree}) &&
769 remove_object $(git rev-parse HEAD:foo.t) &&
770 test_must_fail git fsck --connectivity-only $tree
771 )
772'
773
90cf590f
JS
774test_expect_success 'fsck --name-objects' '
775 rm -rf name-objects &&
776 git init name-objects &&
777 (
778 cd name-objects &&
e89f8936 779 git config core.logAllRefUpdates false &&
90cf590f 780 test_commit julius caesar.t &&
e89f8936
JS
781 test_commit augustus44 &&
782 test_commit caesar &&
0b20f1a2 783 remove_object $(git rev-parse julius:caesar.t) &&
90cf590f 784 tree=$(git rev-parse --verify julius:) &&
e89f8936
JS
785 git tag -d julius &&
786 test_must_fail git fsck --name-objects >out &&
6789275d 787 test_grep "$tree (refs/tags/augustus44\\^:" out
90cf590f
JS
788 )
789'
790
771e7d57
JK
791test_expect_success 'alternate objects are correctly blamed' '
792 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
8de6b383 793 name=$(test_oid numeric) &&
794 path=$(test_oid_to_path "$name") &&
771e7d57
JK
795 git init --bare alt.git &&
796 echo "../../alt.git/objects" >.git/objects/info/alternates &&
8de6b383 797 mkdir alt.git/objects/$(dirname $path) &&
798 >alt.git/objects/$(dirname $path)/$(basename $path) &&
771e7d57 799 test_must_fail git fsck >out 2>&1 &&
6789275d 800 test_grep alt.git out
771e7d57
JK
801'
802
118e6cea
JK
803test_expect_success 'fsck errors in packed objects' '
804 git cat-file commit HEAD >basis &&
805 sed "s/</one/" basis >one &&
806 sed "s/</foo/" basis >two &&
34959d80
JK
807 one=$(git hash-object --literally -t commit -w one) &&
808 two=$(git hash-object --literally -t commit -w two) &&
118e6cea
JK
809 pack=$(
810 {
811 echo $one &&
812 echo $two
813 } | git pack-objects .git/objects/pack/pack
814 ) &&
815 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
816 remove_object $one &&
817 remove_object $two &&
818 test_must_fail git fsck 2>out &&
6789275d
JH
819 test_grep "error in commit $one.* - bad name" out &&
820 test_grep "error in commit $two.* - bad name" out &&
118e6cea
JK
821 ! grep corrupt out
822'
823
a7c28a21
JT
824test_expect_success 'fsck fails on corrupt packfile' '
825 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
826 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
827
828 # Corrupt the first byte of the first object. (It contains 3 type bits,
829 # at least one of which is not zero, so setting the first byte to 0 is
830 # sufficient.)
831 chmod a+w .git/objects/pack/pack-$pack.pack &&
dc156bc3 832 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
a7c28a21
JT
833
834 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
835 remove_object $hsh &&
836 test_must_fail git fsck 2>out &&
6789275d 837 test_grep "checksum mismatch" out
a7c28a21
JT
838'
839
c68b489e
JK
840test_expect_success 'fsck finds problems in duplicate loose objects' '
841 rm -rf broken-duplicate &&
842 git init broken-duplicate &&
843 (
844 cd broken-duplicate &&
845 test_commit duplicate &&
846 # no "-d" here, so we end up with duplicates
847 git repack &&
848 # now corrupt the loose copy
4627c67f
ÆAB
849 oid="$(git rev-parse HEAD)" &&
850 file=$(sha1_file "$oid") &&
c68b489e
JK
851 rm "$file" &&
852 echo broken >"$file" &&
4627c67f
ÆAB
853 test_must_fail git fsck 2>err &&
854
855 cat >expect <<-EOF &&
856 error: inflate: data stream error (incorrect header check)
857 error: unable to unpack header of $file
858 error: $oid: object corrupt or missing: $file
859 EOF
860 grep "^error: " err >actual &&
861 test_cmp expect actual
c68b489e
JK
862 )
863'
864
cce044df
JK
865test_expect_success 'fsck detects trailing loose garbage (commit)' '
866 git cat-file commit HEAD >basis &&
867 echo bump-commit-sha1 >>basis &&
868 commit=$(git hash-object -w -t commit basis) &&
869 file=$(sha1_file $commit) &&
870 test_when_finished "remove_object $commit" &&
871 chmod +w "$file" &&
872 echo garbage >>"$file" &&
873 test_must_fail git fsck 2>out &&
6789275d 874 test_grep "garbage.*$commit" out
cce044df
JK
875'
876
5632baf2 877test_expect_success 'fsck detects trailing loose garbage (large blob)' '
cce044df
JK
878 blob=$(echo trailing | git hash-object -w --stdin) &&
879 file=$(sha1_file $blob) &&
880 test_when_finished "remove_object $blob" &&
881 chmod +w "$file" &&
882 echo garbage >>"$file" &&
5632baf2 883 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
6789275d 884 test_grep "garbage.*$blob" out
cce044df
JK
885'
886
ccdc4819
JK
887test_expect_success 'fsck detects truncated loose object' '
888 # make it big enough that we know we will truncate in the data
889 # portion, not the header
18ad13e5 890 test-tool genrandom truncate 4096 >file &&
ccdc4819
JK
891 blob=$(git hash-object -w file) &&
892 file=$(sha1_file $blob) &&
893 test_when_finished "remove_object $blob" &&
894 test_copy_bytes 1024 <"$file" >tmp &&
895 rm "$file" &&
896 mv -f tmp "$file" &&
897
898 # check both regular and streaming code paths
899 test_must_fail git fsck 2>out &&
6789275d 900 test_grep corrupt.*$blob out &&
ccdc4819
JK
901
902 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
6789275d 903 test_grep corrupt.*$blob out
ccdc4819
JK
904'
905
b4584e4f
JK
906# for each of type, we have one version which is referenced by another object
907# (and so while unreachable, not dangling), and another variant which really is
908# dangling.
8d8c2a5a 909test_expect_success 'create dangling-object repository' '
b4584e4f
JK
910 git init dangling &&
911 (
912 cd dangling &&
913 blob=$(echo not-dangling | git hash-object -w --stdin) &&
914 dblob=$(echo dangling | git hash-object -w --stdin) &&
915 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
916 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
917 commit=$(git commit-tree $tree) &&
918 dcommit=$(git commit-tree -p $commit $tree) &&
919
8d8c2a5a 920 cat >expect <<-EOF
b4584e4f
JK
921 dangling blob $dblob
922 dangling commit $dcommit
923 dangling tree $dtree
924 EOF
8d8c2a5a
JK
925 )
926'
b4584e4f 927
8d8c2a5a
JK
928test_expect_success 'fsck notices dangling objects' '
929 (
930 cd dangling &&
b4584e4f
JK
931 git fsck >actual &&
932 # the output order is non-deterministic, as it comes from a hash
933 sort <actual >actual.sorted &&
1108cea7 934 test_cmp expect actual.sorted
b4584e4f
JK
935 )
936'
937
8d8c2a5a
JK
938test_expect_success 'fsck --connectivity-only notices dangling objects' '
939 (
940 cd dangling &&
941 git fsck --connectivity-only >actual &&
942 # the output order is non-deterministic, as it comes from a hash
943 sort <actual >actual.sorted &&
1108cea7 944 test_cmp expect actual.sorted
8d8c2a5a
JK
945 )
946'
947
c6c7b16d
JK
948test_expect_success 'fsck $name notices bogus $name' '
949 test_must_fail git fsck bogus &&
8125a58b 950 test_must_fail git fsck $ZERO_OID
c6c7b16d
JK
951'
952
c3271a0e
JK
953test_expect_success 'bogus head does not fallback to all heads' '
954 # set up a case that will cause a reachability complaint
955 echo to-be-deleted >foo &&
956 git add foo &&
957 blob=$(git rev-parse :foo) &&
958 test_when_finished "git rm --cached foo" &&
959 remove_object $blob &&
8125a58b 960 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
c3271a0e
JK
961 ! grep $blob out
962'
963
4d9bc37f
JH
964# Corrupt the checksum on the index.
965# Add 1 to the last byte in the SHA.
966corrupt_index_checksum () {
967 perl -w -e '
968 use Fcntl ":seek";
969 open my $fh, "+<", ".git/index" or die "open: $!";
970 binmode $fh;
971 seek $fh, -1, SEEK_END or die "seek: $!";
972 read $fh, my $in_byte, 1 or die "read: $!";
973
974 $in_value = unpack("C", $in_byte);
975 $out_value = ($in_value + 1) & 255;
976
977 $out_byte = pack("C", $out_value);
978
979 seek $fh, -1, SEEK_END or die "seek: $!";
980 print $fh $out_byte;
981 close $fh or die "close: $!";
982 '
983}
984
985# Corrupt the checksum on the index and then
986# verify that only fsck notices.
a33fc72f
JH
987test_expect_success 'detect corrupt index file in fsck' '
988 cp .git/index .git/index.backup &&
989 test_when_finished "mv .git/index.backup .git/index" &&
4d9bc37f
JH
990 corrupt_index_checksum &&
991 test_must_fail git fsck --cache 2>errors &&
6789275d 992 test_grep "bad index file" errors
a33fc72f
JH
993'
994
31deb28f 995test_expect_success 'fsck error and recovery on invalid object type' '
093fffdf
ÆAB
996 git init --bare garbage-type &&
997 (
998 cd garbage-type &&
999
31deb28f 1000 garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
093fffdf 1001
59162ece 1002 test_must_fail git fsck 2>err &&
31deb28f
ÆAB
1003 grep -e "^error" -e "^fatal" err >errors &&
1004 test_line_count = 1 errors &&
1005 grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err
093fffdf
ÆAB
1006 )
1007'
1008
27ab4784
PS
1009test_expect_success 'fsck error on gitattributes with excessive line lengths' '
1010 blob=$(printf "pattern %02048d" 1 | git hash-object -w --stdin) &&
1011 test_when_finished "remove_object $blob" &&
1012 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
1013 test_when_finished "remove_object $tree" &&
1014 cat >expected <<-EOF &&
1015 error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
1016 EOF
1017 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1018 test_cmp expected actual
1019'
1020
1021test_expect_success 'fsck error on gitattributes with excessive size' '
1022 blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
1023 test_when_finished "remove_object $blob" &&
1024 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
1025 test_when_finished "remove_object $tree" &&
1026 cat >expected <<-EOF &&
1027 error in blob $blob: gitattributesLarge: .gitattributes too large to parse
1028 EOF
1029 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1030 test_cmp expected actual
1031'
1032
fb64ca52
JK
1033test_expect_success 'fsck detects problems in worktree index' '
1034 test_when_finished "git worktree remove -f wt" &&
1035 git worktree add wt &&
1036
1037 echo "this will be removed to break the worktree index" >wt/file &&
1038 git -C wt add file &&
1039 blob=$(git -C wt rev-parse :file) &&
1040 remove_object $blob &&
1041
592ec63b
JK
1042 test_must_fail git fsck --name-objects >actual 2>&1 &&
1043 cat >expect <<-EOF &&
1044 missing blob $blob (.git/worktrees/wt/index:file)
1045 EOF
1046 test_cmp expect actual
1047'
1048
6e6a529b 1049test_expect_success 'fsck reports problems in current worktree index without filename' '
592ec63b 1050 test_when_finished "rm -f .git/index && git read-tree HEAD" &&
6e6a529b 1051 echo "this object will be removed to break current worktree index" >file &&
592ec63b
JK
1052 git add file &&
1053 blob=$(git rev-parse :file) &&
1054 remove_object $blob &&
1055
1056 test_must_fail git fsck --name-objects >actual 2>&1 &&
1057 cat >expect <<-EOF &&
1058 missing blob $blob (:file)
1059 EOF
1060 test_cmp expect actual
fb64ca52
JK
1061'
1062
469e2ebf 1063test_done