3 test_description
='git fsck random collection of tests
9 TEST_PASSES_SANITIZE_LEAK
=true
12 test_expect_success setup
'
13 git config gc.auto 0 &&
14 git config i18n.commitencoding ISO-8859-1 &&
15 test_commit A fileA one &&
16 git config --unset i18n.commitencoding &&
17 git checkout HEAD^0 &&
18 test_commit B fileB two &&
19 orig_head=$(git rev-parse HEAD) &&
21 git reflog expire --expire=now --all
24 test_expect_success
'loose objects borrowed from alternate are not missing' '
29 echo ../../../.git/objects >.git/objects/info/alternates &&
30 test_commit C fileC one &&
31 git fsck --no-dangling >../actual 2>&1
33 test_must_be_empty actual
36 test_expect_success
'HEAD is part of refs, valid objects appear valid' '
37 git fsck >actual 2>&1 &&
38 test_must_be_empty actual
41 # Corruption tests follow. Make sure to remove all traces of the
42 # specific corruption you test afterwards, lest a later test trip over
46 git rev-parse
--git-path objects
/$
(test_oid_to_path
"$1")
50 rm "$(sha1_file "$1")"
53 test_expect_success
'object with hash mismatch' '
54 git init --bare hash-mismatch &&
58 oid=$(echo blob | git hash-object -w --stdin) &&
60 old=$(test_oid_to_path "$oid") &&
61 new=$(dirname $old)/$(test_oid ff_2) &&
62 oid="$(dirname $new)$(basename $new)" &&
64 mv objects/$old objects/$new &&
65 git update-index --add --cacheinfo 100644 $oid foo &&
66 tree=$(git write-tree) &&
67 cmt=$(echo bogus | git commit-tree $tree) &&
68 git update-ref refs/heads/bogus $cmt &&
70 test_must_fail git fsck 2>out &&
71 grep "$oldoid: hash-path mismatch, found at: .*$new" out
75 test_expect_success
'object with hash and type mismatch' '
76 git init --bare hash-type-mismatch &&
78 cd hash-type-mismatch &&
80 oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) &&
82 old=$(test_oid_to_path "$oid") &&
83 new=$(dirname $old)/$(test_oid ff_2) &&
84 oid="$(dirname $new)$(basename $new)" &&
86 mv objects/$old objects/$new &&
87 git update-index --add --cacheinfo 100644 $oid foo &&
88 tree=$(git write-tree) &&
89 cmt=$(echo bogus | git commit-tree $tree) &&
90 git update-ref refs/heads/bogus $cmt &&
93 test_must_fail git fsck 2>out &&
94 grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
95 grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
99 test_expect_success
'zlib corrupt loose object output ' '
100 git init --bare corrupt-loose-output &&
102 cd corrupt-loose-output &&
103 oid=$(git hash-object -w --stdin --literally </dev/null) &&
104 oidf=objects/$(test_oid_to_path "$oid") &&
106 echo extra garbage >>$oidf &&
108 cat >expect.error <<-EOF &&
109 error: garbage at end of loose object '\''$oid'\''
110 error: unable to unpack contents of ./$oidf
111 error: $oid: object corrupt or missing: ./$oidf
113 test_must_fail git fsck 2>actual &&
114 grep ^error: actual >error &&
115 test_cmp expect.error error
119 test_expect_success
'branch pointing to non-commit' '
120 tree_oid=$(git rev-parse --verify HEAD^{tree}) &&
121 test_when_finished "git update-ref -d refs/heads/invalid" &&
122 test-tool ref-store main update-ref msg refs/heads/invalid $tree_oid $ZERO_OID REF_SKIP_OID_VERIFICATION &&
123 test_must_fail git fsck 2>out &&
124 test_grep "not a commit" out
127 test_expect_success REFFILES
'HEAD link pointing at a funny object' '
128 test_when_finished "git update-ref HEAD $orig_head" &&
129 echo $ZERO_OID >.git/HEAD &&
130 # avoid corrupt/broken HEAD from interfering with repo discovery
131 test_must_fail env GIT_DIR=.git git fsck 2>out &&
132 test_grep "detached HEAD points" out
135 test_expect_success
'HEAD link pointing at a funny place' '
136 test_when_finished "git update-ref --no-deref HEAD $orig_head" &&
137 test-tool ref-store main create-symref HEAD refs/funny/place &&
138 # avoid corrupt/broken HEAD from interfering with repo discovery
139 test_must_fail env GIT_DIR=.git git fsck 2>out &&
140 test_grep "HEAD points to something strange" out
143 test_expect_success REFFILES
'HEAD link pointing at a funny object (from different wt)' '
144 test_when_finished "git update-ref HEAD $orig_head" &&
145 test_when_finished "git worktree remove -f wt" &&
146 git worktree add wt &&
147 echo $ZERO_OID >.git/HEAD &&
148 # avoid corrupt/broken HEAD from interfering with repo discovery
149 test_must_fail git -C wt fsck 2>out &&
150 test_grep "main-worktree/HEAD: detached HEAD points" out
153 test_expect_success REFFILES
'other worktree HEAD link pointing at a funny object' '
154 test_when_finished "git worktree remove -f other" &&
155 git worktree add other &&
156 echo $ZERO_OID >.git/worktrees/other/HEAD &&
157 test_must_fail git fsck 2>out &&
158 test_grep "worktrees/other/HEAD: detached HEAD points" out
161 test_expect_success
'other worktree HEAD link pointing at missing object' '
162 test_when_finished "git worktree remove -f other" &&
163 git worktree add other &&
164 object_id=$(echo "Contents missing from repo" | git hash-object --stdin) &&
165 test-tool -C other ref-store main update-ref msg HEAD $object_id "" REF_NO_DEREF,REF_SKIP_OID_VERIFICATION &&
166 test_must_fail git fsck 2>out &&
167 test_grep "worktrees/other/HEAD: invalid sha1 pointer" out
170 test_expect_success
'other worktree HEAD link pointing at a funny place' '
171 test_when_finished "git worktree remove -f other" &&
172 git worktree add other &&
173 git -C other symbolic-ref HEAD refs/funny/place &&
174 test_must_fail git fsck 2>out &&
175 test_grep "worktrees/other/HEAD points to something strange" out
178 test_expect_success
'commit with multiple signatures is okay' '
179 git cat-file commit HEAD >basis &&
181 gpgsig -----BEGIN PGP SIGNATURE-----
182 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
183 -----END PGP SIGNATURE-----
184 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
185 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
186 -----END PGP SIGNATURE-----
188 sed -e "/^committer/q" basis >okay &&
191 sed -e "1,/^$/d" basis >>okay &&
193 new=$(git hash-object -t commit -w --stdin <okay) &&
194 test_when_finished "remove_object $new" &&
195 git update-ref refs/heads/bogus "$new" &&
196 test_when_finished "git update-ref -d refs/heads/bogus" &&
199 ! grep "commit $new" out
202 test_expect_success
'email without @ is okay' '
203 git cat-file commit HEAD >basis &&
204 sed "s/@/AT/" basis >okay &&
205 new=$(git hash-object -t commit -w --stdin <okay) &&
206 test_when_finished "remove_object $new" &&
207 git update-ref refs/heads/bogus "$new" &&
208 test_when_finished "git update-ref -d refs/heads/bogus" &&
210 ! grep "commit $new" out
213 test_expect_success
'email with embedded > is not okay' '
214 git cat-file commit HEAD >basis &&
215 sed "s/@[a-z]/&>/" basis >bad-email &&
216 new=$(git hash-object --literally -t commit -w --stdin <bad-email) &&
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 test_grep "error in commit $new" out
224 test_expect_success
'missing < email delimiter is reported nicely' '
225 git cat-file commit HEAD >basis &&
226 sed "s/<//" basis >bad-email-2 &&
227 new=$(git hash-object --literally -t commit -w --stdin <bad-email-2) &&
228 test_when_finished "remove_object $new" &&
229 git update-ref refs/heads/bogus "$new" &&
230 test_when_finished "git update-ref -d refs/heads/bogus" &&
231 test_must_fail git fsck 2>out &&
232 test_grep "error in commit $new.* - bad name" out
235 test_expect_success
'missing email is reported nicely' '
236 git cat-file commit HEAD >basis &&
237 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
238 new=$(git hash-object --literally -t commit -w --stdin <bad-email-3) &&
239 test_when_finished "remove_object $new" &&
240 git update-ref refs/heads/bogus "$new" &&
241 test_when_finished "git update-ref -d refs/heads/bogus" &&
242 test_must_fail git fsck 2>out &&
243 test_grep "error in commit $new.* - missing email" out
246 test_expect_success
'> in name is reported' '
247 git cat-file commit HEAD >basis &&
248 sed "s/ </> </" basis >bad-email-4 &&
249 new=$(git hash-object --literally -t commit -w --stdin <bad-email-4) &&
250 test_when_finished "remove_object $new" &&
251 git update-ref refs/heads/bogus "$new" &&
252 test_when_finished "git update-ref -d refs/heads/bogus" &&
253 test_must_fail git fsck 2>out &&
254 test_grep "error in commit $new" out
258 test_expect_success
'integer overflow in timestamps is reported' '
259 git cat-file commit HEAD >basis &&
260 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
261 <basis >bad-timestamp &&
262 new=$(git hash-object --literally -t commit -w --stdin <bad-timestamp) &&
263 test_when_finished "remove_object $new" &&
264 git update-ref refs/heads/bogus "$new" &&
265 test_when_finished "git update-ref -d refs/heads/bogus" &&
266 test_must_fail git fsck 2>out &&
267 test_grep "error in commit $new.*integer overflow" out
270 test_expect_success
'commit with NUL in header' '
271 git cat-file commit HEAD >basis &&
272 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
273 new=$(git hash-object --literally -t commit -w --stdin <commit-NUL-header) &&
274 test_when_finished "remove_object $new" &&
275 git update-ref refs/heads/bogus "$new" &&
276 test_when_finished "git update-ref -d refs/heads/bogus" &&
277 test_must_fail git fsck 2>out &&
278 test_grep "error in commit $new.*unterminated header: NUL at offset" out
281 test_expect_success
'tree object with duplicate entries' '
282 test_when_finished "for i in \$T; do remove_object \$i; done" &&
284 GIT_INDEX_FILE=test-index &&
285 export GIT_INDEX_FILE &&
290 T=$(git write-tree) &&
293 git cat-file tree $T &&
296 git hash-object --literally -w -t tree --stdin
298 test_must_fail git fsck 2>out &&
299 test_grep "error in tree .*contains duplicate file entries" out
302 check_duplicate_names
() {
306 test_expect_
$expect "tree object with duplicate names: $names" '
307 test_when_finished "remove_object \$blob" &&
308 test_when_finished "remove_object \$tree" &&
309 test_when_finished "remove_object \$badtree" &&
310 blob=$(echo blob | git hash-object -w --stdin) &&
311 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
312 tree=$(git mktree <tree) &&
316 */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
317 *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
320 badtree=$(git mktree <badtree) &&
321 test_must_fail git fsck 2>out &&
322 test_grep "$badtree" out &&
323 test_grep "error in tree .*contains duplicate file entries" out
327 check_duplicate_names success x x
.1 x
/
328 check_duplicate_names success x x
.1.2 x
.1/ x
/
329 check_duplicate_names success x x
.1 x
.1.2 x
/
331 test_expect_success
'unparseable tree object' '
332 test_oid_cache <<-\EOF &&
333 junk sha1:twenty-bytes-of-junk
334 junk sha256:twenty-bytes-of-junk-twelve-more
337 test_when_finished "git update-ref -d refs/heads/wrong" &&
338 test_when_finished "remove_object \$tree_sha1" &&
339 test_when_finished "remove_object \$commit_sha1" &&
340 junk=$(test_oid junk) &&
341 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
342 commit_sha1=$(git commit-tree $tree_sha1) &&
343 git update-ref refs/heads/wrong $commit_sha1 &&
344 test_must_fail git fsck 2>out &&
345 test_grep "error: empty filename in tree entry" out &&
346 test_grep "$tree_sha1" out &&
347 test_grep ! "fatal: empty filename in tree entry" out
350 test_expect_success
'tree entry with type mismatch' '
351 test_when_finished "remove_object \$blob" &&
352 test_when_finished "remove_object \$tree" &&
353 test_when_finished "remove_object \$commit" &&
354 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
355 blob=$(echo blob | git hash-object -w --stdin) &&
356 blob_bin=$(echo $blob | hex2oct) &&
358 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
359 git hash-object -t tree --stdin -w --literally
361 commit=$(git commit-tree $tree) &&
362 git update-ref refs/heads/type_mismatch $commit &&
363 test_must_fail git fsck >out 2>&1 &&
364 test_grep "is a blob, not a tree" out &&
365 test_grep ! "dangling blob" out
368 test_expect_success
'tree entry with bogus mode' '
369 test_when_finished "remove_object \$blob" &&
370 test_when_finished "remove_object \$tree" &&
371 blob=$(echo blob | git hash-object -w --stdin) &&
372 blob_oct=$(echo $blob | hex2oct) &&
373 tree=$(printf "100000 foo\0${blob_oct}" |
374 git hash-object -t tree --stdin -w --literally) &&
376 cat >expect <<-EOF &&
377 warning in tree $tree: badFilemode: contains bad file modes
382 test_expect_success
'tag pointing to nonexistent' '
383 badoid=$(test_oid deadbeef) &&
384 cat >invalid-tag <<-EOF &&
388 tagger T A Gger <tagger@example.com> 1234567890 -0000
390 This is an invalid tag.
393 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
394 test_when_finished "remove_object $tag" &&
395 git update-ref refs/tags/invalid $tag &&
396 test_when_finished "git update-ref -d refs/tags/invalid" &&
397 test_must_fail git fsck --tags >out &&
398 test_grep "broken link" out
401 test_expect_success
'tag pointing to something else than its type' '
402 sha=$(echo blob | git hash-object -w --stdin) &&
403 test_when_finished "remove_object $sha" &&
404 cat >wrong-tag <<-EOF &&
408 tagger T A Gger <tagger@example.com> 1234567890 -0000
410 This is an invalid tag.
413 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
414 test_when_finished "remove_object $tag" &&
415 git update-ref refs/tags/wrong $tag &&
416 test_when_finished "git update-ref -d refs/tags/wrong" &&
417 test_must_fail git fsck --tags
420 test_expect_success
'tag with incorrect tag name & missing tagger' '
421 sha=$(git rev-parse HEAD) &&
422 cat >wrong-tag <<-EOF &&
425 tag wrong name format
427 This is an invalid tag.
430 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
431 test_when_finished "remove_object $tag" &&
432 git update-ref refs/tags/wrong $tag &&
433 test_when_finished "git update-ref -d refs/tags/wrong" &&
434 git fsck --tags 2>out &&
436 cat >expect <<-EOF &&
437 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
438 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
443 test_expect_success
'tag with bad tagger' '
444 sha=$(git rev-parse HEAD) &&
445 cat >wrong-tag <<-EOF &&
449 tagger Bad Tagger Name
451 This is an invalid tag.
454 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
455 test_when_finished "remove_object $tag" &&
456 git update-ref refs/tags/wrong $tag &&
457 test_when_finished "git update-ref -d refs/tags/wrong" &&
458 test_must_fail git fsck --tags 2>out &&
459 test_grep "error in tag .*: invalid author/committer" out
462 test_expect_success
'tag with NUL in header' '
463 sha=$(git rev-parse HEAD) &&
464 q_to_nul >tag-NUL-header <<-EOF &&
467 tag contains-Q-in-header
468 tagger T A Gger <tagger@example.com> 1234567890 -0000
470 This is an invalid tag.
473 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
474 test_when_finished "remove_object $tag" &&
475 git update-ref refs/tags/wrong $tag &&
476 test_when_finished "git update-ref -d refs/tags/wrong" &&
477 test_must_fail git fsck --tags 2>out &&
478 test_grep "error in tag $tag.*unterminated header: NUL at offset" out
481 test_expect_success
'cleaned up' '
482 git fsck >actual 2>&1 &&
483 test_must_be_empty actual
486 test_expect_success
'rev-list --verify-objects' '
487 git rev-list --verify-objects --all >/dev/null 2>out &&
488 test_must_be_empty out
491 test_expect_success
'rev-list --verify-objects with bad sha1' '
492 sha=$(echo blob | git hash-object -w --stdin) &&
493 old=$(test_oid_to_path $sha) &&
494 new=$(dirname $old)/$(test_oid ff_2) &&
495 sha="$(dirname $new)$(basename $new)" &&
496 mv .git/objects/$old .git/objects/$new &&
497 test_when_finished "remove_object $sha" &&
498 git update-index --add --cacheinfo 100644 $sha foo &&
499 test_when_finished "git read-tree -u --reset HEAD" &&
500 tree=$(git write-tree) &&
501 test_when_finished "remove_object $tree" &&
502 cmt=$(echo bogus | git commit-tree $tree) &&
503 test_when_finished "remove_object $cmt" &&
504 git update-ref refs/heads/bogus $cmt &&
505 test_when_finished "git update-ref -d refs/heads/bogus" &&
507 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
508 test_grep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
511 # An actual bit corruption is more likely than swapped commits, but
512 # this provides an easy way to have commits which don't match their purported
513 # hashes, but which aren't so broken we can't read them at all.
514 test_expect_success
'rev-list --verify-objects notices swapped commits' '
515 git init swapped-commits &&
517 cd swapped-commits &&
520 one_oid=$(git rev-parse HEAD) &&
521 two_oid=$(git rev-parse HEAD^) &&
522 one=.git/objects/$(test_oid_to_path $one_oid) &&
523 two=.git/objects/$(test_oid_to_path $two_oid) &&
527 test_must_fail git rev-list --verify-objects HEAD
531 test_expect_success
'set up repository with commit-graph' '
532 git init corrupt-graph &&
537 git commit-graph write --reachable
541 corrupt_graph_obj
() {
542 oid
=$
(git
-C corrupt-graph rev-parse
"$1") &&
543 obj
=corrupt-graph
/.git
/objects
/$
(test_oid_to_path
$oid) &&
544 test_when_finished
'mv backup $obj' &&
549 test_expect_success
'rev-list --verify-objects with commit graph (tip)' '
550 corrupt_graph_obj HEAD &&
551 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
554 test_expect_success
'rev-list --verify-objects with commit graph (parent)' '
555 corrupt_graph_obj HEAD^ &&
556 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
559 test_expect_success
'force fsck to ignore double author' '
560 git cat-file commit HEAD >basis &&
561 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
562 new=$(git hash-object --literally -t commit -w --stdin <multiple-authors) &&
563 test_when_finished "remove_object $new" &&
564 git update-ref refs/heads/bogus "$new" &&
565 test_when_finished "git update-ref -d refs/heads/bogus" &&
566 test_must_fail git fsck &&
567 git -c fsck.multipleAuthors=ignore fsck
571 _bzoid
=$
(printf $ZERO_OID |
sed -e 's/00/\\0/g')
573 test_expect_success
'fsck notices blob entry pointing to null sha1' '
574 (git init null-blob &&
576 sha=$(printf "100644 file$_bz$_bzoid" |
577 git hash-object --literally -w --stdin -t tree) &&
579 test_grep "warning.*null sha1" out
583 test_expect_success
'fsck notices submodule entry pointing to null sha1' '
584 (git init null-commit &&
586 sha=$(printf "160000 submodule$_bz$_bzoid" |
587 git hash-object --literally -w --stdin -t tree) &&
589 test_grep "warning.*null sha1" out
593 test_expect_success
'fsck notices excessively large tree entry name' '
594 git init large-name &&
597 test_commit a-long-name &&
598 git -c fsck.largePathname=warn:10 fsck 2>out &&
599 grep "warning.*large pathname" out
603 while read name path pretty
; do
604 while read mode
type; do
606 test_expect_success
"fsck notices $pretty as $type" '
608 git init $name-$type &&
610 git config core.protectNTFS false &&
611 echo content >file &&
613 git commit -m base &&
614 blob=$(git rev-parse :file) &&
615 tree=$(git rev-parse HEAD^{tree}) &&
616 value=$(eval "echo \$$type") &&
617 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
618 bad_tree=$(git mktree <bad) &&
620 test_grep "warning.*tree $bad_tree" out
631 dotgit-unicode .gI${u200c}T .gI{u200c}T
635 dot-backslash-case .\\\\.GIT\\\\foobar
636 dotgit-case-backslash .git\\\\foobar
639 test_expect_success 'fsck allows .Ňit' '
641 git init not-dotgit &&
643 echo content >file &&
645 git commit -m base &&
646 blob=$(git rev-parse :file) &&
647 printf "100644 blob $blob\t.\\305\\207it" >tree &&
648 tree=$(git mktree <tree) &&
650 test_line_count = 0 err
654 test_expect_success 'NUL in commit' '
655 rm -fr nul-in-commit &&
656 git init nul-in-commit &&
659 git commit --allow-empty -m "initial commitQNUL after message" &&
660 git cat-file commit HEAD >original &&
661 q_to_nul <original >munged &&
662 git hash-object --literally -w -t commit --stdin <munged >name &&
663 git branch bad $(cat name) &&
665 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
666 test_grep nulInCommit warn.1 &&
668 test_grep nulInCommit warn.2
672 # create a static test repo which is broken by omitting
673 # one particular object ($1, which is looked up via rev-parse
674 # in the new repository).
675 create_repo_missing () {
680 git commit -m one --allow-empty &&
682 echo content >subdir/file &&
683 git add subdir/file &&
685 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
686 git tag -m foo tag $unrelated &&
687 sha1=$(git rev-parse --verify "$1") &&
688 path=$(echo $sha1 | sed 's|..|&/|') &&
689 rm .git/objects/$path
693 test_expect_success 'fsck notices missing blob' '
694 create_repo_missing HEAD:subdir/file &&
695 test_must_fail git -C missing fsck
698 test_expect_success 'fsck notices missing subtree' '
699 create_repo_missing HEAD:subdir &&
700 test_must_fail git -C missing fsck
703 test_expect_success 'fsck notices missing root tree' '
704 create_repo_missing HEAD^{tree} &&
705 test_must_fail git -C missing fsck
708 test_expect_success 'fsck notices missing parent' '
709 create_repo_missing HEAD^ &&
710 test_must_fail git -C missing fsck
713 test_expect_success 'fsck notices missing tagged object' '
714 create_repo_missing tag^{blob} &&
715 test_must_fail git -C missing fsck
718 test_expect_success 'fsck notices ref pointing to missing commit' '
719 create_repo_missing HEAD &&
720 test_must_fail git -C missing fsck
723 test_expect_success 'fsck notices ref pointing to missing tag' '
724 create_repo_missing tag &&
725 test_must_fail git -C missing fsck
728 test_expect_success 'fsck --connectivity-only' '
729 rm -rf connectivity-only &&
730 git init connectivity-only &&
732 cd connectivity-only &&
737 # Drop the index now; we want to be sure that we
738 # recursively notice the broken objects
739 # because they are reachable from refs, not because
740 # they are in the index.
743 # corrupt the blob, but in a way that we can still identify
744 # its type. That lets us see that --connectivity-only is
745 # not actually looking at the contents, but leaves it
746 # free to examine the type if it chooses.
747 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
748 blob=$(echo unrelated | git hash-object -w --stdin) &&
749 mv -f $(sha1_file $blob) $empty &&
751 test_must_fail git fsck --strict &&
752 git fsck --strict --connectivity-only &&
753 tree=$(git rev-parse HEAD:) &&
755 tree=.git/objects/${tree%$suffix}/$suffix &&
757 echo invalid >$tree &&
758 test_must_fail git fsck --strict --connectivity-only
762 test_expect_success 'fsck --connectivity-only with explicit head' '
763 rm -rf connectivity-only &&
764 git init connectivity-only &&
766 cd connectivity-only &&
769 tree=$(git rev-parse HEAD^{tree}) &&
770 remove_object $(git rev-parse HEAD:foo.t) &&
771 test_must_fail git fsck --connectivity-only $tree
775 test_expect_success 'fsck --name-objects' '
776 rm -rf name-objects &&
777 git init name-objects &&
780 git config core.logAllRefUpdates false &&
781 test_commit julius caesar.t &&
782 test_commit augustus44 &&
783 test_commit caesar &&
784 remove_object $(git rev-parse julius:caesar.t) &&
785 tree=$(git rev-parse --verify julius:) &&
787 test_must_fail git fsck --name-objects >out &&
788 test_grep "$tree (refs/tags/augustus44\\^:" out
792 test_expect_success 'alternate objects are correctly blamed' '
793 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
794 name=$(test_oid numeric) &&
795 path=$(test_oid_to_path "$name") &&
796 git init --bare alt.git &&
797 echo "../../alt.git/objects" >.git/objects/info/alternates &&
798 mkdir alt.git/objects/$(dirname $path) &&
799 >alt.git/objects/$(dirname $path)/$(basename $path) &&
800 test_must_fail git fsck >out 2>&1 &&
801 test_grep alt.git out
804 test_expect_success 'fsck errors in packed objects' '
805 git cat-file commit HEAD >basis &&
806 sed "s/</one/" basis >one &&
807 sed "s/</foo/" basis >two &&
808 one=$(git hash-object --literally -t commit -w one) &&
809 two=$(git hash-object --literally -t commit -w two) &&
814 } | git pack-objects .git/objects/pack/pack
816 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
817 remove_object $one &&
818 remove_object $two &&
819 test_must_fail git fsck 2>out &&
820 test_grep "error in commit $one.* - bad name
" out &&
821 test_grep "error
in commit
$two.
* - bad name
" out &&
825 test_expect_success 'fsck fails on corrupt packfile' '
826 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
827 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
829 # Corrupt the first byte of the first object. (It contains 3 type bits,
830 # at least one of which is not zero, so setting the first byte to 0 is
832 chmod a+w .git/objects/pack/pack-$pack.pack &&
833 printf "\
0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
835 test_when_finished "rm -f .git
/objects
/pack
/pack-
$pack.
*" &&
836 remove_object $hsh &&
837 test_must_fail git fsck 2>out &&
838 test_grep "checksum mismatch
" out
841 test_expect_success 'fsck finds problems in duplicate loose objects' '
842 rm -rf broken-duplicate &&
843 git init broken-duplicate &&
845 cd broken-duplicate &&
846 test_commit duplicate &&
847 # no "-d" here, so we end up with duplicates
849 # now corrupt the loose copy
850 oid="$
(git rev-parse HEAD
)" &&
851 file=$(sha1_file "$oid") &&
853 echo broken >"$file" &&
854 test_must_fail git fsck 2>err &&
856 cat >expect <<-EOF &&
857 error: inflate: data stream error (incorrect header check)
858 error: unable to unpack header of $file
859 error: $oid: object corrupt or missing: $file
861 grep "^error
: " err >actual &&
862 test_cmp expect actual
866 test_expect_success 'fsck detects trailing loose garbage (commit)' '
867 git cat-file commit HEAD >basis &&
868 echo bump-commit-sha1 >>basis &&
869 commit=$(git hash-object -w -t commit basis) &&
870 file=$(sha1_file $commit) &&
871 test_when_finished "remove_object
$commit" &&
873 echo garbage >>"$file" &&
874 test_must_fail git fsck 2>out &&
875 test_grep "garbage.
*$commit" out
878 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
879 blob=$(echo trailing | git hash-object -w --stdin) &&
880 file=$(sha1_file $blob) &&
881 test_when_finished "remove_object
$blob" &&
883 echo garbage >>"$file" &&
884 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
885 test_grep "garbage.
*$blob" out
888 test_expect_success 'fsck detects truncated loose object' '
889 # make it big enough that we know we will truncate in the data
890 # portion, not the header
891 test-tool genrandom truncate 4096 >file &&
892 blob=$(git hash-object -w file) &&
893 file=$(sha1_file $blob) &&
894 test_when_finished "remove_object
$blob" &&
895 test_copy_bytes 1024 <"$file" >tmp &&
899 # check both regular and streaming code paths
900 test_must_fail git fsck 2>out &&
901 test_grep corrupt.*$blob out &&
903 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
904 test_grep corrupt.*$blob out
907 # for each of type, we have one version which is referenced by another object
908 # (and so while unreachable, not dangling), and another variant which really is
910 test_expect_success 'create dangling-object repository' '
914 blob=$(echo not-dangling | git hash-object -w --stdin) &&
915 dblob=$(echo dangling | git hash-object -w --stdin) &&
916 tree=$(printf "100644 blob
%s
\t%s
\n" $blob one | git mktree) &&
917 dtree=$(printf "100644 blob
%s
\t%s
\n" $blob two | git mktree) &&
918 commit=$(git commit-tree $tree) &&
919 dcommit=$(git commit-tree -p $commit $tree) &&
923 dangling commit $dcommit
929 test_expect_success 'fsck notices dangling objects' '
933 # the output order is non-deterministic, as it comes from a hash
934 sort <actual >actual.sorted &&
935 test_cmp expect actual.sorted
939 test_expect_success 'fsck --connectivity-only notices dangling objects' '
942 git fsck --connectivity-only >actual &&
943 # the output order is non-deterministic, as it comes from a hash
944 sort <actual >actual.sorted &&
945 test_cmp expect actual.sorted
949 test_expect_success 'fsck $name notices bogus $name' '
950 test_must_fail git fsck bogus &&
951 test_must_fail git fsck $ZERO_OID
954 test_expect_success 'bogus head does not fallback to all heads' '
955 # set up a case that will cause a reachability complaint
956 echo to-be-deleted >foo &&
958 blob=$(git rev-parse :foo) &&
959 test_when_finished "git
rm --cached foo
" &&
960 remove_object $blob &&
961 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
965 # Corrupt the checksum on the index.
966 # Add 1 to the last byte in the SHA.
967 corrupt_index_checksum () {
970 open my $fh, "+<", ".git
/index
" or die "open
: $
!";
972 seek $fh, -1, SEEK_END or die "seek
: $
!";
973 read $fh, my $in_byte, 1 or die "read: $
!";
975 $in_value = unpack("C
", $in_byte);
976 $out_value = ($in_value + 1) & 255;
978 $out_byte = pack("C
", $out_value);
980 seek $fh, -1, SEEK_END or die "seek
: $
!";
982 close $fh or die "close
: $
!";
986 # Corrupt the checksum on the index and then
987 # verify that only fsck notices.
988 test_expect_success 'detect corrupt index file in fsck' '
989 cp .git/index .git/index.backup &&
990 test_when_finished "mv .git
/index.backup .git
/index
" &&
991 corrupt_index_checksum &&
992 test_must_fail git fsck --cache 2>errors &&
993 test_grep "bad index
file" errors
996 test_expect_success 'fsck error and recovery on invalid object type' '
997 git init --bare garbage-type &&
1001 garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
1003 test_must_fail git fsck 2>err &&
1004 grep -e "^error
" -e "^fatal
" err >errors &&
1005 test_line_count = 1 errors &&
1006 grep "$garbage_blob: object is of unknown
type '"'"'garbage'"'"':" err
1010 test_expect_success 'fsck error on gitattributes with excessive line lengths' '
1011 blob=$(printf "pattern
%02048d
" 1 | git hash-object -w --stdin) &&
1012 test_when_finished "remove_object
$blob" &&
1013 tree=$(printf "100644 blob
%s
\t%s
\n" $blob .gitattributes | git mktree) &&
1014 test_when_finished "remove_object
$tree" &&
1015 cat >expected <<-EOF &&
1016 error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
1018 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1019 test_cmp expected actual
1022 test_expect_success 'fsck error on gitattributes with excessive size' '
1023 blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
1024 test_when_finished "remove_object
$blob" &&
1025 tree=$(printf "100644 blob
%s
\t%s
\n" $blob .gitattributes | git mktree) &&
1026 test_when_finished "remove_object
$tree" &&
1027 cat >expected <<-EOF &&
1028 error in blob $blob: gitattributesLarge: .gitattributes too large to parse
1030 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1031 test_cmp expected actual
1034 test_expect_success 'fsck detects problems in worktree index' '
1035 test_when_finished "git worktree remove
-f wt
" &&
1036 git worktree add wt &&
1038 echo "this will be removed to
break the worktree index
" >wt/file &&
1039 git -C wt add file &&
1040 blob=$(git -C wt rev-parse :file) &&
1041 remove_object $blob &&
1043 test_must_fail git fsck --name-objects >actual 2>&1 &&
1044 cat >expect <<-EOF &&
1045 missing blob $blob (.git/worktrees/wt/index:file)
1047 test_cmp expect actual
1050 test_expect_success 'fsck reports problems in current worktree index without filename' '
1051 test_when_finished "rm -f .git
/index
&& git read-tree HEAD
" &&
1052 echo "this object will be removed to
break current worktree index
" >file &&
1054 blob=$(git rev-parse :file) &&
1055 remove_object $blob &&
1057 test_must_fail git fsck --name-objects >actual 2>&1 &&
1058 cat >expect <<-EOF &&
1059 missing blob $blob (:file)
1061 test_cmp expect actual