]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1450-fsck.sh
path: add repo_git_path and strbuf_repo_git_path
[thirdparty/git.git] / t / t1450-fsck.sh
CommitLineData
469e2ebf
JH
1#!/bin/sh
2
dbedf8bf
JN
3test_description='git fsck random collection of tests
4
5* (HEAD) B
6* (master) A
7'
469e2ebf
JH
8
9. ./test-lib.sh
10
11test_expect_success setup '
dbedf8bf 12 git config gc.auto 0 &&
0adc6a3d 13 git config i18n.commitencoding ISO-8859-1 &&
469e2ebf 14 test_commit A fileA one &&
0adc6a3d 15 git config --unset i18n.commitencoding &&
469e2ebf
JH
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 git tag -d A B &&
dbedf8bf
JN
19 git reflog expire --expire=now --all &&
20 >empty
469e2ebf
JH
21'
22
e15ef669
JH
23test_expect_success 'loose objects borrowed from alternate are not missing' '
24 mkdir another &&
25 (
26 cd another &&
27 git init &&
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
c6a13b2c 30 git fsck --no-dangling >../actual 2>&1
dbedf8bf 31 ) &&
dbedf8bf 32 test_cmp empty actual
e15ef669
JH
33'
34
dbedf8bf
JN
35test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
37 test_cmp empty actual
0adc6a3d
JN
38'
39
02a6552c
TR
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.
43
dbedf8bf
JN
44test_expect_success 'setup: helpers for corruption tests' '
45 sha1_file() {
0b20f1a2
JK
46 remainder=${1#??} &&
47 firsttwo=${1%$remainder} &&
48 echo ".git/objects/$firsttwo/$remainder"
dbedf8bf
JN
49 } &&
50
51 remove_object() {
0b20f1a2 52 rm "$(sha1_file "$1")"
dbedf8bf
JN
53 }
54'
55
02a6552c
TR
56test_expect_success 'object with bad sha1' '
57 sha=$(echo blob | git hash-object -w --stdin) &&
02a6552c
TR
58 old=$(echo $sha | sed "s+^..+&/+") &&
59 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
a48fcd83 60 sha="$(dirname $new)$(basename $new)" &&
02a6552c 61 mv .git/objects/$old .git/objects/$new &&
dbedf8bf 62 test_when_finished "remove_object $sha" &&
02a6552c 63 git update-index --add --cacheinfo 100644 $sha foo &&
dbedf8bf 64 test_when_finished "git read-tree -u --reset HEAD" &&
02a6552c 65 tree=$(git write-tree) &&
dbedf8bf 66 test_when_finished "remove_object $tree" &&
02a6552c 67 cmt=$(echo bogus | git commit-tree $tree) &&
dbedf8bf 68 test_when_finished "remove_object $cmt" &&
02a6552c 69 git update-ref refs/heads/bogus $cmt &&
dbedf8bf
JN
70 test_when_finished "git update-ref -d refs/heads/bogus" &&
71
2e770fe4 72 test_must_fail git fsck 2>out &&
dbedf8bf
JN
73 cat out &&
74 grep "$sha.*corrupt" out
02a6552c
TR
75'
76
77test_expect_success 'branch pointing to non-commit' '
dbedf8bf
JN
78 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79 test_when_finished "git update-ref -d refs/heads/invalid" &&
122f76f5 80 test_must_fail git fsck 2>out &&
dbedf8bf
JN
81 cat out &&
82 grep "not a commit" out
02a6552c
TR
83'
84
122f76f5
JH
85test_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
93'
94
95test_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
103'
104
daae1922
JN
105test_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) &&
dbedf8bf 109 test_when_finished "remove_object $new" &&
daae1922 110 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 111 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922
JN
112 git fsck 2>out &&
113 cat out &&
dbedf8bf 114 ! grep "commit $new" out
daae1922 115'
daae1922 116
daae1922
JN
117test_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) &&
dbedf8bf 121 test_when_finished "remove_object $new" &&
daae1922 122 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 123 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 124 test_must_fail git fsck 2>out &&
daae1922
JN
125 cat out &&
126 grep "error in commit $new" out
127'
02a6552c 128
53f53cff 129test_expect_success 'missing < email delimiter is reported nicely' '
e3c98120
DI
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" &&
2e770fe4 136 test_must_fail git fsck 2>out &&
e3c98120
DI
137 cat out &&
138 grep "error in commit $new.* - bad name" out
139'
140
53f53cff 141test_expect_success 'missing email is reported nicely' '
e3c98120
DI
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" &&
2e770fe4 148 test_must_fail git fsck 2>out &&
e3c98120
DI
149 cat out &&
150 grep "error in commit $new.* - missing email" out
151'
152
53f53cff 153test_expect_success '> in name is reported' '
e3c98120
DI
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" &&
2e770fe4 160 test_must_fail git fsck 2>out &&
e3c98120
DI
161 cat out &&
162 grep "error in commit $new" out
163'
164
d4b8de04
JK
165# date is 2^64 + 1
166test_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" &&
2e770fe4 174 test_must_fail git fsck 2>out &&
d4b8de04
JK
175 cat out &&
176 grep "error in commit $new.*integer overflow" out
177'
178
80c7f5a0
RS
179test_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
189'
190
8354fa3d 191test_expect_success 'tree object with duplicate entries' '
1ada11ee 192 test_when_finished "for i in \$T; do remove_object \$i; done" &&
2e770fe4
JK
193 T=$(
194 GIT_INDEX_FILE=test-index &&
195 export GIT_INDEX_FILE &&
196 rm -f test-index &&
197 >x &&
198 git add x &&
1ada11ee 199 git rev-parse :x &&
2e770fe4 200 T=$(git write-tree) &&
1ada11ee 201 echo $T &&
2e770fe4
JK
202 (
203 git cat-file tree $T &&
204 git cat-file tree $T
205 ) |
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
210'
211
8354fa3d
DT
212test_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
223'
224
4551d035 225test_expect_success 'tag pointing to nonexistent' '
a48fcd83 226 cat >invalid-tag <<-\EOF &&
dbedf8bf
JN
227 object ffffffffffffffffffffffffffffffffffffffff
228 type commit
229 tag invalid
230 tagger T A Gger <tagger@example.com> 1234567890 -0000
231
232 This is an invalid tag.
233 EOF
234
235 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
236 test_when_finished "remove_object $tag" &&
237 echo $tag >.git/refs/tags/invalid &&
238 test_when_finished "git update-ref -d refs/tags/invalid" &&
4551d035 239 test_must_fail git fsck --tags >out &&
02a6552c 240 cat out &&
dbedf8bf 241 grep "broken link" out
02a6552c
TR
242'
243
4551d035 244test_expect_success 'tag pointing to something else than its type' '
dbedf8bf
JN
245 sha=$(echo blob | git hash-object -w --stdin) &&
246 test_when_finished "remove_object $sha" &&
247 cat >wrong-tag <<-EOF &&
248 object $sha
249 type commit
250 tag wrong
251 tagger T A Gger <tagger@example.com> 1234567890 -0000
252
253 This is an invalid tag.
254 EOF
255
256 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
257 test_when_finished "remove_object $tag" &&
258 echo $tag >.git/refs/tags/wrong &&
259 test_when_finished "git update-ref -d refs/tags/wrong" &&
9dad83be 260 test_must_fail git fsck --tags
02a6552c
TR
261'
262
90e3e5f0
JS
263test_expect_success 'tag with incorrect tag name & missing tagger' '
264 sha=$(git rev-parse HEAD) &&
265 cat >wrong-tag <<-EOF &&
266 object $sha
267 type commit
268 tag wrong name format
269
270 This is an invalid tag.
271 EOF
272
273 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
274 test_when_finished "remove_object $tag" &&
275 echo $tag >.git/refs/tags/wrong &&
276 test_when_finished "git update-ref -d refs/tags/wrong" &&
277 git fsck --tags 2>out &&
7add4419
JK
278
279 cat >expect <<-EOF &&
71ab8fa8
JS
280 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
281 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
7add4419
JK
282 EOF
283 test_cmp expect out
90e3e5f0
JS
284'
285
b659605d
JH
286test_expect_success 'tag with bad tagger' '
287 sha=$(git rev-parse HEAD) &&
288 cat >wrong-tag <<-EOF &&
289 object $sha
290 type commit
291 tag not-quite-wrong
292 tagger Bad Tagger Name
293
294 This is an invalid tag.
295 EOF
296
297 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
298 test_when_finished "remove_object $tag" &&
299 echo $tag >.git/refs/tags/wrong &&
300 test_when_finished "git update-ref -d refs/tags/wrong" &&
301 test_must_fail git fsck --tags 2>out &&
302 grep "error in tag .*: invalid author/committer" out
303'
304
8a272f29 305test_expect_success 'tag with NUL in header' '
80c7f5a0
RS
306 sha=$(git rev-parse HEAD) &&
307 q_to_nul >tag-NUL-header <<-EOF &&
308 object $sha
309 type commit
310 tag contains-Q-in-header
311 tagger T A Gger <tagger@example.com> 1234567890 -0000
312
313 This is an invalid tag.
314 EOF
315
316 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
317 test_when_finished "remove_object $tag" &&
318 echo $tag >.git/refs/tags/wrong &&
319 test_when_finished "git update-ref -d refs/tags/wrong" &&
320 test_must_fail git fsck --tags 2>out &&
321 cat out &&
322 grep "error in tag $tag.*unterminated header: NUL at offset" out
323'
324
dbedf8bf
JN
325test_expect_success 'cleaned up' '
326 git fsck >actual 2>&1 &&
327 test_cmp empty actual
328'
02a6552c 329
cb8da705
CB
330test_expect_success 'rev-list --verify-objects' '
331 git rev-list --verify-objects --all >/dev/null 2>out &&
332 test_cmp empty out
333'
334
335test_expect_success 'rev-list --verify-objects with bad sha1' '
336 sha=$(echo blob | git hash-object -w --stdin) &&
337 old=$(echo $sha | sed "s+^..+&/+") &&
338 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
339 sha="$(dirname $new)$(basename $new)" &&
340 mv .git/objects/$old .git/objects/$new &&
341 test_when_finished "remove_object $sha" &&
342 git update-index --add --cacheinfo 100644 $sha foo &&
343 test_when_finished "git read-tree -u --reset HEAD" &&
344 tree=$(git write-tree) &&
345 test_when_finished "remove_object $tree" &&
346 cmt=$(echo bogus | git commit-tree $tree) &&
347 test_when_finished "remove_object $cmt" &&
348 git update-ref refs/heads/bogus $cmt &&
349 test_when_finished "git update-ref -d refs/heads/bogus" &&
350
351 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
352 cat out &&
353 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
354'
355
2becf00f
JS
356test_expect_success 'force fsck to ignore double author' '
357 git cat-file commit HEAD >basis &&
358 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
359 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
360 test_when_finished "remove_object $new" &&
361 git update-ref refs/heads/bogus "$new" &&
362 test_when_finished "git update-ref -d refs/heads/bogus" &&
363 test_must_fail git fsck &&
364 git -c fsck.multipleAuthors=ignore fsck
365'
366
c479d14a
JK
367_bz='\0'
368_bz5="$_bz$_bz$_bz$_bz$_bz"
369_bz20="$_bz5$_bz5$_bz5$_bz5"
370
371test_expect_success 'fsck notices blob entry pointing to null sha1' '
372 (git init null-blob &&
373 cd null-blob &&
374 sha=$(printf "100644 file$_bz$_bz20" |
375 git hash-object -w --stdin -t tree) &&
376 git fsck 2>out &&
377 cat out &&
378 grep "warning.*null sha1" out
379 )
380'
381
382test_expect_success 'fsck notices submodule entry pointing to null sha1' '
383 (git init null-commit &&
384 cd null-commit &&
385 sha=$(printf "160000 submodule$_bz$_bz20" |
386 git hash-object -w --stdin -t tree) &&
387 git fsck 2>out &&
388 cat out &&
389 grep "warning.*null sha1" out
390 )
391'
392
a18fcc9f 393while read name path pretty; do
450870cb 394 while read mode type; do
a18fcc9f
JK
395 : ${pretty:=$path}
396 test_expect_success "fsck notices $pretty as $type" '
450870cb
JK
397 (
398 git init $name-$type &&
399 cd $name-$type &&
400 echo content >file &&
401 git add file &&
402 git commit -m base &&
403 blob=$(git rev-parse :file) &&
404 tree=$(git rev-parse HEAD^{tree}) &&
405 value=$(eval "echo \$$type") &&
406 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
d08c13b9 407 bad_tree=$(git mktree <bad) &&
450870cb
JK
408 git fsck 2>out &&
409 cat out &&
d08c13b9 410 grep "warning.*tree $bad_tree" out
450870cb
JK
411 )'
412 done <<-\EOF
413 100644 blob
414 040000 tree
415 EOF
a18fcc9f 416done <<-EOF
450870cb
JK
417dot .
418dotdot ..
419dotgit .git
76e86fc6 420dotgit-case .GIT
a18fcc9f 421dotgit-unicode .gI${u200c}T .gI{u200c}T
d08c13b9
JS
422dotgit-case2 .Git
423git-tilde1 git~1
424dotgitdot .git.
425dot-backslash-case .\\\\.GIT\\\\foobar
426dotgit-case-backslash .git\\\\foobar
450870cb 427EOF
5c17f512 428
6aaf956b
JK
429test_expect_success 'fsck allows .Ňit' '
430 (
431 git init not-dotgit &&
432 cd not-dotgit &&
433 echo content >file &&
434 git add file &&
435 git commit -m base &&
436 blob=$(git rev-parse :file) &&
437 printf "100644 blob $blob\t.\\305\\207it" >tree &&
438 tree=$(git mktree <tree) &&
439 git fsck 2>err &&
440 test_line_count = 0 err
441 )
442'
443
6d2d780f
JH
444test_expect_success 'NUL in commit' '
445 rm -fr nul-in-commit &&
446 git init nul-in-commit &&
447 (
448 cd nul-in-commit &&
449 git commit --allow-empty -m "initial commitQNUL after message" &&
450 git cat-file commit HEAD >original &&
451 q_to_nul <original >munged &&
452 git hash-object -w -t commit --stdin <munged >name &&
453 git branch bad $(cat name) &&
454
455 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
456 grep nulInCommit warn.1 &&
457 git fsck 2>warn.2 &&
458 grep nulInCommit warn.2
459 )
460'
461
30d1038d
JK
462# create a static test repo which is broken by omitting
463# one particular object ($1, which is looked up via rev-parse
464# in the new repository).
465create_repo_missing () {
466 rm -rf missing &&
467 git init missing &&
468 (
469 cd missing &&
470 git commit -m one --allow-empty &&
471 mkdir subdir &&
472 echo content >subdir/file &&
473 git add subdir/file &&
474 git commit -m two &&
475 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
476 git tag -m foo tag $unrelated &&
477 sha1=$(git rev-parse --verify "$1") &&
478 path=$(echo $sha1 | sed 's|..|&/|') &&
479 rm .git/objects/$path
480 )
481}
482
483test_expect_success 'fsck notices missing blob' '
484 create_repo_missing HEAD:subdir/file &&
485 test_must_fail git -C missing fsck
486'
487
488test_expect_success 'fsck notices missing subtree' '
489 create_repo_missing HEAD:subdir &&
490 test_must_fail git -C missing fsck
491'
492
493test_expect_success 'fsck notices missing root tree' '
494 create_repo_missing HEAD^{tree} &&
495 test_must_fail git -C missing fsck
496'
497
498test_expect_success 'fsck notices missing parent' '
499 create_repo_missing HEAD^ &&
500 test_must_fail git -C missing fsck
501'
502
503test_expect_success 'fsck notices missing tagged object' '
504 create_repo_missing tag^{blob} &&
505 test_must_fail git -C missing fsck
506'
507
508test_expect_success 'fsck notices ref pointing to missing commit' '
509 create_repo_missing HEAD &&
510 test_must_fail git -C missing fsck
511'
512
513test_expect_success 'fsck notices ref pointing to missing tag' '
514 create_repo_missing tag &&
515 test_must_fail git -C missing fsck
516'
517
02976bf8
JS
518test_expect_success 'fsck --connectivity-only' '
519 rm -rf connectivity-only &&
520 git init connectivity-only &&
521 (
522 cd connectivity-only &&
523 touch empty &&
524 git add empty &&
525 test_commit empty &&
3e3f8bd6
JK
526
527 # Drop the index now; we want to be sure that we
528 # recursively notice the broken objects
529 # because they are reachable from refs, not because
530 # they are in the index.
531 rm -f .git/index &&
532
533 # corrupt the blob, but in a way that we can still identify
534 # its type. That lets us see that --connectivity-only is
535 # not actually looking at the contents, but leaves it
536 # free to examine the type if it chooses.
02976bf8 537 empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
3e3f8bd6 538 blob=$(echo unrelated | git hash-object -w --stdin) &&
c20d4d70 539 mv -f $(sha1_file $blob) $empty &&
3e3f8bd6 540
02976bf8
JS
541 test_must_fail git fsck --strict &&
542 git fsck --strict --connectivity-only &&
543 tree=$(git rev-parse HEAD:) &&
544 suffix=${tree#??} &&
545 tree=.git/objects/${tree%$suffix}/$suffix &&
546 rm -f $tree &&
547 echo invalid >$tree &&
548 test_must_fail git fsck --strict --connectivity-only
549 )
550'
551
3e3f8bd6
JK
552test_expect_success 'fsck --connectivity-only with explicit head' '
553 rm -rf connectivity-only &&
554 git init connectivity-only &&
555 (
556 cd connectivity-only &&
557 test_commit foo &&
558 rm -f .git/index &&
559 tree=$(git rev-parse HEAD^{tree}) &&
560 remove_object $(git rev-parse HEAD:foo.t) &&
561 test_must_fail git fsck --connectivity-only $tree
562 )
563'
564
90cf590f
JS
565test_expect_success 'fsck --name-objects' '
566 rm -rf name-objects &&
567 git init name-objects &&
568 (
569 cd name-objects &&
570 test_commit julius caesar.t &&
571 test_commit augustus &&
572 test_commit caesar &&
0b20f1a2 573 remove_object $(git rev-parse julius:caesar.t) &&
90cf590f
JS
574 test_must_fail git fsck --name-objects >out &&
575 tree=$(git rev-parse --verify julius:) &&
576 grep "$tree (\(refs/heads/master\|HEAD\)@{[0-9]*}:" out
577 )
578'
579
771e7d57
JK
580test_expect_success 'alternate objects are correctly blamed' '
581 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
582 git init --bare alt.git &&
583 echo "../../alt.git/objects" >.git/objects/info/alternates &&
584 mkdir alt.git/objects/12 &&
585 >alt.git/objects/12/34567890123456789012345678901234567890 &&
586 test_must_fail git fsck >out 2>&1 &&
587 grep alt.git out
588'
589
118e6cea
JK
590test_expect_success 'fsck errors in packed objects' '
591 git cat-file commit HEAD >basis &&
592 sed "s/</one/" basis >one &&
593 sed "s/</foo/" basis >two &&
594 one=$(git hash-object -t commit -w one) &&
595 two=$(git hash-object -t commit -w two) &&
596 pack=$(
597 {
598 echo $one &&
599 echo $two
600 } | git pack-objects .git/objects/pack/pack
601 ) &&
602 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
603 remove_object $one &&
604 remove_object $two &&
605 test_must_fail git fsck 2>out &&
606 grep "error in commit $one.* - bad name" out &&
607 grep "error in commit $two.* - bad name" out &&
608 ! grep corrupt out
609'
610
c68b489e
JK
611test_expect_success 'fsck finds problems in duplicate loose objects' '
612 rm -rf broken-duplicate &&
613 git init broken-duplicate &&
614 (
615 cd broken-duplicate &&
616 test_commit duplicate &&
617 # no "-d" here, so we end up with duplicates
618 git repack &&
619 # now corrupt the loose copy
620 file=$(sha1_file "$(git rev-parse HEAD)") &&
621 rm "$file" &&
622 echo broken >"$file" &&
623 test_must_fail git fsck
624 )
625'
626
cce044df
JK
627test_expect_success 'fsck detects trailing loose garbage (commit)' '
628 git cat-file commit HEAD >basis &&
629 echo bump-commit-sha1 >>basis &&
630 commit=$(git hash-object -w -t commit basis) &&
631 file=$(sha1_file $commit) &&
632 test_when_finished "remove_object $commit" &&
633 chmod +w "$file" &&
634 echo garbage >>"$file" &&
635 test_must_fail git fsck 2>out &&
636 test_i18ngrep "garbage.*$commit" out
637'
638
639test_expect_success 'fsck detects trailing loose garbage (blob)' '
640 blob=$(echo trailing | git hash-object -w --stdin) &&
641 file=$(sha1_file $blob) &&
642 test_when_finished "remove_object $blob" &&
643 chmod +w "$file" &&
644 echo garbage >>"$file" &&
645 test_must_fail git fsck 2>out &&
646 test_i18ngrep "garbage.*$blob" out
647'
648
b4584e4f
JK
649# for each of type, we have one version which is referenced by another object
650# (and so while unreachable, not dangling), and another variant which really is
651# dangling.
652test_expect_success 'fsck notices dangling objects' '
653 git init dangling &&
654 (
655 cd dangling &&
656 blob=$(echo not-dangling | git hash-object -w --stdin) &&
657 dblob=$(echo dangling | git hash-object -w --stdin) &&
658 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
659 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
660 commit=$(git commit-tree $tree) &&
661 dcommit=$(git commit-tree -p $commit $tree) &&
662
663 cat >expect <<-EOF &&
664 dangling blob $dblob
665 dangling commit $dcommit
666 dangling tree $dtree
667 EOF
668
669 git fsck >actual &&
670 # the output order is non-deterministic, as it comes from a hash
671 sort <actual >actual.sorted &&
672 test_cmp expect actual.sorted
673 )
674'
675
c6c7b16d
JK
676test_expect_success 'fsck $name notices bogus $name' '
677 test_must_fail git fsck bogus &&
678 test_must_fail git fsck $_z40
679'
680
c3271a0e
JK
681test_expect_success 'bogus head does not fallback to all heads' '
682 # set up a case that will cause a reachability complaint
683 echo to-be-deleted >foo &&
684 git add foo &&
685 blob=$(git rev-parse :foo) &&
686 test_when_finished "git rm --cached foo" &&
687 remove_object $blob &&
688 test_must_fail git fsck $_z40 >out 2>&1 &&
689 ! grep $blob out
690'
691
4d9bc37f
JH
692# Corrupt the checksum on the index.
693# Add 1 to the last byte in the SHA.
694corrupt_index_checksum () {
695 perl -w -e '
696 use Fcntl ":seek";
697 open my $fh, "+<", ".git/index" or die "open: $!";
698 binmode $fh;
699 seek $fh, -1, SEEK_END or die "seek: $!";
700 read $fh, my $in_byte, 1 or die "read: $!";
701
702 $in_value = unpack("C", $in_byte);
703 $out_value = ($in_value + 1) & 255;
704
705 $out_byte = pack("C", $out_value);
706
707 seek $fh, -1, SEEK_END or die "seek: $!";
708 print $fh $out_byte;
709 close $fh or die "close: $!";
710 '
711}
712
713# Corrupt the checksum on the index and then
714# verify that only fsck notices.
a33fc72f
JH
715test_expect_success 'detect corrupt index file in fsck' '
716 cp .git/index .git/index.backup &&
717 test_when_finished "mv .git/index.backup .git/index" &&
4d9bc37f
JH
718 corrupt_index_checksum &&
719 test_must_fail git fsck --cache 2>errors &&
720 grep "bad index file" errors
a33fc72f
JH
721'
722
469e2ebf 723test_done