]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1450-fsck.sh
Merge branch 'maint-2.36' into maint-2.37
[thirdparty/git.git] / t / t1450-fsck.sh
CommitLineData
469e2ebf
JH
1#!/bin/sh
2
dbedf8bf
JN
3test_description='git fsck random collection of tests
4
5* (HEAD) B
06d53148 6* (main) A
dbedf8bf 7'
469e2ebf
JH
8
9. ./test-lib.sh
10
11test_expect_success setup '
dbedf8bf 12 git config gc.auto 0 &&
0adc6a3d 13 git config i18n.commitencoding ISO-8859-1 &&
469e2ebf 14 test_commit A fileA one &&
0adc6a3d 15 git config --unset i18n.commitencoding &&
469e2ebf
JH
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 git tag -d A B &&
1c5e94f4 19 git reflog expire --expire=now --all
469e2ebf
JH
20'
21
e15ef669
JH
22test_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 &&
c6a13b2c 29 git fsck --no-dangling >../actual 2>&1
dbedf8bf 30 ) &&
1c5e94f4 31 test_must_be_empty actual
e15ef669
JH
32'
33
dbedf8bf
JN
34test_expect_success 'HEAD is part of refs, valid objects appear valid' '
35 git fsck >actual 2>&1 &&
1c5e94f4 36 test_must_be_empty actual
0adc6a3d
JN
37'
38
02a6552c
TR
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
8c891eed
JS
43sha1_file () {
44 git rev-parse --git-path objects/$(test_oid_to_path "$1")
45}
dbedf8bf 46
8c891eed
JS
47remove_object () {
48 rm "$(sha1_file "$1")"
49}
dbedf8bf 50
f7a0dba7
ÆAB
51test_expect_success 'object with hash mismatch' '
52 git init --bare hash-mismatch &&
53 (
54 cd hash-mismatch &&
dbedf8bf 55
f7a0dba7 56 oid=$(echo blob | git hash-object -w --stdin) &&
96e41f58 57 oldoid=$oid &&
f7a0dba7
ÆAB
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 &&
96e41f58 69 grep "$oldoid: hash-path mismatch, found at: .*$new" out
f7a0dba7 70 )
02a6552c
TR
71'
72
42cd635b
ÆAB
73test_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) &&
96e41f58 79 oldoid=$oid &&
42cd635b
ÆAB
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
31deb28f
ÆAB
90
91 test_must_fail git fsck 2>out &&
96e41f58
ÆAB
92 grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
93 grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
42cd635b
ÆAB
94 )
95'
96
59069107 97test_expect_success 'zlib corrupt loose object output ' '
a5ed3331
ÆAB
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") &&
59069107 103 chmod +w $oidf &&
a5ed3331
ÆAB
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
02a6552c 117test_expect_success 'branch pointing to non-commit' '
dbedf8bf
JN
118 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
119 test_when_finished "git update-ref -d refs/heads/invalid" &&
122f76f5 120 test_must_fail git fsck 2>out &&
674ba340 121 test_i18ngrep "not a commit" out
02a6552c
TR
122'
123
122f76f5
JH
124test_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 &&
8de6b383 127 echo $ZERO_OID >.git/HEAD &&
122f76f5
JH
128 # avoid corrupt/broken HEAD from interfering with repo discovery
129 test_must_fail env GIT_DIR=.git git fsck 2>out &&
674ba340 130 test_i18ngrep "detached HEAD points" out
122f76f5
JH
131'
132
133test_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 &&
674ba340 139 test_i18ngrep "HEAD points to something strange" out
122f76f5
JH
140'
141
b29759d8
NTND
142test_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 &&
3813a89f 150 test_i18ngrep "main-worktree/HEAD: detached HEAD points" out
b29759d8
NTND
151'
152
153test_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 &&
3813a89f 158 test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out
b29759d8
NTND
159'
160
161test_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 &&
3813a89f 166 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
b29759d8
NTND
167'
168
169test_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 &&
3813a89f 174 test_i18ngrep "worktrees/other/HEAD points to something strange" out
b29759d8
NTND
175'
176
42d4e1d1 177test_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
daae1922
JN
201test_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) &&
dbedf8bf 205 test_when_finished "remove_object $new" &&
daae1922 206 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 207 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922 208 git fsck 2>out &&
dbedf8bf 209 ! grep "commit $new" out
daae1922 210'
daae1922 211
daae1922
JN
212test_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 -t commit -w --stdin <bad-email) &&
dbedf8bf 216 test_when_finished "remove_object $new" &&
daae1922 217 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 218 test_when_finished "git update-ref -d refs/heads/bogus" &&
2e770fe4 219 test_must_fail git fsck 2>out &&
674ba340 220 test_i18ngrep "error in commit $new" out
daae1922 221'
02a6552c 222
53f53cff 223test_expect_success 'missing < email delimiter is reported nicely' '
e3c98120
DI
224 git cat-file commit HEAD >basis &&
225 sed "s/<//" basis >bad-email-2 &&
226 new=$(git hash-object -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" &&
2e770fe4 230 test_must_fail git fsck 2>out &&
674ba340 231 test_i18ngrep "error in commit $new.* - bad name" out
e3c98120
DI
232'
233
53f53cff 234test_expect_success 'missing email is reported nicely' '
e3c98120
DI
235 git cat-file commit HEAD >basis &&
236 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
237 new=$(git hash-object -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" &&
2e770fe4 241 test_must_fail git fsck 2>out &&
674ba340 242 test_i18ngrep "error in commit $new.* - missing email" out
e3c98120
DI
243'
244
53f53cff 245test_expect_success '> in name is reported' '
e3c98120
DI
246 git cat-file commit HEAD >basis &&
247 sed "s/ </> </" basis >bad-email-4 &&
248 new=$(git hash-object -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" &&
2e770fe4 252 test_must_fail git fsck 2>out &&
674ba340 253 test_i18ngrep "error in commit $new" out
e3c98120
DI
254'
255
d4b8de04
JK
256# date is 2^64 + 1
257test_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 -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" &&
2e770fe4 265 test_must_fail git fsck 2>out &&
674ba340 266 test_i18ngrep "error in commit $new.*integer overflow" out
d4b8de04
JK
267'
268
80c7f5a0
RS
269test_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 -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 &&
674ba340 277 test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out
80c7f5a0
RS
278'
279
8354fa3d 280test_expect_success 'tree object with duplicate entries' '
1ada11ee 281 test_when_finished "for i in \$T; do remove_object \$i; done" &&
2e770fe4
JK
282 T=$(
283 GIT_INDEX_FILE=test-index &&
284 export GIT_INDEX_FILE &&
285 rm -f test-index &&
286 >x &&
287 git add x &&
1ada11ee 288 git rev-parse :x &&
2e770fe4 289 T=$(git write-tree) &&
1ada11ee 290 echo $T &&
2e770fe4
JK
291 (
292 git cat-file tree $T &&
293 git cat-file tree $T
294 ) |
295 git hash-object -w -t tree --stdin
296 ) &&
297 test_must_fail git fsck 2>out &&
674ba340 298 test_i18ngrep "error in tree .*contains duplicate file entries" out
2e770fe4
JK
299'
300
fc12aa7b
RS
301check_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
326check_duplicate_names success x x.1 x/
327check_duplicate_names success x x.1.2 x.1/ x/
fe747043 328check_duplicate_names success x x.1 x.1.2 x/
9068cfb2 329
8354fa3d 330test_expect_success 'unparseable tree object' '
8de6b383 331 test_oid_cache <<-\EOF &&
332 junk sha1:twenty-bytes-of-junk
333 junk sha256:twenty-bytes-of-junk-twelve-more
334 EOF
335
8354fa3d
DT
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" &&
8de6b383 339 junk=$(test_oid junk) &&
340 tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
8354fa3d
DT
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
2720f6db
RS
349test_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
53602a93
JK
367test_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
4551d035 381test_expect_success 'tag pointing to nonexistent' '
8de6b383 382 badoid=$(test_oid deadbeef) &&
383 cat >invalid-tag <<-EOF &&
384 object $badoid
dbedf8bf
JN
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" &&
4551d035 396 test_must_fail git fsck --tags >out &&
674ba340 397 test_i18ngrep "broken link" out
02a6552c
TR
398'
399
4551d035 400test_expect_success 'tag pointing to something else than its type' '
dbedf8bf
JN
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" &&
9dad83be 416 test_must_fail git fsck --tags
02a6552c
TR
417'
418
90e3e5f0
JS
419test_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 -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 &&
7add4419
JK
434
435 cat >expect <<-EOF &&
71ab8fa8
JS
436 warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
437 warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
7add4419 438 EOF
1108cea7 439 test_cmp expect out
90e3e5f0
JS
440'
441
b659605d
JH
442test_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 &&
674ba340 458 test_i18ngrep "error in tag .*: invalid author/committer" out
b659605d
JH
459'
460
8a272f29 461test_expect_success 'tag with NUL in header' '
80c7f5a0
RS
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 &&
674ba340 477 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
80c7f5a0
RS
478'
479
dbedf8bf
JN
480test_expect_success 'cleaned up' '
481 git fsck >actual 2>&1 &&
1c5e94f4 482 test_must_be_empty actual
dbedf8bf 483'
02a6552c 484
cb8da705
CB
485test_expect_success 'rev-list --verify-objects' '
486 git rev-list --verify-objects --all >/dev/null 2>out &&
1c5e94f4 487 test_must_be_empty out
cb8da705
CB
488'
489
490test_expect_success 'rev-list --verify-objects with bad sha1' '
491 sha=$(echo blob | git hash-object -w --stdin) &&
8de6b383 492 old=$(test_oid_to_path $sha) &&
493 new=$(dirname $old)/$(test_oid ff_2) &&
cb8da705
CB
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 &&
8de6b383 507 test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
cb8da705
CB
508'
509
2becf00f
JS
510test_expect_success 'force fsck to ignore double author' '
511 git cat-file commit HEAD >basis &&
512 sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
513 new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
514 test_when_finished "remove_object $new" &&
515 git update-ref refs/heads/bogus "$new" &&
516 test_when_finished "git update-ref -d refs/heads/bogus" &&
517 test_must_fail git fsck &&
518 git -c fsck.multipleAuthors=ignore fsck
519'
520
c479d14a 521_bz='\0'
8de6b383 522_bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
c479d14a
JK
523
524test_expect_success 'fsck notices blob entry pointing to null sha1' '
525 (git init null-blob &&
526 cd null-blob &&
8de6b383 527 sha=$(printf "100644 file$_bz$_bzoid" |
c479d14a
JK
528 git hash-object -w --stdin -t tree) &&
529 git fsck 2>out &&
674ba340 530 test_i18ngrep "warning.*null sha1" out
c479d14a
JK
531 )
532'
533
534test_expect_success 'fsck notices submodule entry pointing to null sha1' '
535 (git init null-commit &&
536 cd null-commit &&
8de6b383 537 sha=$(printf "160000 submodule$_bz$_bzoid" |
c479d14a
JK
538 git hash-object -w --stdin -t tree) &&
539 git fsck 2>out &&
674ba340 540 test_i18ngrep "warning.*null sha1" out
c479d14a
JK
541 )
542'
543
a18fcc9f 544while read name path pretty; do
450870cb 545 while read mode type; do
a18fcc9f
JK
546 : ${pretty:=$path}
547 test_expect_success "fsck notices $pretty as $type" '
450870cb
JK
548 (
549 git init $name-$type &&
550 cd $name-$type &&
e1d911dd 551 git config core.protectNTFS false &&
450870cb
JK
552 echo content >file &&
553 git add file &&
554 git commit -m base &&
555 blob=$(git rev-parse :file) &&
556 tree=$(git rev-parse HEAD^{tree}) &&
557 value=$(eval "echo \$$type") &&
558 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
d08c13b9 559 bad_tree=$(git mktree <bad) &&
450870cb 560 git fsck 2>out &&
674ba340 561 test_i18ngrep "warning.*tree $bad_tree" out
450870cb
JK
562 )'
563 done <<-\EOF
564 100644 blob
565 040000 tree
566 EOF
a18fcc9f 567done <<-EOF
450870cb
JK
568dot .
569dotdot ..
570dotgit .git
76e86fc6 571dotgit-case .GIT
a18fcc9f 572dotgit-unicode .gI${u200c}T .gI{u200c}T
d08c13b9
JS
573dotgit-case2 .Git
574git-tilde1 git~1
575dotgitdot .git.
576dot-backslash-case .\\\\.GIT\\\\foobar
577dotgit-case-backslash .git\\\\foobar
450870cb 578EOF
5c17f512 579
6aaf956b
JK
580test_expect_success 'fsck allows .Ňit' '
581 (
582 git init not-dotgit &&
583 cd not-dotgit &&
584 echo content >file &&
585 git add file &&
586 git commit -m base &&
587 blob=$(git rev-parse :file) &&
588 printf "100644 blob $blob\t.\\305\\207it" >tree &&
589 tree=$(git mktree <tree) &&
590 git fsck 2>err &&
591 test_line_count = 0 err
592 )
593'
594
6d2d780f
JH
595test_expect_success 'NUL in commit' '
596 rm -fr nul-in-commit &&
597 git init nul-in-commit &&
598 (
599 cd nul-in-commit &&
600 git commit --allow-empty -m "initial commitQNUL after message" &&
601 git cat-file commit HEAD >original &&
602 q_to_nul <original >munged &&
603 git hash-object -w -t commit --stdin <munged >name &&
604 git branch bad $(cat name) &&
605
606 test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
674ba340 607 test_i18ngrep nulInCommit warn.1 &&
6d2d780f 608 git fsck 2>warn.2 &&
674ba340 609 test_i18ngrep nulInCommit warn.2
6d2d780f
JH
610 )
611'
612
30d1038d
JK
613# create a static test repo which is broken by omitting
614# one particular object ($1, which is looked up via rev-parse
615# in the new repository).
616create_repo_missing () {
617 rm -rf missing &&
618 git init missing &&
619 (
620 cd missing &&
621 git commit -m one --allow-empty &&
622 mkdir subdir &&
623 echo content >subdir/file &&
624 git add subdir/file &&
625 git commit -m two &&
626 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
627 git tag -m foo tag $unrelated &&
628 sha1=$(git rev-parse --verify "$1") &&
629 path=$(echo $sha1 | sed 's|..|&/|') &&
630 rm .git/objects/$path
631 )
632}
633
634test_expect_success 'fsck notices missing blob' '
635 create_repo_missing HEAD:subdir/file &&
636 test_must_fail git -C missing fsck
637'
638
639test_expect_success 'fsck notices missing subtree' '
640 create_repo_missing HEAD:subdir &&
641 test_must_fail git -C missing fsck
642'
643
644test_expect_success 'fsck notices missing root tree' '
645 create_repo_missing HEAD^{tree} &&
646 test_must_fail git -C missing fsck
647'
648
649test_expect_success 'fsck notices missing parent' '
650 create_repo_missing HEAD^ &&
651 test_must_fail git -C missing fsck
652'
653
654test_expect_success 'fsck notices missing tagged object' '
655 create_repo_missing tag^{blob} &&
656 test_must_fail git -C missing fsck
657'
658
659test_expect_success 'fsck notices ref pointing to missing commit' '
660 create_repo_missing HEAD &&
661 test_must_fail git -C missing fsck
662'
663
664test_expect_success 'fsck notices ref pointing to missing tag' '
665 create_repo_missing tag &&
666 test_must_fail git -C missing fsck
667'
668
02976bf8
JS
669test_expect_success 'fsck --connectivity-only' '
670 rm -rf connectivity-only &&
671 git init connectivity-only &&
672 (
673 cd connectivity-only &&
674 touch empty &&
675 git add empty &&
676 test_commit empty &&
3e3f8bd6
JK
677
678 # Drop the index now; we want to be sure that we
679 # recursively notice the broken objects
680 # because they are reachable from refs, not because
681 # they are in the index.
682 rm -f .git/index &&
683
684 # corrupt the blob, but in a way that we can still identify
685 # its type. That lets us see that --connectivity-only is
686 # not actually looking at the contents, but leaves it
687 # free to examine the type if it chooses.
8de6b383 688 empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
3e3f8bd6 689 blob=$(echo unrelated | git hash-object -w --stdin) &&
c20d4d70 690 mv -f $(sha1_file $blob) $empty &&
3e3f8bd6 691
02976bf8
JS
692 test_must_fail git fsck --strict &&
693 git fsck --strict --connectivity-only &&
694 tree=$(git rev-parse HEAD:) &&
695 suffix=${tree#??} &&
696 tree=.git/objects/${tree%$suffix}/$suffix &&
697 rm -f $tree &&
698 echo invalid >$tree &&
699 test_must_fail git fsck --strict --connectivity-only
700 )
701'
702
3e3f8bd6
JK
703test_expect_success 'fsck --connectivity-only with explicit head' '
704 rm -rf connectivity-only &&
705 git init connectivity-only &&
706 (
707 cd connectivity-only &&
708 test_commit foo &&
709 rm -f .git/index &&
710 tree=$(git rev-parse HEAD^{tree}) &&
711 remove_object $(git rev-parse HEAD:foo.t) &&
712 test_must_fail git fsck --connectivity-only $tree
713 )
714'
715
90cf590f
JS
716test_expect_success 'fsck --name-objects' '
717 rm -rf name-objects &&
718 git init name-objects &&
719 (
720 cd name-objects &&
e89f8936 721 git config core.logAllRefUpdates false &&
90cf590f 722 test_commit julius caesar.t &&
e89f8936
JS
723 test_commit augustus44 &&
724 test_commit caesar &&
0b20f1a2 725 remove_object $(git rev-parse julius:caesar.t) &&
90cf590f 726 tree=$(git rev-parse --verify julius:) &&
e89f8936
JS
727 git tag -d julius &&
728 test_must_fail git fsck --name-objects >out &&
729 test_i18ngrep "$tree (refs/tags/augustus44\\^:" out
90cf590f
JS
730 )
731'
732
771e7d57
JK
733test_expect_success 'alternate objects are correctly blamed' '
734 test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
8de6b383 735 name=$(test_oid numeric) &&
736 path=$(test_oid_to_path "$name") &&
771e7d57
JK
737 git init --bare alt.git &&
738 echo "../../alt.git/objects" >.git/objects/info/alternates &&
8de6b383 739 mkdir alt.git/objects/$(dirname $path) &&
740 >alt.git/objects/$(dirname $path)/$(basename $path) &&
771e7d57 741 test_must_fail git fsck >out 2>&1 &&
674ba340 742 test_i18ngrep alt.git out
771e7d57
JK
743'
744
118e6cea
JK
745test_expect_success 'fsck errors in packed objects' '
746 git cat-file commit HEAD >basis &&
747 sed "s/</one/" basis >one &&
748 sed "s/</foo/" basis >two &&
749 one=$(git hash-object -t commit -w one) &&
750 two=$(git hash-object -t commit -w two) &&
751 pack=$(
752 {
753 echo $one &&
754 echo $two
755 } | git pack-objects .git/objects/pack/pack
756 ) &&
757 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
758 remove_object $one &&
759 remove_object $two &&
760 test_must_fail git fsck 2>out &&
674ba340
NTND
761 test_i18ngrep "error in commit $one.* - bad name" out &&
762 test_i18ngrep "error in commit $two.* - bad name" out &&
118e6cea
JK
763 ! grep corrupt out
764'
765
a7c28a21
JT
766test_expect_success 'fsck fails on corrupt packfile' '
767 hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
768 pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
769
770 # Corrupt the first byte of the first object. (It contains 3 type bits,
771 # at least one of which is not zero, so setting the first byte to 0 is
772 # sufficient.)
773 chmod a+w .git/objects/pack/pack-$pack.pack &&
dc156bc3 774 printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
a7c28a21
JT
775
776 test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
777 remove_object $hsh &&
778 test_must_fail git fsck 2>out &&
779 test_i18ngrep "checksum mismatch" out
780'
781
c68b489e
JK
782test_expect_success 'fsck finds problems in duplicate loose objects' '
783 rm -rf broken-duplicate &&
784 git init broken-duplicate &&
785 (
786 cd broken-duplicate &&
787 test_commit duplicate &&
788 # no "-d" here, so we end up with duplicates
789 git repack &&
790 # now corrupt the loose copy
4627c67f
ÆAB
791 oid="$(git rev-parse HEAD)" &&
792 file=$(sha1_file "$oid") &&
c68b489e
JK
793 rm "$file" &&
794 echo broken >"$file" &&
4627c67f
ÆAB
795 test_must_fail git fsck 2>err &&
796
797 cat >expect <<-EOF &&
798 error: inflate: data stream error (incorrect header check)
799 error: unable to unpack header of $file
800 error: $oid: object corrupt or missing: $file
801 EOF
802 grep "^error: " err >actual &&
803 test_cmp expect actual
c68b489e
JK
804 )
805'
806
cce044df
JK
807test_expect_success 'fsck detects trailing loose garbage (commit)' '
808 git cat-file commit HEAD >basis &&
809 echo bump-commit-sha1 >>basis &&
810 commit=$(git hash-object -w -t commit basis) &&
811 file=$(sha1_file $commit) &&
812 test_when_finished "remove_object $commit" &&
813 chmod +w "$file" &&
814 echo garbage >>"$file" &&
815 test_must_fail git fsck 2>out &&
816 test_i18ngrep "garbage.*$commit" out
817'
818
5632baf2 819test_expect_success 'fsck detects trailing loose garbage (large blob)' '
cce044df
JK
820 blob=$(echo trailing | git hash-object -w --stdin) &&
821 file=$(sha1_file $blob) &&
822 test_when_finished "remove_object $blob" &&
823 chmod +w "$file" &&
824 echo garbage >>"$file" &&
5632baf2 825 test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
cce044df
JK
826 test_i18ngrep "garbage.*$blob" out
827'
828
ccdc4819
JK
829test_expect_success 'fsck detects truncated loose object' '
830 # make it big enough that we know we will truncate in the data
831 # portion, not the header
18ad13e5 832 test-tool genrandom truncate 4096 >file &&
ccdc4819
JK
833 blob=$(git hash-object -w file) &&
834 file=$(sha1_file $blob) &&
835 test_when_finished "remove_object $blob" &&
836 test_copy_bytes 1024 <"$file" >tmp &&
837 rm "$file" &&
838 mv -f tmp "$file" &&
839
840 # check both regular and streaming code paths
841 test_must_fail git fsck 2>out &&
842 test_i18ngrep corrupt.*$blob out &&
843
844 test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
845 test_i18ngrep corrupt.*$blob out
846'
847
b4584e4f
JK
848# for each of type, we have one version which is referenced by another object
849# (and so while unreachable, not dangling), and another variant which really is
850# dangling.
8d8c2a5a 851test_expect_success 'create dangling-object repository' '
b4584e4f
JK
852 git init dangling &&
853 (
854 cd dangling &&
855 blob=$(echo not-dangling | git hash-object -w --stdin) &&
856 dblob=$(echo dangling | git hash-object -w --stdin) &&
857 tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
858 dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
859 commit=$(git commit-tree $tree) &&
860 dcommit=$(git commit-tree -p $commit $tree) &&
861
8d8c2a5a 862 cat >expect <<-EOF
b4584e4f
JK
863 dangling blob $dblob
864 dangling commit $dcommit
865 dangling tree $dtree
866 EOF
8d8c2a5a
JK
867 )
868'
b4584e4f 869
8d8c2a5a
JK
870test_expect_success 'fsck notices dangling objects' '
871 (
872 cd dangling &&
b4584e4f
JK
873 git fsck >actual &&
874 # the output order is non-deterministic, as it comes from a hash
875 sort <actual >actual.sorted &&
1108cea7 876 test_cmp expect actual.sorted
b4584e4f
JK
877 )
878'
879
8d8c2a5a
JK
880test_expect_success 'fsck --connectivity-only notices dangling objects' '
881 (
882 cd dangling &&
883 git fsck --connectivity-only >actual &&
884 # the output order is non-deterministic, as it comes from a hash
885 sort <actual >actual.sorted &&
1108cea7 886 test_cmp expect actual.sorted
8d8c2a5a
JK
887 )
888'
889
c6c7b16d
JK
890test_expect_success 'fsck $name notices bogus $name' '
891 test_must_fail git fsck bogus &&
8125a58b 892 test_must_fail git fsck $ZERO_OID
c6c7b16d
JK
893'
894
c3271a0e
JK
895test_expect_success 'bogus head does not fallback to all heads' '
896 # set up a case that will cause a reachability complaint
897 echo to-be-deleted >foo &&
898 git add foo &&
899 blob=$(git rev-parse :foo) &&
900 test_when_finished "git rm --cached foo" &&
901 remove_object $blob &&
8125a58b 902 test_must_fail git fsck $ZERO_OID >out 2>&1 &&
c3271a0e
JK
903 ! grep $blob out
904'
905
4d9bc37f
JH
906# Corrupt the checksum on the index.
907# Add 1 to the last byte in the SHA.
908corrupt_index_checksum () {
909 perl -w -e '
910 use Fcntl ":seek";
911 open my $fh, "+<", ".git/index" or die "open: $!";
912 binmode $fh;
913 seek $fh, -1, SEEK_END or die "seek: $!";
914 read $fh, my $in_byte, 1 or die "read: $!";
915
916 $in_value = unpack("C", $in_byte);
917 $out_value = ($in_value + 1) & 255;
918
919 $out_byte = pack("C", $out_value);
920
921 seek $fh, -1, SEEK_END or die "seek: $!";
922 print $fh $out_byte;
923 close $fh or die "close: $!";
924 '
925}
926
927# Corrupt the checksum on the index and then
928# verify that only fsck notices.
a33fc72f
JH
929test_expect_success 'detect corrupt index file in fsck' '
930 cp .git/index .git/index.backup &&
931 test_when_finished "mv .git/index.backup .git/index" &&
4d9bc37f
JH
932 corrupt_index_checksum &&
933 test_must_fail git fsck --cache 2>errors &&
9d0a9e90 934 test_i18ngrep "bad index file" errors
a33fc72f
JH
935'
936
31deb28f 937test_expect_success 'fsck error and recovery on invalid object type' '
093fffdf
ÆAB
938 git init --bare garbage-type &&
939 (
940 cd garbage-type &&
941
31deb28f 942 garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
093fffdf
ÆAB
943
944 cat >err.expect <<-\EOF &&
945 fatal: invalid object type
946 EOF
947 test_must_fail git fsck >out 2>err &&
31deb28f
ÆAB
948 grep -e "^error" -e "^fatal" err >errors &&
949 test_line_count = 1 errors &&
950 grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err
093fffdf
ÆAB
951 )
952'
953
27ab4784
PS
954test_expect_success 'fsck error on gitattributes with excessive line lengths' '
955 blob=$(printf "pattern %02048d" 1 | git hash-object -w --stdin) &&
956 test_when_finished "remove_object $blob" &&
957 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
958 test_when_finished "remove_object $tree" &&
959 cat >expected <<-EOF &&
960 error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
961 EOF
962 test_must_fail git fsck --no-dangling >actual 2>&1 &&
963 test_cmp expected actual
964'
965
966test_expect_success 'fsck error on gitattributes with excessive size' '
967 blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
968 test_when_finished "remove_object $blob" &&
969 tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
970 test_when_finished "remove_object $tree" &&
971 cat >expected <<-EOF &&
972 error in blob $blob: gitattributesLarge: .gitattributes too large to parse
973 EOF
974 test_must_fail git fsck --no-dangling >actual 2>&1 &&
975 test_cmp expected actual
976'
977
469e2ebf 978test_done