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