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