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