]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1450-fsck.sh
Merge branch 'ab/detox-gettext-tests'
[thirdparty/git.git] / t / t1450-fsck.sh
1 #!/bin/sh
2
3 test_description='git fsck random collection of tests
4
5 * (HEAD) B
6 * (main) A
7 '
8
9 . ./test-lib.sh
10
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 '
21
22 test_expect_success 'loose objects borrowed from alternate are not missing' '
23 mkdir another &&
24 (
25 cd another &&
26 git init &&
27 echo ../../../.git/objects >.git/objects/info/alternates &&
28 test_commit C fileC one &&
29 git fsck --no-dangling >../actual 2>&1
30 ) &&
31 test_must_be_empty actual
32 '
33
34 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
35 git fsck >actual 2>&1 &&
36 test_must_be_empty actual
37 '
38
39 # Corruption tests follow. Make sure to remove all traces of the
40 # specific corruption you test afterwards, lest a later test trip over
41 # it.
42
43 sha1_file () {
44 git rev-parse --git-path objects/$(test_oid_to_path "$1")
45 }
46
47 remove_object () {
48 rm "$(sha1_file "$1")"
49 }
50
51 test_expect_success 'object with bad sha1' '
52 sha=$(echo blob | git hash-object -w --stdin) &&
53 old=$(test_oid_to_path "$sha") &&
54 new=$(dirname $old)/$(test_oid ff_2) &&
55 sha="$(dirname $new)$(basename $new)" &&
56 mv .git/objects/$old .git/objects/$new &&
57 test_when_finished "remove_object $sha" &&
58 git update-index --add --cacheinfo 100644 $sha foo &&
59 test_when_finished "git read-tree -u --reset HEAD" &&
60 tree=$(git write-tree) &&
61 test_when_finished "remove_object $tree" &&
62 cmt=$(echo bogus | git commit-tree $tree) &&
63 test_when_finished "remove_object $cmt" &&
64 git update-ref refs/heads/bogus $cmt &&
65 test_when_finished "git update-ref -d refs/heads/bogus" &&
66
67 test_must_fail git fsck 2>out &&
68 test_i18ngrep "$sha.*corrupt" out
69 '
70
71 test_expect_success 'branch pointing to non-commit' '
72 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
73 test_when_finished "git update-ref -d refs/heads/invalid" &&
74 test_must_fail git fsck 2>out &&
75 test_i18ngrep "not a commit" out
76 '
77
78 test_expect_success 'HEAD link pointing at a funny object' '
79 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
80 mv .git/HEAD .git/SAVED_HEAD &&
81 echo $ZERO_OID >.git/HEAD &&
82 # avoid corrupt/broken HEAD from interfering with repo discovery
83 test_must_fail env GIT_DIR=.git git fsck 2>out &&
84 test_i18ngrep "detached HEAD points" out
85 '
86
87 test_expect_success 'HEAD link pointing at a funny place' '
88 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
89 mv .git/HEAD .git/SAVED_HEAD &&
90 echo "ref: refs/funny/place" >.git/HEAD &&
91 # avoid corrupt/broken HEAD from interfering with repo discovery
92 test_must_fail env GIT_DIR=.git git fsck 2>out &&
93 test_i18ngrep "HEAD points to something strange" out
94 '
95
96 test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
97 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
98 test_when_finished "rm -rf .git/worktrees wt" &&
99 git worktree add wt &&
100 mv .git/HEAD .git/SAVED_HEAD &&
101 echo $ZERO_OID >.git/HEAD &&
102 # avoid corrupt/broken HEAD from interfering with repo discovery
103 test_must_fail git -C wt fsck 2>out &&
104 test_i18ngrep "main-worktree/HEAD: detached HEAD points" out
105 '
106
107 test_expect_success 'other worktree HEAD link pointing at a funny object' '
108 test_when_finished "rm -rf .git/worktrees other" &&
109 git worktree add other &&
110 echo $ZERO_OID >.git/worktrees/other/HEAD &&
111 test_must_fail git fsck 2>out &&
112 test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out
113 '
114
115 test_expect_success 'other worktree HEAD link pointing at missing object' '
116 test_when_finished "rm -rf .git/worktrees other" &&
117 git worktree add other &&
118 echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
119 test_must_fail git fsck 2>out &&
120 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
121 '
122
123 test_expect_success 'other worktree HEAD link pointing at a funny place' '
124 test_when_finished "rm -rf .git/worktrees other" &&
125 git worktree add other &&
126 echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
127 test_must_fail git fsck 2>out &&
128 test_i18ngrep "worktrees/other/HEAD points to something strange" out
129 '
130
131 test_expect_success 'commit with multiple signatures is okay' '
132 git cat-file commit HEAD >basis &&
133 cat >sigs <<-EOF &&
134 gpgsig -----BEGIN PGP SIGNATURE-----
135 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
136 -----END PGP SIGNATURE-----
137 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
138 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
139 -----END PGP SIGNATURE-----
140 EOF
141 sed -e "/^committer/q" basis >okay &&
142 cat sigs >>okay &&
143 echo >>okay &&
144 sed -e "1,/^$/d" basis >>okay &&
145 cat okay &&
146 new=$(git hash-object -t commit -w --stdin <okay) &&
147 test_when_finished "remove_object $new" &&
148 git update-ref refs/heads/bogus "$new" &&
149 test_when_finished "git update-ref -d refs/heads/bogus" &&
150 git fsck 2>out &&
151 cat out &&
152 ! grep "commit $new" out
153 '
154
155 test_expect_success 'email without @ is okay' '
156 git cat-file commit HEAD >basis &&
157 sed "s/@/AT/" basis >okay &&
158 new=$(git hash-object -t commit -w --stdin <okay) &&
159 test_when_finished "remove_object $new" &&
160 git update-ref refs/heads/bogus "$new" &&
161 test_when_finished "git update-ref -d refs/heads/bogus" &&
162 git fsck 2>out &&
163 ! grep "commit $new" out
164 '
165
166 test_expect_success 'email with embedded > is not okay' '
167 git cat-file commit HEAD >basis &&
168 sed "s/@[a-z]/&>/" basis >bad-email &&
169 new=$(git hash-object -t commit -w --stdin <bad-email) &&
170 test_when_finished "remove_object $new" &&
171 git update-ref refs/heads/bogus "$new" &&
172 test_when_finished "git update-ref -d refs/heads/bogus" &&
173 test_must_fail git fsck 2>out &&
174 test_i18ngrep "error in commit $new" out
175 '
176
177 test_expect_success 'missing < email delimiter is reported nicely' '
178 git cat-file commit HEAD >basis &&
179 sed "s/<//" basis >bad-email-2 &&
180 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
181 test_when_finished "remove_object $new" &&
182 git update-ref refs/heads/bogus "$new" &&
183 test_when_finished "git update-ref -d refs/heads/bogus" &&
184 test_must_fail git fsck 2>out &&
185 test_i18ngrep "error in commit $new.* - bad name" out
186 '
187
188 test_expect_success 'missing email is reported nicely' '
189 git cat-file commit HEAD >basis &&
190 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
191 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
192 test_when_finished "remove_object $new" &&
193 git update-ref refs/heads/bogus "$new" &&
194 test_when_finished "git update-ref -d refs/heads/bogus" &&
195 test_must_fail git fsck 2>out &&
196 test_i18ngrep "error in commit $new.* - missing email" out
197 '
198
199 test_expect_success '> in name is reported' '
200 git cat-file commit HEAD >basis &&
201 sed "s/ </> </" basis >bad-email-4 &&
202 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
203 test_when_finished "remove_object $new" &&
204 git update-ref refs/heads/bogus "$new" &&
205 test_when_finished "git update-ref -d refs/heads/bogus" &&
206 test_must_fail git fsck 2>out &&
207 test_i18ngrep "error in commit $new" out
208 '
209
210 # date is 2^64 + 1
211 test_expect_success 'integer overflow in timestamps is reported' '
212 git cat-file commit HEAD >basis &&
213 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
214 <basis >bad-timestamp &&
215 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
216 test_when_finished "remove_object $new" &&
217 git update-ref refs/heads/bogus "$new" &&
218 test_when_finished "git update-ref -d refs/heads/bogus" &&
219 test_must_fail git fsck 2>out &&
220 test_i18ngrep "error in commit $new.*integer overflow" out
221 '
222
223 test_expect_success 'commit with NUL in header' '
224 git cat-file commit HEAD >basis &&
225 sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
226 new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
227 test_when_finished "remove_object $new" &&
228 git update-ref refs/heads/bogus "$new" &&
229 test_when_finished "git update-ref -d refs/heads/bogus" &&
230 test_must_fail git fsck 2>out &&
231 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
232 '
233
234 test_expect_success 'tree object with duplicate entries' '
235 test_when_finished "for i in \$T; do remove_object \$i; done" &&
236 T=$(
237 GIT_INDEX_FILE=test-index &&
238 export GIT_INDEX_FILE &&
239 rm -f test-index &&
240 >x &&
241 git add x &&
242 git rev-parse :x &&
243 T=$(git write-tree) &&
244 echo $T &&
245 (
246 git cat-file tree $T &&
247 git cat-file tree $T
248 ) |
249 git hash-object -w -t tree --stdin
250 ) &&
251 test_must_fail git fsck 2>out &&
252 test_i18ngrep "error in tree .*contains duplicate file entries" out
253 '
254
255 check_duplicate_names () {
256 expect=$1 &&
257 shift &&
258 names=$@ &&
259 test_expect_$expect "tree object with duplicate names: $names" '
260 test_when_finished "remove_object \$blob" &&
261 test_when_finished "remove_object \$tree" &&
262 test_when_finished "remove_object \$badtree" &&
263 blob=$(echo blob | git hash-object -w --stdin) &&
264 printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
265 tree=$(git mktree <tree) &&
266 for name in $names
267 do
268 case "$name" in
269 */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
270 *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
271 esac
272 done >badtree &&
273 badtree=$(git mktree <badtree) &&
274 test_must_fail git fsck 2>out &&
275 test_i18ngrep "$badtree" out &&
276 test_i18ngrep "error in tree .*contains duplicate file entries" out
277 '
278 }
279
280 check_duplicate_names success x x.1 x/
281 check_duplicate_names success x x.1.2 x.1/ x/
282 check_duplicate_names success x x.1 x.1.2 x/
283
284 test_expect_success 'unparseable tree object' '
285 test_oid_cache <<-\EOF &&
286 junk sha1:twenty-bytes-of-junk
287 junk sha256:twenty-bytes-of-junk-twelve-more
288 EOF
289
290 test_when_finished "git update-ref -d refs/heads/wrong" &&
291 test_when_finished "remove_object \$tree_sha1" &&
292 test_when_finished "remove_object \$commit_sha1" &&
293 junk=$(test_oid junk) &&
294 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
295 commit_sha1=$(git commit-tree $tree_sha1) &&
296 git update-ref refs/heads/wrong $commit_sha1 &&
297 test_must_fail git fsck 2>out &&
298 test_i18ngrep "error: empty filename in tree entry" out &&
299 test_i18ngrep "$tree_sha1" out &&
300 test_i18ngrep ! "fatal: empty filename in tree entry" out
301 '
302
303 test_expect_success 'tree entry with type mismatch' '
304 test_when_finished "remove_object \$blob" &&
305 test_when_finished "remove_object \$tree" &&
306 test_when_finished "remove_object \$commit" &&
307 test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
308 blob=$(echo blob | git hash-object -w --stdin) &&
309 blob_bin=$(echo $blob | hex2oct) &&
310 tree=$(
311 printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
312 git hash-object -t tree --stdin -w --literally
313 ) &&
314 commit=$(git commit-tree $tree) &&
315 git update-ref refs/heads/type_mismatch $commit &&
316 test_must_fail git fsck >out 2>&1 &&
317 test_i18ngrep "is a blob, not a tree" out &&
318 test_i18ngrep ! "dangling blob" out
319 '
320
321 test_expect_success 'tag pointing to nonexistent' '
322 badoid=$(test_oid deadbeef) &&
323 cat >invalid-tag <<-EOF &&
324 object $badoid
325 type commit
326 tag invalid
327 tagger T A Gger <tagger@example.com> 1234567890 -0000
328
329 This is an invalid tag.
330 EOF
331
332 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
333 test_when_finished "remove_object $tag" &&
334 echo $tag >.git/refs/tags/invalid &&
335 test_when_finished "git update-ref -d refs/tags/invalid" &&
336 test_must_fail git fsck --tags >out &&
337 test_i18ngrep "broken link" out
338 '
339
340 test_expect_success 'tag pointing to something else than its type' '
341 sha=$(echo blob | git hash-object -w --stdin) &&
342 test_when_finished "remove_object $sha" &&
343 cat >wrong-tag <<-EOF &&
344 object $sha
345 type commit
346 tag wrong
347 tagger T A Gger <tagger@example.com> 1234567890 -0000
348
349 This is an invalid tag.
350 EOF
351
352 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
353 test_when_finished "remove_object $tag" &&
354 echo $tag >.git/refs/tags/wrong &&
355 test_when_finished "git update-ref -d refs/tags/wrong" &&
356 test_must_fail git fsck --tags
357 '
358
359 test_expect_success 'tag with incorrect tag name & missing tagger' '
360 sha=$(git rev-parse HEAD) &&
361 cat >wrong-tag <<-EOF &&
362 object $sha
363 type commit
364 tag wrong name format
365
366 This is an invalid tag.
367 EOF
368
369 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
370 test_when_finished "remove_object $tag" &&
371 echo $tag >.git/refs/tags/wrong &&
372 test_when_finished "git update-ref -d refs/tags/wrong" &&
373 git fsck --tags 2>out &&
374
375 cat >expect <<-EOF &&
376 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
377 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
378 EOF
379 test_cmp expect out
380 '
381
382 test_expect_success 'tag with bad tagger' '
383 sha=$(git rev-parse HEAD) &&
384 cat >wrong-tag <<-EOF &&
385 object $sha
386 type commit
387 tag not-quite-wrong
388 tagger Bad Tagger Name
389
390 This is an invalid tag.
391 EOF
392
393 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
394 test_when_finished "remove_object $tag" &&
395 echo $tag >.git/refs/tags/wrong &&
396 test_when_finished "git update-ref -d refs/tags/wrong" &&
397 test_must_fail git fsck --tags 2>out &&
398 test_i18ngrep "error in tag .*: invalid author/committer" out
399 '
400
401 test_expect_success 'tag with NUL in header' '
402 sha=$(git rev-parse HEAD) &&
403 q_to_nul >tag-NUL-header <<-EOF &&
404 object $sha
405 type commit
406 tag contains-Q-in-header
407 tagger T A Gger <tagger@example.com> 1234567890 -0000
408
409 This is an invalid tag.
410 EOF
411
412 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
413 test_when_finished "remove_object $tag" &&
414 echo $tag >.git/refs/tags/wrong &&
415 test_when_finished "git update-ref -d refs/tags/wrong" &&
416 test_must_fail git fsck --tags 2>out &&
417 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
418 '
419
420 test_expect_success 'cleaned up' '
421 git fsck >actual 2>&1 &&
422 test_must_be_empty actual
423 '
424
425 test_expect_success 'rev-list --verify-objects' '
426 git rev-list --verify-objects --all >/dev/null 2>out &&
427 test_must_be_empty out
428 '
429
430 test_expect_success 'rev-list --verify-objects with bad sha1' '
431 sha=$(echo blob | git hash-object -w --stdin) &&
432 old=$(test_oid_to_path $sha) &&
433 new=$(dirname $old)/$(test_oid ff_2) &&
434 sha="$(dirname $new)$(basename $new)" &&
435 mv .git/objects/$old .git/objects/$new &&
436 test_when_finished "remove_object $sha" &&
437 git update-index --add --cacheinfo 100644 $sha foo &&
438 test_when_finished "git read-tree -u --reset HEAD" &&
439 tree=$(git write-tree) &&
440 test_when_finished "remove_object $tree" &&
441 cmt=$(echo bogus | git commit-tree $tree) &&
442 test_when_finished "remove_object $cmt" &&
443 git update-ref refs/heads/bogus $cmt &&
444 test_when_finished "git update-ref -d refs/heads/bogus" &&
445
446 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
447 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
448 '
449
450 test_expect_success 'force fsck to ignore double author' '
451 git cat-file commit HEAD >basis &&
452 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
453 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
454 test_when_finished "remove_object $new" &&
455 git update-ref refs/heads/bogus "$new" &&
456 test_when_finished "git update-ref -d refs/heads/bogus" &&
457 test_must_fail git fsck &&
458 git -c fsck.multipleAuthors=ignore fsck
459 '
460
461 _bz='\0'
462 _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
463
464 test_expect_success 'fsck notices blob entry pointing to null sha1' '
465 (git init null-blob &&
466 cd null-blob &&
467 sha=$(printf "100644 file$_bz$_bzoid" |
468 git hash-object -w --stdin -t tree) &&
469 git fsck 2>out &&
470 test_i18ngrep "warning.*null sha1" out
471 )
472 '
473
474 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
475 (git init null-commit &&
476 cd null-commit &&
477 sha=$(printf "160000 submodule$_bz$_bzoid" |
478 git hash-object -w --stdin -t tree) &&
479 git fsck 2>out &&
480 test_i18ngrep "warning.*null sha1" out
481 )
482 '
483
484 while read name path pretty; do
485 while read mode type; do
486 : ${pretty:=$path}
487 test_expect_success "fsck notices $pretty as $type" '
488 (
489 git init $name-$type &&
490 cd $name-$type &&
491 git config core.protectNTFS false &&
492 echo content >file &&
493 git add file &&
494 git commit -m base &&
495 blob=$(git rev-parse :file) &&
496 tree=$(git rev-parse HEAD^{tree}) &&
497 value=$(eval "echo \$$type") &&
498 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
499 bad_tree=$(git mktree <bad) &&
500 git fsck 2>out &&
501 test_i18ngrep "warning.*tree $bad_tree" out
502 )'
503 done <<-\EOF
504 100644 blob
505 040000 tree
506 EOF
507 done <<-EOF
508 dot .
509 dotdot ..
510 dotgit .git
511 dotgit-case .GIT
512 dotgit-unicode .gI${u200c}T .gI{u200c}T
513 dotgit-case2 .Git
514 git-tilde1 git~1
515 dotgitdot .git.
516 dot-backslash-case .\\\\.GIT\\\\foobar
517 dotgit-case-backslash .git\\\\foobar
518 EOF
519
520 test_expect_success 'fsck allows .Ňit' '
521 (
522 git init not-dotgit &&
523 cd not-dotgit &&
524 echo content >file &&
525 git add file &&
526 git commit -m base &&
527 blob=$(git rev-parse :file) &&
528 printf "100644 blob $blob\t.\\305\\207it" >tree &&
529 tree=$(git mktree <tree) &&
530 git fsck 2>err &&
531 test_line_count = 0 err
532 )
533 '
534
535 test_expect_success 'NUL in commit' '
536 rm -fr nul-in-commit &&
537 git init nul-in-commit &&
538 (
539 cd nul-in-commit &&
540 git commit --allow-empty -m "initial commitQNUL after message" &&
541 git cat-file commit HEAD >original &&
542 q_to_nul <original >munged &&
543 git hash-object -w -t commit --stdin <munged >name &&
544 git branch bad $(cat name) &&
545
546 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
547 test_i18ngrep nulInCommit warn.1 &&
548 git fsck 2>warn.2 &&
549 test_i18ngrep nulInCommit warn.2
550 )
551 '
552
553 # create a static test repo which is broken by omitting
554 # one particular object ($1, which is looked up via rev-parse
555 # in the new repository).
556 create_repo_missing () {
557 rm -rf missing &&
558 git init missing &&
559 (
560 cd missing &&
561 git commit -m one --allow-empty &&
562 mkdir subdir &&
563 echo content >subdir/file &&
564 git add subdir/file &&
565 git commit -m two &&
566 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
567 git tag -m foo tag $unrelated &&
568 sha1=$(git rev-parse --verify "$1") &&
569 path=$(echo $sha1 | sed 's|..|&/|') &&
570 rm .git/objects/$path
571 )
572 }
573
574 test_expect_success 'fsck notices missing blob' '
575 create_repo_missing HEAD:subdir/file &&
576 test_must_fail git -C missing fsck
577 '
578
579 test_expect_success 'fsck notices missing subtree' '
580 create_repo_missing HEAD:subdir &&
581 test_must_fail git -C missing fsck
582 '
583
584 test_expect_success 'fsck notices missing root tree' '
585 create_repo_missing HEAD^{tree} &&
586 test_must_fail git -C missing fsck
587 '
588
589 test_expect_success 'fsck notices missing parent' '
590 create_repo_missing HEAD^ &&
591 test_must_fail git -C missing fsck
592 '
593
594 test_expect_success 'fsck notices missing tagged object' '
595 create_repo_missing tag^{blob} &&
596 test_must_fail git -C missing fsck
597 '
598
599 test_expect_success 'fsck notices ref pointing to missing commit' '
600 create_repo_missing HEAD &&
601 test_must_fail git -C missing fsck
602 '
603
604 test_expect_success 'fsck notices ref pointing to missing tag' '
605 create_repo_missing tag &&
606 test_must_fail git -C missing fsck
607 '
608
609 test_expect_success 'fsck --connectivity-only' '
610 rm -rf connectivity-only &&
611 git init connectivity-only &&
612 (
613 cd connectivity-only &&
614 touch empty &&
615 git add empty &&
616 test_commit empty &&
617
618 # Drop the index now; we want to be sure that we
619 # recursively notice the broken objects
620 # because they are reachable from refs, not because
621 # they are in the index.
622 rm -f .git/index &&
623
624 # corrupt the blob, but in a way that we can still identify
625 # its type. That lets us see that --connectivity-only is
626 # not actually looking at the contents, but leaves it
627 # free to examine the type if it chooses.
628 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
629 blob=$(echo unrelated | git hash-object -w --stdin) &&
630 mv -f $(sha1_file $blob) $empty &&
631
632 test_must_fail git fsck --strict &&
633 git fsck --strict --connectivity-only &&
634 tree=$(git rev-parse HEAD:) &&
635 suffix=${tree#??} &&
636 tree=.git/objects/${tree%$suffix}/$suffix &&
637 rm -f $tree &&
638 echo invalid >$tree &&
639 test_must_fail git fsck --strict --connectivity-only
640 )
641 '
642
643 test_expect_success 'fsck --connectivity-only with explicit head' '
644 rm -rf connectivity-only &&
645 git init connectivity-only &&
646 (
647 cd connectivity-only &&
648 test_commit foo &&
649 rm -f .git/index &&
650 tree=$(git rev-parse HEAD^{tree}) &&
651 remove_object $(git rev-parse HEAD:foo.t) &&
652 test_must_fail git fsck --connectivity-only $tree
653 )
654 '
655
656 test_expect_success 'fsck --name-objects' '
657 rm -rf name-objects &&
658 git init name-objects &&
659 (
660 cd name-objects &&
661 git config core.logAllRefUpdates false &&
662 test_commit julius caesar.t &&
663 test_commit augustus44 &&
664 test_commit caesar &&
665 remove_object $(git rev-parse julius:caesar.t) &&
666 tree=$(git rev-parse --verify julius:) &&
667 git tag -d julius &&
668 test_must_fail git fsck --name-objects >out &&
669 test_i18ngrep "$tree (refs/tags/augustus44\\^:" out
670 )
671 '
672
673 test_expect_success 'alternate objects are correctly blamed' '
674 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
675 name=$(test_oid numeric) &&
676 path=$(test_oid_to_path "$name") &&
677 git init --bare alt.git &&
678 echo "../../alt.git/objects" >.git/objects/info/alternates &&
679 mkdir alt.git/objects/$(dirname $path) &&
680 >alt.git/objects/$(dirname $path)/$(basename $path) &&
681 test_must_fail git fsck >out 2>&1 &&
682 test_i18ngrep alt.git out
683 '
684
685 test_expect_success 'fsck errors in packed objects' '
686 git cat-file commit HEAD >basis &&
687 sed "s/</one/" basis >one &&
688 sed "s/</foo/" basis >two &&
689 one=$(git hash-object -t commit -w one) &&
690 two=$(git hash-object -t commit -w two) &&
691 pack=$(
692 {
693 echo $one &&
694 echo $two
695 } | git pack-objects .git/objects/pack/pack
696 ) &&
697 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
698 remove_object $one &&
699 remove_object $two &&
700 test_must_fail git fsck 2>out &&
701 test_i18ngrep "error in commit $one.* - bad name" out &&
702 test_i18ngrep "error in commit $two.* - bad name" out &&
703 ! grep corrupt out
704 '
705
706 test_expect_success 'fsck fails on corrupt packfile' '
707 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
708 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
709
710 # Corrupt the first byte of the first object. (It contains 3 type bits,
711 # at least one of which is not zero, so setting the first byte to 0 is
712 # sufficient.)
713 chmod a+w .git/objects/pack/pack-$pack.pack &&
714 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
715
716 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
717 remove_object $hsh &&
718 test_must_fail git fsck 2>out &&
719 test_i18ngrep "checksum mismatch" out
720 '
721
722 test_expect_success 'fsck finds problems in duplicate loose objects' '
723 rm -rf broken-duplicate &&
724 git init broken-duplicate &&
725 (
726 cd broken-duplicate &&
727 test_commit duplicate &&
728 # no "-d" here, so we end up with duplicates
729 git repack &&
730 # now corrupt the loose copy
731 file=$(sha1_file "$(git rev-parse HEAD)") &&
732 rm "$file" &&
733 echo broken >"$file" &&
734 test_must_fail git fsck
735 )
736 '
737
738 test_expect_success 'fsck detects trailing loose garbage (commit)' '
739 git cat-file commit HEAD >basis &&
740 echo bump-commit-sha1 >>basis &&
741 commit=$(git hash-object -w -t commit basis) &&
742 file=$(sha1_file $commit) &&
743 test_when_finished "remove_object $commit" &&
744 chmod +w "$file" &&
745 echo garbage >>"$file" &&
746 test_must_fail git fsck 2>out &&
747 test_i18ngrep "garbage.*$commit" out
748 '
749
750 test_expect_success 'fsck detects trailing loose garbage (large blob)' '
751 blob=$(echo trailing | git hash-object -w --stdin) &&
752 file=$(sha1_file $blob) &&
753 test_when_finished "remove_object $blob" &&
754 chmod +w "$file" &&
755 echo garbage >>"$file" &&
756 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
757 test_i18ngrep "garbage.*$blob" out
758 '
759
760 test_expect_success 'fsck detects truncated loose object' '
761 # make it big enough that we know we will truncate in the data
762 # portion, not the header
763 test-tool genrandom truncate 4096 >file &&
764 blob=$(git hash-object -w file) &&
765 file=$(sha1_file $blob) &&
766 test_when_finished "remove_object $blob" &&
767 test_copy_bytes 1024 <"$file" >tmp &&
768 rm "$file" &&
769 mv -f tmp "$file" &&
770
771 # check both regular and streaming code paths
772 test_must_fail git fsck 2>out &&
773 test_i18ngrep corrupt.*$blob out &&
774
775 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
776 test_i18ngrep corrupt.*$blob out
777 '
778
779 # for each of type, we have one version which is referenced by another object
780 # (and so while unreachable, not dangling), and another variant which really is
781 # dangling.
782 test_expect_success 'create dangling-object repository' '
783 git init dangling &&
784 (
785 cd dangling &&
786 blob=$(echo not-dangling | git hash-object -w --stdin) &&
787 dblob=$(echo dangling | git hash-object -w --stdin) &&
788 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
789 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
790 commit=$(git commit-tree $tree) &&
791 dcommit=$(git commit-tree -p $commit $tree) &&
792
793 cat >expect <<-EOF
794 dangling blob $dblob
795 dangling commit $dcommit
796 dangling tree $dtree
797 EOF
798 )
799 '
800
801 test_expect_success 'fsck notices dangling objects' '
802 (
803 cd dangling &&
804 git fsck >actual &&
805 # the output order is non-deterministic, as it comes from a hash
806 sort <actual >actual.sorted &&
807 test_cmp expect actual.sorted
808 )
809 '
810
811 test_expect_success 'fsck --connectivity-only notices dangling objects' '
812 (
813 cd dangling &&
814 git fsck --connectivity-only >actual &&
815 # the output order is non-deterministic, as it comes from a hash
816 sort <actual >actual.sorted &&
817 test_cmp expect actual.sorted
818 )
819 '
820
821 test_expect_success 'fsck $name notices bogus $name' '
822 test_must_fail git fsck bogus &&
823 test_must_fail git fsck $ZERO_OID
824 '
825
826 test_expect_success 'bogus head does not fallback to all heads' '
827 # set up a case that will cause a reachability complaint
828 echo to-be-deleted >foo &&
829 git add foo &&
830 blob=$(git rev-parse :foo) &&
831 test_when_finished "git rm --cached foo" &&
832 remove_object $blob &&
833 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
834 ! grep $blob out
835 '
836
837 # Corrupt the checksum on the index.
838 # Add 1 to the last byte in the SHA.
839 corrupt_index_checksum () {
840 perl -w -e '
841 use Fcntl ":seek";
842 open my $fh, "+<", ".git/index" or die "open: $!";
843 binmode $fh;
844 seek $fh, -1, SEEK_END or die "seek: $!";
845 read $fh, my $in_byte, 1 or die "read: $!";
846
847 $in_value = unpack("C", $in_byte);
848 $out_value = ($in_value + 1) & 255;
849
850 $out_byte = pack("C", $out_value);
851
852 seek $fh, -1, SEEK_END or die "seek: $!";
853 print $fh $out_byte;
854 close $fh or die "close: $!";
855 '
856 }
857
858 # Corrupt the checksum on the index and then
859 # verify that only fsck notices.
860 test_expect_success 'detect corrupt index file in fsck' '
861 cp .git/index .git/index.backup &&
862 test_when_finished "mv .git/index.backup .git/index" &&
863 corrupt_index_checksum &&
864 test_must_fail git fsck --cache 2>errors &&
865 test_i18ngrep "bad index file" errors
866 '
867
868 test_done