argv-array: return the pushed string from argv_push*()
[alt-git.git] / t / t1450-fsck.sh
blobcb4b66e29dd8c38c734f5d29df3e8062c78884a9
1 #!/bin/sh
3 test_description='git fsck random collection of tests
5 * (HEAD) B
6 * (master) 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 &&
20 >empty
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_cmp empty actual
35 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
37 test_cmp 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 test_expect_success 'setup: helpers for corruption tests' '
45 sha1_file() {
46 remainder=${1#??} &&
47 firsttwo=${1%$remainder} &&
48 echo ".git/objects/$firsttwo/$remainder"
49 } &&
51 remove_object() {
52 rm "$(sha1_file "$1")"
56 test_expect_success 'object with bad sha1' '
57 sha=$(echo blob | git hash-object -w --stdin) &&
58 old=$(echo $sha | sed "s+^..+&/+") &&
59 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
60 sha="$(dirname $new)$(basename $new)" &&
61 mv .git/objects/$old .git/objects/$new &&
62 test_when_finished "remove_object $sha" &&
63 git update-index --add --cacheinfo 100644 $sha foo &&
64 test_when_finished "git read-tree -u --reset HEAD" &&
65 tree=$(git write-tree) &&
66 test_when_finished "remove_object $tree" &&
67 cmt=$(echo bogus | git commit-tree $tree) &&
68 test_when_finished "remove_object $cmt" &&
69 git update-ref refs/heads/bogus $cmt &&
70 test_when_finished "git update-ref -d refs/heads/bogus" &&
72 test_must_fail git fsck 2>out &&
73 cat out &&
74 grep "$sha.*corrupt" out
77 test_expect_success 'branch pointing to non-commit' '
78 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79 test_when_finished "git update-ref -d refs/heads/invalid" &&
80 test_must_fail git fsck 2>out &&
81 cat out &&
82 grep "not a commit" out
85 test_expect_success 'HEAD link pointing at a funny object' '
86 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
87 mv .git/HEAD .git/SAVED_HEAD &&
88 echo 0000000000000000000000000000000000000000 >.git/HEAD &&
89 # avoid corrupt/broken HEAD from interfering with repo discovery
90 test_must_fail env GIT_DIR=.git git fsck 2>out &&
91 cat out &&
92 grep "detached HEAD points" out
95 test_expect_success 'HEAD link pointing at a funny place' '
96 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
97 mv .git/HEAD .git/SAVED_HEAD &&
98 echo "ref: refs/funny/place" >.git/HEAD &&
99 # avoid corrupt/broken HEAD from interfering with repo discovery
100 test_must_fail env GIT_DIR=.git git fsck 2>out &&
101 cat out &&
102 grep "HEAD points to something strange" out
105 test_expect_success 'email without @ is okay' '
106 git cat-file commit HEAD >basis &&
107 sed "s/@/AT/" basis >okay &&
108 new=$(git hash-object -t commit -w --stdin <okay) &&
109 test_when_finished "remove_object $new" &&
110 git update-ref refs/heads/bogus "$new" &&
111 test_when_finished "git update-ref -d refs/heads/bogus" &&
112 git fsck 2>out &&
113 cat out &&
114 ! grep "commit $new" out
117 test_expect_success 'email with embedded > is not okay' '
118 git cat-file commit HEAD >basis &&
119 sed "s/@[a-z]/&>/" basis >bad-email &&
120 new=$(git hash-object -t commit -w --stdin <bad-email) &&
121 test_when_finished "remove_object $new" &&
122 git update-ref refs/heads/bogus "$new" &&
123 test_when_finished "git update-ref -d refs/heads/bogus" &&
124 test_must_fail git fsck 2>out &&
125 cat out &&
126 grep "error in commit $new" out
129 test_expect_success 'missing < email delimiter is reported nicely' '
130 git cat-file commit HEAD >basis &&
131 sed "s/<//" basis >bad-email-2 &&
132 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
133 test_when_finished "remove_object $new" &&
134 git update-ref refs/heads/bogus "$new" &&
135 test_when_finished "git update-ref -d refs/heads/bogus" &&
136 test_must_fail git fsck 2>out &&
137 cat out &&
138 grep "error in commit $new.* - bad name" out
141 test_expect_success 'missing email is reported nicely' '
142 git cat-file commit HEAD >basis &&
143 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
144 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
145 test_when_finished "remove_object $new" &&
146 git update-ref refs/heads/bogus "$new" &&
147 test_when_finished "git update-ref -d refs/heads/bogus" &&
148 test_must_fail git fsck 2>out &&
149 cat out &&
150 grep "error in commit $new.* - missing email" out
153 test_expect_success '> in name is reported' '
154 git cat-file commit HEAD >basis &&
155 sed "s/ </> </" basis >bad-email-4 &&
156 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
157 test_when_finished "remove_object $new" &&
158 git update-ref refs/heads/bogus "$new" &&
159 test_when_finished "git update-ref -d refs/heads/bogus" &&
160 test_must_fail git fsck 2>out &&
161 cat out &&
162 grep "error in commit $new" out
165 # date is 2^64 + 1
166 test_expect_success 'integer overflow in timestamps is reported' '
167 git cat-file commit HEAD >basis &&
168 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
169 <basis >bad-timestamp &&
170 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
171 test_when_finished "remove_object $new" &&
172 git update-ref refs/heads/bogus "$new" &&
173 test_when_finished "git update-ref -d refs/heads/bogus" &&
174 test_must_fail git fsck 2>out &&
175 cat out &&
176 grep "error in commit $new.*integer overflow" out
179 test_expect_success 'commit with NUL in header' '
180 git cat-file commit HEAD >basis &&
181 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
182 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
183 test_when_finished "remove_object $new" &&
184 git update-ref refs/heads/bogus "$new" &&
185 test_when_finished "git update-ref -d refs/heads/bogus" &&
186 test_must_fail git fsck 2>out &&
187 cat out &&
188 grep "error in commit $new.*unterminated header: NUL at offset" out
191 test_expect_success 'tree object with duplicate entries' '
192 test_when_finished "for i in \$T; do remove_object \$i; done" &&
193 T=$(
194 GIT_INDEX_FILE=test-index &&
195 export GIT_INDEX_FILE &&
196 rm -f test-index &&
197 >x &&
198 git add x &&
199 git rev-parse :x &&
200 T=$(git write-tree) &&
201 echo $T &&
203 git cat-file tree $T &&
204 git cat-file tree $T
206 git hash-object -w -t tree --stdin
207 ) &&
208 test_must_fail git fsck 2>out &&
209 grep "error in tree .*contains duplicate file entries" out
212 test_expect_success 'unparseable tree object' '
213 test_when_finished "git update-ref -d refs/heads/wrong" &&
214 test_when_finished "remove_object \$tree_sha1" &&
215 test_when_finished "remove_object \$commit_sha1" &&
216 tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
217 commit_sha1=$(git commit-tree $tree_sha1) &&
218 git update-ref refs/heads/wrong $commit_sha1 &&
219 test_must_fail git fsck 2>out &&
220 test_i18ngrep "error: empty filename in tree entry" out &&
221 test_i18ngrep "$tree_sha1" out &&
222 test_i18ngrep ! "fatal: empty filename in tree entry" out
225 hex2oct() {
226 perl -ne 'printf "\\%03o", hex for /../g'
229 test_expect_success 'tree entry with type mismatch' '
230 test_when_finished "remove_object \$blob" &&
231 test_when_finished "remove_object \$tree" &&
232 test_when_finished "remove_object \$commit" &&
233 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
234 blob=$(echo blob | git hash-object -w --stdin) &&
235 blob_bin=$(echo $blob | hex2oct) &&
236 tree=$(
237 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
238 git hash-object -t tree --stdin -w --literally
239 ) &&
240 commit=$(git commit-tree $tree) &&
241 git update-ref refs/heads/type_mismatch $commit &&
242 test_must_fail git fsck >out 2>&1 &&
243 test_i18ngrep "is a blob, not a tree" out &&
244 test_i18ngrep ! "dangling blob" out
247 test_expect_success 'tag pointing to nonexistent' '
248 cat >invalid-tag <<-\EOF &&
249 object ffffffffffffffffffffffffffffffffffffffff
250 type commit
251 tag invalid
252 tagger T A Gger <tagger@example.com> 1234567890 -0000
254 This is an invalid tag.
257 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
258 test_when_finished "remove_object $tag" &&
259 echo $tag >.git/refs/tags/invalid &&
260 test_when_finished "git update-ref -d refs/tags/invalid" &&
261 test_must_fail git fsck --tags >out &&
262 cat out &&
263 grep "broken link" out
266 test_expect_success 'tag pointing to something else than its type' '
267 sha=$(echo blob | git hash-object -w --stdin) &&
268 test_when_finished "remove_object $sha" &&
269 cat >wrong-tag <<-EOF &&
270 object $sha
271 type commit
272 tag wrong
273 tagger T A Gger <tagger@example.com> 1234567890 -0000
275 This is an invalid tag.
278 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
279 test_when_finished "remove_object $tag" &&
280 echo $tag >.git/refs/tags/wrong &&
281 test_when_finished "git update-ref -d refs/tags/wrong" &&
282 test_must_fail git fsck --tags
285 test_expect_success 'tag with incorrect tag name & missing tagger' '
286 sha=$(git rev-parse HEAD) &&
287 cat >wrong-tag <<-EOF &&
288 object $sha
289 type commit
290 tag wrong name format
292 This is an invalid tag.
295 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
296 test_when_finished "remove_object $tag" &&
297 echo $tag >.git/refs/tags/wrong &&
298 test_when_finished "git update-ref -d refs/tags/wrong" &&
299 git fsck --tags 2>out &&
301 cat >expect <<-EOF &&
302 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
303 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
305 test_cmp expect out
308 test_expect_success 'tag with bad tagger' '
309 sha=$(git rev-parse HEAD) &&
310 cat >wrong-tag <<-EOF &&
311 object $sha
312 type commit
313 tag not-quite-wrong
314 tagger Bad Tagger Name
316 This is an invalid tag.
319 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
320 test_when_finished "remove_object $tag" &&
321 echo $tag >.git/refs/tags/wrong &&
322 test_when_finished "git update-ref -d refs/tags/wrong" &&
323 test_must_fail git fsck --tags 2>out &&
324 grep "error in tag .*: invalid author/committer" out
327 test_expect_success 'tag with NUL in header' '
328 sha=$(git rev-parse HEAD) &&
329 q_to_nul >tag-NUL-header <<-EOF &&
330 object $sha
331 type commit
332 tag contains-Q-in-header
333 tagger T A Gger <tagger@example.com> 1234567890 -0000
335 This is an invalid tag.
338 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
339 test_when_finished "remove_object $tag" &&
340 echo $tag >.git/refs/tags/wrong &&
341 test_when_finished "git update-ref -d refs/tags/wrong" &&
342 test_must_fail git fsck --tags 2>out &&
343 cat out &&
344 grep "error in tag $tag.*unterminated header: NUL at offset" out
347 test_expect_success 'cleaned up' '
348 git fsck >actual 2>&1 &&
349 test_cmp empty actual
352 test_expect_success 'rev-list --verify-objects' '
353 git rev-list --verify-objects --all >/dev/null 2>out &&
354 test_cmp empty out
357 test_expect_success 'rev-list --verify-objects with bad sha1' '
358 sha=$(echo blob | git hash-object -w --stdin) &&
359 old=$(echo $sha | sed "s+^..+&/+") &&
360 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
361 sha="$(dirname $new)$(basename $new)" &&
362 mv .git/objects/$old .git/objects/$new &&
363 test_when_finished "remove_object $sha" &&
364 git update-index --add --cacheinfo 100644 $sha foo &&
365 test_when_finished "git read-tree -u --reset HEAD" &&
366 tree=$(git write-tree) &&
367 test_when_finished "remove_object $tree" &&
368 cmt=$(echo bogus | git commit-tree $tree) &&
369 test_when_finished "remove_object $cmt" &&
370 git update-ref refs/heads/bogus $cmt &&
371 test_when_finished "git update-ref -d refs/heads/bogus" &&
373 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
374 cat out &&
375 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
378 test_expect_success 'force fsck to ignore double author' '
379 git cat-file commit HEAD >basis &&
380 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
381 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
382 test_when_finished "remove_object $new" &&
383 git update-ref refs/heads/bogus "$new" &&
384 test_when_finished "git update-ref -d refs/heads/bogus" &&
385 test_must_fail git fsck &&
386 git -c fsck.multipleAuthors=ignore fsck
389 _bz='\0'
390 _bz5="$_bz$_bz$_bz$_bz$_bz"
391 _bz20="$_bz5$_bz5$_bz5$_bz5"
393 test_expect_success 'fsck notices blob entry pointing to null sha1' '
394 (git init null-blob &&
395 cd null-blob &&
396 sha=$(printf "100644 file$_bz$_bz20" |
397 git hash-object -w --stdin -t tree) &&
398 git fsck 2>out &&
399 cat out &&
400 grep "warning.*null sha1" out
404 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
405 (git init null-commit &&
406 cd null-commit &&
407 sha=$(printf "160000 submodule$_bz$_bz20" |
408 git hash-object -w --stdin -t tree) &&
409 git fsck 2>out &&
410 cat out &&
411 grep "warning.*null sha1" out
415 while read name path pretty; do
416 while read mode type; do
417 : ${pretty:=$path}
418 test_expect_success "fsck notices $pretty as $type" '
420 git init $name-$type &&
421 cd $name-$type &&
422 echo content >file &&
423 git add file &&
424 git commit -m base &&
425 blob=$(git rev-parse :file) &&
426 tree=$(git rev-parse HEAD^{tree}) &&
427 value=$(eval "echo \$$type") &&
428 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
429 bad_tree=$(git mktree <bad) &&
430 git fsck 2>out &&
431 cat out &&
432 grep "warning.*tree $bad_tree" out
434 done <<-\EOF
435 100644 blob
436 040000 tree
438 done <<-EOF
439 dot .
440 dotdot ..
441 dotgit .git
442 dotgit-case .GIT
443 dotgit-unicode .gI${u200c}T .gI{u200c}T
444 dotgit-case2 .Git
445 git-tilde1 git~1
446 dotgitdot .git.
447 dot-backslash-case .\\\\.GIT\\\\foobar
448 dotgit-case-backslash .git\\\\foobar
451 test_expect_success 'fsck allows .Ňit' '
453 git init not-dotgit &&
454 cd not-dotgit &&
455 echo content >file &&
456 git add file &&
457 git commit -m base &&
458 blob=$(git rev-parse :file) &&
459 printf "100644 blob $blob\t.\\305\\207it" >tree &&
460 tree=$(git mktree <tree) &&
461 git fsck 2>err &&
462 test_line_count = 0 err
466 test_expect_success 'NUL in commit' '
467 rm -fr nul-in-commit &&
468 git init nul-in-commit &&
470 cd nul-in-commit &&
471 git commit --allow-empty -m "initial commitQNUL after message" &&
472 git cat-file commit HEAD >original &&
473 q_to_nul <original >munged &&
474 git hash-object -w -t commit --stdin <munged >name &&
475 git branch bad $(cat name) &&
477 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
478 grep nulInCommit warn.1 &&
479 git fsck 2>warn.2 &&
480 grep nulInCommit warn.2
484 # create a static test repo which is broken by omitting
485 # one particular object ($1, which is looked up via rev-parse
486 # in the new repository).
487 create_repo_missing () {
488 rm -rf missing &&
489 git init missing &&
491 cd missing &&
492 git commit -m one --allow-empty &&
493 mkdir subdir &&
494 echo content >subdir/file &&
495 git add subdir/file &&
496 git commit -m two &&
497 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
498 git tag -m foo tag $unrelated &&
499 sha1=$(git rev-parse --verify "$1") &&
500 path=$(echo $sha1 | sed 's|..|&/|') &&
501 rm .git/objects/$path
505 test_expect_success 'fsck notices missing blob' '
506 create_repo_missing HEAD:subdir/file &&
507 test_must_fail git -C missing fsck
510 test_expect_success 'fsck notices missing subtree' '
511 create_repo_missing HEAD:subdir &&
512 test_must_fail git -C missing fsck
515 test_expect_success 'fsck notices missing root tree' '
516 create_repo_missing HEAD^{tree} &&
517 test_must_fail git -C missing fsck
520 test_expect_success 'fsck notices missing parent' '
521 create_repo_missing HEAD^ &&
522 test_must_fail git -C missing fsck
525 test_expect_success 'fsck notices missing tagged object' '
526 create_repo_missing tag^{blob} &&
527 test_must_fail git -C missing fsck
530 test_expect_success 'fsck notices ref pointing to missing commit' '
531 create_repo_missing HEAD &&
532 test_must_fail git -C missing fsck
535 test_expect_success 'fsck notices ref pointing to missing tag' '
536 create_repo_missing tag &&
537 test_must_fail git -C missing fsck
540 test_expect_success 'fsck --connectivity-only' '
541 rm -rf connectivity-only &&
542 git init connectivity-only &&
544 cd connectivity-only &&
545 touch empty &&
546 git add empty &&
547 test_commit empty &&
549 # Drop the index now; we want to be sure that we
550 # recursively notice the broken objects
551 # because they are reachable from refs, not because
552 # they are in the index.
553 rm -f .git/index &&
555 # corrupt the blob, but in a way that we can still identify
556 # its type. That lets us see that --connectivity-only is
557 # not actually looking at the contents, but leaves it
558 # free to examine the type if it chooses.
559 empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
560 blob=$(echo unrelated | git hash-object -w --stdin) &&
561 mv -f $(sha1_file $blob) $empty &&
563 test_must_fail git fsck --strict &&
564 git fsck --strict --connectivity-only &&
565 tree=$(git rev-parse HEAD:) &&
566 suffix=${tree#??} &&
567 tree=.git/objects/${tree%$suffix}/$suffix &&
568 rm -f $tree &&
569 echo invalid >$tree &&
570 test_must_fail git fsck --strict --connectivity-only
574 test_expect_success 'fsck --connectivity-only with explicit head' '
575 rm -rf connectivity-only &&
576 git init connectivity-only &&
578 cd connectivity-only &&
579 test_commit foo &&
580 rm -f .git/index &&
581 tree=$(git rev-parse HEAD^{tree}) &&
582 remove_object $(git rev-parse HEAD:foo.t) &&
583 test_must_fail git fsck --connectivity-only $tree
587 test_expect_success 'fsck --name-objects' '
588 rm -rf name-objects &&
589 git init name-objects &&
591 cd name-objects &&
592 test_commit julius caesar.t &&
593 test_commit augustus &&
594 test_commit caesar &&
595 remove_object $(git rev-parse julius:caesar.t) &&
596 test_must_fail git fsck --name-objects >out &&
597 tree=$(git rev-parse --verify julius:) &&
598 egrep "$tree \((refs/heads/master|HEAD)@\{[0-9]*\}:" out
602 test_expect_success 'alternate objects are correctly blamed' '
603 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
604 git init --bare alt.git &&
605 echo "../../alt.git/objects" >.git/objects/info/alternates &&
606 mkdir alt.git/objects/12 &&
607 >alt.git/objects/12/34567890123456789012345678901234567890 &&
608 test_must_fail git fsck >out 2>&1 &&
609 grep alt.git out
612 test_expect_success 'fsck errors in packed objects' '
613 git cat-file commit HEAD >basis &&
614 sed "s/</one/" basis >one &&
615 sed "s/</foo/" basis >two &&
616 one=$(git hash-object -t commit -w one) &&
617 two=$(git hash-object -t commit -w two) &&
618 pack=$(
620 echo $one &&
621 echo $two
622 } | git pack-objects .git/objects/pack/pack
623 ) &&
624 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
625 remove_object $one &&
626 remove_object $two &&
627 test_must_fail git fsck 2>out &&
628 grep "error in commit $one.* - bad name" out &&
629 grep "error in commit $two.* - bad name" out &&
630 ! grep corrupt out
633 test_expect_success 'fsck fails on corrupt packfile' '
634 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
635 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
637 # Corrupt the first byte of the first object. (It contains 3 type bits,
638 # at least one of which is not zero, so setting the first byte to 0 is
639 # sufficient.)
640 chmod a+w .git/objects/pack/pack-$pack.pack &&
641 printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
643 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
644 remove_object $hsh &&
645 test_must_fail git fsck 2>out &&
646 test_i18ngrep "checksum mismatch" out
649 test_expect_success 'fsck finds problems in duplicate loose objects' '
650 rm -rf broken-duplicate &&
651 git init broken-duplicate &&
653 cd broken-duplicate &&
654 test_commit duplicate &&
655 # no "-d" here, so we end up with duplicates
656 git repack &&
657 # now corrupt the loose copy
658 file=$(sha1_file "$(git rev-parse HEAD)") &&
659 rm "$file" &&
660 echo broken >"$file" &&
661 test_must_fail git fsck
665 test_expect_success 'fsck detects trailing loose garbage (commit)' '
666 git cat-file commit HEAD >basis &&
667 echo bump-commit-sha1 >>basis &&
668 commit=$(git hash-object -w -t commit basis) &&
669 file=$(sha1_file $commit) &&
670 test_when_finished "remove_object $commit" &&
671 chmod +w "$file" &&
672 echo garbage >>"$file" &&
673 test_must_fail git fsck 2>out &&
674 test_i18ngrep "garbage.*$commit" out
677 test_expect_success 'fsck detects trailing loose garbage (blob)' '
678 blob=$(echo trailing | git hash-object -w --stdin) &&
679 file=$(sha1_file $blob) &&
680 test_when_finished "remove_object $blob" &&
681 chmod +w "$file" &&
682 echo garbage >>"$file" &&
683 test_must_fail git fsck 2>out &&
684 test_i18ngrep "garbage.*$blob" out
687 # for each of type, we have one version which is referenced by another object
688 # (and so while unreachable, not dangling), and another variant which really is
689 # dangling.
690 test_expect_success 'fsck notices dangling objects' '
691 git init dangling &&
693 cd dangling &&
694 blob=$(echo not-dangling | git hash-object -w --stdin) &&
695 dblob=$(echo dangling | git hash-object -w --stdin) &&
696 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
697 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
698 commit=$(git commit-tree $tree) &&
699 dcommit=$(git commit-tree -p $commit $tree) &&
701 cat >expect <<-EOF &&
702 dangling blob $dblob
703 dangling commit $dcommit
704 dangling tree $dtree
707 git fsck >actual &&
708 # the output order is non-deterministic, as it comes from a hash
709 sort <actual >actual.sorted &&
710 test_cmp expect actual.sorted
714 test_expect_success 'fsck $name notices bogus $name' '
715 test_must_fail git fsck bogus &&
716 test_must_fail git fsck $_z40
719 test_expect_success 'bogus head does not fallback to all heads' '
720 # set up a case that will cause a reachability complaint
721 echo to-be-deleted >foo &&
722 git add foo &&
723 blob=$(git rev-parse :foo) &&
724 test_when_finished "git rm --cached foo" &&
725 remove_object $blob &&
726 test_must_fail git fsck $_z40 >out 2>&1 &&
727 ! grep $blob out
730 # Corrupt the checksum on the index.
731 # Add 1 to the last byte in the SHA.
732 corrupt_index_checksum () {
733 perl -w -e '
734 use Fcntl ":seek";
735 open my $fh, "+<", ".git/index" or die "open: $!";
736 binmode $fh;
737 seek $fh, -1, SEEK_END or die "seek: $!";
738 read $fh, my $in_byte, 1 or die "read: $!";
740 $in_value = unpack("C", $in_byte);
741 $out_value = ($in_value + 1) & 255;
743 $out_byte = pack("C", $out_value);
745 seek $fh, -1, SEEK_END or die "seek: $!";
746 print $fh $out_byte;
747 close $fh or die "close: $!";
751 # Corrupt the checksum on the index and then
752 # verify that only fsck notices.
753 test_expect_success 'detect corrupt index file in fsck' '
754 cp .git/index .git/index.backup &&
755 test_when_finished "mv .git/index.backup .git/index" &&
756 corrupt_index_checksum &&
757 test_must_fail git fsck --cache 2>errors &&
758 grep "bad index file" errors
761 test_done