3 test_description='git fsck random collection of tests
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 &&
19 git reflog expire --expire=now --all
22 test_expect_success 'loose objects borrowed from alternate are not missing' '
27 echo ../../../.git/objects >.git/objects/info/alternates &&
28 test_commit C fileC one &&
29 git fsck --no-dangling >../actual 2>&1
31 test_must_be_empty actual
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
39 # Corruption tests follow. Make sure to remove all traces of the
40 # specific corruption you test afterwards, lest a later test trip over
43 test_expect_success 'setup: helpers for corruption tests' '
46 firsttwo=${1%$remainder} &&
47 echo ".git/objects/$firsttwo/$remainder"
51 rm "$(sha1_file "$1")"
55 test_expect_success 'object with bad sha1' '
56 sha=$(echo blob | git hash-object -w --stdin) &&
57 old=$(test_oid_to_path "$sha") &&
58 new=$(dirname $old)/$(test_oid ff_2) &&
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" &&
71 test_must_fail git fsck 2>out &&
72 test_i18ngrep "$sha.*corrupt" out
75 test_expect_success 'branch pointing to non-commit' '
76 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
77 test_when_finished "git update-ref -d refs/heads/invalid" &&
78 test_must_fail git fsck 2>out &&
79 test_i18ngrep "not a commit" out
82 test_expect_success 'HEAD link pointing at a funny object' '
83 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
84 mv .git/HEAD .git/SAVED_HEAD &&
85 echo $ZERO_OID >.git/HEAD &&
86 # avoid corrupt/broken HEAD from interfering with repo discovery
87 test_must_fail env GIT_DIR=.git git fsck 2>out &&
88 test_i18ngrep "detached HEAD points" out
91 test_expect_success 'HEAD link pointing at a funny place' '
92 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
93 mv .git/HEAD .git/SAVED_HEAD &&
94 echo "ref: refs/funny/place" >.git/HEAD &&
95 # avoid corrupt/broken HEAD from interfering with repo discovery
96 test_must_fail env GIT_DIR=.git git fsck 2>out &&
97 test_i18ngrep "HEAD points to something strange" out
100 test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
101 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
102 test_when_finished "rm -rf .git/worktrees wt" &&
103 git worktree add wt &&
104 mv .git/HEAD .git/SAVED_HEAD &&
105 echo $ZERO_OID >.git/HEAD &&
106 # avoid corrupt/broken HEAD from interfering with repo discovery
107 test_must_fail git -C wt fsck 2>out &&
108 test_i18ngrep "main-worktree/HEAD: detached HEAD points" out
111 test_expect_success 'other worktree HEAD link pointing at a funny object' '
112 test_when_finished "rm -rf .git/worktrees other" &&
113 git worktree add other &&
114 echo $ZERO_OID >.git/worktrees/other/HEAD &&
115 test_must_fail git fsck 2>out &&
116 test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out
119 test_expect_success 'other worktree HEAD link pointing at missing object' '
120 test_when_finished "rm -rf .git/worktrees other" &&
121 git worktree add other &&
122 echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
123 test_must_fail git fsck 2>out &&
124 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
127 test_expect_success 'other worktree HEAD link pointing at a funny place' '
128 test_when_finished "rm -rf .git/worktrees other" &&
129 git worktree add other &&
130 echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
131 test_must_fail git fsck 2>out &&
132 test_i18ngrep "worktrees/other/HEAD points to something strange" out
135 test_expect_success 'commit with multiple signatures is okay' '
136 git cat-file commit HEAD >basis &&
138 gpgsig -----BEGIN PGP SIGNATURE-----
139 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
140 -----END PGP SIGNATURE-----
141 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
142 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
143 -----END PGP SIGNATURE-----
145 sed -e "/^committer/q" basis >okay &&
148 sed -e "1,/^$/d" basis >>okay &&
150 new=$(git hash-object -t commit -w --stdin <okay) &&
151 test_when_finished "remove_object $new" &&
152 git update-ref refs/heads/bogus "$new" &&
153 test_when_finished "git update-ref -d refs/heads/bogus" &&
156 ! grep "commit $new" out
159 test_expect_success 'email without @ is okay' '
160 git cat-file commit HEAD >basis &&
161 sed "s/@/AT/" basis >okay &&
162 new=$(git hash-object -t commit -w --stdin <okay) &&
163 test_when_finished "remove_object $new" &&
164 git update-ref refs/heads/bogus "$new" &&
165 test_when_finished "git update-ref -d refs/heads/bogus" &&
167 ! grep "commit $new" out
170 test_expect_success 'email with embedded > is not okay' '
171 git cat-file commit HEAD >basis &&
172 sed "s/@[a-z]/&>/" basis >bad-email &&
173 new=$(git hash-object -t commit -w --stdin <bad-email) &&
174 test_when_finished "remove_object $new" &&
175 git update-ref refs/heads/bogus "$new" &&
176 test_when_finished "git update-ref -d refs/heads/bogus" &&
177 test_must_fail git fsck 2>out &&
178 test_i18ngrep "error in commit $new" out
181 test_expect_success 'missing < email delimiter is reported nicely' '
182 git cat-file commit HEAD >basis &&
183 sed "s/<//" basis >bad-email-2 &&
184 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
185 test_when_finished "remove_object $new" &&
186 git update-ref refs/heads/bogus "$new" &&
187 test_when_finished "git update-ref -d refs/heads/bogus" &&
188 test_must_fail git fsck 2>out &&
189 test_i18ngrep "error in commit $new.* - bad name" out
192 test_expect_success 'missing email is reported nicely' '
193 git cat-file commit HEAD >basis &&
194 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
195 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
196 test_when_finished "remove_object $new" &&
197 git update-ref refs/heads/bogus "$new" &&
198 test_when_finished "git update-ref -d refs/heads/bogus" &&
199 test_must_fail git fsck 2>out &&
200 test_i18ngrep "error in commit $new.* - missing email" out
203 test_expect_success '> in name is reported' '
204 git cat-file commit HEAD >basis &&
205 sed "s/ </> </" basis >bad-email-4 &&
206 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
207 test_when_finished "remove_object $new" &&
208 git update-ref refs/heads/bogus "$new" &&
209 test_when_finished "git update-ref -d refs/heads/bogus" &&
210 test_must_fail git fsck 2>out &&
211 test_i18ngrep "error in commit $new" out
215 test_expect_success 'integer overflow in timestamps is reported' '
216 git cat-file commit HEAD >basis &&
217 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
218 <basis >bad-timestamp &&
219 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
220 test_when_finished "remove_object $new" &&
221 git update-ref refs/heads/bogus "$new" &&
222 test_when_finished "git update-ref -d refs/heads/bogus" &&
223 test_must_fail git fsck 2>out &&
224 test_i18ngrep "error in commit $new.*integer overflow" out
227 test_expect_success 'commit with NUL in header' '
228 git cat-file commit HEAD >basis &&
229 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
230 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
231 test_when_finished "remove_object $new" &&
232 git update-ref refs/heads/bogus "$new" &&
233 test_when_finished "git update-ref -d refs/heads/bogus" &&
234 test_must_fail git fsck 2>out &&
235 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
238 test_expect_success 'tree object with duplicate entries' '
239 test_when_finished "for i in \$T; do remove_object \$i; done" &&
241 GIT_INDEX_FILE=test-index &&
242 export GIT_INDEX_FILE &&
247 T=$(git write-tree) &&
250 git cat-file tree $T &&
253 git hash-object -w -t tree --stdin
255 test_must_fail git fsck 2>out &&
256 test_i18ngrep "error in tree .*contains duplicate file entries" out
259 check_duplicate_names () {
263 test_expect_$expect "tree object with duplicate names: $names" '
264 test_when_finished "remove_object \$blob" &&
265 test_when_finished "remove_object \$tree" &&
266 test_when_finished "remove_object \$badtree" &&
267 blob=$(echo blob | git hash-object -w --stdin) &&
268 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
269 tree=$(git mktree <tree) &&
273 */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
274 *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
277 badtree=$(git mktree <badtree) &&
278 test_must_fail git fsck 2>out &&
279 test_i18ngrep "$badtree" out &&
280 test_i18ngrep "error in tree .*contains duplicate file entries" out
284 check_duplicate_names success x x.1 x/
285 check_duplicate_names success x x.1.2 x.1/ x/
286 check_duplicate_names success x x.1 x.1.2 x/
288 test_expect_success 'unparseable tree object' '
289 test_oid_cache <<-\EOF &&
290 junk sha1:twenty-bytes-of-junk
291 junk sha256:twenty-bytes-of-junk-twelve-more
294 test_when_finished "git update-ref -d refs/heads/wrong" &&
295 test_when_finished "remove_object \$tree_sha1" &&
296 test_when_finished "remove_object \$commit_sha1" &&
297 junk=$(test_oid junk) &&
298 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
299 commit_sha1=$(git commit-tree $tree_sha1) &&
300 git update-ref refs/heads/wrong $commit_sha1 &&
301 test_must_fail git fsck 2>out &&
302 test_i18ngrep "error: empty filename in tree entry" out &&
303 test_i18ngrep "$tree_sha1" out &&
304 test_i18ngrep ! "fatal: empty filename in tree entry" out
307 test_expect_success 'tree entry with type mismatch' '
308 test_when_finished "remove_object \$blob" &&
309 test_when_finished "remove_object \$tree" &&
310 test_when_finished "remove_object \$commit" &&
311 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
312 blob=$(echo blob | git hash-object -w --stdin) &&
313 blob_bin=$(echo $blob | hex2oct) &&
315 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
316 git hash-object -t tree --stdin -w --literally
318 commit=$(git commit-tree $tree) &&
319 git update-ref refs/heads/type_mismatch $commit &&
320 test_must_fail git fsck >out 2>&1 &&
321 test_i18ngrep "is a blob, not a tree" out &&
322 test_i18ngrep ! "dangling blob" out
325 test_expect_success 'tag pointing to nonexistent' '
326 badoid=$(test_oid deadbeef) &&
327 cat >invalid-tag <<-EOF &&
331 tagger T A Gger <tagger@example.com> 1234567890 -0000
333 This is an invalid tag.
336 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
337 test_when_finished "remove_object $tag" &&
338 echo $tag >.git/refs/tags/invalid &&
339 test_when_finished "git update-ref -d refs/tags/invalid" &&
340 test_must_fail git fsck --tags >out &&
341 test_i18ngrep "broken link" out
344 test_expect_success 'tag pointing to something else than its type' '
345 sha=$(echo blob | git hash-object -w --stdin) &&
346 test_when_finished "remove_object $sha" &&
347 cat >wrong-tag <<-EOF &&
351 tagger T A Gger <tagger@example.com> 1234567890 -0000
353 This is an invalid tag.
356 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
357 test_when_finished "remove_object $tag" &&
358 echo $tag >.git/refs/tags/wrong &&
359 test_when_finished "git update-ref -d refs/tags/wrong" &&
360 test_must_fail git fsck --tags
363 test_expect_success 'tag with incorrect tag name & missing tagger' '
364 sha=$(git rev-parse HEAD) &&
365 cat >wrong-tag <<-EOF &&
368 tag wrong name format
370 This is an invalid tag.
373 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
374 test_when_finished "remove_object $tag" &&
375 echo $tag >.git/refs/tags/wrong &&
376 test_when_finished "git update-ref -d refs/tags/wrong" &&
377 git fsck --tags 2>out &&
379 cat >expect <<-EOF &&
380 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
381 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
383 test_i18ncmp expect out
386 test_expect_success 'tag with bad tagger' '
387 sha=$(git rev-parse HEAD) &&
388 cat >wrong-tag <<-EOF &&
392 tagger Bad Tagger Name
394 This is an invalid tag.
397 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
398 test_when_finished "remove_object $tag" &&
399 echo $tag >.git/refs/tags/wrong &&
400 test_when_finished "git update-ref -d refs/tags/wrong" &&
401 test_must_fail git fsck --tags 2>out &&
402 test_i18ngrep "error in tag .*: invalid author/committer" out
405 test_expect_success 'tag with NUL in header' '
406 sha=$(git rev-parse HEAD) &&
407 q_to_nul >tag-NUL-header <<-EOF &&
410 tag contains-Q-in-header
411 tagger T A Gger <tagger@example.com> 1234567890 -0000
413 This is an invalid tag.
416 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
417 test_when_finished "remove_object $tag" &&
418 echo $tag >.git/refs/tags/wrong &&
419 test_when_finished "git update-ref -d refs/tags/wrong" &&
420 test_must_fail git fsck --tags 2>out &&
421 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
424 test_expect_success 'cleaned up' '
425 git fsck >actual 2>&1 &&
426 test_must_be_empty actual
429 test_expect_success 'rev-list --verify-objects' '
430 git rev-list --verify-objects --all >/dev/null 2>out &&
431 test_must_be_empty out
434 test_expect_success 'rev-list --verify-objects with bad sha1' '
435 sha=$(echo blob | git hash-object -w --stdin) &&
436 old=$(test_oid_to_path $sha) &&
437 new=$(dirname $old)/$(test_oid ff_2) &&
438 sha="$(dirname $new)$(basename $new)" &&
439 mv .git/objects/$old .git/objects/$new &&
440 test_when_finished "remove_object $sha" &&
441 git update-index --add --cacheinfo 100644 $sha foo &&
442 test_when_finished "git read-tree -u --reset HEAD" &&
443 tree=$(git write-tree) &&
444 test_when_finished "remove_object $tree" &&
445 cmt=$(echo bogus | git commit-tree $tree) &&
446 test_when_finished "remove_object $cmt" &&
447 git update-ref refs/heads/bogus $cmt &&
448 test_when_finished "git update-ref -d refs/heads/bogus" &&
450 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
451 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
454 test_expect_success 'force fsck to ignore double author' '
455 git cat-file commit HEAD >basis &&
456 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
457 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
458 test_when_finished "remove_object $new" &&
459 git update-ref refs/heads/bogus "$new" &&
460 test_when_finished "git update-ref -d refs/heads/bogus" &&
461 test_must_fail git fsck &&
462 git -c fsck.multipleAuthors=ignore fsck
466 _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
468 test_expect_success 'fsck notices blob entry pointing to null sha1' '
469 (git init null-blob &&
471 sha=$(printf "100644 file$_bz$_bzoid" |
472 git hash-object -w --stdin -t tree) &&
474 test_i18ngrep "warning.*null sha1" out
478 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
479 (git init null-commit &&
481 sha=$(printf "160000 submodule$_bz$_bzoid" |
482 git hash-object -w --stdin -t tree) &&
484 test_i18ngrep "warning.*null sha1" out
488 while read name path pretty; do
489 while read mode type; do
491 test_expect_success "fsck notices $pretty as $type" '
493 git init $name-$type &&
495 git config core.protectNTFS false &&
496 echo content >file &&
498 git commit -m base &&
499 blob=$(git rev-parse :file) &&
500 tree=$(git rev-parse HEAD^{tree}) &&
501 value=$(eval "echo \$$type") &&
502 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
503 bad_tree=$(git mktree <bad) &&
505 test_i18ngrep "warning.*tree $bad_tree" out
516 dotgit-unicode .gI${u200c}T .gI{u200c}T
520 dot-backslash-case .\\\\.GIT\\\\foobar
521 dotgit-case-backslash .git\\\\foobar
524 test_expect_success 'fsck allows .Ňit' '
526 git init not-dotgit &&
528 echo content >file &&
530 git commit -m base &&
531 blob=$(git rev-parse :file) &&
532 printf "100644 blob $blob\t.\\305\\207it" >tree &&
533 tree=$(git mktree <tree) &&
535 test_line_count = 0 err
539 test_expect_success 'NUL in commit' '
540 rm -fr nul-in-commit &&
541 git init nul-in-commit &&
544 git commit --allow-empty -m "initial commitQNUL after message" &&
545 git cat-file commit HEAD >original &&
546 q_to_nul <original >munged &&
547 git hash-object -w -t commit --stdin <munged >name &&
548 git branch bad $(cat name) &&
550 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
551 test_i18ngrep nulInCommit warn.1 &&
553 test_i18ngrep nulInCommit warn.2
557 # create a static test repo which is broken by omitting
558 # one particular object ($1, which is looked up via rev-parse
559 # in the new repository).
560 create_repo_missing () {
565 git commit -m one --allow-empty &&
567 echo content >subdir/file &&
568 git add subdir/file &&
570 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
571 git tag -m foo tag $unrelated &&
572 sha1=$(git rev-parse --verify "$1") &&
573 path=$(echo $sha1 | sed 's|..|&/|') &&
574 rm .git/objects/$path
578 test_expect_success 'fsck notices missing blob' '
579 create_repo_missing HEAD:subdir/file &&
580 test_must_fail git -C missing fsck
583 test_expect_success 'fsck notices missing subtree' '
584 create_repo_missing HEAD:subdir &&
585 test_must_fail git -C missing fsck
588 test_expect_success 'fsck notices missing root tree' '
589 create_repo_missing HEAD^{tree} &&
590 test_must_fail git -C missing fsck
593 test_expect_success 'fsck notices missing parent' '
594 create_repo_missing HEAD^ &&
595 test_must_fail git -C missing fsck
598 test_expect_success 'fsck notices missing tagged object' '
599 create_repo_missing tag^{blob} &&
600 test_must_fail git -C missing fsck
603 test_expect_success 'fsck notices ref pointing to missing commit' '
604 create_repo_missing HEAD &&
605 test_must_fail git -C missing fsck
608 test_expect_success 'fsck notices ref pointing to missing tag' '
609 create_repo_missing tag &&
610 test_must_fail git -C missing fsck
613 test_expect_success 'fsck --connectivity-only' '
614 rm -rf connectivity-only &&
615 git init connectivity-only &&
617 cd connectivity-only &&
622 # Drop the index now; we want to be sure that we
623 # recursively notice the broken objects
624 # because they are reachable from refs, not because
625 # they are in the index.
628 # corrupt the blob, but in a way that we can still identify
629 # its type. That lets us see that --connectivity-only is
630 # not actually looking at the contents, but leaves it
631 # free to examine the type if it chooses.
632 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
633 blob=$(echo unrelated | git hash-object -w --stdin) &&
634 mv -f $(sha1_file $blob) $empty &&
636 test_must_fail git fsck --strict &&
637 git fsck --strict --connectivity-only &&
638 tree=$(git rev-parse HEAD:) &&
640 tree=.git/objects/${tree%$suffix}/$suffix &&
642 echo invalid >$tree &&
643 test_must_fail git fsck --strict --connectivity-only
647 test_expect_success 'fsck --connectivity-only with explicit head' '
648 rm -rf connectivity-only &&
649 git init connectivity-only &&
651 cd connectivity-only &&
654 tree=$(git rev-parse HEAD^{tree}) &&
655 remove_object $(git rev-parse HEAD:foo.t) &&
656 test_must_fail git fsck --connectivity-only $tree
660 test_expect_success 'fsck --name-objects' '
661 rm -rf name-objects &&
662 git init name-objects &&
665 test_commit julius caesar.t &&
666 test_commit augustus &&
667 test_commit caesar &&
668 remove_object $(git rev-parse julius:caesar.t) &&
669 test_must_fail git fsck --name-objects >out &&
670 tree=$(git rev-parse --verify julius:) &&
671 test_i18ngrep "$tree (refs/tags/julius:" out
675 test_expect_success 'alternate objects are correctly blamed' '
676 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
677 name=$(test_oid numeric) &&
678 path=$(test_oid_to_path "$name") &&
679 git init --bare alt.git &&
680 echo "../../alt.git/objects" >.git/objects/info/alternates &&
681 mkdir alt.git/objects/$(dirname $path) &&
682 >alt.git/objects/$(dirname $path)/$(basename $path) &&
683 test_must_fail git fsck >out 2>&1 &&
684 test_i18ngrep alt.git out
687 test_expect_success 'fsck errors in packed objects' '
688 git cat-file commit HEAD >basis &&
689 sed "s/</one/" basis >one &&
690 sed "s/</foo/" basis >two &&
691 one=$(git hash-object -t commit -w one) &&
692 two=$(git hash-object -t commit -w two) &&
697 } | git pack-objects .git/objects/pack/pack
699 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
700 remove_object $one &&
701 remove_object $two &&
702 test_must_fail git fsck 2>out &&
703 test_i18ngrep "error in commit $one.* - bad name" out &&
704 test_i18ngrep "error in commit $two.* - bad name" out &&
708 test_expect_success 'fsck fails on corrupt packfile' '
709 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
710 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
712 # Corrupt the first byte of the first object. (It contains 3 type bits,
713 # at least one of which is not zero, so setting the first byte to 0 is
715 chmod a+w .git/objects/pack/pack-$pack.pack &&
716 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
718 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
719 remove_object $hsh &&
720 test_must_fail git fsck 2>out &&
721 test_i18ngrep "checksum mismatch" out
724 test_expect_success 'fsck finds problems in duplicate loose objects' '
725 rm -rf broken-duplicate &&
726 git init broken-duplicate &&
728 cd broken-duplicate &&
729 test_commit duplicate &&
730 # no "-d" here, so we end up with duplicates
732 # now corrupt the loose copy
733 file=$(sha1_file "$(git rev-parse HEAD)") &&
735 echo broken >"$file" &&
736 test_must_fail git fsck
740 test_expect_success 'fsck detects trailing loose garbage (commit)' '
741 git cat-file commit HEAD >basis &&
742 echo bump-commit-sha1 >>basis &&
743 commit=$(git hash-object -w -t commit basis) &&
744 file=$(sha1_file $commit) &&
745 test_when_finished "remove_object $commit" &&
747 echo garbage >>"$file" &&
748 test_must_fail git fsck 2>out &&
749 test_i18ngrep "garbage.*$commit" out
752 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
753 blob=$(echo trailing | git hash-object -w --stdin) &&
754 file=$(sha1_file $blob) &&
755 test_when_finished "remove_object $blob" &&
757 echo garbage >>"$file" &&
758 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
759 test_i18ngrep "garbage.*$blob" out
762 test_expect_success 'fsck detects truncated loose object' '
763 # make it big enough that we know we will truncate in the data
764 # portion, not the header
765 test-tool genrandom truncate 4096 >file &&
766 blob=$(git hash-object -w file) &&
767 file=$(sha1_file $blob) &&
768 test_when_finished "remove_object $blob" &&
769 test_copy_bytes 1024 <"$file" >tmp &&
773 # check both regular and streaming code paths
774 test_must_fail git fsck 2>out &&
775 test_i18ngrep corrupt.*$blob out &&
777 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
778 test_i18ngrep corrupt.*$blob out
781 # for each of type, we have one version which is referenced by another object
782 # (and so while unreachable, not dangling), and another variant which really is
784 test_expect_success 'create dangling-object repository' '
788 blob=$(echo not-dangling | git hash-object -w --stdin) &&
789 dblob=$(echo dangling | git hash-object -w --stdin) &&
790 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
791 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
792 commit=$(git commit-tree $tree) &&
793 dcommit=$(git commit-tree -p $commit $tree) &&
797 dangling commit $dcommit
803 test_expect_success 'fsck notices dangling objects' '
807 # the output order is non-deterministic, as it comes from a hash
808 sort <actual >actual.sorted &&
809 test_i18ncmp expect actual.sorted
813 test_expect_success 'fsck --connectivity-only notices dangling objects' '
816 git fsck --connectivity-only >actual &&
817 # the output order is non-deterministic, as it comes from a hash
818 sort <actual >actual.sorted &&
819 test_i18ncmp expect actual.sorted
823 test_expect_success 'fsck $name notices bogus $name' '
824 test_must_fail git fsck bogus &&
825 test_must_fail git fsck $ZERO_OID
828 test_expect_success 'bogus head does not fallback to all heads' '
829 # set up a case that will cause a reachability complaint
830 echo to-be-deleted >foo &&
832 blob=$(git rev-parse :foo) &&
833 test_when_finished "git rm --cached foo" &&
834 remove_object $blob &&
835 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
839 # Corrupt the checksum on the index.
840 # Add 1 to the last byte in the SHA.
841 corrupt_index_checksum () {
844 open my $fh, "+<", ".git/index" or die "open: $!";
846 seek $fh, -1, SEEK_END or die "seek: $!";
847 read $fh, my $in_byte, 1 or die "read: $!";
849 $in_value = unpack("C", $in_byte);
850 $out_value = ($in_value + 1) & 255;
852 $out_byte = pack("C", $out_value);
854 seek $fh, -1, SEEK_END or die "seek: $!";
856 close $fh or die "close: $!";
860 # Corrupt the checksum on the index and then
861 # verify that only fsck notices.
862 test_expect_success 'detect corrupt index file in fsck' '
863 cp .git/index .git/index.backup &&
864 test_when_finished "mv .git/index.backup .git/index" &&
865 corrupt_index_checksum &&
866 test_must_fail git fsck --cache 2>errors &&
867 test_i18ngrep "bad index file" errors