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 &&
23 test_expect_success 'loose objects borrowed from alternate are not missing' '
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
30 git fsck --no-dangling >../actual 2>&1
35 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
40 # Corruption tests follow. Make sure to remove all traces of the
41 # specific corruption you test afterwards, lest a later test trip over
44 test_expect_success 'setup: helpers for corruption tests' '
47 firsttwo=${1%$remainder} &&
48 echo ".git/objects/$firsttwo/$remainder"
52 rm "$(sha1_file "$1")"
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" &&
72 test_must_fail git fsck 2>out &&
74 grep "$sha.*corrupt" out
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 &&
82 grep "not a commit" out
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 &&
92 grep "detached HEAD points" out
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 &&
102 grep "HEAD points to something strange" out
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" &&
114 ! grep "commit $new" out
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 &&
126 grep "error in commit $new" out
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 &&
138 grep "error in commit $new.* - bad name" out
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 &&
150 grep "error in commit $new.* - missing email" out
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 &&
162 grep "error in commit $new" out
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 &&
176 grep "error in commit $new.*integer overflow" out
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 &&
188 grep "error in commit $new.*unterminated header: NUL at offset" out
191 test_expect_success 'tree object with duplicate entries' '
192 test_when_finished "for i in \$T; do remove_object \$i; done" &&
194 GIT_INDEX_FILE=test-index &&
195 export GIT_INDEX_FILE &&
200 T=$(git write-tree) &&
203 git cat-file tree $T &&
206 git hash-object -w -t tree --stdin
208 test_must_fail git fsck 2>out &&
209 grep "error in tree .*contains duplicate file entries" out
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
225 test_expect_success 'tag pointing to nonexistent' '
226 cat >invalid-tag <<-\EOF &&
227 object ffffffffffffffffffffffffffffffffffffffff
230 tagger T A Gger <tagger@example.com> 1234567890 -0000
232 This is an invalid tag.
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 &&
241 grep "broken link" out
244 test_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 &&
251 tagger T A Gger <tagger@example.com> 1234567890 -0000
253 This is an invalid tag.
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
263 test_expect_success 'tag with incorrect tag name & missing tagger' '
264 sha=$(git rev-parse HEAD) &&
265 cat >wrong-tag <<-EOF &&
268 tag wrong name format
270 This is an invalid tag.
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 &&
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
286 test_expect_success 'tag with bad tagger' '
287 sha=$(git rev-parse HEAD) &&
288 cat >wrong-tag <<-EOF &&
292 tagger Bad Tagger Name
294 This is an invalid tag.
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
305 test_expect_success 'tag with NUL in header' '
306 sha=$(git rev-parse HEAD) &&
307 q_to_nul >tag-NUL-header <<-EOF &&
310 tag contains-Q-in-header
311 tagger T A Gger <tagger@example.com> 1234567890 -0000
313 This is an invalid tag.
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 &&
322 grep "error in tag $tag.*unterminated header: NUL at offset" out
325 test_expect_success 'cleaned up' '
326 git fsck >actual 2>&1 &&
327 test_cmp empty actual
330 test_expect_success 'rev-list --verify-objects' '
331 git rev-list --verify-objects --all >/dev/null 2>out &&
335 test_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" &&
351 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
353 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
356 test_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
368 _bz5="$_bz$_bz$_bz$_bz$_bz"
369 _bz20="$_bz5$_bz5$_bz5$_bz5"
371 test_expect_success 'fsck notices blob entry pointing to null sha1' '
372 (git init null-blob &&
374 sha=$(printf "100644 file$_bz$_bz20" |
375 git hash-object -w --stdin -t tree) &&
378 grep "warning.*null sha1" out
382 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
383 (git init null-commit &&
385 sha=$(printf "160000 submodule$_bz$_bz20" |
386 git hash-object -w --stdin -t tree) &&
389 grep "warning.*null sha1" out
393 while read name path pretty; do
394 while read mode type; do
396 test_expect_success "fsck notices $pretty as $type" '
398 git init $name-$type &&
400 echo content >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) &&
410 grep "warning.*tree $bad_tree" out
421 dotgit-unicode .gI${u200c}T .gI{u200c}T
425 dot-backslash-case .\\\\.GIT\\\\foobar
426 dotgit-case-backslash .git\\\\foobar
429 test_expect_success 'fsck allows .Ňit' '
431 git init not-dotgit &&
433 echo content >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) &&
440 test_line_count = 0 err
444 test_expect_success 'NUL in commit' '
445 rm -fr nul-in-commit &&
446 git init 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) &&
455 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
456 grep nulInCommit warn.1 &&
458 grep nulInCommit warn.2
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).
465 create_repo_missing () {
470 git commit -m one --allow-empty &&
472 echo content >subdir/file &&
473 git add subdir/file &&
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
483 test_expect_success 'fsck notices missing blob' '
484 create_repo_missing HEAD:subdir/file &&
485 test_must_fail git -C missing fsck
488 test_expect_success 'fsck notices missing subtree' '
489 create_repo_missing HEAD:subdir &&
490 test_must_fail git -C missing fsck
493 test_expect_success 'fsck notices missing root tree' '
494 create_repo_missing HEAD^{tree} &&
495 test_must_fail git -C missing fsck
498 test_expect_success 'fsck notices missing parent' '
499 create_repo_missing HEAD^ &&
500 test_must_fail git -C missing fsck
503 test_expect_success 'fsck notices missing tagged object' '
504 create_repo_missing tag^{blob} &&
505 test_must_fail git -C missing fsck
508 test_expect_success 'fsck notices ref pointing to missing commit' '
509 create_repo_missing HEAD &&
510 test_must_fail git -C missing fsck
513 test_expect_success 'fsck notices ref pointing to missing tag' '
514 create_repo_missing tag &&
515 test_must_fail git -C missing fsck
518 test_expect_success 'fsck --connectivity-only' '
519 rm -rf connectivity-only &&
520 git init connectivity-only &&
522 cd connectivity-only &&
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.
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 &&
541 test_must_fail git fsck --strict &&
542 git fsck --strict --connectivity-only &&
543 tree=$(git rev-parse HEAD:) &&
545 tree=.git/objects/${tree%$suffix}/$suffix &&
547 echo invalid >$tree &&
548 test_must_fail git fsck --strict --connectivity-only
552 test_expect_success 'fsck --connectivity-only with explicit head' '
553 rm -rf connectivity-only &&
554 git init connectivity-only &&
556 cd connectivity-only &&
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
565 test_expect_success 'fsck --name-objects' '
566 rm -rf name-objects &&
567 git init 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
580 test_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 &&
590 test_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) &&
600 } | git pack-objects .git/objects/pack/pack
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 &&
611 test_expect_success 'fsck finds problems in duplicate loose objects' '
612 rm -rf broken-duplicate &&
613 git init broken-duplicate &&
615 cd broken-duplicate &&
616 test_commit duplicate &&
617 # no "-d" here, so we end up with duplicates
619 # now corrupt the loose copy
620 file=$(sha1_file "$(git rev-parse HEAD)") &&
622 echo broken >"$file" &&
623 test_must_fail git fsck
627 test_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" &&
634 echo garbage >>"$file" &&
635 test_must_fail git fsck 2>out &&
636 test_i18ngrep "garbage.*$commit" out
639 test_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" &&
644 echo garbage >>"$file" &&
645 test_must_fail git fsck 2>out &&
646 test_i18ngrep "garbage.*$blob" out
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
652 test_expect_success 'fsck notices dangling objects' '
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) &&
663 cat >expect <<-EOF &&
665 dangling commit $dcommit
670 # the output order is non-deterministic, as it comes from a hash
671 sort <actual >actual.sorted &&
672 test_cmp expect actual.sorted
676 test_expect_success 'fsck $name notices bogus $name' '
677 test_must_fail git fsck bogus &&
678 test_must_fail git fsck $_z40
681 test_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 &&
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 &&
692 # Corrupt the checksum on the index.
693 # Add 1 to the last byte in the SHA.
694 corrupt_index_checksum () {
697 open my $fh, "+<", ".git/index" or die "open: $!";
699 seek $fh, -1, SEEK_END or die "seek: $!";
700 read $fh, my $in_byte, 1 or die "read: $!";
702 $in_value = unpack("C", $in_byte);
703 $out_value = ($in_value + 1) & 255;
705 $out_byte = pack("C", $out_value);
707 seek $fh, -1, SEEK_END or die "seek: $!";
709 close $fh or die "close: $!";
713 # Corrupt the checksum on the index and then
714 # verify that only fsck notices.
715 test_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