Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t1450-fsck.sh
blobab7f31f1dcd5b5b7224ded4099f8316171bdea7d
1 #!/bin/sh
3 test_description='git fsck random collection of tests
5 * (HEAD) B
6 * (main) A
9 . ./test-lib.sh
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 &&
18 git tag -d A B &&
19 git reflog expire --expire=now --all
22 test_expect_success 'loose objects borrowed from alternate are not missing' '
23 mkdir another &&
25 cd another &&
26 git init &&
27 echo ../../../.git/objects >.git/objects/info/alternates &&
28 test_commit C fileC one &&
29 git fsck --no-dangling >../actual 2>&1
30 ) &&
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
41 # it.
43 sha1_file () {
44 git rev-parse --git-path objects/$(test_oid_to_path "$1")
47 remove_object () {
48 rm "$(sha1_file "$1")"
51 test_expect_success 'object with hash mismatch' '
52 git init --bare hash-mismatch &&
54 cd hash-mismatch &&
56 oid=$(echo blob | git hash-object -w --stdin) &&
57 oldoid=$oid &&
58 old=$(test_oid_to_path "$oid") &&
59 new=$(dirname $old)/$(test_oid ff_2) &&
60 oid="$(dirname $new)$(basename $new)" &&
62 mv objects/$old objects/$new &&
63 git update-index --add --cacheinfo 100644 $oid foo &&
64 tree=$(git write-tree) &&
65 cmt=$(echo bogus | git commit-tree $tree) &&
66 git update-ref refs/heads/bogus $cmt &&
68 test_must_fail git fsck 2>out &&
69 grep "$oldoid: hash-path mismatch, found at: .*$new" out
73 test_expect_success 'object with hash and type mismatch' '
74 git init --bare hash-type-mismatch &&
76 cd hash-type-mismatch &&
78 oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) &&
79 oldoid=$oid &&
80 old=$(test_oid_to_path "$oid") &&
81 new=$(dirname $old)/$(test_oid ff_2) &&
82 oid="$(dirname $new)$(basename $new)" &&
84 mv objects/$old objects/$new &&
85 git update-index --add --cacheinfo 100644 $oid foo &&
86 tree=$(git write-tree) &&
87 cmt=$(echo bogus | git commit-tree $tree) &&
88 git update-ref refs/heads/bogus $cmt &&
91 test_must_fail git fsck 2>out &&
92 grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
93 grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
97 test_expect_success 'zlib corrupt loose object output ' '
98 git init --bare corrupt-loose-output &&
100 cd corrupt-loose-output &&
101 oid=$(git hash-object -w --stdin --literally </dev/null) &&
102 oidf=objects/$(test_oid_to_path "$oid") &&
103 chmod +w $oidf &&
104 echo extra garbage >>$oidf &&
106 cat >expect.error <<-EOF &&
107 error: garbage at end of loose object '\''$oid'\''
108 error: unable to unpack contents of ./$oidf
109 error: $oid: object corrupt or missing: ./$oidf
111 test_must_fail git fsck 2>actual &&
112 grep ^error: actual >error &&
113 test_cmp expect.error error
117 test_expect_success 'branch pointing to non-commit' '
118 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
119 test_when_finished "git update-ref -d refs/heads/invalid" &&
120 test_must_fail git fsck 2>out &&
121 test_i18ngrep "not a commit" out
124 test_expect_success 'HEAD link pointing at a funny object' '
125 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
126 mv .git/HEAD .git/SAVED_HEAD &&
127 echo $ZERO_OID >.git/HEAD &&
128 # avoid corrupt/broken HEAD from interfering with repo discovery
129 test_must_fail env GIT_DIR=.git git fsck 2>out &&
130 test_i18ngrep "detached HEAD points" out
133 test_expect_success 'HEAD link pointing at a funny place' '
134 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
135 mv .git/HEAD .git/SAVED_HEAD &&
136 echo "ref: refs/funny/place" >.git/HEAD &&
137 # avoid corrupt/broken HEAD from interfering with repo discovery
138 test_must_fail env GIT_DIR=.git git fsck 2>out &&
139 test_i18ngrep "HEAD points to something strange" out
142 test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
143 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
144 test_when_finished "rm -rf .git/worktrees wt" &&
145 git worktree add wt &&
146 mv .git/HEAD .git/SAVED_HEAD &&
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_i18ngrep "main-worktree/HEAD: detached HEAD points" out
153 test_expect_success 'other worktree HEAD link pointing at a funny object' '
154 test_when_finished "rm -rf .git/worktrees other" &&
155 git worktree add other &&
156 echo $ZERO_OID >.git/worktrees/other/HEAD &&
157 test_must_fail git fsck 2>out &&
158 test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out
161 test_expect_success 'other worktree HEAD link pointing at missing object' '
162 test_when_finished "rm -rf .git/worktrees other" &&
163 git worktree add other &&
164 echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
165 test_must_fail git fsck 2>out &&
166 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
169 test_expect_success 'other worktree HEAD link pointing at a funny place' '
170 test_when_finished "rm -rf .git/worktrees other" &&
171 git worktree add other &&
172 echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
173 test_must_fail git fsck 2>out &&
174 test_i18ngrep "worktrees/other/HEAD points to something strange" out
177 test_expect_success 'commit with multiple signatures is okay' '
178 git cat-file commit HEAD >basis &&
179 cat >sigs <<-EOF &&
180 gpgsig -----BEGIN PGP SIGNATURE-----
181 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
182 -----END PGP SIGNATURE-----
183 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
184 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
185 -----END PGP SIGNATURE-----
187 sed -e "/^committer/q" basis >okay &&
188 cat sigs >>okay &&
189 echo >>okay &&
190 sed -e "1,/^$/d" basis >>okay &&
191 cat okay &&
192 new=$(git hash-object -t commit -w --stdin <okay) &&
193 test_when_finished "remove_object $new" &&
194 git update-ref refs/heads/bogus "$new" &&
195 test_when_finished "git update-ref -d refs/heads/bogus" &&
196 git fsck 2>out &&
197 cat out &&
198 ! grep "commit $new" out
201 test_expect_success 'email without @ is okay' '
202 git cat-file commit HEAD >basis &&
203 sed "s/@/AT/" basis >okay &&
204 new=$(git hash-object -t commit -w --stdin <okay) &&
205 test_when_finished "remove_object $new" &&
206 git update-ref refs/heads/bogus "$new" &&
207 test_when_finished "git update-ref -d refs/heads/bogus" &&
208 git fsck 2>out &&
209 ! grep "commit $new" out
212 test_expect_success 'email with embedded > is not okay' '
213 git cat-file commit HEAD >basis &&
214 sed "s/@[a-z]/&>/" basis >bad-email &&
215 new=$(git hash-object -t commit -w --stdin <bad-email) &&
216 test_when_finished "remove_object $new" &&
217 git update-ref refs/heads/bogus "$new" &&
218 test_when_finished "git update-ref -d refs/heads/bogus" &&
219 test_must_fail git fsck 2>out &&
220 test_i18ngrep "error in commit $new" out
223 test_expect_success 'missing < email delimiter is reported nicely' '
224 git cat-file commit HEAD >basis &&
225 sed "s/<//" basis >bad-email-2 &&
226 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
227 test_when_finished "remove_object $new" &&
228 git update-ref refs/heads/bogus "$new" &&
229 test_when_finished "git update-ref -d refs/heads/bogus" &&
230 test_must_fail git fsck 2>out &&
231 test_i18ngrep "error in commit $new.* - bad name" out
234 test_expect_success 'missing email is reported nicely' '
235 git cat-file commit HEAD >basis &&
236 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
237 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
238 test_when_finished "remove_object $new" &&
239 git update-ref refs/heads/bogus "$new" &&
240 test_when_finished "git update-ref -d refs/heads/bogus" &&
241 test_must_fail git fsck 2>out &&
242 test_i18ngrep "error in commit $new.* - missing email" out
245 test_expect_success '> in name is reported' '
246 git cat-file commit HEAD >basis &&
247 sed "s/ </> </" basis >bad-email-4 &&
248 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
249 test_when_finished "remove_object $new" &&
250 git update-ref refs/heads/bogus "$new" &&
251 test_when_finished "git update-ref -d refs/heads/bogus" &&
252 test_must_fail git fsck 2>out &&
253 test_i18ngrep "error in commit $new" out
256 # date is 2^64 + 1
257 test_expect_success 'integer overflow in timestamps is reported' '
258 git cat-file commit HEAD >basis &&
259 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
260 <basis >bad-timestamp &&
261 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
262 test_when_finished "remove_object $new" &&
263 git update-ref refs/heads/bogus "$new" &&
264 test_when_finished "git update-ref -d refs/heads/bogus" &&
265 test_must_fail git fsck 2>out &&
266 test_i18ngrep "error in commit $new.*integer overflow" out
269 test_expect_success 'commit with NUL in header' '
270 git cat-file commit HEAD >basis &&
271 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
272 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
273 test_when_finished "remove_object $new" &&
274 git update-ref refs/heads/bogus "$new" &&
275 test_when_finished "git update-ref -d refs/heads/bogus" &&
276 test_must_fail git fsck 2>out &&
277 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
280 test_expect_success 'tree object with duplicate entries' '
281 test_when_finished "for i in \$T; do remove_object \$i; done" &&
282 T=$(
283 GIT_INDEX_FILE=test-index &&
284 export GIT_INDEX_FILE &&
285 rm -f test-index &&
286 >x &&
287 git add x &&
288 git rev-parse :x &&
289 T=$(git write-tree) &&
290 echo $T &&
292 git cat-file tree $T &&
293 git cat-file tree $T
295 git hash-object -w -t tree --stdin
296 ) &&
297 test_must_fail git fsck 2>out &&
298 test_i18ngrep "error in tree .*contains duplicate file entries" out
301 check_duplicate_names () {
302 expect=$1 &&
303 shift &&
304 names=$@ &&
305 test_expect_$expect "tree object with duplicate names: $names" '
306 test_when_finished "remove_object \$blob" &&
307 test_when_finished "remove_object \$tree" &&
308 test_when_finished "remove_object \$badtree" &&
309 blob=$(echo blob | git hash-object -w --stdin) &&
310 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
311 tree=$(git mktree <tree) &&
312 for name in $names
314 case "$name" in
315 */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
316 *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
317 esac
318 done >badtree &&
319 badtree=$(git mktree <badtree) &&
320 test_must_fail git fsck 2>out &&
321 test_i18ngrep "$badtree" out &&
322 test_i18ngrep "error in tree .*contains duplicate file entries" out
326 check_duplicate_names success x x.1 x/
327 check_duplicate_names success x x.1.2 x.1/ x/
328 check_duplicate_names success x x.1 x.1.2 x/
330 test_expect_success 'unparseable tree object' '
331 test_oid_cache <<-\EOF &&
332 junk sha1:twenty-bytes-of-junk
333 junk sha256:twenty-bytes-of-junk-twelve-more
336 test_when_finished "git update-ref -d refs/heads/wrong" &&
337 test_when_finished "remove_object \$tree_sha1" &&
338 test_when_finished "remove_object \$commit_sha1" &&
339 junk=$(test_oid junk) &&
340 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
341 commit_sha1=$(git commit-tree $tree_sha1) &&
342 git update-ref refs/heads/wrong $commit_sha1 &&
343 test_must_fail git fsck 2>out &&
344 test_i18ngrep "error: empty filename in tree entry" out &&
345 test_i18ngrep "$tree_sha1" out &&
346 test_i18ngrep ! "fatal: empty filename in tree entry" out
349 test_expect_success 'tree entry with type mismatch' '
350 test_when_finished "remove_object \$blob" &&
351 test_when_finished "remove_object \$tree" &&
352 test_when_finished "remove_object \$commit" &&
353 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
354 blob=$(echo blob | git hash-object -w --stdin) &&
355 blob_bin=$(echo $blob | hex2oct) &&
356 tree=$(
357 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
358 git hash-object -t tree --stdin -w --literally
359 ) &&
360 commit=$(git commit-tree $tree) &&
361 git update-ref refs/heads/type_mismatch $commit &&
362 test_must_fail git fsck >out 2>&1 &&
363 test_i18ngrep "is a blob, not a tree" out &&
364 test_i18ngrep ! "dangling blob" out
367 test_expect_success 'tag pointing to nonexistent' '
368 badoid=$(test_oid deadbeef) &&
369 cat >invalid-tag <<-EOF &&
370 object $badoid
371 type commit
372 tag invalid
373 tagger T A Gger <tagger@example.com> 1234567890 -0000
375 This is an invalid tag.
378 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
379 test_when_finished "remove_object $tag" &&
380 echo $tag >.git/refs/tags/invalid &&
381 test_when_finished "git update-ref -d refs/tags/invalid" &&
382 test_must_fail git fsck --tags >out &&
383 test_i18ngrep "broken link" out
386 test_expect_success 'tag pointing to something else than its type' '
387 sha=$(echo blob | git hash-object -w --stdin) &&
388 test_when_finished "remove_object $sha" &&
389 cat >wrong-tag <<-EOF &&
390 object $sha
391 type commit
392 tag wrong
393 tagger T A Gger <tagger@example.com> 1234567890 -0000
395 This is an invalid tag.
398 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
399 test_when_finished "remove_object $tag" &&
400 echo $tag >.git/refs/tags/wrong &&
401 test_when_finished "git update-ref -d refs/tags/wrong" &&
402 test_must_fail git fsck --tags
405 test_expect_success 'tag with incorrect tag name & missing tagger' '
406 sha=$(git rev-parse HEAD) &&
407 cat >wrong-tag <<-EOF &&
408 object $sha
409 type commit
410 tag wrong name format
412 This is an invalid tag.
415 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
416 test_when_finished "remove_object $tag" &&
417 echo $tag >.git/refs/tags/wrong &&
418 test_when_finished "git update-ref -d refs/tags/wrong" &&
419 git fsck --tags 2>out &&
421 cat >expect <<-EOF &&
422 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
423 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
425 test_cmp expect out
428 test_expect_success 'tag with bad tagger' '
429 sha=$(git rev-parse HEAD) &&
430 cat >wrong-tag <<-EOF &&
431 object $sha
432 type commit
433 tag not-quite-wrong
434 tagger Bad Tagger Name
436 This is an invalid tag.
439 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
440 test_when_finished "remove_object $tag" &&
441 echo $tag >.git/refs/tags/wrong &&
442 test_when_finished "git update-ref -d refs/tags/wrong" &&
443 test_must_fail git fsck --tags 2>out &&
444 test_i18ngrep "error in tag .*: invalid author/committer" out
447 test_expect_success 'tag with NUL in header' '
448 sha=$(git rev-parse HEAD) &&
449 q_to_nul >tag-NUL-header <<-EOF &&
450 object $sha
451 type commit
452 tag contains-Q-in-header
453 tagger T A Gger <tagger@example.com> 1234567890 -0000
455 This is an invalid tag.
458 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
459 test_when_finished "remove_object $tag" &&
460 echo $tag >.git/refs/tags/wrong &&
461 test_when_finished "git update-ref -d refs/tags/wrong" &&
462 test_must_fail git fsck --tags 2>out &&
463 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
466 test_expect_success 'cleaned up' '
467 git fsck >actual 2>&1 &&
468 test_must_be_empty actual
471 test_expect_success 'rev-list --verify-objects' '
472 git rev-list --verify-objects --all >/dev/null 2>out &&
473 test_must_be_empty out
476 test_expect_success 'rev-list --verify-objects with bad sha1' '
477 sha=$(echo blob | git hash-object -w --stdin) &&
478 old=$(test_oid_to_path $sha) &&
479 new=$(dirname $old)/$(test_oid ff_2) &&
480 sha="$(dirname $new)$(basename $new)" &&
481 mv .git/objects/$old .git/objects/$new &&
482 test_when_finished "remove_object $sha" &&
483 git update-index --add --cacheinfo 100644 $sha foo &&
484 test_when_finished "git read-tree -u --reset HEAD" &&
485 tree=$(git write-tree) &&
486 test_when_finished "remove_object $tree" &&
487 cmt=$(echo bogus | git commit-tree $tree) &&
488 test_when_finished "remove_object $cmt" &&
489 git update-ref refs/heads/bogus $cmt &&
490 test_when_finished "git update-ref -d refs/heads/bogus" &&
492 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
493 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
496 test_expect_success 'force fsck to ignore double author' '
497 git cat-file commit HEAD >basis &&
498 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
499 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
500 test_when_finished "remove_object $new" &&
501 git update-ref refs/heads/bogus "$new" &&
502 test_when_finished "git update-ref -d refs/heads/bogus" &&
503 test_must_fail git fsck &&
504 git -c fsck.multipleAuthors=ignore fsck
507 _bz='\0'
508 _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
510 test_expect_success 'fsck notices blob entry pointing to null sha1' '
511 (git init null-blob &&
512 cd null-blob &&
513 sha=$(printf "100644 file$_bz$_bzoid" |
514 git hash-object -w --stdin -t tree) &&
515 git fsck 2>out &&
516 test_i18ngrep "warning.*null sha1" out
520 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
521 (git init null-commit &&
522 cd null-commit &&
523 sha=$(printf "160000 submodule$_bz$_bzoid" |
524 git hash-object -w --stdin -t tree) &&
525 git fsck 2>out &&
526 test_i18ngrep "warning.*null sha1" out
530 while read name path pretty; do
531 while read mode type; do
532 : ${pretty:=$path}
533 test_expect_success "fsck notices $pretty as $type" '
535 git init $name-$type &&
536 cd $name-$type &&
537 git config core.protectNTFS false &&
538 echo content >file &&
539 git add file &&
540 git commit -m base &&
541 blob=$(git rev-parse :file) &&
542 tree=$(git rev-parse HEAD^{tree}) &&
543 value=$(eval "echo \$$type") &&
544 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
545 bad_tree=$(git mktree <bad) &&
546 git fsck 2>out &&
547 test_i18ngrep "warning.*tree $bad_tree" out
549 done <<-\EOF
550 100644 blob
551 040000 tree
553 done <<-EOF
554 dot .
555 dotdot ..
556 dotgit .git
557 dotgit-case .GIT
558 dotgit-unicode .gI${u200c}T .gI{u200c}T
559 dotgit-case2 .Git
560 git-tilde1 git~1
561 dotgitdot .git.
562 dot-backslash-case .\\\\.GIT\\\\foobar
563 dotgit-case-backslash .git\\\\foobar
566 test_expect_success 'fsck allows .Ňit' '
568 git init not-dotgit &&
569 cd not-dotgit &&
570 echo content >file &&
571 git add file &&
572 git commit -m base &&
573 blob=$(git rev-parse :file) &&
574 printf "100644 blob $blob\t.\\305\\207it" >tree &&
575 tree=$(git mktree <tree) &&
576 git fsck 2>err &&
577 test_line_count = 0 err
581 test_expect_success 'NUL in commit' '
582 rm -fr nul-in-commit &&
583 git init nul-in-commit &&
585 cd nul-in-commit &&
586 git commit --allow-empty -m "initial commitQNUL after message" &&
587 git cat-file commit HEAD >original &&
588 q_to_nul <original >munged &&
589 git hash-object -w -t commit --stdin <munged >name &&
590 git branch bad $(cat name) &&
592 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
593 test_i18ngrep nulInCommit warn.1 &&
594 git fsck 2>warn.2 &&
595 test_i18ngrep nulInCommit warn.2
599 # create a static test repo which is broken by omitting
600 # one particular object ($1, which is looked up via rev-parse
601 # in the new repository).
602 create_repo_missing () {
603 rm -rf missing &&
604 git init missing &&
606 cd missing &&
607 git commit -m one --allow-empty &&
608 mkdir subdir &&
609 echo content >subdir/file &&
610 git add subdir/file &&
611 git commit -m two &&
612 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
613 git tag -m foo tag $unrelated &&
614 sha1=$(git rev-parse --verify "$1") &&
615 path=$(echo $sha1 | sed 's|..|&/|') &&
616 rm .git/objects/$path
620 test_expect_success 'fsck notices missing blob' '
621 create_repo_missing HEAD:subdir/file &&
622 test_must_fail git -C missing fsck
625 test_expect_success 'fsck notices missing subtree' '
626 create_repo_missing HEAD:subdir &&
627 test_must_fail git -C missing fsck
630 test_expect_success 'fsck notices missing root tree' '
631 create_repo_missing HEAD^{tree} &&
632 test_must_fail git -C missing fsck
635 test_expect_success 'fsck notices missing parent' '
636 create_repo_missing HEAD^ &&
637 test_must_fail git -C missing fsck
640 test_expect_success 'fsck notices missing tagged object' '
641 create_repo_missing tag^{blob} &&
642 test_must_fail git -C missing fsck
645 test_expect_success 'fsck notices ref pointing to missing commit' '
646 create_repo_missing HEAD &&
647 test_must_fail git -C missing fsck
650 test_expect_success 'fsck notices ref pointing to missing tag' '
651 create_repo_missing tag &&
652 test_must_fail git -C missing fsck
655 test_expect_success 'fsck --connectivity-only' '
656 rm -rf connectivity-only &&
657 git init connectivity-only &&
659 cd connectivity-only &&
660 touch empty &&
661 git add empty &&
662 test_commit empty &&
664 # Drop the index now; we want to be sure that we
665 # recursively notice the broken objects
666 # because they are reachable from refs, not because
667 # they are in the index.
668 rm -f .git/index &&
670 # corrupt the blob, but in a way that we can still identify
671 # its type. That lets us see that --connectivity-only is
672 # not actually looking at the contents, but leaves it
673 # free to examine the type if it chooses.
674 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
675 blob=$(echo unrelated | git hash-object -w --stdin) &&
676 mv -f $(sha1_file $blob) $empty &&
678 test_must_fail git fsck --strict &&
679 git fsck --strict --connectivity-only &&
680 tree=$(git rev-parse HEAD:) &&
681 suffix=${tree#??} &&
682 tree=.git/objects/${tree%$suffix}/$suffix &&
683 rm -f $tree &&
684 echo invalid >$tree &&
685 test_must_fail git fsck --strict --connectivity-only
689 test_expect_success 'fsck --connectivity-only with explicit head' '
690 rm -rf connectivity-only &&
691 git init connectivity-only &&
693 cd connectivity-only &&
694 test_commit foo &&
695 rm -f .git/index &&
696 tree=$(git rev-parse HEAD^{tree}) &&
697 remove_object $(git rev-parse HEAD:foo.t) &&
698 test_must_fail git fsck --connectivity-only $tree
702 test_expect_success 'fsck --name-objects' '
703 rm -rf name-objects &&
704 git init name-objects &&
706 cd name-objects &&
707 git config core.logAllRefUpdates false &&
708 test_commit julius caesar.t &&
709 test_commit augustus44 &&
710 test_commit caesar &&
711 remove_object $(git rev-parse julius:caesar.t) &&
712 tree=$(git rev-parse --verify julius:) &&
713 git tag -d julius &&
714 test_must_fail git fsck --name-objects >out &&
715 test_i18ngrep "$tree (refs/tags/augustus44\\^:" out
719 test_expect_success 'alternate objects are correctly blamed' '
720 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
721 name=$(test_oid numeric) &&
722 path=$(test_oid_to_path "$name") &&
723 git init --bare alt.git &&
724 echo "../../alt.git/objects" >.git/objects/info/alternates &&
725 mkdir alt.git/objects/$(dirname $path) &&
726 >alt.git/objects/$(dirname $path)/$(basename $path) &&
727 test_must_fail git fsck >out 2>&1 &&
728 test_i18ngrep alt.git out
731 test_expect_success 'fsck errors in packed objects' '
732 git cat-file commit HEAD >basis &&
733 sed "s/</one/" basis >one &&
734 sed "s/</foo/" basis >two &&
735 one=$(git hash-object -t commit -w one) &&
736 two=$(git hash-object -t commit -w two) &&
737 pack=$(
739 echo $one &&
740 echo $two
741 } | git pack-objects .git/objects/pack/pack
742 ) &&
743 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
744 remove_object $one &&
745 remove_object $two &&
746 test_must_fail git fsck 2>out &&
747 test_i18ngrep "error in commit $one.* - bad name" out &&
748 test_i18ngrep "error in commit $two.* - bad name" out &&
749 ! grep corrupt out
752 test_expect_success 'fsck fails on corrupt packfile' '
753 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
754 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
756 # Corrupt the first byte of the first object. (It contains 3 type bits,
757 # at least one of which is not zero, so setting the first byte to 0 is
758 # sufficient.)
759 chmod a+w .git/objects/pack/pack-$pack.pack &&
760 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
762 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
763 remove_object $hsh &&
764 test_must_fail git fsck 2>out &&
765 test_i18ngrep "checksum mismatch" out
768 test_expect_success 'fsck finds problems in duplicate loose objects' '
769 rm -rf broken-duplicate &&
770 git init broken-duplicate &&
772 cd broken-duplicate &&
773 test_commit duplicate &&
774 # no "-d" here, so we end up with duplicates
775 git repack &&
776 # now corrupt the loose copy
777 oid="$(git rev-parse HEAD)" &&
778 file=$(sha1_file "$oid") &&
779 rm "$file" &&
780 echo broken >"$file" &&
781 test_must_fail git fsck 2>err &&
783 cat >expect <<-EOF &&
784 error: inflate: data stream error (incorrect header check)
785 error: unable to unpack header of $file
786 error: $oid: object corrupt or missing: $file
788 grep "^error: " err >actual &&
789 test_cmp expect actual
793 test_expect_success 'fsck detects trailing loose garbage (commit)' '
794 git cat-file commit HEAD >basis &&
795 echo bump-commit-sha1 >>basis &&
796 commit=$(git hash-object -w -t commit basis) &&
797 file=$(sha1_file $commit) &&
798 test_when_finished "remove_object $commit" &&
799 chmod +w "$file" &&
800 echo garbage >>"$file" &&
801 test_must_fail git fsck 2>out &&
802 test_i18ngrep "garbage.*$commit" out
805 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
806 blob=$(echo trailing | git hash-object -w --stdin) &&
807 file=$(sha1_file $blob) &&
808 test_when_finished "remove_object $blob" &&
809 chmod +w "$file" &&
810 echo garbage >>"$file" &&
811 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
812 test_i18ngrep "garbage.*$blob" out
815 test_expect_success 'fsck detects truncated loose object' '
816 # make it big enough that we know we will truncate in the data
817 # portion, not the header
818 test-tool genrandom truncate 4096 >file &&
819 blob=$(git hash-object -w file) &&
820 file=$(sha1_file $blob) &&
821 test_when_finished "remove_object $blob" &&
822 test_copy_bytes 1024 <"$file" >tmp &&
823 rm "$file" &&
824 mv -f tmp "$file" &&
826 # check both regular and streaming code paths
827 test_must_fail git fsck 2>out &&
828 test_i18ngrep corrupt.*$blob out &&
830 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
831 test_i18ngrep corrupt.*$blob out
834 # for each of type, we have one version which is referenced by another object
835 # (and so while unreachable, not dangling), and another variant which really is
836 # dangling.
837 test_expect_success 'create dangling-object repository' '
838 git init dangling &&
840 cd dangling &&
841 blob=$(echo not-dangling | git hash-object -w --stdin) &&
842 dblob=$(echo dangling | git hash-object -w --stdin) &&
843 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
844 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
845 commit=$(git commit-tree $tree) &&
846 dcommit=$(git commit-tree -p $commit $tree) &&
848 cat >expect <<-EOF
849 dangling blob $dblob
850 dangling commit $dcommit
851 dangling tree $dtree
856 test_expect_success 'fsck notices dangling objects' '
858 cd dangling &&
859 git fsck >actual &&
860 # the output order is non-deterministic, as it comes from a hash
861 sort <actual >actual.sorted &&
862 test_cmp expect actual.sorted
866 test_expect_success 'fsck --connectivity-only notices dangling objects' '
868 cd dangling &&
869 git fsck --connectivity-only >actual &&
870 # the output order is non-deterministic, as it comes from a hash
871 sort <actual >actual.sorted &&
872 test_cmp expect actual.sorted
876 test_expect_success 'fsck $name notices bogus $name' '
877 test_must_fail git fsck bogus &&
878 test_must_fail git fsck $ZERO_OID
881 test_expect_success 'bogus head does not fallback to all heads' '
882 # set up a case that will cause a reachability complaint
883 echo to-be-deleted >foo &&
884 git add foo &&
885 blob=$(git rev-parse :foo) &&
886 test_when_finished "git rm --cached foo" &&
887 remove_object $blob &&
888 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
889 ! grep $blob out
892 # Corrupt the checksum on the index.
893 # Add 1 to the last byte in the SHA.
894 corrupt_index_checksum () {
895 perl -w -e '
896 use Fcntl ":seek";
897 open my $fh, "+<", ".git/index" or die "open: $!";
898 binmode $fh;
899 seek $fh, -1, SEEK_END or die "seek: $!";
900 read $fh, my $in_byte, 1 or die "read: $!";
902 $in_value = unpack("C", $in_byte);
903 $out_value = ($in_value + 1) & 255;
905 $out_byte = pack("C", $out_value);
907 seek $fh, -1, SEEK_END or die "seek: $!";
908 print $fh $out_byte;
909 close $fh or die "close: $!";
913 # Corrupt the checksum on the index and then
914 # verify that only fsck notices.
915 test_expect_success 'detect corrupt index file in fsck' '
916 cp .git/index .git/index.backup &&
917 test_when_finished "mv .git/index.backup .git/index" &&
918 corrupt_index_checksum &&
919 test_must_fail git fsck --cache 2>errors &&
920 test_i18ngrep "bad index file" errors
923 test_expect_success 'fsck error and recovery on invalid object type' '
924 git init --bare garbage-type &&
926 cd garbage-type &&
928 garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
930 cat >err.expect <<-\EOF &&
931 fatal: invalid object type
933 test_must_fail git fsck >out 2>err &&
934 grep -e "^error" -e "^fatal" err >errors &&
935 test_line_count = 1 errors &&
936 grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err
940 test_done