Start the 2.46 cycle
[alt-git.git] / t / t1450-fsck.sh
blob8a456b1142d1cc463f97c5e8809bcfc83ffa1e96
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 orig_head=$(git rev-parse HEAD) &&
19 git tag -d A B &&
20 git reflog expire --expire=now --all
23 test_expect_success 'loose objects borrowed from alternate are not missing' '
24 mkdir another &&
26 cd another &&
27 git init &&
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
30 git fsck --no-dangling >../actual 2>&1
31 ) &&
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
42 # it.
44 sha1_file () {
45 git rev-parse --git-path objects/$(test_oid_to_path "$1")
48 remove_object () {
49 rm "$(sha1_file "$1")"
52 test_expect_success 'object with hash mismatch' '
53 git init --bare hash-mismatch &&
55 cd hash-mismatch &&
57 oid=$(echo blob | git hash-object -w --stdin) &&
58 oldoid=$oid &&
59 old=$(test_oid_to_path "$oid") &&
60 new=$(dirname $old)/$(test_oid ff_2) &&
61 oid="$(dirname $new)$(basename $new)" &&
63 mv objects/$old objects/$new &&
64 git update-index --add --cacheinfo 100644 $oid foo &&
65 tree=$(git write-tree) &&
66 cmt=$(echo bogus | git commit-tree $tree) &&
67 git update-ref refs/heads/bogus $cmt &&
69 test_must_fail git fsck 2>out &&
70 grep "$oldoid: hash-path mismatch, found at: .*$new" out
74 test_expect_success 'object with hash and type mismatch' '
75 git init --bare hash-type-mismatch &&
77 cd hash-type-mismatch &&
79 oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) &&
80 oldoid=$oid &&
81 old=$(test_oid_to_path "$oid") &&
82 new=$(dirname $old)/$(test_oid ff_2) &&
83 oid="$(dirname $new)$(basename $new)" &&
85 mv objects/$old objects/$new &&
86 git update-index --add --cacheinfo 100644 $oid foo &&
87 tree=$(git write-tree) &&
88 cmt=$(echo bogus | git commit-tree $tree) &&
89 git update-ref refs/heads/bogus $cmt &&
92 test_must_fail git fsck 2>out &&
93 grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
94 grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
98 test_expect_success 'zlib corrupt loose object output ' '
99 git init --bare corrupt-loose-output &&
101 cd corrupt-loose-output &&
102 oid=$(git hash-object -w --stdin --literally </dev/null) &&
103 oidf=objects/$(test_oid_to_path "$oid") &&
104 chmod +w $oidf &&
105 echo extra garbage >>$oidf &&
107 cat >expect.error <<-EOF &&
108 error: garbage at end of loose object '\''$oid'\''
109 error: unable to unpack contents of ./$oidf
110 error: $oid: object corrupt or missing: ./$oidf
112 test_must_fail git fsck 2>actual &&
113 grep ^error: actual >error &&
114 test_cmp expect.error error
118 test_expect_success 'branch pointing to non-commit' '
119 tree_oid=$(git rev-parse --verify HEAD^{tree}) &&
120 test_when_finished "git update-ref -d refs/heads/invalid" &&
121 test-tool ref-store main update-ref msg refs/heads/invalid $tree_oid $ZERO_OID REF_SKIP_OID_VERIFICATION &&
122 test_must_fail git fsck 2>out &&
123 test_grep "not a commit" out
126 test_expect_success REFFILES 'HEAD link pointing at a funny object' '
127 test_when_finished "git update-ref HEAD $orig_head" &&
128 echo $ZERO_OID >.git/HEAD &&
129 # avoid corrupt/broken HEAD from interfering with repo discovery
130 test_must_fail env GIT_DIR=.git git fsck 2>out &&
131 test_grep "detached HEAD points" out
134 test_expect_success 'HEAD link pointing at a funny place' '
135 test_when_finished "git update-ref --no-deref HEAD $orig_head" &&
136 test-tool ref-store main create-symref HEAD refs/funny/place &&
137 # avoid corrupt/broken HEAD from interfering with repo discovery
138 test_must_fail env GIT_DIR=.git git fsck 2>out &&
139 test_grep "HEAD points to something strange" out
142 test_expect_success REFFILES 'HEAD link pointing at a funny object (from different wt)' '
143 test_when_finished "git update-ref HEAD $orig_head" &&
144 test_when_finished "git worktree remove -f wt" &&
145 git worktree add wt &&
146 echo $ZERO_OID >.git/HEAD &&
147 # avoid corrupt/broken HEAD from interfering with repo discovery
148 test_must_fail git -C wt fsck 2>out &&
149 test_grep "main-worktree/HEAD: detached HEAD points" out
152 test_expect_success REFFILES 'other worktree HEAD link pointing at a funny object' '
153 test_when_finished "git worktree remove -f other" &&
154 git worktree add other &&
155 echo $ZERO_OID >.git/worktrees/other/HEAD &&
156 test_must_fail git fsck 2>out &&
157 test_grep "worktrees/other/HEAD: detached HEAD points" out
160 test_expect_success 'other worktree HEAD link pointing at missing object' '
161 test_when_finished "git worktree remove -f other" &&
162 git worktree add other &&
163 object_id=$(echo "Contents missing from repo" | git hash-object --stdin) &&
164 test-tool -C other ref-store main update-ref msg HEAD $object_id "" REF_NO_DEREF,REF_SKIP_OID_VERIFICATION &&
165 test_must_fail git fsck 2>out &&
166 test_grep "worktrees/other/HEAD: invalid sha1 pointer" out
169 test_expect_success 'other worktree HEAD link pointing at a funny place' '
170 test_when_finished "git worktree remove -f other" &&
171 git worktree add other &&
172 git -C other symbolic-ref HEAD refs/funny/place &&
173 test_must_fail git fsck 2>out &&
174 test_grep "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 --literally -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_grep "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 --literally -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_grep "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 --literally -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_grep "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 --literally -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_grep "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 --literally -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_grep "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 --literally -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_grep "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 --literally -w -t tree --stdin
296 ) &&
297 test_must_fail git fsck 2>out &&
298 test_grep "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_grep "$badtree" out &&
322 test_grep "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_grep "error: empty filename in tree entry" out &&
345 test_grep "$tree_sha1" out &&
346 test_grep ! "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_grep "is a blob, not a tree" out &&
364 test_grep ! "dangling blob" out
367 test_expect_success 'tree entry with bogus mode' '
368 test_when_finished "remove_object \$blob" &&
369 test_when_finished "remove_object \$tree" &&
370 blob=$(echo blob | git hash-object -w --stdin) &&
371 blob_oct=$(echo $blob | hex2oct) &&
372 tree=$(printf "100000 foo\0${blob_oct}" |
373 git hash-object -t tree --stdin -w --literally) &&
374 git fsck 2>err &&
375 cat >expect <<-EOF &&
376 warning in tree $tree: badFilemode: contains bad file modes
378 test_cmp expect err
381 test_expect_success 'tag pointing to nonexistent' '
382 badoid=$(test_oid deadbeef) &&
383 cat >invalid-tag <<-EOF &&
384 object $badoid
385 type commit
386 tag invalid
387 tagger T A Gger <tagger@example.com> 1234567890 -0000
389 This is an invalid tag.
392 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
393 test_when_finished "remove_object $tag" &&
394 git update-ref refs/tags/invalid $tag &&
395 test_when_finished "git update-ref -d refs/tags/invalid" &&
396 test_must_fail git fsck --tags >out &&
397 test_grep "broken link" out
400 test_expect_success 'tag pointing to something else than its type' '
401 sha=$(echo blob | git hash-object -w --stdin) &&
402 test_when_finished "remove_object $sha" &&
403 cat >wrong-tag <<-EOF &&
404 object $sha
405 type commit
406 tag wrong
407 tagger T A Gger <tagger@example.com> 1234567890 -0000
409 This is an invalid tag.
412 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
413 test_when_finished "remove_object $tag" &&
414 git update-ref refs/tags/wrong $tag &&
415 test_when_finished "git update-ref -d refs/tags/wrong" &&
416 test_must_fail git fsck --tags
419 test_expect_success 'tag with incorrect tag name & missing tagger' '
420 sha=$(git rev-parse HEAD) &&
421 cat >wrong-tag <<-EOF &&
422 object $sha
423 type commit
424 tag wrong name format
426 This is an invalid tag.
429 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
430 test_when_finished "remove_object $tag" &&
431 git update-ref refs/tags/wrong $tag &&
432 test_when_finished "git update-ref -d refs/tags/wrong" &&
433 git fsck --tags 2>out &&
435 cat >expect <<-EOF &&
436 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
437 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
439 test_cmp expect out
442 test_expect_success 'tag with bad tagger' '
443 sha=$(git rev-parse HEAD) &&
444 cat >wrong-tag <<-EOF &&
445 object $sha
446 type commit
447 tag not-quite-wrong
448 tagger Bad Tagger Name
450 This is an invalid tag.
453 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
454 test_when_finished "remove_object $tag" &&
455 git update-ref refs/tags/wrong $tag &&
456 test_when_finished "git update-ref -d refs/tags/wrong" &&
457 test_must_fail git fsck --tags 2>out &&
458 test_grep "error in tag .*: invalid author/committer" out
461 test_expect_success 'tag with NUL in header' '
462 sha=$(git rev-parse HEAD) &&
463 q_to_nul >tag-NUL-header <<-EOF &&
464 object $sha
465 type commit
466 tag contains-Q-in-header
467 tagger T A Gger <tagger@example.com> 1234567890 -0000
469 This is an invalid tag.
472 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
473 test_when_finished "remove_object $tag" &&
474 git update-ref refs/tags/wrong $tag &&
475 test_when_finished "git update-ref -d refs/tags/wrong" &&
476 test_must_fail git fsck --tags 2>out &&
477 test_grep "error in tag $tag.*unterminated header: NUL at offset" out
480 test_expect_success 'cleaned up' '
481 git fsck >actual 2>&1 &&
482 test_must_be_empty actual
485 test_expect_success 'rev-list --verify-objects' '
486 git rev-list --verify-objects --all >/dev/null 2>out &&
487 test_must_be_empty out
490 test_expect_success 'rev-list --verify-objects with bad sha1' '
491 sha=$(echo blob | git hash-object -w --stdin) &&
492 old=$(test_oid_to_path $sha) &&
493 new=$(dirname $old)/$(test_oid ff_2) &&
494 sha="$(dirname $new)$(basename $new)" &&
495 mv .git/objects/$old .git/objects/$new &&
496 test_when_finished "remove_object $sha" &&
497 git update-index --add --cacheinfo 100644 $sha foo &&
498 test_when_finished "git read-tree -u --reset HEAD" &&
499 tree=$(git write-tree) &&
500 test_when_finished "remove_object $tree" &&
501 cmt=$(echo bogus | git commit-tree $tree) &&
502 test_when_finished "remove_object $cmt" &&
503 git update-ref refs/heads/bogus $cmt &&
504 test_when_finished "git update-ref -d refs/heads/bogus" &&
506 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
507 test_grep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
510 # An actual bit corruption is more likely than swapped commits, but
511 # this provides an easy way to have commits which don't match their purported
512 # hashes, but which aren't so broken we can't read them at all.
513 test_expect_success 'rev-list --verify-objects notices swapped commits' '
514 git init swapped-commits &&
516 cd swapped-commits &&
517 test_commit one &&
518 test_commit two &&
519 one_oid=$(git rev-parse HEAD) &&
520 two_oid=$(git rev-parse HEAD^) &&
521 one=.git/objects/$(test_oid_to_path $one_oid) &&
522 two=.git/objects/$(test_oid_to_path $two_oid) &&
523 mv $one tmp &&
524 mv $two $one &&
525 mv tmp $two &&
526 test_must_fail git rev-list --verify-objects HEAD
530 test_expect_success 'set up repository with commit-graph' '
531 git init corrupt-graph &&
533 cd corrupt-graph &&
534 test_commit one &&
535 test_commit two &&
536 git commit-graph write --reachable
540 corrupt_graph_obj () {
541 oid=$(git -C corrupt-graph rev-parse "$1") &&
542 obj=corrupt-graph/.git/objects/$(test_oid_to_path $oid) &&
543 test_when_finished 'mv backup $obj' &&
544 mv $obj backup &&
545 echo garbage >$obj
548 test_expect_success 'rev-list --verify-objects with commit graph (tip)' '
549 corrupt_graph_obj HEAD &&
550 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
553 test_expect_success 'rev-list --verify-objects with commit graph (parent)' '
554 corrupt_graph_obj HEAD^ &&
555 test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
558 test_expect_success 'force fsck to ignore double author' '
559 git cat-file commit HEAD >basis &&
560 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
561 new=$(git hash-object --literally -t commit -w --stdin <multiple-authors) &&
562 test_when_finished "remove_object $new" &&
563 git update-ref refs/heads/bogus "$new" &&
564 test_when_finished "git update-ref -d refs/heads/bogus" &&
565 test_must_fail git fsck &&
566 git -c fsck.multipleAuthors=ignore fsck
569 _bz='\0'
570 _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
572 test_expect_success 'fsck notices blob entry pointing to null sha1' '
573 (git init null-blob &&
574 cd null-blob &&
575 sha=$(printf "100644 file$_bz$_bzoid" |
576 git hash-object --literally -w --stdin -t tree) &&
577 git fsck 2>out &&
578 test_grep "warning.*null sha1" out
582 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
583 (git init null-commit &&
584 cd null-commit &&
585 sha=$(printf "160000 submodule$_bz$_bzoid" |
586 git hash-object --literally -w --stdin -t tree) &&
587 git fsck 2>out &&
588 test_grep "warning.*null sha1" out
592 test_expect_success 'fsck notices excessively large tree entry name' '
593 git init large-name &&
595 cd large-name &&
596 test_commit a-long-name &&
597 git -c fsck.largePathname=warn:10 fsck 2>out &&
598 grep "warning.*large pathname" out
602 while read name path pretty; do
603 while read mode type; do
604 : ${pretty:=$path}
605 test_expect_success "fsck notices $pretty as $type" '
607 git init $name-$type &&
608 cd $name-$type &&
609 git config core.protectNTFS false &&
610 echo content >file &&
611 git add file &&
612 git commit -m base &&
613 blob=$(git rev-parse :file) &&
614 tree=$(git rev-parse HEAD^{tree}) &&
615 value=$(eval "echo \$$type") &&
616 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
617 bad_tree=$(git mktree <bad) &&
618 git fsck 2>out &&
619 test_grep "warning.*tree $bad_tree" out
621 done <<-\EOF
622 100644 blob
623 040000 tree
625 done <<-EOF
626 dot .
627 dotdot ..
628 dotgit .git
629 dotgit-case .GIT
630 dotgit-unicode .gI${u200c}T .gI{u200c}T
631 dotgit-case2 .Git
632 git-tilde1 git~1
633 dotgitdot .git.
634 dot-backslash-case .\\\\.GIT\\\\foobar
635 dotgit-case-backslash .git\\\\foobar
638 test_expect_success 'fsck allows .Ňit' '
640 git init not-dotgit &&
641 cd not-dotgit &&
642 echo content >file &&
643 git add file &&
644 git commit -m base &&
645 blob=$(git rev-parse :file) &&
646 printf "100644 blob $blob\t.\\305\\207it" >tree &&
647 tree=$(git mktree <tree) &&
648 git fsck 2>err &&
649 test_line_count = 0 err
653 test_expect_success 'NUL in commit' '
654 rm -fr nul-in-commit &&
655 git init nul-in-commit &&
657 cd nul-in-commit &&
658 git commit --allow-empty -m "initial commitQNUL after message" &&
659 git cat-file commit HEAD >original &&
660 q_to_nul <original >munged &&
661 git hash-object --literally -w -t commit --stdin <munged >name &&
662 git branch bad $(cat name) &&
664 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
665 test_grep nulInCommit warn.1 &&
666 git fsck 2>warn.2 &&
667 test_grep nulInCommit warn.2
671 # create a static test repo which is broken by omitting
672 # one particular object ($1, which is looked up via rev-parse
673 # in the new repository).
674 create_repo_missing () {
675 rm -rf missing &&
676 git init missing &&
678 cd missing &&
679 git commit -m one --allow-empty &&
680 mkdir subdir &&
681 echo content >subdir/file &&
682 git add subdir/file &&
683 git commit -m two &&
684 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
685 git tag -m foo tag $unrelated &&
686 sha1=$(git rev-parse --verify "$1") &&
687 path=$(echo $sha1 | sed 's|..|&/|') &&
688 rm .git/objects/$path
692 test_expect_success 'fsck notices missing blob' '
693 create_repo_missing HEAD:subdir/file &&
694 test_must_fail git -C missing fsck
697 test_expect_success 'fsck notices missing subtree' '
698 create_repo_missing HEAD:subdir &&
699 test_must_fail git -C missing fsck
702 test_expect_success 'fsck notices missing root tree' '
703 create_repo_missing HEAD^{tree} &&
704 test_must_fail git -C missing fsck
707 test_expect_success 'fsck notices missing parent' '
708 create_repo_missing HEAD^ &&
709 test_must_fail git -C missing fsck
712 test_expect_success 'fsck notices missing tagged object' '
713 create_repo_missing tag^{blob} &&
714 test_must_fail git -C missing fsck
717 test_expect_success 'fsck notices ref pointing to missing commit' '
718 create_repo_missing HEAD &&
719 test_must_fail git -C missing fsck
722 test_expect_success 'fsck notices ref pointing to missing tag' '
723 create_repo_missing tag &&
724 test_must_fail git -C missing fsck
727 test_expect_success 'fsck --connectivity-only' '
728 rm -rf connectivity-only &&
729 git init connectivity-only &&
731 cd connectivity-only &&
732 touch empty &&
733 git add empty &&
734 test_commit empty &&
736 # Drop the index now; we want to be sure that we
737 # recursively notice the broken objects
738 # because they are reachable from refs, not because
739 # they are in the index.
740 rm -f .git/index &&
742 # corrupt the blob, but in a way that we can still identify
743 # its type. That lets us see that --connectivity-only is
744 # not actually looking at the contents, but leaves it
745 # free to examine the type if it chooses.
746 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
747 blob=$(echo unrelated | git hash-object -w --stdin) &&
748 mv -f $(sha1_file $blob) $empty &&
750 test_must_fail git fsck --strict &&
751 git fsck --strict --connectivity-only &&
752 tree=$(git rev-parse HEAD:) &&
753 suffix=${tree#??} &&
754 tree=.git/objects/${tree%$suffix}/$suffix &&
755 rm -f $tree &&
756 echo invalid >$tree &&
757 test_must_fail git fsck --strict --connectivity-only
761 test_expect_success 'fsck --connectivity-only with explicit head' '
762 rm -rf connectivity-only &&
763 git init connectivity-only &&
765 cd connectivity-only &&
766 test_commit foo &&
767 rm -f .git/index &&
768 tree=$(git rev-parse HEAD^{tree}) &&
769 remove_object $(git rev-parse HEAD:foo.t) &&
770 test_must_fail git fsck --connectivity-only $tree
774 test_expect_success 'fsck --name-objects' '
775 rm -rf name-objects &&
776 git init name-objects &&
778 cd name-objects &&
779 git config core.logAllRefUpdates false &&
780 test_commit julius caesar.t &&
781 test_commit augustus44 &&
782 test_commit caesar &&
783 remove_object $(git rev-parse julius:caesar.t) &&
784 tree=$(git rev-parse --verify julius:) &&
785 git tag -d julius &&
786 test_must_fail git fsck --name-objects >out &&
787 test_grep "$tree (refs/tags/augustus44\\^:" out
791 test_expect_success 'alternate objects are correctly blamed' '
792 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
793 name=$(test_oid numeric) &&
794 path=$(test_oid_to_path "$name") &&
795 git init --bare alt.git &&
796 echo "../../alt.git/objects" >.git/objects/info/alternates &&
797 mkdir alt.git/objects/$(dirname $path) &&
798 >alt.git/objects/$(dirname $path)/$(basename $path) &&
799 test_must_fail git fsck >out 2>&1 &&
800 test_grep alt.git out
803 test_expect_success 'fsck errors in packed objects' '
804 git cat-file commit HEAD >basis &&
805 sed "s/</one/" basis >one &&
806 sed "s/</foo/" basis >two &&
807 one=$(git hash-object --literally -t commit -w one) &&
808 two=$(git hash-object --literally -t commit -w two) &&
809 pack=$(
811 echo $one &&
812 echo $two
813 } | git pack-objects .git/objects/pack/pack
814 ) &&
815 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
816 remove_object $one &&
817 remove_object $two &&
818 test_must_fail git fsck 2>out &&
819 test_grep "error in commit $one.* - bad name" out &&
820 test_grep "error in commit $two.* - bad name" out &&
821 ! grep corrupt out
824 test_expect_success 'fsck fails on corrupt packfile' '
825 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
826 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
828 # Corrupt the first byte of the first object. (It contains 3 type bits,
829 # at least one of which is not zero, so setting the first byte to 0 is
830 # sufficient.)
831 chmod a+w .git/objects/pack/pack-$pack.pack &&
832 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
834 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
835 remove_object $hsh &&
836 test_must_fail git fsck 2>out &&
837 test_grep "checksum mismatch" out
840 test_expect_success 'fsck finds problems in duplicate loose objects' '
841 rm -rf broken-duplicate &&
842 git init broken-duplicate &&
844 cd broken-duplicate &&
845 test_commit duplicate &&
846 # no "-d" here, so we end up with duplicates
847 git repack &&
848 # now corrupt the loose copy
849 oid="$(git rev-parse HEAD)" &&
850 file=$(sha1_file "$oid") &&
851 rm "$file" &&
852 echo broken >"$file" &&
853 test_must_fail git fsck 2>err &&
855 cat >expect <<-EOF &&
856 error: inflate: data stream error (incorrect header check)
857 error: unable to unpack header of $file
858 error: $oid: object corrupt or missing: $file
860 grep "^error: " err >actual &&
861 test_cmp expect actual
865 test_expect_success 'fsck detects trailing loose garbage (commit)' '
866 git cat-file commit HEAD >basis &&
867 echo bump-commit-sha1 >>basis &&
868 commit=$(git hash-object -w -t commit basis) &&
869 file=$(sha1_file $commit) &&
870 test_when_finished "remove_object $commit" &&
871 chmod +w "$file" &&
872 echo garbage >>"$file" &&
873 test_must_fail git fsck 2>out &&
874 test_grep "garbage.*$commit" out
877 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
878 blob=$(echo trailing | git hash-object -w --stdin) &&
879 file=$(sha1_file $blob) &&
880 test_when_finished "remove_object $blob" &&
881 chmod +w "$file" &&
882 echo garbage >>"$file" &&
883 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
884 test_grep "garbage.*$blob" out
887 test_expect_success 'fsck detects truncated loose object' '
888 # make it big enough that we know we will truncate in the data
889 # portion, not the header
890 test-tool genrandom truncate 4096 >file &&
891 blob=$(git hash-object -w file) &&
892 file=$(sha1_file $blob) &&
893 test_when_finished "remove_object $blob" &&
894 test_copy_bytes 1024 <"$file" >tmp &&
895 rm "$file" &&
896 mv -f tmp "$file" &&
898 # check both regular and streaming code paths
899 test_must_fail git fsck 2>out &&
900 test_grep corrupt.*$blob out &&
902 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
903 test_grep corrupt.*$blob out
906 # for each of type, we have one version which is referenced by another object
907 # (and so while unreachable, not dangling), and another variant which really is
908 # dangling.
909 test_expect_success 'create dangling-object repository' '
910 git init dangling &&
912 cd dangling &&
913 blob=$(echo not-dangling | git hash-object -w --stdin) &&
914 dblob=$(echo dangling | git hash-object -w --stdin) &&
915 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
916 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
917 commit=$(git commit-tree $tree) &&
918 dcommit=$(git commit-tree -p $commit $tree) &&
920 cat >expect <<-EOF
921 dangling blob $dblob
922 dangling commit $dcommit
923 dangling tree $dtree
928 test_expect_success 'fsck notices dangling objects' '
930 cd dangling &&
931 git fsck >actual &&
932 # the output order is non-deterministic, as it comes from a hash
933 sort <actual >actual.sorted &&
934 test_cmp expect actual.sorted
938 test_expect_success 'fsck --connectivity-only notices dangling objects' '
940 cd dangling &&
941 git fsck --connectivity-only >actual &&
942 # the output order is non-deterministic, as it comes from a hash
943 sort <actual >actual.sorted &&
944 test_cmp expect actual.sorted
948 test_expect_success 'fsck $name notices bogus $name' '
949 test_must_fail git fsck bogus &&
950 test_must_fail git fsck $ZERO_OID
953 test_expect_success 'bogus head does not fallback to all heads' '
954 # set up a case that will cause a reachability complaint
955 echo to-be-deleted >foo &&
956 git add foo &&
957 blob=$(git rev-parse :foo) &&
958 test_when_finished "git rm --cached foo" &&
959 remove_object $blob &&
960 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
961 ! grep $blob out
964 # Corrupt the checksum on the index.
965 # Add 1 to the last byte in the SHA.
966 corrupt_index_checksum () {
967 perl -w -e '
968 use Fcntl ":seek";
969 open my $fh, "+<", ".git/index" or die "open: $!";
970 binmode $fh;
971 seek $fh, -1, SEEK_END or die "seek: $!";
972 read $fh, my $in_byte, 1 or die "read: $!";
974 $in_value = unpack("C", $in_byte);
975 $out_value = ($in_value + 1) & 255;
977 $out_byte = pack("C", $out_value);
979 seek $fh, -1, SEEK_END or die "seek: $!";
980 print $fh $out_byte;
981 close $fh or die "close: $!";
985 # Corrupt the checksum on the index and then
986 # verify that only fsck notices.
987 test_expect_success 'detect corrupt index file in fsck' '
988 cp .git/index .git/index.backup &&
989 test_when_finished "mv .git/index.backup .git/index" &&
990 corrupt_index_checksum &&
991 test_must_fail git fsck --cache 2>errors &&
992 test_grep "bad index file" errors
995 test_expect_success 'fsck error and recovery on invalid object type' '
996 git init --bare garbage-type &&
998 cd garbage-type &&
1000 garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
1002 test_must_fail git fsck 2>err &&
1003 grep -e "^error" -e "^fatal" err >errors &&
1004 test_line_count = 1 errors &&
1005 grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err
1009 test_expect_success 'fsck error on gitattributes with excessive line lengths' '
1010 blob=$(printf "pattern %02048d" 1 | git hash-object -w --stdin) &&
1011 test_when_finished "remove_object $blob" &&
1012 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
1013 test_when_finished "remove_object $tree" &&
1014 cat >expected <<-EOF &&
1015 error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
1017 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1018 test_cmp expected actual
1021 test_expect_success 'fsck error on gitattributes with excessive size' '
1022 blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
1023 test_when_finished "remove_object $blob" &&
1024 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
1025 test_when_finished "remove_object $tree" &&
1026 cat >expected <<-EOF &&
1027 error in blob $blob: gitattributesLarge: .gitattributes too large to parse
1029 test_must_fail git fsck --no-dangling >actual 2>&1 &&
1030 test_cmp expected actual
1033 test_expect_success 'fsck detects problems in worktree index' '
1034 test_when_finished "git worktree remove -f wt" &&
1035 git worktree add wt &&
1037 echo "this will be removed to break the worktree index" >wt/file &&
1038 git -C wt add file &&
1039 blob=$(git -C wt rev-parse :file) &&
1040 remove_object $blob &&
1042 test_must_fail git fsck --name-objects >actual 2>&1 &&
1043 cat >expect <<-EOF &&
1044 missing blob $blob (.git/worktrees/wt/index:file)
1046 test_cmp expect actual
1049 test_expect_success 'fsck reports problems in current worktree index without filename' '
1050 test_when_finished "rm -f .git/index && git read-tree HEAD" &&
1051 echo "this object will be removed to break current worktree index" >file &&
1052 git add file &&
1053 blob=$(git rev-parse :file) &&
1054 remove_object $blob &&
1056 test_must_fail git fsck --name-objects >actual 2>&1 &&
1057 cat >expect <<-EOF &&
1058 missing blob $blob (:file)
1060 test_cmp expect actual
1063 test_done