grep: replace grep_read_mutex by internal obj read lock
[git.git] / t / t1450-fsck.sh
blob02478bc4ece20564a7d5ea27df91cb0695d6e5ec
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 test_oid_init &&
13 git config gc.auto 0 &&
14 git config i18n.commitencoding ISO-8859-1 &&
15 test_commit A fileA one &&
16 git config --unset i18n.commitencoding &&
17 git checkout HEAD^0 &&
18 test_commit B fileB two &&
19 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 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=$(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 'email without @ is okay' '
137 git cat-file commit HEAD >basis &&
138 sed "s/@/AT/" basis >okay &&
139 new=$(git hash-object -t commit -w --stdin <okay) &&
140 test_when_finished "remove_object $new" &&
141 git update-ref refs/heads/bogus "$new" &&
142 test_when_finished "git update-ref -d refs/heads/bogus" &&
143 git fsck 2>out &&
144 cat out &&
145 ! grep "commit $new" out
148 test_expect_success 'email with embedded > is not okay' '
149 git cat-file commit HEAD >basis &&
150 sed "s/@[a-z]/&>/" basis >bad-email &&
151 new=$(git hash-object -t commit -w --stdin <bad-email) &&
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" &&
155 test_must_fail git fsck 2>out &&
156 test_i18ngrep "error in commit $new" out
159 test_expect_success 'missing < email delimiter is reported nicely' '
160 git cat-file commit HEAD >basis &&
161 sed "s/<//" basis >bad-email-2 &&
162 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
163 test_when_finished "remove_object $new" &&
164 git update-ref refs/heads/bogus "$new" &&
165 test_when_finished "git update-ref -d refs/heads/bogus" &&
166 test_must_fail git fsck 2>out &&
167 test_i18ngrep "error in commit $new.* - bad name" out
170 test_expect_success 'missing email is reported nicely' '
171 git cat-file commit HEAD >basis &&
172 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
173 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
174 test_when_finished "remove_object $new" &&
175 git update-ref refs/heads/bogus "$new" &&
176 test_when_finished "git update-ref -d refs/heads/bogus" &&
177 test_must_fail git fsck 2>out &&
178 test_i18ngrep "error in commit $new.* - missing email" out
181 test_expect_success '> in name is reported' '
182 git cat-file commit HEAD >basis &&
183 sed "s/ </> </" basis >bad-email-4 &&
184 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
185 test_when_finished "remove_object $new" &&
186 git update-ref refs/heads/bogus "$new" &&
187 test_when_finished "git update-ref -d refs/heads/bogus" &&
188 test_must_fail git fsck 2>out &&
189 test_i18ngrep "error in commit $new" out
192 # date is 2^64 + 1
193 test_expect_success 'integer overflow in timestamps is reported' '
194 git cat-file commit HEAD >basis &&
195 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
196 <basis >bad-timestamp &&
197 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
198 test_when_finished "remove_object $new" &&
199 git update-ref refs/heads/bogus "$new" &&
200 test_when_finished "git update-ref -d refs/heads/bogus" &&
201 test_must_fail git fsck 2>out &&
202 test_i18ngrep "error in commit $new.*integer overflow" out
205 test_expect_success 'commit with NUL in header' '
206 git cat-file commit HEAD >basis &&
207 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
208 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
209 test_when_finished "remove_object $new" &&
210 git update-ref refs/heads/bogus "$new" &&
211 test_when_finished "git update-ref -d refs/heads/bogus" &&
212 test_must_fail git fsck 2>out &&
213 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
216 test_expect_success 'tree object with duplicate entries' '
217 test_when_finished "for i in \$T; do remove_object \$i; done" &&
218 T=$(
219 GIT_INDEX_FILE=test-index &&
220 export GIT_INDEX_FILE &&
221 rm -f test-index &&
222 >x &&
223 git add x &&
224 git rev-parse :x &&
225 T=$(git write-tree) &&
226 echo $T &&
228 git cat-file tree $T &&
229 git cat-file tree $T
231 git hash-object -w -t tree --stdin
232 ) &&
233 test_must_fail git fsck 2>out &&
234 test_i18ngrep "error in tree .*contains duplicate file entries" out
237 test_expect_success 'unparseable tree object' '
238 test_oid_cache <<-\EOF &&
239 junk sha1:twenty-bytes-of-junk
240 junk sha256:twenty-bytes-of-junk-twelve-more
243 test_when_finished "git update-ref -d refs/heads/wrong" &&
244 test_when_finished "remove_object \$tree_sha1" &&
245 test_when_finished "remove_object \$commit_sha1" &&
246 junk=$(test_oid junk) &&
247 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
248 commit_sha1=$(git commit-tree $tree_sha1) &&
249 git update-ref refs/heads/wrong $commit_sha1 &&
250 test_must_fail git fsck 2>out &&
251 test_i18ngrep "error: empty filename in tree entry" out &&
252 test_i18ngrep "$tree_sha1" out &&
253 test_i18ngrep ! "fatal: empty filename in tree entry" out
256 test_expect_success 'tree entry with type mismatch' '
257 test_when_finished "remove_object \$blob" &&
258 test_when_finished "remove_object \$tree" &&
259 test_when_finished "remove_object \$commit" &&
260 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
261 blob=$(echo blob | git hash-object -w --stdin) &&
262 blob_bin=$(echo $blob | hex2oct) &&
263 tree=$(
264 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
265 git hash-object -t tree --stdin -w --literally
266 ) &&
267 commit=$(git commit-tree $tree) &&
268 git update-ref refs/heads/type_mismatch $commit &&
269 test_must_fail git fsck >out 2>&1 &&
270 test_i18ngrep "is a blob, not a tree" out &&
271 test_i18ngrep ! "dangling blob" out
274 test_expect_success 'tag pointing to nonexistent' '
275 badoid=$(test_oid deadbeef) &&
276 cat >invalid-tag <<-EOF &&
277 object $badoid
278 type commit
279 tag invalid
280 tagger T A Gger <tagger@example.com> 1234567890 -0000
282 This is an invalid tag.
285 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
286 test_when_finished "remove_object $tag" &&
287 echo $tag >.git/refs/tags/invalid &&
288 test_when_finished "git update-ref -d refs/tags/invalid" &&
289 test_must_fail git fsck --tags >out &&
290 test_i18ngrep "broken link" out
293 test_expect_success 'tag pointing to something else than its type' '
294 sha=$(echo blob | git hash-object -w --stdin) &&
295 test_when_finished "remove_object $sha" &&
296 cat >wrong-tag <<-EOF &&
297 object $sha
298 type commit
299 tag wrong
300 tagger T A Gger <tagger@example.com> 1234567890 -0000
302 This is an invalid tag.
305 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
306 test_when_finished "remove_object $tag" &&
307 echo $tag >.git/refs/tags/wrong &&
308 test_when_finished "git update-ref -d refs/tags/wrong" &&
309 test_must_fail git fsck --tags
312 test_expect_success 'tag with incorrect tag name & missing tagger' '
313 sha=$(git rev-parse HEAD) &&
314 cat >wrong-tag <<-EOF &&
315 object $sha
316 type commit
317 tag wrong name format
319 This is an invalid tag.
322 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
323 test_when_finished "remove_object $tag" &&
324 echo $tag >.git/refs/tags/wrong &&
325 test_when_finished "git update-ref -d refs/tags/wrong" &&
326 git fsck --tags 2>out &&
328 cat >expect <<-EOF &&
329 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
330 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
332 test_i18ncmp expect out
335 test_expect_success 'tag with bad tagger' '
336 sha=$(git rev-parse HEAD) &&
337 cat >wrong-tag <<-EOF &&
338 object $sha
339 type commit
340 tag not-quite-wrong
341 tagger Bad Tagger Name
343 This is an invalid tag.
346 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
347 test_when_finished "remove_object $tag" &&
348 echo $tag >.git/refs/tags/wrong &&
349 test_when_finished "git update-ref -d refs/tags/wrong" &&
350 test_must_fail git fsck --tags 2>out &&
351 test_i18ngrep "error in tag .*: invalid author/committer" out
354 test_expect_success 'tag with NUL in header' '
355 sha=$(git rev-parse HEAD) &&
356 q_to_nul >tag-NUL-header <<-EOF &&
357 object $sha
358 type commit
359 tag contains-Q-in-header
360 tagger T A Gger <tagger@example.com> 1234567890 -0000
362 This is an invalid tag.
365 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
366 test_when_finished "remove_object $tag" &&
367 echo $tag >.git/refs/tags/wrong &&
368 test_when_finished "git update-ref -d refs/tags/wrong" &&
369 test_must_fail git fsck --tags 2>out &&
370 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
373 test_expect_success 'cleaned up' '
374 git fsck >actual 2>&1 &&
375 test_must_be_empty actual
378 test_expect_success 'rev-list --verify-objects' '
379 git rev-list --verify-objects --all >/dev/null 2>out &&
380 test_must_be_empty out
383 test_expect_success 'rev-list --verify-objects with bad sha1' '
384 sha=$(echo blob | git hash-object -w --stdin) &&
385 old=$(test_oid_to_path $sha) &&
386 new=$(dirname $old)/$(test_oid ff_2) &&
387 sha="$(dirname $new)$(basename $new)" &&
388 mv .git/objects/$old .git/objects/$new &&
389 test_when_finished "remove_object $sha" &&
390 git update-index --add --cacheinfo 100644 $sha foo &&
391 test_when_finished "git read-tree -u --reset HEAD" &&
392 tree=$(git write-tree) &&
393 test_when_finished "remove_object $tree" &&
394 cmt=$(echo bogus | git commit-tree $tree) &&
395 test_when_finished "remove_object $cmt" &&
396 git update-ref refs/heads/bogus $cmt &&
397 test_when_finished "git update-ref -d refs/heads/bogus" &&
399 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
400 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
403 test_expect_success 'force fsck to ignore double author' '
404 git cat-file commit HEAD >basis &&
405 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
406 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
407 test_when_finished "remove_object $new" &&
408 git update-ref refs/heads/bogus "$new" &&
409 test_when_finished "git update-ref -d refs/heads/bogus" &&
410 test_must_fail git fsck &&
411 git -c fsck.multipleAuthors=ignore fsck
414 _bz='\0'
415 _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
417 test_expect_success 'fsck notices blob entry pointing to null sha1' '
418 (git init null-blob &&
419 cd null-blob &&
420 sha=$(printf "100644 file$_bz$_bzoid" |
421 git hash-object -w --stdin -t tree) &&
422 git fsck 2>out &&
423 test_i18ngrep "warning.*null sha1" out
427 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
428 (git init null-commit &&
429 cd null-commit &&
430 sha=$(printf "160000 submodule$_bz$_bzoid" |
431 git hash-object -w --stdin -t tree) &&
432 git fsck 2>out &&
433 test_i18ngrep "warning.*null sha1" out
437 while read name path pretty; do
438 while read mode type; do
439 : ${pretty:=$path}
440 test_expect_success "fsck notices $pretty as $type" '
442 git init $name-$type &&
443 cd $name-$type &&
444 git config core.protectNTFS false &&
445 echo content >file &&
446 git add file &&
447 git commit -m base &&
448 blob=$(git rev-parse :file) &&
449 tree=$(git rev-parse HEAD^{tree}) &&
450 value=$(eval "echo \$$type") &&
451 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
452 bad_tree=$(git mktree <bad) &&
453 git fsck 2>out &&
454 test_i18ngrep "warning.*tree $bad_tree" out
456 done <<-\EOF
457 100644 blob
458 040000 tree
460 done <<-EOF
461 dot .
462 dotdot ..
463 dotgit .git
464 dotgit-case .GIT
465 dotgit-unicode .gI${u200c}T .gI{u200c}T
466 dotgit-case2 .Git
467 git-tilde1 git~1
468 dotgitdot .git.
469 dot-backslash-case .\\\\.GIT\\\\foobar
470 dotgit-case-backslash .git\\\\foobar
473 test_expect_success 'fsck allows .Ňit' '
475 git init not-dotgit &&
476 cd not-dotgit &&
477 echo content >file &&
478 git add file &&
479 git commit -m base &&
480 blob=$(git rev-parse :file) &&
481 printf "100644 blob $blob\t.\\305\\207it" >tree &&
482 tree=$(git mktree <tree) &&
483 git fsck 2>err &&
484 test_line_count = 0 err
488 test_expect_success 'NUL in commit' '
489 rm -fr nul-in-commit &&
490 git init nul-in-commit &&
492 cd nul-in-commit &&
493 git commit --allow-empty -m "initial commitQNUL after message" &&
494 git cat-file commit HEAD >original &&
495 q_to_nul <original >munged &&
496 git hash-object -w -t commit --stdin <munged >name &&
497 git branch bad $(cat name) &&
499 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
500 test_i18ngrep nulInCommit warn.1 &&
501 git fsck 2>warn.2 &&
502 test_i18ngrep nulInCommit warn.2
506 # create a static test repo which is broken by omitting
507 # one particular object ($1, which is looked up via rev-parse
508 # in the new repository).
509 create_repo_missing () {
510 rm -rf missing &&
511 git init missing &&
513 cd missing &&
514 git commit -m one --allow-empty &&
515 mkdir subdir &&
516 echo content >subdir/file &&
517 git add subdir/file &&
518 git commit -m two &&
519 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
520 git tag -m foo tag $unrelated &&
521 sha1=$(git rev-parse --verify "$1") &&
522 path=$(echo $sha1 | sed 's|..|&/|') &&
523 rm .git/objects/$path
527 test_expect_success 'fsck notices missing blob' '
528 create_repo_missing HEAD:subdir/file &&
529 test_must_fail git -C missing fsck
532 test_expect_success 'fsck notices missing subtree' '
533 create_repo_missing HEAD:subdir &&
534 test_must_fail git -C missing fsck
537 test_expect_success 'fsck notices missing root tree' '
538 create_repo_missing HEAD^{tree} &&
539 test_must_fail git -C missing fsck
542 test_expect_success 'fsck notices missing parent' '
543 create_repo_missing HEAD^ &&
544 test_must_fail git -C missing fsck
547 test_expect_success 'fsck notices missing tagged object' '
548 create_repo_missing tag^{blob} &&
549 test_must_fail git -C missing fsck
552 test_expect_success 'fsck notices ref pointing to missing commit' '
553 create_repo_missing HEAD &&
554 test_must_fail git -C missing fsck
557 test_expect_success 'fsck notices ref pointing to missing tag' '
558 create_repo_missing tag &&
559 test_must_fail git -C missing fsck
562 test_expect_success 'fsck --connectivity-only' '
563 rm -rf connectivity-only &&
564 git init connectivity-only &&
566 cd connectivity-only &&
567 touch empty &&
568 git add empty &&
569 test_commit empty &&
571 # Drop the index now; we want to be sure that we
572 # recursively notice the broken objects
573 # because they are reachable from refs, not because
574 # they are in the index.
575 rm -f .git/index &&
577 # corrupt the blob, but in a way that we can still identify
578 # its type. That lets us see that --connectivity-only is
579 # not actually looking at the contents, but leaves it
580 # free to examine the type if it chooses.
581 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
582 blob=$(echo unrelated | git hash-object -w --stdin) &&
583 mv -f $(sha1_file $blob) $empty &&
585 test_must_fail git fsck --strict &&
586 git fsck --strict --connectivity-only &&
587 tree=$(git rev-parse HEAD:) &&
588 suffix=${tree#??} &&
589 tree=.git/objects/${tree%$suffix}/$suffix &&
590 rm -f $tree &&
591 echo invalid >$tree &&
592 test_must_fail git fsck --strict --connectivity-only
596 test_expect_success 'fsck --connectivity-only with explicit head' '
597 rm -rf connectivity-only &&
598 git init connectivity-only &&
600 cd connectivity-only &&
601 test_commit foo &&
602 rm -f .git/index &&
603 tree=$(git rev-parse HEAD^{tree}) &&
604 remove_object $(git rev-parse HEAD:foo.t) &&
605 test_must_fail git fsck --connectivity-only $tree
609 test_expect_success 'fsck --name-objects' '
610 rm -rf name-objects &&
611 git init name-objects &&
613 cd name-objects &&
614 test_commit julius caesar.t &&
615 test_commit augustus &&
616 test_commit caesar &&
617 remove_object $(git rev-parse julius:caesar.t) &&
618 test_must_fail git fsck --name-objects >out &&
619 tree=$(git rev-parse --verify julius:) &&
620 test_i18ngrep "$tree (refs/tags/julius:" out
624 test_expect_success 'alternate objects are correctly blamed' '
625 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
626 name=$(test_oid numeric) &&
627 path=$(test_oid_to_path "$name") &&
628 git init --bare alt.git &&
629 echo "../../alt.git/objects" >.git/objects/info/alternates &&
630 mkdir alt.git/objects/$(dirname $path) &&
631 >alt.git/objects/$(dirname $path)/$(basename $path) &&
632 test_must_fail git fsck >out 2>&1 &&
633 test_i18ngrep alt.git out
636 test_expect_success 'fsck errors in packed objects' '
637 git cat-file commit HEAD >basis &&
638 sed "s/</one/" basis >one &&
639 sed "s/</foo/" basis >two &&
640 one=$(git hash-object -t commit -w one) &&
641 two=$(git hash-object -t commit -w two) &&
642 pack=$(
644 echo $one &&
645 echo $two
646 } | git pack-objects .git/objects/pack/pack
647 ) &&
648 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
649 remove_object $one &&
650 remove_object $two &&
651 test_must_fail git fsck 2>out &&
652 test_i18ngrep "error in commit $one.* - bad name" out &&
653 test_i18ngrep "error in commit $two.* - bad name" out &&
654 ! grep corrupt out
657 test_expect_success 'fsck fails on corrupt packfile' '
658 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
659 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
661 # Corrupt the first byte of the first object. (It contains 3 type bits,
662 # at least one of which is not zero, so setting the first byte to 0 is
663 # sufficient.)
664 chmod a+w .git/objects/pack/pack-$pack.pack &&
665 printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
667 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
668 remove_object $hsh &&
669 test_must_fail git fsck 2>out &&
670 test_i18ngrep "checksum mismatch" out
673 test_expect_success 'fsck finds problems in duplicate loose objects' '
674 rm -rf broken-duplicate &&
675 git init broken-duplicate &&
677 cd broken-duplicate &&
678 test_commit duplicate &&
679 # no "-d" here, so we end up with duplicates
680 git repack &&
681 # now corrupt the loose copy
682 file=$(sha1_file "$(git rev-parse HEAD)") &&
683 rm "$file" &&
684 echo broken >"$file" &&
685 test_must_fail git fsck
689 test_expect_success 'fsck detects trailing loose garbage (commit)' '
690 git cat-file commit HEAD >basis &&
691 echo bump-commit-sha1 >>basis &&
692 commit=$(git hash-object -w -t commit basis) &&
693 file=$(sha1_file $commit) &&
694 test_when_finished "remove_object $commit" &&
695 chmod +w "$file" &&
696 echo garbage >>"$file" &&
697 test_must_fail git fsck 2>out &&
698 test_i18ngrep "garbage.*$commit" out
701 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
702 blob=$(echo trailing | git hash-object -w --stdin) &&
703 file=$(sha1_file $blob) &&
704 test_when_finished "remove_object $blob" &&
705 chmod +w "$file" &&
706 echo garbage >>"$file" &&
707 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
708 test_i18ngrep "garbage.*$blob" out
711 test_expect_success 'fsck detects truncated loose object' '
712 # make it big enough that we know we will truncate in the data
713 # portion, not the header
714 test-tool genrandom truncate 4096 >file &&
715 blob=$(git hash-object -w file) &&
716 file=$(sha1_file $blob) &&
717 test_when_finished "remove_object $blob" &&
718 test_copy_bytes 1024 <"$file" >tmp &&
719 rm "$file" &&
720 mv -f tmp "$file" &&
722 # check both regular and streaming code paths
723 test_must_fail git fsck 2>out &&
724 test_i18ngrep corrupt.*$blob out &&
726 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
727 test_i18ngrep corrupt.*$blob out
730 # for each of type, we have one version which is referenced by another object
731 # (and so while unreachable, not dangling), and another variant which really is
732 # dangling.
733 test_expect_success 'create dangling-object repository' '
734 git init dangling &&
736 cd dangling &&
737 blob=$(echo not-dangling | git hash-object -w --stdin) &&
738 dblob=$(echo dangling | git hash-object -w --stdin) &&
739 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
740 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
741 commit=$(git commit-tree $tree) &&
742 dcommit=$(git commit-tree -p $commit $tree) &&
744 cat >expect <<-EOF
745 dangling blob $dblob
746 dangling commit $dcommit
747 dangling tree $dtree
752 test_expect_success 'fsck notices dangling objects' '
754 cd dangling &&
755 git fsck >actual &&
756 # the output order is non-deterministic, as it comes from a hash
757 sort <actual >actual.sorted &&
758 test_i18ncmp expect actual.sorted
762 test_expect_success 'fsck --connectivity-only notices dangling objects' '
764 cd dangling &&
765 git fsck --connectivity-only >actual &&
766 # the output order is non-deterministic, as it comes from a hash
767 sort <actual >actual.sorted &&
768 test_i18ncmp expect actual.sorted
772 test_expect_success 'fsck $name notices bogus $name' '
773 test_must_fail git fsck bogus &&
774 test_must_fail git fsck $ZERO_OID
777 test_expect_success 'bogus head does not fallback to all heads' '
778 # set up a case that will cause a reachability complaint
779 echo to-be-deleted >foo &&
780 git add foo &&
781 blob=$(git rev-parse :foo) &&
782 test_when_finished "git rm --cached foo" &&
783 remove_object $blob &&
784 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
785 ! grep $blob out
788 # Corrupt the checksum on the index.
789 # Add 1 to the last byte in the SHA.
790 corrupt_index_checksum () {
791 perl -w -e '
792 use Fcntl ":seek";
793 open my $fh, "+<", ".git/index" or die "open: $!";
794 binmode $fh;
795 seek $fh, -1, SEEK_END or die "seek: $!";
796 read $fh, my $in_byte, 1 or die "read: $!";
798 $in_value = unpack("C", $in_byte);
799 $out_value = ($in_value + 1) & 255;
801 $out_byte = pack("C", $out_value);
803 seek $fh, -1, SEEK_END or die "seek: $!";
804 print $fh $out_byte;
805 close $fh or die "close: $!";
809 # Corrupt the checksum on the index and then
810 # verify that only fsck notices.
811 test_expect_success 'detect corrupt index file in fsck' '
812 cp .git/index .git/index.backup &&
813 test_when_finished "mv .git/index.backup .git/index" &&
814 corrupt_index_checksum &&
815 test_must_fail git fsck --cache 2>errors &&
816 test_i18ngrep "bad index file" errors
819 test_done