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