3 test_description
='git fsck random collection of tests
11 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 &&
20 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
32 test_must_be_empty actual
35 test_expect_success
'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
37 test_must_be_empty actual
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=$(test_oid_to_path "$sha") &&
59 new=$(dirname $old)/$(test_oid ff_2) &&
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 &&
73 test_i18ngrep "$sha.*corrupt" out
76 test_expect_success
'branch pointing to non-commit' '
77 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
78 test_when_finished "git update-ref -d refs/heads/invalid" &&
79 test_must_fail git fsck 2>out &&
80 test_i18ngrep "not a commit" out
83 test_expect_success
'HEAD link pointing at a funny object' '
84 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
85 mv .git/HEAD .git/SAVED_HEAD &&
86 echo $ZERO_OID >.git/HEAD &&
87 # avoid corrupt/broken HEAD from interfering with repo discovery
88 test_must_fail env GIT_DIR=.git git fsck 2>out &&
89 test_i18ngrep "detached HEAD points" out
92 test_expect_success
'HEAD link pointing at a funny place' '
93 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
94 mv .git/HEAD .git/SAVED_HEAD &&
95 echo "ref: refs/funny/place" >.git/HEAD &&
96 # avoid corrupt/broken HEAD from interfering with repo discovery
97 test_must_fail env GIT_DIR=.git git fsck 2>out &&
98 test_i18ngrep "HEAD points to something strange" out
101 test_expect_success
'HEAD link pointing at a funny object (from different wt)' '
102 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
103 test_when_finished "rm -rf .git/worktrees wt" &&
104 git worktree add wt &&
105 mv .git/HEAD .git/SAVED_HEAD &&
106 echo $ZERO_OID >.git/HEAD &&
107 # avoid corrupt/broken HEAD from interfering with repo discovery
108 test_must_fail git -C wt fsck 2>out &&
109 test_i18ngrep "main-worktree/HEAD: detached HEAD points" out
112 test_expect_success
'other worktree HEAD link pointing at a funny object' '
113 test_when_finished "rm -rf .git/worktrees other" &&
114 git worktree add other &&
115 echo $ZERO_OID >.git/worktrees/other/HEAD &&
116 test_must_fail git fsck 2>out &&
117 test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out
120 test_expect_success
'other worktree HEAD link pointing at missing object' '
121 test_when_finished "rm -rf .git/worktrees other" &&
122 git worktree add other &&
123 echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
124 test_must_fail git fsck 2>out &&
125 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
128 test_expect_success
'other worktree HEAD link pointing at a funny place' '
129 test_when_finished "rm -rf .git/worktrees other" &&
130 git worktree add other &&
131 echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
132 test_must_fail git fsck 2>out &&
133 test_i18ngrep "worktrees/other/HEAD points to something strange" out
136 test_expect_success
'commit with multiple signatures is okay' '
137 git cat-file commit HEAD >basis &&
139 gpgsig -----BEGIN PGP SIGNATURE-----
140 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
141 -----END PGP SIGNATURE-----
142 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
143 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
144 -----END PGP SIGNATURE-----
146 sed -e "/^committer/q" basis >okay &&
149 sed -e "1,/^$/d" basis >>okay &&
151 new=$(git hash-object -t commit -w --stdin <okay) &&
152 test_when_finished "remove_object $new" &&
153 git update-ref refs/heads/bogus "$new" &&
154 test_when_finished "git update-ref -d refs/heads/bogus" &&
157 ! grep "commit $new" out
160 test_expect_success
'email without @ is okay' '
161 git cat-file commit HEAD >basis &&
162 sed "s/@/AT/" basis >okay &&
163 new=$(git hash-object -t commit -w --stdin <okay) &&
164 test_when_finished "remove_object $new" &&
165 git update-ref refs/heads/bogus "$new" &&
166 test_when_finished "git update-ref -d refs/heads/bogus" &&
168 ! grep "commit $new" out
171 test_expect_success
'email with embedded > is not okay' '
172 git cat-file commit HEAD >basis &&
173 sed "s/@[a-z]/&>/" basis >bad-email &&
174 new=$(git hash-object -t commit -w --stdin <bad-email) &&
175 test_when_finished "remove_object $new" &&
176 git update-ref refs/heads/bogus "$new" &&
177 test_when_finished "git update-ref -d refs/heads/bogus" &&
178 test_must_fail git fsck 2>out &&
179 test_i18ngrep "error in commit $new" out
182 test_expect_success
'missing < email delimiter is reported nicely' '
183 git cat-file commit HEAD >basis &&
184 sed "s/<//" basis >bad-email-2 &&
185 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
186 test_when_finished "remove_object $new" &&
187 git update-ref refs/heads/bogus "$new" &&
188 test_when_finished "git update-ref -d refs/heads/bogus" &&
189 test_must_fail git fsck 2>out &&
190 test_i18ngrep "error in commit $new.* - bad name" out
193 test_expect_success
'missing email is reported nicely' '
194 git cat-file commit HEAD >basis &&
195 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
196 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
197 test_when_finished "remove_object $new" &&
198 git update-ref refs/heads/bogus "$new" &&
199 test_when_finished "git update-ref -d refs/heads/bogus" &&
200 test_must_fail git fsck 2>out &&
201 test_i18ngrep "error in commit $new.* - missing email" out
204 test_expect_success
'> in name is reported' '
205 git cat-file commit HEAD >basis &&
206 sed "s/ </> </" basis >bad-email-4 &&
207 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
208 test_when_finished "remove_object $new" &&
209 git update-ref refs/heads/bogus "$new" &&
210 test_when_finished "git update-ref -d refs/heads/bogus" &&
211 test_must_fail git fsck 2>out &&
212 test_i18ngrep "error in commit $new" out
216 test_expect_success
'integer overflow in timestamps is reported' '
217 git cat-file commit HEAD >basis &&
218 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
219 <basis >bad-timestamp &&
220 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
221 test_when_finished "remove_object $new" &&
222 git update-ref refs/heads/bogus "$new" &&
223 test_when_finished "git update-ref -d refs/heads/bogus" &&
224 test_must_fail git fsck 2>out &&
225 test_i18ngrep "error in commit $new.*integer overflow" out
228 test_expect_success
'commit with NUL in header' '
229 git cat-file commit HEAD >basis &&
230 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
231 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
232 test_when_finished "remove_object $new" &&
233 git update-ref refs/heads/bogus "$new" &&
234 test_when_finished "git update-ref -d refs/heads/bogus" &&
235 test_must_fail git fsck 2>out &&
236 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
239 test_expect_success
'tree object with duplicate entries' '
240 test_when_finished "for i in \$T; do remove_object \$i; done" &&
242 GIT_INDEX_FILE=test-index &&
243 export GIT_INDEX_FILE &&
248 T=$(git write-tree) &&
251 git cat-file tree $T &&
254 git hash-object -w -t tree --stdin
256 test_must_fail git fsck 2>out &&
257 test_i18ngrep "error in tree .*contains duplicate file entries" out
260 test_expect_success
'tree object with dublicate names' '
261 test_when_finished "remove_object \$blob" &&
262 test_when_finished "remove_object \$tree" &&
263 test_when_finished "remove_object \$badtree" &&
264 blob=$(echo blob | git hash-object -w --stdin) &&
265 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
266 tree=$(git mktree <tree) &&
267 printf "100644 blob %s\t%s\n" $blob x.1 >badtree &&
268 printf "100644 blob %s\t%s\n" $blob x >>badtree &&
269 printf "040000 tree %s\t%s\n" $tree x >>badtree &&
270 badtree=$(git mktree <badtree) &&
271 test_must_fail git fsck 2>out &&
272 test_i18ngrep "$badtree" out &&
273 test_i18ngrep "error in tree .*contains duplicate file entries" out
276 test_expect_success
'unparseable tree object' '
277 test_oid_cache <<-\EOF &&
278 junk sha1:twenty-bytes-of-junk
279 junk sha256:twenty-bytes-of-junk-twelve-more
282 test_when_finished "git update-ref -d refs/heads/wrong" &&
283 test_when_finished "remove_object \$tree_sha1" &&
284 test_when_finished "remove_object \$commit_sha1" &&
285 junk=$(test_oid junk) &&
286 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
287 commit_sha1=$(git commit-tree $tree_sha1) &&
288 git update-ref refs/heads/wrong $commit_sha1 &&
289 test_must_fail git fsck 2>out &&
290 test_i18ngrep "error: empty filename in tree entry" out &&
291 test_i18ngrep "$tree_sha1" out &&
292 test_i18ngrep ! "fatal: empty filename in tree entry" out
295 test_expect_success
'tree entry with type mismatch' '
296 test_when_finished "remove_object \$blob" &&
297 test_when_finished "remove_object \$tree" &&
298 test_when_finished "remove_object \$commit" &&
299 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
300 blob=$(echo blob | git hash-object -w --stdin) &&
301 blob_bin=$(echo $blob | hex2oct) &&
303 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
304 git hash-object -t tree --stdin -w --literally
306 commit=$(git commit-tree $tree) &&
307 git update-ref refs/heads/type_mismatch $commit &&
308 test_must_fail git fsck >out 2>&1 &&
309 test_i18ngrep "is a blob, not a tree" out &&
310 test_i18ngrep ! "dangling blob" out
313 test_expect_success
'tag pointing to nonexistent' '
314 badoid=$(test_oid deadbeef) &&
315 cat >invalid-tag <<-EOF &&
319 tagger T A Gger <tagger@example.com> 1234567890 -0000
321 This is an invalid tag.
324 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
325 test_when_finished "remove_object $tag" &&
326 echo $tag >.git/refs/tags/invalid &&
327 test_when_finished "git update-ref -d refs/tags/invalid" &&
328 test_must_fail git fsck --tags >out &&
329 test_i18ngrep "broken link" out
332 test_expect_success
'tag pointing to something else than its type' '
333 sha=$(echo blob | git hash-object -w --stdin) &&
334 test_when_finished "remove_object $sha" &&
335 cat >wrong-tag <<-EOF &&
339 tagger T A Gger <tagger@example.com> 1234567890 -0000
341 This is an invalid tag.
344 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
345 test_when_finished "remove_object $tag" &&
346 echo $tag >.git/refs/tags/wrong &&
347 test_when_finished "git update-ref -d refs/tags/wrong" &&
348 test_must_fail git fsck --tags
351 test_expect_success
'tag with incorrect tag name & missing tagger' '
352 sha=$(git rev-parse HEAD) &&
353 cat >wrong-tag <<-EOF &&
356 tag wrong name format
358 This is an invalid tag.
361 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
362 test_when_finished "remove_object $tag" &&
363 echo $tag >.git/refs/tags/wrong &&
364 test_when_finished "git update-ref -d refs/tags/wrong" &&
365 git fsck --tags 2>out &&
367 cat >expect <<-EOF &&
368 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
369 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
371 test_i18ncmp expect out
374 test_expect_success
'tag with bad tagger' '
375 sha=$(git rev-parse HEAD) &&
376 cat >wrong-tag <<-EOF &&
380 tagger Bad Tagger Name
382 This is an invalid tag.
385 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
386 test_when_finished "remove_object $tag" &&
387 echo $tag >.git/refs/tags/wrong &&
388 test_when_finished "git update-ref -d refs/tags/wrong" &&
389 test_must_fail git fsck --tags 2>out &&
390 test_i18ngrep "error in tag .*: invalid author/committer" out
393 test_expect_success
'tag with NUL in header' '
394 sha=$(git rev-parse HEAD) &&
395 q_to_nul >tag-NUL-header <<-EOF &&
398 tag contains-Q-in-header
399 tagger T A Gger <tagger@example.com> 1234567890 -0000
401 This is an invalid tag.
404 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
405 test_when_finished "remove_object $tag" &&
406 echo $tag >.git/refs/tags/wrong &&
407 test_when_finished "git update-ref -d refs/tags/wrong" &&
408 test_must_fail git fsck --tags 2>out &&
409 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
412 test_expect_success
'cleaned up' '
413 git fsck >actual 2>&1 &&
414 test_must_be_empty actual
417 test_expect_success
'rev-list --verify-objects' '
418 git rev-list --verify-objects --all >/dev/null 2>out &&
419 test_must_be_empty out
422 test_expect_success
'rev-list --verify-objects with bad sha1' '
423 sha=$(echo blob | git hash-object -w --stdin) &&
424 old=$(test_oid_to_path $sha) &&
425 new=$(dirname $old)/$(test_oid ff_2) &&
426 sha="$(dirname $new)$(basename $new)" &&
427 mv .git/objects/$old .git/objects/$new &&
428 test_when_finished "remove_object $sha" &&
429 git update-index --add --cacheinfo 100644 $sha foo &&
430 test_when_finished "git read-tree -u --reset HEAD" &&
431 tree=$(git write-tree) &&
432 test_when_finished "remove_object $tree" &&
433 cmt=$(echo bogus | git commit-tree $tree) &&
434 test_when_finished "remove_object $cmt" &&
435 git update-ref refs/heads/bogus $cmt &&
436 test_when_finished "git update-ref -d refs/heads/bogus" &&
438 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
439 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
442 test_expect_success
'force fsck to ignore double author' '
443 git cat-file commit HEAD >basis &&
444 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
445 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
446 test_when_finished "remove_object $new" &&
447 git update-ref refs/heads/bogus "$new" &&
448 test_when_finished "git update-ref -d refs/heads/bogus" &&
449 test_must_fail git fsck &&
450 git -c fsck.multipleAuthors=ignore fsck
454 _bzoid
=$
(printf $ZERO_OID |
sed -e 's/00/\\0/g')
456 test_expect_success
'fsck notices blob entry pointing to null sha1' '
457 (git init null-blob &&
459 sha=$(printf "100644 file$_bz$_bzoid" |
460 git hash-object -w --stdin -t tree) &&
462 test_i18ngrep "warning.*null sha1" out
466 test_expect_success
'fsck notices submodule entry pointing to null sha1' '
467 (git init null-commit &&
469 sha=$(printf "160000 submodule$_bz$_bzoid" |
470 git hash-object -w --stdin -t tree) &&
472 test_i18ngrep "warning.*null sha1" out
476 while read name path pretty
; do
477 while read mode
type; do
479 test_expect_success
"fsck notices $pretty as $type" '
481 git init $name-$type &&
483 git config core.protectNTFS false &&
484 echo content >file &&
486 git commit -m base &&
487 blob=$(git rev-parse :file) &&
488 tree=$(git rev-parse HEAD^{tree}) &&
489 value=$(eval "echo \$$type") &&
490 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
491 bad_tree=$(git mktree <bad) &&
493 test_i18ngrep "warning.*tree $bad_tree" out
504 dotgit-unicode .gI${u200c}T .gI{u200c}T
508 dot-backslash-case .\\\\.GIT\\\\foobar
509 dotgit-case-backslash .git\\\\foobar
512 test_expect_success 'fsck allows .Ňit' '
514 git init not-dotgit &&
516 echo content >file &&
518 git commit -m base &&
519 blob=$(git rev-parse :file) &&
520 printf "100644 blob $blob\t.\\305\\207it" >tree &&
521 tree=$(git mktree <tree) &&
523 test_line_count = 0 err
527 test_expect_success 'NUL in commit' '
528 rm -fr nul-in-commit &&
529 git init nul-in-commit &&
532 git commit --allow-empty -m "initial commitQNUL after message" &&
533 git cat-file commit HEAD >original &&
534 q_to_nul <original >munged &&
535 git hash-object -w -t commit --stdin <munged >name &&
536 git branch bad $(cat name) &&
538 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
539 test_i18ngrep nulInCommit warn.1 &&
541 test_i18ngrep nulInCommit warn.2
545 # create a static test repo which is broken by omitting
546 # one particular object ($1, which is looked up via rev-parse
547 # in the new repository).
548 create_repo_missing () {
553 git commit -m one --allow-empty &&
555 echo content >subdir/file &&
556 git add subdir/file &&
558 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
559 git tag -m foo tag $unrelated &&
560 sha1=$(git rev-parse --verify "$1") &&
561 path=$(echo $sha1 | sed 's|..|&/|') &&
562 rm .git/objects/$path
566 test_expect_success 'fsck notices missing blob' '
567 create_repo_missing HEAD:subdir/file &&
568 test_must_fail git -C missing fsck
571 test_expect_success 'fsck notices missing subtree' '
572 create_repo_missing HEAD:subdir &&
573 test_must_fail git -C missing fsck
576 test_expect_success 'fsck notices missing root tree' '
577 create_repo_missing HEAD^{tree} &&
578 test_must_fail git -C missing fsck
581 test_expect_success 'fsck notices missing parent' '
582 create_repo_missing HEAD^ &&
583 test_must_fail git -C missing fsck
586 test_expect_success 'fsck notices missing tagged object' '
587 create_repo_missing tag^{blob} &&
588 test_must_fail git -C missing fsck
591 test_expect_success 'fsck notices ref pointing to missing commit' '
592 create_repo_missing HEAD &&
593 test_must_fail git -C missing fsck
596 test_expect_success 'fsck notices ref pointing to missing tag' '
597 create_repo_missing tag &&
598 test_must_fail git -C missing fsck
601 test_expect_success 'fsck --connectivity-only' '
602 rm -rf connectivity-only &&
603 git init connectivity-only &&
605 cd connectivity-only &&
610 # Drop the index now; we want to be sure that we
611 # recursively notice the broken objects
612 # because they are reachable from refs, not because
613 # they are in the index.
616 # corrupt the blob, but in a way that we can still identify
617 # its type. That lets us see that --connectivity-only is
618 # not actually looking at the contents, but leaves it
619 # free to examine the type if it chooses.
620 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
621 blob=$(echo unrelated | git hash-object -w --stdin) &&
622 mv -f $(sha1_file $blob) $empty &&
624 test_must_fail git fsck --strict &&
625 git fsck --strict --connectivity-only &&
626 tree=$(git rev-parse HEAD:) &&
628 tree=.git/objects/${tree%$suffix}/$suffix &&
630 echo invalid >$tree &&
631 test_must_fail git fsck --strict --connectivity-only
635 test_expect_success 'fsck --connectivity-only with explicit head' '
636 rm -rf connectivity-only &&
637 git init connectivity-only &&
639 cd connectivity-only &&
642 tree=$(git rev-parse HEAD^{tree}) &&
643 remove_object $(git rev-parse HEAD:foo.t) &&
644 test_must_fail git fsck --connectivity-only $tree
648 test_expect_success 'fsck --name-objects' '
649 rm -rf name-objects &&
650 git init name-objects &&
653 test_commit julius caesar.t &&
654 test_commit augustus &&
655 test_commit caesar &&
656 remove_object $(git rev-parse julius:caesar.t) &&
657 test_must_fail git fsck --name-objects >out &&
658 tree=$(git rev-parse --verify julius:) &&
659 test_i18ngrep "$tree (refs/tags/julius:" out
663 test_expect_success 'alternate objects are correctly blamed' '
664 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
665 name=$(test_oid numeric) &&
666 path=$(test_oid_to_path "$name") &&
667 git init --bare alt.git &&
668 echo "../../alt.git/objects" >.git/objects/info/alternates &&
669 mkdir alt.git/objects/$(dirname $path) &&
670 >alt.git/objects/$(dirname $path)/$(basename $path) &&
671 test_must_fail git fsck >out 2>&1 &&
672 test_i18ngrep alt.git out
675 test_expect_success 'fsck errors in packed objects' '
676 git cat-file commit HEAD >basis &&
677 sed "s/</one/" basis >one &&
678 sed "s/</foo/" basis >two &&
679 one=$(git hash-object -t commit -w one) &&
680 two=$(git hash-object -t commit -w two) &&
685 } | git pack-objects .git/objects/pack/pack
687 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
688 remove_object $one &&
689 remove_object $two &&
690 test_must_fail git fsck 2>out &&
691 test_i18ngrep "error in commit $one.* - bad name
" out &&
692 test_i18ngrep "error
in commit
$two.
* - bad name
" out &&
696 test_expect_success 'fsck fails on corrupt packfile' '
697 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
698 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
700 # Corrupt the first byte of the first object. (It contains 3 type bits,
701 # at least one of which is not zero, so setting the first byte to 0 is
703 chmod a+w .git/objects/pack/pack-$pack.pack &&
704 printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
706 test_when_finished "rm -f .git
/objects
/pack
/pack-
$pack.
*" &&
707 remove_object $hsh &&
708 test_must_fail git fsck 2>out &&
709 test_i18ngrep "checksum mismatch
" out
712 test_expect_success 'fsck finds problems in duplicate loose objects' '
713 rm -rf broken-duplicate &&
714 git init broken-duplicate &&
716 cd broken-duplicate &&
717 test_commit duplicate &&
718 # no "-d" here, so we end up with duplicates
720 # now corrupt the loose copy
721 file=$(sha1_file "$
(git rev-parse HEAD
)") &&
723 echo broken >"$file" &&
724 test_must_fail git fsck
728 test_expect_success 'fsck detects trailing loose garbage (commit)' '
729 git cat-file commit HEAD >basis &&
730 echo bump-commit-sha1 >>basis &&
731 commit=$(git hash-object -w -t commit basis) &&
732 file=$(sha1_file $commit) &&
733 test_when_finished "remove_object
$commit" &&
735 echo garbage >>"$file" &&
736 test_must_fail git fsck 2>out &&
737 test_i18ngrep "garbage.
*$commit" out
740 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
741 blob=$(echo trailing | git hash-object -w --stdin) &&
742 file=$(sha1_file $blob) &&
743 test_when_finished "remove_object
$blob" &&
745 echo garbage >>"$file" &&
746 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
747 test_i18ngrep "garbage.
*$blob" out
750 test_expect_success 'fsck detects truncated loose object' '
751 # make it big enough that we know we will truncate in the data
752 # portion, not the header
753 test-tool genrandom truncate 4096 >file &&
754 blob=$(git hash-object -w file) &&
755 file=$(sha1_file $blob) &&
756 test_when_finished "remove_object
$blob" &&
757 test_copy_bytes 1024 <"$file" >tmp &&
761 # check both regular and streaming code paths
762 test_must_fail git fsck 2>out &&
763 test_i18ngrep corrupt.*$blob out &&
765 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
766 test_i18ngrep corrupt.*$blob out
769 # for each of type, we have one version which is referenced by another object
770 # (and so while unreachable, not dangling), and another variant which really is
772 test_expect_success 'create dangling-object repository' '
776 blob=$(echo not-dangling | git hash-object -w --stdin) &&
777 dblob=$(echo dangling | git hash-object -w --stdin) &&
778 tree=$(printf "100644 blob
%s
\t%s
\n" $blob one | git mktree) &&
779 dtree=$(printf "100644 blob
%s
\t%s
\n" $blob two | git mktree) &&
780 commit=$(git commit-tree $tree) &&
781 dcommit=$(git commit-tree -p $commit $tree) &&
785 dangling commit $dcommit
791 test_expect_success 'fsck notices dangling objects' '
795 # the output order is non-deterministic, as it comes from a hash
796 sort <actual >actual.sorted &&
797 test_i18ncmp expect actual.sorted
801 test_expect_success 'fsck --connectivity-only notices dangling objects' '
804 git fsck --connectivity-only >actual &&
805 # the output order is non-deterministic, as it comes from a hash
806 sort <actual >actual.sorted &&
807 test_i18ncmp expect actual.sorted
811 test_expect_success 'fsck $name notices bogus $name' '
812 test_must_fail git fsck bogus &&
813 test_must_fail git fsck $ZERO_OID
816 test_expect_success 'bogus head does not fallback to all heads' '
817 # set up a case that will cause a reachability complaint
818 echo to-be-deleted >foo &&
820 blob=$(git rev-parse :foo) &&
821 test_when_finished "git
rm --cached foo
" &&
822 remove_object $blob &&
823 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
827 # Corrupt the checksum on the index.
828 # Add 1 to the last byte in the SHA.
829 corrupt_index_checksum () {
832 open my $fh, "+<", ".git
/index
" or die "open
: $
!";
834 seek $fh, -1, SEEK_END or die "seek
: $
!";
835 read $fh, my $in_byte, 1 or die "read: $
!";
837 $in_value = unpack("C
", $in_byte);
838 $out_value = ($in_value + 1) & 255;
840 $out_byte = pack("C
", $out_value);
842 seek $fh, -1, SEEK_END or die "seek
: $
!";
844 close $fh or die "close
: $
!";
848 # Corrupt the checksum on the index and then
849 # verify that only fsck notices.
850 test_expect_success 'detect corrupt index file in fsck' '
851 cp .git/index .git/index.backup &&
852 test_when_finished "mv .git
/index.backup .git
/index
" &&
853 corrupt_index_checksum &&
854 test_must_fail git fsck --cache 2>errors &&
855 test_i18ngrep "bad index
file" errors