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