Imported Upstream version 2.17.3
[platform/upstream/git.git] / t / t1450-fsck.sh
1 #!/bin/sh
2
3 test_description='git fsck random collection of tests
4
5 * (HEAD) B
6 * (master) A
7 '
8
9 . ./test-lib.sh
10
11 test_expect_success setup '
12         git config gc.auto 0 &&
13         git config i18n.commitencoding ISO-8859-1 &&
14         test_commit A fileA one &&
15         git config --unset i18n.commitencoding &&
16         git checkout HEAD^0 &&
17         test_commit B fileB two &&
18         git tag -d A B &&
19         git reflog expire --expire=now --all &&
20         >empty
21 '
22
23 test_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 &&
30                 git fsck --no-dangling >../actual 2>&1
31         ) &&
32         test_cmp empty actual
33 '
34
35 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36         git fsck >actual 2>&1 &&
37         test_cmp empty actual
38 '
39
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
44 test_expect_success 'setup: helpers for corruption tests' '
45         sha1_file() {
46                 remainder=${1#??} &&
47                 firsttwo=${1%$remainder} &&
48                 echo ".git/objects/$firsttwo/$remainder"
49         } &&
50
51         remove_object() {
52                 rm "$(sha1_file "$1")"
53         }
54 '
55
56 test_expect_success 'object with bad sha1' '
57         sha=$(echo blob | git hash-object -w --stdin) &&
58         old=$(echo $sha | sed "s+^..+&/+") &&
59         new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
60         sha="$(dirname $new)$(basename $new)" &&
61         mv .git/objects/$old .git/objects/$new &&
62         test_when_finished "remove_object $sha" &&
63         git update-index --add --cacheinfo 100644 $sha foo &&
64         test_when_finished "git read-tree -u --reset HEAD" &&
65         tree=$(git write-tree) &&
66         test_when_finished "remove_object $tree" &&
67         cmt=$(echo bogus | git commit-tree $tree) &&
68         test_when_finished "remove_object $cmt" &&
69         git update-ref refs/heads/bogus $cmt &&
70         test_when_finished "git update-ref -d refs/heads/bogus" &&
71
72         test_must_fail git fsck 2>out &&
73         cat out &&
74         grep "$sha.*corrupt" out
75 '
76
77 test_expect_success 'branch pointing to non-commit' '
78         git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79         test_when_finished "git update-ref -d refs/heads/invalid" &&
80         test_must_fail git fsck 2>out &&
81         cat out &&
82         grep "not a commit" out
83 '
84
85 test_expect_success 'HEAD link pointing at a funny object' '
86         test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
87         mv .git/HEAD .git/SAVED_HEAD &&
88         echo 0000000000000000000000000000000000000000 >.git/HEAD &&
89         # avoid corrupt/broken HEAD from interfering with repo discovery
90         test_must_fail env GIT_DIR=.git git fsck 2>out &&
91         cat out &&
92         grep "detached HEAD points" out
93 '
94
95 test_expect_success 'HEAD link pointing at a funny place' '
96         test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
97         mv .git/HEAD .git/SAVED_HEAD &&
98         echo "ref: refs/funny/place" >.git/HEAD &&
99         # avoid corrupt/broken HEAD from interfering with repo discovery
100         test_must_fail env GIT_DIR=.git git fsck 2>out &&
101         cat out &&
102         grep "HEAD points to something strange" out
103 '
104
105 test_expect_success 'email without @ is okay' '
106         git cat-file commit HEAD >basis &&
107         sed "s/@/AT/" basis >okay &&
108         new=$(git hash-object -t commit -w --stdin <okay) &&
109         test_when_finished "remove_object $new" &&
110         git update-ref refs/heads/bogus "$new" &&
111         test_when_finished "git update-ref -d refs/heads/bogus" &&
112         git fsck 2>out &&
113         cat out &&
114         ! grep "commit $new" out
115 '
116
117 test_expect_success 'email with embedded > is not okay' '
118         git cat-file commit HEAD >basis &&
119         sed "s/@[a-z]/&>/" basis >bad-email &&
120         new=$(git hash-object -t commit -w --stdin <bad-email) &&
121         test_when_finished "remove_object $new" &&
122         git update-ref refs/heads/bogus "$new" &&
123         test_when_finished "git update-ref -d refs/heads/bogus" &&
124         test_must_fail git fsck 2>out &&
125         cat out &&
126         grep "error in commit $new" out
127 '
128
129 test_expect_success 'missing < email delimiter is reported nicely' '
130         git cat-file commit HEAD >basis &&
131         sed "s/<//" basis >bad-email-2 &&
132         new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
133         test_when_finished "remove_object $new" &&
134         git update-ref refs/heads/bogus "$new" &&
135         test_when_finished "git update-ref -d refs/heads/bogus" &&
136         test_must_fail git fsck 2>out &&
137         cat out &&
138         grep "error in commit $new.* - bad name" out
139 '
140
141 test_expect_success 'missing email is reported nicely' '
142         git cat-file commit HEAD >basis &&
143         sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
144         new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
145         test_when_finished "remove_object $new" &&
146         git update-ref refs/heads/bogus "$new" &&
147         test_when_finished "git update-ref -d refs/heads/bogus" &&
148         test_must_fail git fsck 2>out &&
149         cat out &&
150         grep "error in commit $new.* - missing email" out
151 '
152
153 test_expect_success '> in name is reported' '
154         git cat-file commit HEAD >basis &&
155         sed "s/ </> </" basis >bad-email-4 &&
156         new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
157         test_when_finished "remove_object $new" &&
158         git update-ref refs/heads/bogus "$new" &&
159         test_when_finished "git update-ref -d refs/heads/bogus" &&
160         test_must_fail git fsck 2>out &&
161         cat out &&
162         grep "error in commit $new" out
163 '
164
165 # date is 2^64 + 1
166 test_expect_success 'integer overflow in timestamps is reported' '
167         git cat-file commit HEAD >basis &&
168         sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
169                 <basis >bad-timestamp &&
170         new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
171         test_when_finished "remove_object $new" &&
172         git update-ref refs/heads/bogus "$new" &&
173         test_when_finished "git update-ref -d refs/heads/bogus" &&
174         test_must_fail git fsck 2>out &&
175         cat out &&
176         grep "error in commit $new.*integer overflow" out
177 '
178
179 test_expect_success 'commit with NUL in header' '
180         git cat-file commit HEAD >basis &&
181         sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
182         new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
183         test_when_finished "remove_object $new" &&
184         git update-ref refs/heads/bogus "$new" &&
185         test_when_finished "git update-ref -d refs/heads/bogus" &&
186         test_must_fail git fsck 2>out &&
187         cat out &&
188         grep "error in commit $new.*unterminated header: NUL at offset" out
189 '
190
191 test_expect_success 'tree object with duplicate entries' '
192         test_when_finished "for i in \$T; do remove_object \$i; done" &&
193         T=$(
194                 GIT_INDEX_FILE=test-index &&
195                 export GIT_INDEX_FILE &&
196                 rm -f test-index &&
197                 >x &&
198                 git add x &&
199                 git rev-parse :x &&
200                 T=$(git write-tree) &&
201                 echo $T &&
202                 (
203                         git cat-file tree $T &&
204                         git cat-file tree $T
205                 ) |
206                 git hash-object -w -t tree --stdin
207         ) &&
208         test_must_fail git fsck 2>out &&
209         grep "error in tree .*contains duplicate file entries" out
210 '
211
212 test_expect_success 'unparseable tree object' '
213         test_when_finished "git update-ref -d refs/heads/wrong" &&
214         test_when_finished "remove_object \$tree_sha1" &&
215         test_when_finished "remove_object \$commit_sha1" &&
216         tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
217         commit_sha1=$(git commit-tree $tree_sha1) &&
218         git update-ref refs/heads/wrong $commit_sha1 &&
219         test_must_fail git fsck 2>out &&
220         test_i18ngrep "error: empty filename in tree entry" out &&
221         test_i18ngrep "$tree_sha1" out &&
222         test_i18ngrep ! "fatal: empty filename in tree entry" out
223 '
224
225 hex2oct() {
226         perl -ne 'printf "\\%03o", hex for /../g'
227 }
228
229 test_expect_success 'tree entry with type mismatch' '
230         test_when_finished "remove_object \$blob" &&
231         test_when_finished "remove_object \$tree" &&
232         test_when_finished "remove_object \$commit" &&
233         test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
234         blob=$(echo blob | git hash-object -w --stdin) &&
235         blob_bin=$(echo $blob | hex2oct) &&
236         tree=$(
237                 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
238                 git hash-object -t tree --stdin -w --literally
239         ) &&
240         commit=$(git commit-tree $tree) &&
241         git update-ref refs/heads/type_mismatch $commit &&
242         test_must_fail git fsck >out 2>&1 &&
243         test_i18ngrep "is a blob, not a tree" out &&
244         test_i18ngrep ! "dangling blob" out
245 '
246
247 test_expect_success 'tag pointing to nonexistent' '
248         cat >invalid-tag <<-\EOF &&
249         object ffffffffffffffffffffffffffffffffffffffff
250         type commit
251         tag invalid
252         tagger T A Gger <tagger@example.com> 1234567890 -0000
253
254         This is an invalid tag.
255         EOF
256
257         tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
258         test_when_finished "remove_object $tag" &&
259         echo $tag >.git/refs/tags/invalid &&
260         test_when_finished "git update-ref -d refs/tags/invalid" &&
261         test_must_fail git fsck --tags >out &&
262         cat out &&
263         grep "broken link" out
264 '
265
266 test_expect_success 'tag pointing to something else than its type' '
267         sha=$(echo blob | git hash-object -w --stdin) &&
268         test_when_finished "remove_object $sha" &&
269         cat >wrong-tag <<-EOF &&
270         object $sha
271         type commit
272         tag wrong
273         tagger T A Gger <tagger@example.com> 1234567890 -0000
274
275         This is an invalid tag.
276         EOF
277
278         tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
279         test_when_finished "remove_object $tag" &&
280         echo $tag >.git/refs/tags/wrong &&
281         test_when_finished "git update-ref -d refs/tags/wrong" &&
282         test_must_fail git fsck --tags
283 '
284
285 test_expect_success 'tag with incorrect tag name & missing tagger' '
286         sha=$(git rev-parse HEAD) &&
287         cat >wrong-tag <<-EOF &&
288         object $sha
289         type commit
290         tag wrong name format
291
292         This is an invalid tag.
293         EOF
294
295         tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
296         test_when_finished "remove_object $tag" &&
297         echo $tag >.git/refs/tags/wrong &&
298         test_when_finished "git update-ref -d refs/tags/wrong" &&
299         git fsck --tags 2>out &&
300
301         cat >expect <<-EOF &&
302         warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
303         warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
304         EOF
305         test_cmp expect out
306 '
307
308 test_expect_success 'tag with bad tagger' '
309         sha=$(git rev-parse HEAD) &&
310         cat >wrong-tag <<-EOF &&
311         object $sha
312         type commit
313         tag not-quite-wrong
314         tagger Bad Tagger Name
315
316         This is an invalid tag.
317         EOF
318
319         tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
320         test_when_finished "remove_object $tag" &&
321         echo $tag >.git/refs/tags/wrong &&
322         test_when_finished "git update-ref -d refs/tags/wrong" &&
323         test_must_fail git fsck --tags 2>out &&
324         grep "error in tag .*: invalid author/committer" out
325 '
326
327 test_expect_success 'tag with NUL in header' '
328         sha=$(git rev-parse HEAD) &&
329         q_to_nul >tag-NUL-header <<-EOF &&
330         object $sha
331         type commit
332         tag contains-Q-in-header
333         tagger T A Gger <tagger@example.com> 1234567890 -0000
334
335         This is an invalid tag.
336         EOF
337
338         tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
339         test_when_finished "remove_object $tag" &&
340         echo $tag >.git/refs/tags/wrong &&
341         test_when_finished "git update-ref -d refs/tags/wrong" &&
342         test_must_fail git fsck --tags 2>out &&
343         cat out &&
344         grep "error in tag $tag.*unterminated header: NUL at offset" out
345 '
346
347 test_expect_success 'cleaned up' '
348         git fsck >actual 2>&1 &&
349         test_cmp empty actual
350 '
351
352 test_expect_success 'rev-list --verify-objects' '
353         git rev-list --verify-objects --all >/dev/null 2>out &&
354         test_cmp empty out
355 '
356
357 test_expect_success 'rev-list --verify-objects with bad sha1' '
358         sha=$(echo blob | git hash-object -w --stdin) &&
359         old=$(echo $sha | sed "s+^..+&/+") &&
360         new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
361         sha="$(dirname $new)$(basename $new)" &&
362         mv .git/objects/$old .git/objects/$new &&
363         test_when_finished "remove_object $sha" &&
364         git update-index --add --cacheinfo 100644 $sha foo &&
365         test_when_finished "git read-tree -u --reset HEAD" &&
366         tree=$(git write-tree) &&
367         test_when_finished "remove_object $tree" &&
368         cmt=$(echo bogus | git commit-tree $tree) &&
369         test_when_finished "remove_object $cmt" &&
370         git update-ref refs/heads/bogus $cmt &&
371         test_when_finished "git update-ref -d refs/heads/bogus" &&
372
373         test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
374         cat out &&
375         grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
376 '
377
378 test_expect_success 'force fsck to ignore double author' '
379         git cat-file commit HEAD >basis &&
380         sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
381         new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
382         test_when_finished "remove_object $new" &&
383         git update-ref refs/heads/bogus "$new" &&
384         test_when_finished "git update-ref -d refs/heads/bogus" &&
385         test_must_fail git fsck &&
386         git -c fsck.multipleAuthors=ignore fsck
387 '
388
389 _bz='\0'
390 _bz5="$_bz$_bz$_bz$_bz$_bz"
391 _bz20="$_bz5$_bz5$_bz5$_bz5"
392
393 test_expect_success 'fsck notices blob entry pointing to null sha1' '
394         (git init null-blob &&
395          cd null-blob &&
396          sha=$(printf "100644 file$_bz$_bz20" |
397                git hash-object -w --stdin -t tree) &&
398           git fsck 2>out &&
399           cat out &&
400           grep "warning.*null sha1" out
401         )
402 '
403
404 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
405         (git init null-commit &&
406          cd null-commit &&
407          sha=$(printf "160000 submodule$_bz$_bz20" |
408                git hash-object -w --stdin -t tree) &&
409           git fsck 2>out &&
410           cat out &&
411           grep "warning.*null sha1" out
412         )
413 '
414
415 while read name path pretty; do
416         while read mode type; do
417                 : ${pretty:=$path}
418                 test_expect_success "fsck notices $pretty as $type" '
419                 (
420                         git init $name-$type &&
421                         cd $name-$type &&
422                         git config core.protectNTFS false &&
423                         echo content >file &&
424                         git add file &&
425                         git commit -m base &&
426                         blob=$(git rev-parse :file) &&
427                         tree=$(git rev-parse HEAD^{tree}) &&
428                         value=$(eval "echo \$$type") &&
429                         printf "$mode $type %s\t%s" "$value" "$path" >bad &&
430                         bad_tree=$(git mktree <bad) &&
431                         git fsck 2>out &&
432                         cat out &&
433                         grep "warning.*tree $bad_tree" out
434                 )'
435         done <<-\EOF
436         100644 blob
437         040000 tree
438         EOF
439 done <<-EOF
440 dot .
441 dotdot ..
442 dotgit .git
443 dotgit-case .GIT
444 dotgit-unicode .gI${u200c}T .gI{u200c}T
445 dotgit-case2 .Git
446 git-tilde1 git~1
447 dotgitdot .git.
448 dot-backslash-case .\\\\.GIT\\\\foobar
449 dotgit-case-backslash .git\\\\foobar
450 EOF
451
452 test_expect_success 'fsck allows .Ňit' '
453         (
454                 git init not-dotgit &&
455                 cd not-dotgit &&
456                 echo content >file &&
457                 git add file &&
458                 git commit -m base &&
459                 blob=$(git rev-parse :file) &&
460                 printf "100644 blob $blob\t.\\305\\207it" >tree &&
461                 tree=$(git mktree <tree) &&
462                 git fsck 2>err &&
463                 test_line_count = 0 err
464         )
465 '
466
467 test_expect_success 'NUL in commit' '
468         rm -fr nul-in-commit &&
469         git init nul-in-commit &&
470         (
471                 cd nul-in-commit &&
472                 git commit --allow-empty -m "initial commitQNUL after message" &&
473                 git cat-file commit HEAD >original &&
474                 q_to_nul <original >munged &&
475                 git hash-object -w -t commit --stdin <munged >name &&
476                 git branch bad $(cat name) &&
477
478                 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
479                 grep nulInCommit warn.1 &&
480                 git fsck 2>warn.2 &&
481                 grep nulInCommit warn.2
482         )
483 '
484
485 # create a static test repo which is broken by omitting
486 # one particular object ($1, which is looked up via rev-parse
487 # in the new repository).
488 create_repo_missing () {
489         rm -rf missing &&
490         git init missing &&
491         (
492                 cd missing &&
493                 git commit -m one --allow-empty &&
494                 mkdir subdir &&
495                 echo content >subdir/file &&
496                 git add subdir/file &&
497                 git commit -m two &&
498                 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
499                 git tag -m foo tag $unrelated &&
500                 sha1=$(git rev-parse --verify "$1") &&
501                 path=$(echo $sha1 | sed 's|..|&/|') &&
502                 rm .git/objects/$path
503         )
504 }
505
506 test_expect_success 'fsck notices missing blob' '
507         create_repo_missing HEAD:subdir/file &&
508         test_must_fail git -C missing fsck
509 '
510
511 test_expect_success 'fsck notices missing subtree' '
512         create_repo_missing HEAD:subdir &&
513         test_must_fail git -C missing fsck
514 '
515
516 test_expect_success 'fsck notices missing root tree' '
517         create_repo_missing HEAD^{tree} &&
518         test_must_fail git -C missing fsck
519 '
520
521 test_expect_success 'fsck notices missing parent' '
522         create_repo_missing HEAD^ &&
523         test_must_fail git -C missing fsck
524 '
525
526 test_expect_success 'fsck notices missing tagged object' '
527         create_repo_missing tag^{blob} &&
528         test_must_fail git -C missing fsck
529 '
530
531 test_expect_success 'fsck notices ref pointing to missing commit' '
532         create_repo_missing HEAD &&
533         test_must_fail git -C missing fsck
534 '
535
536 test_expect_success 'fsck notices ref pointing to missing tag' '
537         create_repo_missing tag &&
538         test_must_fail git -C missing fsck
539 '
540
541 test_expect_success 'fsck --connectivity-only' '
542         rm -rf connectivity-only &&
543         git init connectivity-only &&
544         (
545                 cd connectivity-only &&
546                 touch empty &&
547                 git add empty &&
548                 test_commit empty &&
549
550                 # Drop the index now; we want to be sure that we
551                 # recursively notice the broken objects
552                 # because they are reachable from refs, not because
553                 # they are in the index.
554                 rm -f .git/index &&
555
556                 # corrupt the blob, but in a way that we can still identify
557                 # its type. That lets us see that --connectivity-only is
558                 # not actually looking at the contents, but leaves it
559                 # free to examine the type if it chooses.
560                 empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
561                 blob=$(echo unrelated | git hash-object -w --stdin) &&
562                 mv -f $(sha1_file $blob) $empty &&
563
564                 test_must_fail git fsck --strict &&
565                 git fsck --strict --connectivity-only &&
566                 tree=$(git rev-parse HEAD:) &&
567                 suffix=${tree#??} &&
568                 tree=.git/objects/${tree%$suffix}/$suffix &&
569                 rm -f $tree &&
570                 echo invalid >$tree &&
571                 test_must_fail git fsck --strict --connectivity-only
572         )
573 '
574
575 test_expect_success 'fsck --connectivity-only with explicit head' '
576         rm -rf connectivity-only &&
577         git init connectivity-only &&
578         (
579                 cd connectivity-only &&
580                 test_commit foo &&
581                 rm -f .git/index &&
582                 tree=$(git rev-parse HEAD^{tree}) &&
583                 remove_object $(git rev-parse HEAD:foo.t) &&
584                 test_must_fail git fsck --connectivity-only $tree
585         )
586 '
587
588 test_expect_success 'fsck --name-objects' '
589         rm -rf name-objects &&
590         git init name-objects &&
591         (
592                 cd name-objects &&
593                 test_commit julius caesar.t &&
594                 test_commit augustus &&
595                 test_commit caesar &&
596                 remove_object $(git rev-parse julius:caesar.t) &&
597                 test_must_fail git fsck --name-objects >out &&
598                 tree=$(git rev-parse --verify julius:) &&
599                 egrep "$tree \((refs/heads/master|HEAD)@\{[0-9]*\}:" out
600         )
601 '
602
603 test_expect_success 'alternate objects are correctly blamed' '
604         test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
605         git init --bare alt.git &&
606         echo "../../alt.git/objects" >.git/objects/info/alternates &&
607         mkdir alt.git/objects/12 &&
608         >alt.git/objects/12/34567890123456789012345678901234567890 &&
609         test_must_fail git fsck >out 2>&1 &&
610         grep alt.git out
611 '
612
613 test_expect_success 'fsck errors in packed objects' '
614         git cat-file commit HEAD >basis &&
615         sed "s/</one/" basis >one &&
616         sed "s/</foo/" basis >two &&
617         one=$(git hash-object -t commit -w one) &&
618         two=$(git hash-object -t commit -w two) &&
619         pack=$(
620                 {
621                         echo $one &&
622                         echo $two
623                 } | git pack-objects .git/objects/pack/pack
624         ) &&
625         test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
626         remove_object $one &&
627         remove_object $two &&
628         test_must_fail git fsck 2>out &&
629         grep "error in commit $one.* - bad name" out &&
630         grep "error in commit $two.* - bad name" out &&
631         ! grep corrupt out
632 '
633
634 test_expect_success 'fsck fails on corrupt packfile' '
635         hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
636         pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
637
638         # Corrupt the first byte of the first object. (It contains 3 type bits,
639         # at least one of which is not zero, so setting the first byte to 0 is
640         # sufficient.)
641         chmod a+w .git/objects/pack/pack-$pack.pack &&
642         printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
643
644         test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
645         remove_object $hsh &&
646         test_must_fail git fsck 2>out &&
647         test_i18ngrep "checksum mismatch" out
648 '
649
650 test_expect_success 'fsck finds problems in duplicate loose objects' '
651         rm -rf broken-duplicate &&
652         git init broken-duplicate &&
653         (
654                 cd broken-duplicate &&
655                 test_commit duplicate &&
656                 # no "-d" here, so we end up with duplicates
657                 git repack &&
658                 # now corrupt the loose copy
659                 file=$(sha1_file "$(git rev-parse HEAD)") &&
660                 rm "$file" &&
661                 echo broken >"$file" &&
662                 test_must_fail git fsck
663         )
664 '
665
666 test_expect_success 'fsck detects trailing loose garbage (commit)' '
667         git cat-file commit HEAD >basis &&
668         echo bump-commit-sha1 >>basis &&
669         commit=$(git hash-object -w -t commit basis) &&
670         file=$(sha1_file $commit) &&
671         test_when_finished "remove_object $commit" &&
672         chmod +w "$file" &&
673         echo garbage >>"$file" &&
674         test_must_fail git fsck 2>out &&
675         test_i18ngrep "garbage.*$commit" out
676 '
677
678 test_expect_success 'fsck detects trailing loose garbage (blob)' '
679         blob=$(echo trailing | git hash-object -w --stdin) &&
680         file=$(sha1_file $blob) &&
681         test_when_finished "remove_object $blob" &&
682         chmod +w "$file" &&
683         echo garbage >>"$file" &&
684         test_must_fail git fsck 2>out &&
685         test_i18ngrep "garbage.*$blob" out
686 '
687
688 # for each of type, we have one version which is referenced by another object
689 # (and so while unreachable, not dangling), and another variant which really is
690 # dangling.
691 test_expect_success 'fsck notices dangling objects' '
692         git init dangling &&
693         (
694                 cd dangling &&
695                 blob=$(echo not-dangling | git hash-object -w --stdin) &&
696                 dblob=$(echo dangling | git hash-object -w --stdin) &&
697                 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
698                 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
699                 commit=$(git commit-tree $tree) &&
700                 dcommit=$(git commit-tree -p $commit $tree) &&
701
702                 cat >expect <<-EOF &&
703                 dangling blob $dblob
704                 dangling commit $dcommit
705                 dangling tree $dtree
706                 EOF
707
708                 git fsck >actual &&
709                 # the output order is non-deterministic, as it comes from a hash
710                 sort <actual >actual.sorted &&
711                 test_cmp expect actual.sorted
712         )
713 '
714
715 test_expect_success 'fsck $name notices bogus $name' '
716         test_must_fail git fsck bogus &&
717         test_must_fail git fsck $_z40
718 '
719
720 test_expect_success 'bogus head does not fallback to all heads' '
721         # set up a case that will cause a reachability complaint
722         echo to-be-deleted >foo &&
723         git add foo &&
724         blob=$(git rev-parse :foo) &&
725         test_when_finished "git rm --cached foo" &&
726         remove_object $blob &&
727         test_must_fail git fsck $_z40 >out 2>&1 &&
728         ! grep $blob out
729 '
730
731 # Corrupt the checksum on the index.
732 # Add 1 to the last byte in the SHA.
733 corrupt_index_checksum () {
734     perl -w -e '
735         use Fcntl ":seek";
736         open my $fh, "+<", ".git/index" or die "open: $!";
737         binmode $fh;
738         seek $fh, -1, SEEK_END or die "seek: $!";
739         read $fh, my $in_byte, 1 or die "read: $!";
740
741         $in_value = unpack("C", $in_byte);
742         $out_value = ($in_value + 1) & 255;
743
744         $out_byte = pack("C", $out_value);
745
746         seek $fh, -1, SEEK_END or die "seek: $!";
747         print $fh $out_byte;
748         close $fh or die "close: $!";
749     '
750 }
751
752 # Corrupt the checksum on the index and then
753 # verify that only fsck notices.
754 test_expect_success 'detect corrupt index file in fsck' '
755         cp .git/index .git/index.backup &&
756         test_when_finished "mv .git/index.backup .git/index" &&
757         corrupt_index_checksum &&
758         test_must_fail git fsck --cache 2>errors &&
759         grep "bad index file" errors
760 '
761
762 test_done