Merge branch 'jk/grep-e-could-be-extended-beyond-posix' into maint
[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         >empty
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_cmp 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_cmp 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                 echo "$*" | sed "s#..#.git/objects/&/#"
47         } &&
48
49         remove_object() {
50                 file=$(sha1_file "$*") &&
51                 test -e "$file" &&
52                 rm -f "$file"
53         }
54 '
55
56 test_expect_success 'object with bad sha1' '
57         sha=$(echo blob | git hash-object -w --stdin) &&
58         old=$(echo $sha | sed "s+^..+&/+") &&
59         new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
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         cat out &&
74         grep "$sha.*corrupt" out
75 '
76
77 test_expect_success 'branch pointing to non-commit' '
78         git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79         test_when_finished "git update-ref -d refs/heads/invalid" &&
80         test_must_fail git fsck 2>out &&
81         cat out &&
82         grep "not a commit" out
83 '
84
85 test_expect_success 'HEAD link pointing at a funny object' '
86         test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
87         mv .git/HEAD .git/SAVED_HEAD &&
88         echo 0000000000000000000000000000000000000000 >.git/HEAD &&
89         # avoid corrupt/broken HEAD from interfering with repo discovery
90         test_must_fail env GIT_DIR=.git git fsck 2>out &&
91         cat out &&
92         grep "detached HEAD points" out
93 '
94
95 test_expect_success 'HEAD link pointing at a funny place' '
96         test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
97         mv .git/HEAD .git/SAVED_HEAD &&
98         echo "ref: refs/funny/place" >.git/HEAD &&
99         # avoid corrupt/broken HEAD from interfering with repo discovery
100         test_must_fail env GIT_DIR=.git git fsck 2>out &&
101         cat out &&
102         grep "HEAD points to something strange" out
103 '
104
105 test_expect_success 'email without @ is okay' '
106         git cat-file commit HEAD >basis &&
107         sed "s/@/AT/" basis >okay &&
108         new=$(git hash-object -t commit -w --stdin <okay) &&
109         test_when_finished "remove_object $new" &&
110         git update-ref refs/heads/bogus "$new" &&
111         test_when_finished "git update-ref -d refs/heads/bogus" &&
112         git fsck 2>out &&
113         cat out &&
114         ! grep "commit $new" out
115 '
116
117 test_expect_success 'email with embedded > is not okay' '
118         git cat-file commit HEAD >basis &&
119         sed "s/@[a-z]/&>/" basis >bad-email &&
120         new=$(git hash-object -t commit -w --stdin <bad-email) &&
121         test_when_finished "remove_object $new" &&
122         git update-ref refs/heads/bogus "$new" &&
123         test_when_finished "git update-ref -d refs/heads/bogus" &&
124         test_must_fail git fsck 2>out &&
125         cat out &&
126         grep "error in commit $new" out
127 '
128
129 test_expect_success 'missing < email delimiter is reported nicely' '
130         git cat-file commit HEAD >basis &&
131         sed "s/<//" basis >bad-email-2 &&
132         new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
133         test_when_finished "remove_object $new" &&
134         git update-ref refs/heads/bogus "$new" &&
135         test_when_finished "git update-ref -d refs/heads/bogus" &&
136         test_must_fail git fsck 2>out &&
137         cat out &&
138         grep "error in commit $new.* - bad name" out
139 '
140
141 test_expect_success 'missing email is reported nicely' '
142         git cat-file commit HEAD >basis &&
143         sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
144         new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
145         test_when_finished "remove_object $new" &&
146         git update-ref refs/heads/bogus "$new" &&
147         test_when_finished "git update-ref -d refs/heads/bogus" &&
148         test_must_fail git fsck 2>out &&
149         cat out &&
150         grep "error in commit $new.* - missing email" out
151 '
152
153 test_expect_success '> in name is reported' '
154         git cat-file commit HEAD >basis &&
155         sed "s/ </> </" basis >bad-email-4 &&
156         new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
157         test_when_finished "remove_object $new" &&
158         git update-ref refs/heads/bogus "$new" &&
159         test_when_finished "git update-ref -d refs/heads/bogus" &&
160         test_must_fail git fsck 2>out &&
161         cat out &&
162         grep "error in commit $new" out
163 '
164
165 # date is 2^64 + 1
166 test_expect_success 'integer overflow in timestamps is reported' '
167         git cat-file commit HEAD >basis &&
168         sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
169                 <basis >bad-timestamp &&
170         new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
171         test_when_finished "remove_object $new" &&
172         git update-ref refs/heads/bogus "$new" &&
173         test_when_finished "git update-ref -d refs/heads/bogus" &&
174         test_must_fail git fsck 2>out &&
175         cat out &&
176         grep "error in commit $new.*integer overflow" out
177 '
178
179 test_expect_success 'commit with NUL in header' '
180         git cat-file commit HEAD >basis &&
181         sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
182         new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
183         test_when_finished "remove_object $new" &&
184         git update-ref refs/heads/bogus "$new" &&
185         test_when_finished "git update-ref -d refs/heads/bogus" &&
186         test_must_fail git fsck 2>out &&
187         cat out &&
188         grep "error in commit $new.*unterminated header: NUL at offset" out
189 '
190
191 test_expect_success 'tree object with duplicate entries' '
192         test_when_finished "remove_object \$T" &&
193         T=$(
194                 GIT_INDEX_FILE=test-index &&
195                 export GIT_INDEX_FILE &&
196                 rm -f test-index &&
197                 >x &&
198                 git add x &&
199                 T=$(git write-tree) &&
200                 (
201                         git cat-file tree $T &&
202                         git cat-file tree $T
203                 ) |
204                 git hash-object -w -t tree --stdin
205         ) &&
206         test_must_fail git fsck 2>out &&
207         grep "error in tree .*contains duplicate file entries" out
208 '
209
210 test_expect_success 'unparseable tree object' '
211         test_when_finished "git update-ref -d refs/heads/wrong" &&
212         test_when_finished "remove_object \$tree_sha1" &&
213         test_when_finished "remove_object \$commit_sha1" &&
214         tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
215         commit_sha1=$(git commit-tree $tree_sha1) &&
216         git update-ref refs/heads/wrong $commit_sha1 &&
217         test_must_fail git fsck 2>out &&
218         test_i18ngrep "error: empty filename in tree entry" out &&
219         test_i18ngrep "$tree_sha1" out &&
220         test_i18ngrep ! "fatal: empty filename in tree entry" out
221 '
222
223 test_expect_success 'tag pointing to nonexistent' '
224         cat >invalid-tag <<-\EOF &&
225         object ffffffffffffffffffffffffffffffffffffffff
226         type commit
227         tag invalid
228         tagger T A Gger <tagger@example.com> 1234567890 -0000
229
230         This is an invalid tag.
231         EOF
232
233         tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
234         test_when_finished "remove_object $tag" &&
235         echo $tag >.git/refs/tags/invalid &&
236         test_when_finished "git update-ref -d refs/tags/invalid" &&
237         test_must_fail git fsck --tags >out &&
238         cat out &&
239         grep "broken link" out
240 '
241
242 test_expect_success 'tag pointing to something else than its type' '
243         sha=$(echo blob | git hash-object -w --stdin) &&
244         test_when_finished "remove_object $sha" &&
245         cat >wrong-tag <<-EOF &&
246         object $sha
247         type commit
248         tag wrong
249         tagger T A Gger <tagger@example.com> 1234567890 -0000
250
251         This is an invalid tag.
252         EOF
253
254         tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
255         test_when_finished "remove_object $tag" &&
256         echo $tag >.git/refs/tags/wrong &&
257         test_when_finished "git update-ref -d refs/tags/wrong" &&
258         test_must_fail git fsck --tags
259 '
260
261 test_expect_success 'tag with incorrect tag name & missing tagger' '
262         sha=$(git rev-parse HEAD) &&
263         cat >wrong-tag <<-EOF &&
264         object $sha
265         type commit
266         tag wrong name format
267
268         This is an invalid tag.
269         EOF
270
271         tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
272         test_when_finished "remove_object $tag" &&
273         echo $tag >.git/refs/tags/wrong &&
274         test_when_finished "git update-ref -d refs/tags/wrong" &&
275         git fsck --tags 2>out &&
276
277         cat >expect <<-EOF &&
278         warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
279         warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
280         EOF
281         test_cmp expect out
282 '
283
284 test_expect_success 'tag with bad tagger' '
285         sha=$(git rev-parse HEAD) &&
286         cat >wrong-tag <<-EOF &&
287         object $sha
288         type commit
289         tag not-quite-wrong
290         tagger Bad Tagger Name
291
292         This is an invalid tag.
293         EOF
294
295         tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
296         test_when_finished "remove_object $tag" &&
297         echo $tag >.git/refs/tags/wrong &&
298         test_when_finished "git update-ref -d refs/tags/wrong" &&
299         test_must_fail git fsck --tags 2>out &&
300         grep "error in tag .*: invalid author/committer" out
301 '
302
303 test_expect_success 'tag with NUL in header' '
304         sha=$(git rev-parse HEAD) &&
305         q_to_nul >tag-NUL-header <<-EOF &&
306         object $sha
307         type commit
308         tag contains-Q-in-header
309         tagger T A Gger <tagger@example.com> 1234567890 -0000
310
311         This is an invalid tag.
312         EOF
313
314         tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
315         test_when_finished "remove_object $tag" &&
316         echo $tag >.git/refs/tags/wrong &&
317         test_when_finished "git update-ref -d refs/tags/wrong" &&
318         test_must_fail git fsck --tags 2>out &&
319         cat out &&
320         grep "error in tag $tag.*unterminated header: NUL at offset" out
321 '
322
323 test_expect_success 'cleaned up' '
324         git fsck >actual 2>&1 &&
325         test_cmp empty actual
326 '
327
328 test_expect_success 'rev-list --verify-objects' '
329         git rev-list --verify-objects --all >/dev/null 2>out &&
330         test_cmp empty out
331 '
332
333 test_expect_success 'rev-list --verify-objects with bad sha1' '
334         sha=$(echo blob | git hash-object -w --stdin) &&
335         old=$(echo $sha | sed "s+^..+&/+") &&
336         new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
337         sha="$(dirname $new)$(basename $new)" &&
338         mv .git/objects/$old .git/objects/$new &&
339         test_when_finished "remove_object $sha" &&
340         git update-index --add --cacheinfo 100644 $sha foo &&
341         test_when_finished "git read-tree -u --reset HEAD" &&
342         tree=$(git write-tree) &&
343         test_when_finished "remove_object $tree" &&
344         cmt=$(echo bogus | git commit-tree $tree) &&
345         test_when_finished "remove_object $cmt" &&
346         git update-ref refs/heads/bogus $cmt &&
347         test_when_finished "git update-ref -d refs/heads/bogus" &&
348
349         test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
350         cat out &&
351         grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
352 '
353
354 test_expect_success 'force fsck to ignore double author' '
355         git cat-file commit HEAD >basis &&
356         sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
357         new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
358         test_when_finished "remove_object $new" &&
359         git update-ref refs/heads/bogus "$new" &&
360         test_when_finished "git update-ref -d refs/heads/bogus" &&
361         test_must_fail git fsck &&
362         git -c fsck.multipleAuthors=ignore fsck
363 '
364
365 _bz='\0'
366 _bz5="$_bz$_bz$_bz$_bz$_bz"
367 _bz20="$_bz5$_bz5$_bz5$_bz5"
368
369 test_expect_success 'fsck notices blob entry pointing to null sha1' '
370         (git init null-blob &&
371          cd null-blob &&
372          sha=$(printf "100644 file$_bz$_bz20" |
373                git hash-object -w --stdin -t tree) &&
374           git fsck 2>out &&
375           cat out &&
376           grep "warning.*null sha1" out
377         )
378 '
379
380 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
381         (git init null-commit &&
382          cd null-commit &&
383          sha=$(printf "160000 submodule$_bz$_bz20" |
384                git hash-object -w --stdin -t tree) &&
385           git fsck 2>out &&
386           cat out &&
387           grep "warning.*null sha1" out
388         )
389 '
390
391 while read name path pretty; do
392         while read mode type; do
393                 : ${pretty:=$path}
394                 test_expect_success "fsck notices $pretty as $type" '
395                 (
396                         git init $name-$type &&
397                         cd $name-$type &&
398                         echo content >file &&
399                         git add file &&
400                         git commit -m base &&
401                         blob=$(git rev-parse :file) &&
402                         tree=$(git rev-parse HEAD^{tree}) &&
403                         value=$(eval "echo \$$type") &&
404                         printf "$mode $type %s\t%s" "$value" "$path" >bad &&
405                         bad_tree=$(git mktree <bad) &&
406                         git fsck 2>out &&
407                         cat out &&
408                         grep "warning.*tree $bad_tree" out
409                 )'
410         done <<-\EOF
411         100644 blob
412         040000 tree
413         EOF
414 done <<-EOF
415 dot .
416 dotdot ..
417 dotgit .git
418 dotgit-case .GIT
419 dotgit-unicode .gI${u200c}T .gI{u200c}T
420 dotgit-case2 .Git
421 git-tilde1 git~1
422 dotgitdot .git.
423 dot-backslash-case .\\\\.GIT\\\\foobar
424 dotgit-case-backslash .git\\\\foobar
425 EOF
426
427 test_expect_success 'fsck allows .Ňit' '
428         (
429                 git init not-dotgit &&
430                 cd not-dotgit &&
431                 echo content >file &&
432                 git add file &&
433                 git commit -m base &&
434                 blob=$(git rev-parse :file) &&
435                 printf "100644 blob $blob\t.\\305\\207it" >tree &&
436                 tree=$(git mktree <tree) &&
437                 git fsck 2>err &&
438                 test_line_count = 0 err
439         )
440 '
441
442 test_expect_success 'NUL in commit' '
443         rm -fr nul-in-commit &&
444         git init nul-in-commit &&
445         (
446                 cd nul-in-commit &&
447                 git commit --allow-empty -m "initial commitQNUL after message" &&
448                 git cat-file commit HEAD >original &&
449                 q_to_nul <original >munged &&
450                 git hash-object -w -t commit --stdin <munged >name &&
451                 git branch bad $(cat name) &&
452
453                 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
454                 grep nulInCommit warn.1 &&
455                 git fsck 2>warn.2 &&
456                 grep nulInCommit warn.2
457         )
458 '
459
460 # create a static test repo which is broken by omitting
461 # one particular object ($1, which is looked up via rev-parse
462 # in the new repository).
463 create_repo_missing () {
464         rm -rf missing &&
465         git init missing &&
466         (
467                 cd missing &&
468                 git commit -m one --allow-empty &&
469                 mkdir subdir &&
470                 echo content >subdir/file &&
471                 git add subdir/file &&
472                 git commit -m two &&
473                 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
474                 git tag -m foo tag $unrelated &&
475                 sha1=$(git rev-parse --verify "$1") &&
476                 path=$(echo $sha1 | sed 's|..|&/|') &&
477                 rm .git/objects/$path
478         )
479 }
480
481 test_expect_success 'fsck notices missing blob' '
482         create_repo_missing HEAD:subdir/file &&
483         test_must_fail git -C missing fsck
484 '
485
486 test_expect_success 'fsck notices missing subtree' '
487         create_repo_missing HEAD:subdir &&
488         test_must_fail git -C missing fsck
489 '
490
491 test_expect_success 'fsck notices missing root tree' '
492         create_repo_missing HEAD^{tree} &&
493         test_must_fail git -C missing fsck
494 '
495
496 test_expect_success 'fsck notices missing parent' '
497         create_repo_missing HEAD^ &&
498         test_must_fail git -C missing fsck
499 '
500
501 test_expect_success 'fsck notices missing tagged object' '
502         create_repo_missing tag^{blob} &&
503         test_must_fail git -C missing fsck
504 '
505
506 test_expect_success 'fsck notices ref pointing to missing commit' '
507         create_repo_missing HEAD &&
508         test_must_fail git -C missing fsck
509 '
510
511 test_expect_success 'fsck notices ref pointing to missing tag' '
512         create_repo_missing tag &&
513         test_must_fail git -C missing fsck
514 '
515
516 test_expect_success 'fsck --connectivity-only' '
517         rm -rf connectivity-only &&
518         git init connectivity-only &&
519         (
520                 cd connectivity-only &&
521                 touch empty &&
522                 git add empty &&
523                 test_commit empty &&
524                 empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
525                 rm -f $empty &&
526                 echo invalid >$empty &&
527                 test_must_fail git fsck --strict &&
528                 git fsck --strict --connectivity-only &&
529                 tree=$(git rev-parse HEAD:) &&
530                 suffix=${tree#??} &&
531                 tree=.git/objects/${tree%$suffix}/$suffix &&
532                 rm -f $tree &&
533                 echo invalid >$tree &&
534                 test_must_fail git fsck --strict --connectivity-only
535         )
536 '
537
538 remove_loose_object () {
539         sha1="$(git rev-parse "$1")" &&
540         remainder=${sha1#??} &&
541         firsttwo=${sha1%$remainder} &&
542         rm .git/objects/$firsttwo/$remainder
543 }
544
545 test_expect_success 'fsck --name-objects' '
546         rm -rf name-objects &&
547         git init name-objects &&
548         (
549                 cd name-objects &&
550                 test_commit julius caesar.t &&
551                 test_commit augustus &&
552                 test_commit caesar &&
553                 remove_loose_object $(git rev-parse julius:caesar.t) &&
554                 test_must_fail git fsck --name-objects >out &&
555                 tree=$(git rev-parse --verify julius:) &&
556                 grep "$tree (\(refs/heads/master\|HEAD\)@{[0-9]*}:" out
557         )
558 '
559
560 test_done