1#!/bin/sh
2
3test_description='git fsck random collection of tests
4
5* (HEAD) B
6* (master) A
7'
8
9. ./test-lib.sh
10
11test_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
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 &&
30 git fsck --no-dangling >../actual 2>&1
31 ) &&
32 test_cmp empty actual
33'
34
35test_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
44test_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
56test_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
77test_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
85test_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
95test_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
105test_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
117test_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
129test_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
141test_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
153test_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
166test_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
179test_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
191test_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
212test_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
225test_expect_success 'tag pointing to nonexistent' '
226 cat >invalid-tag <<-\EOF &&
227 object ffffffffffffffffffffffffffffffffffffffff
228 type commit
229 tag invalid
230 tagger T A Gger <tagger@example.com> 1234567890 -0000
231
232 This is an invalid tag.
233 EOF
234
235 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
236 test_when_finished "remove_object $tag" &&
237 echo $tag >.git/refs/tags/invalid &&
238 test_when_finished "git update-ref -d refs/tags/invalid" &&
239 test_must_fail git fsck --tags >out &&
240 cat out &&
241 grep "broken link" out
242'
243
244test_expect_success 'tag pointing to something else than its type' '
245 sha=$(echo blob | git hash-object -w --stdin) &&
246 test_when_finished "remove_object $sha" &&
247 cat >wrong-tag <<-EOF &&
248 object $sha
249 type commit
250 tag wrong
251 tagger T A Gger <tagger@example.com> 1234567890 -0000
252
253 This is an invalid tag.
254 EOF
255
256 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
257 test_when_finished "remove_object $tag" &&
258 echo $tag >.git/refs/tags/wrong &&
259 test_when_finished "git update-ref -d refs/tags/wrong" &&
260 test_must_fail git fsck --tags
261'
262
263test_expect_success 'tag with incorrect tag name & missing tagger' '
264 sha=$(git rev-parse HEAD) &&
265 cat >wrong-tag <<-EOF &&
266 object $sha
267 type commit
268 tag wrong name format
269
270 This is an invalid tag.
271 EOF
272
273 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
274 test_when_finished "remove_object $tag" &&
275 echo $tag >.git/refs/tags/wrong &&
276 test_when_finished "git update-ref -d refs/tags/wrong" &&
277 git fsck --tags 2>out &&
278
279 cat >expect <<-EOF &&
280 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
281 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
282 EOF
283 test_cmp expect out
284'
285
286test_expect_success 'tag with bad tagger' '
287 sha=$(git rev-parse HEAD) &&
288 cat >wrong-tag <<-EOF &&
289 object $sha
290 type commit
291 tag not-quite-wrong
292 tagger Bad Tagger Name
293
294 This is an invalid tag.
295 EOF
296
297 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
298 test_when_finished "remove_object $tag" &&
299 echo $tag >.git/refs/tags/wrong &&
300 test_when_finished "git update-ref -d refs/tags/wrong" &&
301 test_must_fail git fsck --tags 2>out &&
302 grep "error in tag .*: invalid author/committer" out
303'
304
305test_expect_success 'tag with NUL in header' '
306 sha=$(git rev-parse HEAD) &&
307 q_to_nul >tag-NUL-header <<-EOF &&
308 object $sha
309 type commit
310 tag contains-Q-in-header
311 tagger T A Gger <tagger@example.com> 1234567890 -0000
312
313 This is an invalid tag.
314 EOF
315
316 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
317 test_when_finished "remove_object $tag" &&
318 echo $tag >.git/refs/tags/wrong &&
319 test_when_finished "git update-ref -d refs/tags/wrong" &&
320 test_must_fail git fsck --tags 2>out &&
321 cat out &&
322 grep "error in tag $tag.*unterminated header: NUL at offset" out
323'
324
325test_expect_success 'cleaned up' '
326 git fsck >actual 2>&1 &&
327 test_cmp empty actual
328'
329
330test_expect_success 'rev-list --verify-objects' '
331 git rev-list --verify-objects --all >/dev/null 2>out &&
332 test_cmp empty out
333'
334
335test_expect_success 'rev-list --verify-objects with bad sha1' '
336 sha=$(echo blob | git hash-object -w --stdin) &&
337 old=$(echo $sha | sed "s+^..+&/+") &&
338 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
339 sha="$(dirname $new)$(basename $new)" &&
340 mv .git/objects/$old .git/objects/$new &&
341 test_when_finished "remove_object $sha" &&
342 git update-index --add --cacheinfo 100644 $sha foo &&
343 test_when_finished "git read-tree -u --reset HEAD" &&
344 tree=$(git write-tree) &&
345 test_when_finished "remove_object $tree" &&
346 cmt=$(echo bogus | git commit-tree $tree) &&
347 test_when_finished "remove_object $cmt" &&
348 git update-ref refs/heads/bogus $cmt &&
349 test_when_finished "git update-ref -d refs/heads/bogus" &&
350
351 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
352 cat out &&
353 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
354'
355
356test_expect_success 'force fsck to ignore double author' '
357 git cat-file commit HEAD >basis &&
358 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
359 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
360 test_when_finished "remove_object $new" &&
361 git update-ref refs/heads/bogus "$new" &&
362 test_when_finished "git update-ref -d refs/heads/bogus" &&
363 test_must_fail git fsck &&
364 git -c fsck.multipleAuthors=ignore fsck
365'
366
367_bz='\0'
368_bz5="$_bz$_bz$_bz$_bz$_bz"
369_bz20="$_bz5$_bz5$_bz5$_bz5"
370
371test_expect_success 'fsck notices blob entry pointing to null sha1' '
372 (git init null-blob &&
373 cd null-blob &&
374 sha=$(printf "100644 file$_bz$_bz20" |
375 git hash-object -w --stdin -t tree) &&
376 git fsck 2>out &&
377 cat out &&
378 grep "warning.*null sha1" out
379 )
380'
381
382test_expect_success 'fsck notices submodule entry pointing to null sha1' '
383 (git init null-commit &&
384 cd null-commit &&
385 sha=$(printf "160000 submodule$_bz$_bz20" |
386 git hash-object -w --stdin -t tree) &&
387 git fsck 2>out &&
388 cat out &&
389 grep "warning.*null sha1" out
390 )
391'
392
393while read name path pretty; do
394 while read mode type; do
395 : ${pretty:=$path}
396 test_expect_success "fsck notices $pretty as $type" '
397 (
398 git init $name-$type &&
399 cd $name-$type &&
400 echo content >file &&
401 git add file &&
402 git commit -m base &&
403 blob=$(git rev-parse :file) &&
404 tree=$(git rev-parse HEAD^{tree}) &&
405 value=$(eval "echo \$$type") &&
406 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
407 bad_tree=$(git mktree <bad) &&
408 git fsck 2>out &&
409 cat out &&
410 grep "warning.*tree $bad_tree" out
411 )'
412 done <<-\EOF
413 100644 blob
414 040000 tree
415 EOF
416done <<-EOF
417dot .
418dotdot ..
419dotgit .git
420dotgit-case .GIT
421dotgit-unicode .gI${u200c}T .gI{u200c}T
422dotgit-case2 .Git
423git-tilde1 git~1
424dotgitdot .git.
425dot-backslash-case .\\\\.GIT\\\\foobar
426dotgit-case-backslash .git\\\\foobar
427EOF
428
429test_expect_success 'fsck allows .Ňit' '
430 (
431 git init not-dotgit &&
432 cd not-dotgit &&
433 echo content >file &&
434 git add file &&
435 git commit -m base &&
436 blob=$(git rev-parse :file) &&
437 printf "100644 blob $blob\t.\\305\\207it" >tree &&
438 tree=$(git mktree <tree) &&
439 git fsck 2>err &&
440 test_line_count = 0 err
441 )
442'
443
444test_expect_success 'NUL in commit' '
445 rm -fr nul-in-commit &&
446 git init nul-in-commit &&
447 (
448 cd nul-in-commit &&
449 git commit --allow-empty -m "initial commitQNUL after message" &&
450 git cat-file commit HEAD >original &&
451 q_to_nul <original >munged &&
452 git hash-object -w -t commit --stdin <munged >name &&
453 git branch bad $(cat name) &&
454
455 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
456 grep nulInCommit warn.1 &&
457 git fsck 2>warn.2 &&
458 grep nulInCommit warn.2
459 )
460'
461
462# create a static test repo which is broken by omitting
463# one particular object ($1, which is looked up via rev-parse
464# in the new repository).
465create_repo_missing () {
466 rm -rf missing &&
467 git init missing &&
468 (
469 cd missing &&
470 git commit -m one --allow-empty &&
471 mkdir subdir &&
472 echo content >subdir/file &&
473 git add subdir/file &&
474 git commit -m two &&
475 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
476 git tag -m foo tag $unrelated &&
477 sha1=$(git rev-parse --verify "$1") &&
478 path=$(echo $sha1 | sed 's|..|&/|') &&
479 rm .git/objects/$path
480 )
481}
482
483test_expect_success 'fsck notices missing blob' '
484 create_repo_missing HEAD:subdir/file &&
485 test_must_fail git -C missing fsck
486'
487
488test_expect_success 'fsck notices missing subtree' '
489 create_repo_missing HEAD:subdir &&
490 test_must_fail git -C missing fsck
491'
492
493test_expect_success 'fsck notices missing root tree' '
494 create_repo_missing HEAD^{tree} &&
495 test_must_fail git -C missing fsck
496'
497
498test_expect_success 'fsck notices missing parent' '
499 create_repo_missing HEAD^ &&
500 test_must_fail git -C missing fsck
501'
502
503test_expect_success 'fsck notices missing tagged object' '
504 create_repo_missing tag^{blob} &&
505 test_must_fail git -C missing fsck
506'
507
508test_expect_success 'fsck notices ref pointing to missing commit' '
509 create_repo_missing HEAD &&
510 test_must_fail git -C missing fsck
511'
512
513test_expect_success 'fsck notices ref pointing to missing tag' '
514 create_repo_missing tag &&
515 test_must_fail git -C missing fsck
516'
517
518test_expect_success 'fsck --connectivity-only' '
519 rm -rf connectivity-only &&
520 git init connectivity-only &&
521 (
522 cd connectivity-only &&
523 touch empty &&
524 git add empty &&
525 test_commit empty &&
526
527 # Drop the index now; we want to be sure that we
528 # recursively notice the broken objects
529 # because they are reachable from refs, not because
530 # they are in the index.
531 rm -f .git/index &&
532
533 # corrupt the blob, but in a way that we can still identify
534 # its type. That lets us see that --connectivity-only is
535 # not actually looking at the contents, but leaves it
536 # free to examine the type if it chooses.
537 empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
538 blob=$(echo unrelated | git hash-object -w --stdin) &&
539 mv -f $(sha1_file $blob) $empty &&
540
541 test_must_fail git fsck --strict &&
542 git fsck --strict --connectivity-only &&
543 tree=$(git rev-parse HEAD:) &&
544 suffix=${tree#??} &&
545 tree=.git/objects/${tree%$suffix}/$suffix &&
546 rm -f $tree &&
547 echo invalid >$tree &&
548 test_must_fail git fsck --strict --connectivity-only
549 )
550'
551
552test_expect_success 'fsck --connectivity-only with explicit head' '
553 rm -rf connectivity-only &&
554 git init connectivity-only &&
555 (
556 cd connectivity-only &&
557 test_commit foo &&
558 rm -f .git/index &&
559 tree=$(git rev-parse HEAD^{tree}) &&
560 remove_object $(git rev-parse HEAD:foo.t) &&
561 test_must_fail git fsck --connectivity-only $tree
562 )
563'
564
565test_expect_success 'fsck --name-objects' '
566 rm -rf name-objects &&
567 git init name-objects &&
568 (
569 cd name-objects &&
570 test_commit julius caesar.t &&
571 test_commit augustus &&
572 test_commit caesar &&
573 remove_object $(git rev-parse julius:caesar.t) &&
574 test_must_fail git fsck --name-objects >out &&
575 tree=$(git rev-parse --verify julius:) &&
576 grep "$tree (\(refs/heads/master\|HEAD\)@{[0-9]*}:" out
577 )
578'
579
580test_expect_success 'alternate objects are correctly blamed' '
581 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
582 git init --bare alt.git &&
583 echo "../../alt.git/objects" >.git/objects/info/alternates &&
584 mkdir alt.git/objects/12 &&
585 >alt.git/objects/12/34567890123456789012345678901234567890 &&
586 test_must_fail git fsck >out 2>&1 &&
587 grep alt.git out
588'
589
590test_expect_success 'fsck errors in packed objects' '
591 git cat-file commit HEAD >basis &&
592 sed "s/</one/" basis >one &&
593 sed "s/</foo/" basis >two &&
594 one=$(git hash-object -t commit -w one) &&
595 two=$(git hash-object -t commit -w two) &&
596 pack=$(
597 {
598 echo $one &&
599 echo $two
600 } | git pack-objects .git/objects/pack/pack
601 ) &&
602 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
603 remove_object $one &&
604 remove_object $two &&
605 test_must_fail git fsck 2>out &&
606 grep "error in commit $one.* - bad name" out &&
607 grep "error in commit $two.* - bad name" out &&
608 ! grep corrupt out
609'
610
611test_expect_success 'fsck finds problems in duplicate loose objects' '
612 rm -rf broken-duplicate &&
613 git init broken-duplicate &&
614 (
615 cd broken-duplicate &&
616 test_commit duplicate &&
617 # no "-d" here, so we end up with duplicates
618 git repack &&
619 # now corrupt the loose copy
620 file=$(sha1_file "$(git rev-parse HEAD)") &&
621 rm "$file" &&
622 echo broken >"$file" &&
623 test_must_fail git fsck
624 )
625'
626
627test_expect_success 'fsck detects trailing loose garbage (commit)' '
628 git cat-file commit HEAD >basis &&
629 echo bump-commit-sha1 >>basis &&
630 commit=$(git hash-object -w -t commit basis) &&
631 file=$(sha1_file $commit) &&
632 test_when_finished "remove_object $commit" &&
633 chmod +w "$file" &&
634 echo garbage >>"$file" &&
635 test_must_fail git fsck 2>out &&
636 test_i18ngrep "garbage.*$commit" out
637'
638
639test_expect_success 'fsck detects trailing loose garbage (blob)' '
640 blob=$(echo trailing | git hash-object -w --stdin) &&
641 file=$(sha1_file $blob) &&
642 test_when_finished "remove_object $blob" &&
643 chmod +w "$file" &&
644 echo garbage >>"$file" &&
645 test_must_fail git fsck 2>out &&
646 test_i18ngrep "garbage.*$blob" out
647'
648
649# for each of type, we have one version which is referenced by another object
650# (and so while unreachable, not dangling), and another variant which really is
651# dangling.
652test_expect_success 'fsck notices dangling objects' '
653 git init dangling &&
654 (
655 cd dangling &&
656 blob=$(echo not-dangling | git hash-object -w --stdin) &&
657 dblob=$(echo dangling | git hash-object -w --stdin) &&
658 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
659 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
660 commit=$(git commit-tree $tree) &&
661 dcommit=$(git commit-tree -p $commit $tree) &&
662
663 cat >expect <<-EOF &&
664 dangling blob $dblob
665 dangling commit $dcommit
666 dangling tree $dtree
667 EOF
668
669 git fsck >actual &&
670 # the output order is non-deterministic, as it comes from a hash
671 sort <actual >actual.sorted &&
672 test_cmp expect actual.sorted
673 )
674'
675
676test_expect_success 'fsck $name notices bogus $name' '
677 test_must_fail git fsck bogus &&
678 test_must_fail git fsck $_z40
679'
680
681test_expect_success 'bogus head does not fallback to all heads' '
682 # set up a case that will cause a reachability complaint
683 echo to-be-deleted >foo &&
684 git add foo &&
685 blob=$(git rev-parse :foo) &&
686 test_when_finished "git rm --cached foo" &&
687 remove_object $blob &&
688 test_must_fail git fsck $_z40 >out 2>&1 &&
689 ! grep $blob out
690'
691
692# Corrupt the checksum on the index.
693# Add 1 to the last byte in the SHA.
694corrupt_index_checksum () {
695 perl -w -e '
696 use Fcntl ":seek";
697 open my $fh, "+<", ".git/index" or die "open: $!";
698 binmode $fh;
699 seek $fh, -1, SEEK_END or die "seek: $!";
700 read $fh, my $in_byte, 1 or die "read: $!";
701
702 $in_value = unpack("C", $in_byte);
703 $out_value = ($in_value + 1) & 255;
704
705 $out_byte = pack("C", $out_value);
706
707 seek $fh, -1, SEEK_END or die "seek: $!";
708 print $fh $out_byte;
709 close $fh or die "close: $!";
710 '
711}
712
713# Corrupt the checksum on the index and then
714# verify that only fsck notices.
715test_expect_success 'detect corrupt index file in fsck' '
716 cp .git/index .git/index.backup &&
717 test_when_finished "mv .git/index.backup .git/index" &&
718 corrupt_index_checksum &&
719 test_must_fail git fsck --cache 2>errors &&
720 grep "bad index file" errors
721'
722
723test_done