]>
Commit | Line | Data |
---|---|---|
469e2ebf JH |
1 | #!/bin/sh |
2 | ||
dbedf8bf JN |
3 | test_description='git fsck random collection of tests |
4 | ||
5 | * (HEAD) B | |
6 | * (master) A | |
7 | ' | |
469e2ebf JH |
8 | |
9 | . ./test-lib.sh | |
10 | ||
11 | test_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 |
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 && | |
c6a13b2c | 29 | git fsck --no-dangling >../actual 2>&1 |
dbedf8bf | 30 | ) && |
1c5e94f4 | 31 | test_must_be_empty actual |
e15ef669 JH |
32 | ' |
33 | ||
dbedf8bf JN |
34 | test_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 | ||
dbedf8bf JN |
43 | test_expect_success 'setup: helpers for corruption tests' ' |
44 | sha1_file() { | |
0b20f1a2 JK |
45 | remainder=${1#??} && |
46 | firsttwo=${1%$remainder} && | |
47 | echo ".git/objects/$firsttwo/$remainder" | |
dbedf8bf JN |
48 | } && |
49 | ||
50 | remove_object() { | |
0b20f1a2 | 51 | rm "$(sha1_file "$1")" |
dbedf8bf JN |
52 | } |
53 | ' | |
54 | ||
02a6552c TR |
55 | test_expect_success 'object with bad sha1' ' |
56 | sha=$(echo blob | git hash-object -w --stdin) && | |
8de6b383 | 57 | old=$(test_oid_to_path "$sha") && |
58 | new=$(dirname $old)/$(test_oid ff_2) && | |
a48fcd83 | 59 | sha="$(dirname $new)$(basename $new)" && |
02a6552c | 60 | mv .git/objects/$old .git/objects/$new && |
dbedf8bf | 61 | test_when_finished "remove_object $sha" && |
02a6552c | 62 | git update-index --add --cacheinfo 100644 $sha foo && |
dbedf8bf | 63 | test_when_finished "git read-tree -u --reset HEAD" && |
02a6552c | 64 | tree=$(git write-tree) && |
dbedf8bf | 65 | test_when_finished "remove_object $tree" && |
02a6552c | 66 | cmt=$(echo bogus | git commit-tree $tree) && |
dbedf8bf | 67 | test_when_finished "remove_object $cmt" && |
02a6552c | 68 | git update-ref refs/heads/bogus $cmt && |
dbedf8bf JN |
69 | test_when_finished "git update-ref -d refs/heads/bogus" && |
70 | ||
2e770fe4 | 71 | test_must_fail git fsck 2>out && |
674ba340 | 72 | test_i18ngrep "$sha.*corrupt" out |
02a6552c TR |
73 | ' |
74 | ||
75 | test_expect_success 'branch pointing to non-commit' ' | |
dbedf8bf JN |
76 | git rev-parse HEAD^{tree} >.git/refs/heads/invalid && |
77 | test_when_finished "git update-ref -d refs/heads/invalid" && | |
122f76f5 | 78 | test_must_fail git fsck 2>out && |
674ba340 | 79 | test_i18ngrep "not a commit" out |
02a6552c TR |
80 | ' |
81 | ||
122f76f5 JH |
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 && | |
8de6b383 | 85 | echo $ZERO_OID >.git/HEAD && |
122f76f5 JH |
86 | # avoid corrupt/broken HEAD from interfering with repo discovery |
87 | test_must_fail env GIT_DIR=.git git fsck 2>out && | |
674ba340 | 88 | test_i18ngrep "detached HEAD points" out |
122f76f5 JH |
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 && | |
674ba340 | 97 | test_i18ngrep "HEAD points to something strange" out |
122f76f5 JH |
98 | ' |
99 | ||
b29759d8 NTND |
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 && | |
3813a89f | 108 | test_i18ngrep "main-worktree/HEAD: detached HEAD points" out |
b29759d8 NTND |
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 && | |
3813a89f | 116 | test_i18ngrep "worktrees/other/HEAD: detached HEAD points" out |
b29759d8 NTND |
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 && | |
3813a89f | 124 | test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out |
b29759d8 NTND |
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 && | |
3813a89f | 132 | test_i18ngrep "worktrees/other/HEAD points to something strange" out |
b29759d8 NTND |
133 | ' |
134 | ||
42d4e1d1 | 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 | ||
daae1922 JN |
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) && | |
dbedf8bf | 163 | test_when_finished "remove_object $new" && |
daae1922 | 164 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 165 | test_when_finished "git update-ref -d refs/heads/bogus" && |
daae1922 | 166 | git fsck 2>out && |
dbedf8bf | 167 | ! grep "commit $new" out |
daae1922 | 168 | ' |
daae1922 | 169 | |
daae1922 JN |
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) && | |
dbedf8bf | 174 | test_when_finished "remove_object $new" && |
daae1922 | 175 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 176 | test_when_finished "git update-ref -d refs/heads/bogus" && |
2e770fe4 | 177 | test_must_fail git fsck 2>out && |
674ba340 | 178 | test_i18ngrep "error in commit $new" out |
daae1922 | 179 | ' |
02a6552c | 180 | |
53f53cff | 181 | test_expect_success 'missing < email delimiter is reported nicely' ' |
e3c98120 DI |
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" && | |
2e770fe4 | 188 | test_must_fail git fsck 2>out && |
674ba340 | 189 | test_i18ngrep "error in commit $new.* - bad name" out |
e3c98120 DI |
190 | ' |
191 | ||
53f53cff | 192 | test_expect_success 'missing email is reported nicely' ' |
e3c98120 DI |
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" && | |
2e770fe4 | 199 | test_must_fail git fsck 2>out && |
674ba340 | 200 | test_i18ngrep "error in commit $new.* - missing email" out |
e3c98120 DI |
201 | ' |
202 | ||
53f53cff | 203 | test_expect_success '> in name is reported' ' |
e3c98120 DI |
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" && | |
2e770fe4 | 210 | test_must_fail git fsck 2>out && |
674ba340 | 211 | test_i18ngrep "error in commit $new" out |
e3c98120 DI |
212 | ' |
213 | ||
d4b8de04 JK |
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" && | |
2e770fe4 | 223 | test_must_fail git fsck 2>out && |
674ba340 | 224 | test_i18ngrep "error in commit $new.*integer overflow" out |
d4b8de04 JK |
225 | ' |
226 | ||
80c7f5a0 RS |
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 && | |
674ba340 | 235 | test_i18ngrep "error in commit $new.*unterminated header: NUL at offset" out |
80c7f5a0 RS |
236 | ' |
237 | ||
8354fa3d | 238 | test_expect_success 'tree object with duplicate entries' ' |
1ada11ee | 239 | test_when_finished "for i in \$T; do remove_object \$i; done" && |
2e770fe4 JK |
240 | T=$( |
241 | GIT_INDEX_FILE=test-index && | |
242 | export GIT_INDEX_FILE && | |
243 | rm -f test-index && | |
244 | >x && | |
245 | git add x && | |
1ada11ee | 246 | git rev-parse :x && |
2e770fe4 | 247 | T=$(git write-tree) && |
1ada11ee | 248 | echo $T && |
2e770fe4 JK |
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 && | |
674ba340 | 256 | test_i18ngrep "error in tree .*contains duplicate file entries" out |
2e770fe4 JK |
257 | ' |
258 | ||
fc12aa7b RS |
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/ | |
fe747043 | 286 | check_duplicate_names success x x.1 x.1.2 x/ |
9068cfb2 | 287 | |
8354fa3d | 288 | test_expect_success 'unparseable tree object' ' |
8de6b383 | 289 | test_oid_cache <<-\EOF && |
290 | junk sha1:twenty-bytes-of-junk | |
291 | junk sha256:twenty-bytes-of-junk-twelve-more | |
292 | EOF | |
293 | ||
8354fa3d DT |
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" && | |
8de6b383 | 297 | junk=$(test_oid junk) && |
298 | tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) && | |
8354fa3d DT |
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 | ||
2720f6db RS |
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 | ||
4551d035 | 325 | test_expect_success 'tag pointing to nonexistent' ' |
8de6b383 | 326 | badoid=$(test_oid deadbeef) && |
327 | cat >invalid-tag <<-EOF && | |
328 | object $badoid | |
dbedf8bf JN |
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" && | |
4551d035 | 340 | test_must_fail git fsck --tags >out && |
674ba340 | 341 | test_i18ngrep "broken link" out |
02a6552c TR |
342 | ' |
343 | ||
4551d035 | 344 | test_expect_success 'tag pointing to something else than its type' ' |
dbedf8bf JN |
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" && | |
9dad83be | 360 | test_must_fail git fsck --tags |
02a6552c TR |
361 | ' |
362 | ||
90e3e5f0 JS |
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 && | |
7add4419 JK |
378 | |
379 | cat >expect <<-EOF && | |
71ab8fa8 JS |
380 | warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format |
381 | warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line | |
7add4419 | 382 | EOF |
674ba340 | 383 | test_i18ncmp expect out |
90e3e5f0 JS |
384 | ' |
385 | ||
b659605d JH |
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 && | |
674ba340 | 402 | test_i18ngrep "error in tag .*: invalid author/committer" out |
b659605d JH |
403 | ' |
404 | ||
8a272f29 | 405 | test_expect_success 'tag with NUL in header' ' |
80c7f5a0 RS |
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 && | |
674ba340 | 421 | test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out |
80c7f5a0 RS |
422 | ' |
423 | ||
dbedf8bf JN |
424 | test_expect_success 'cleaned up' ' |
425 | git fsck >actual 2>&1 && | |
1c5e94f4 | 426 | test_must_be_empty actual |
dbedf8bf | 427 | ' |
02a6552c | 428 | |
cb8da705 CB |
429 | test_expect_success 'rev-list --verify-objects' ' |
430 | git rev-list --verify-objects --all >/dev/null 2>out && | |
1c5e94f4 | 431 | test_must_be_empty out |
cb8da705 CB |
432 | ' |
433 | ||
434 | test_expect_success 'rev-list --verify-objects with bad sha1' ' | |
435 | sha=$(echo blob | git hash-object -w --stdin) && | |
8de6b383 | 436 | old=$(test_oid_to_path $sha) && |
437 | new=$(dirname $old)/$(test_oid ff_2) && | |
cb8da705 CB |
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 && | |
8de6b383 | 451 | test_i18ngrep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out |
cb8da705 CB |
452 | ' |
453 | ||
2becf00f JS |
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 | ||
c479d14a | 465 | _bz='\0' |
8de6b383 | 466 | _bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g') |
c479d14a JK |
467 | |
468 | test_expect_success 'fsck notices blob entry pointing to null sha1' ' | |
469 | (git init null-blob && | |
470 | cd null-blob && | |
8de6b383 | 471 | sha=$(printf "100644 file$_bz$_bzoid" | |
c479d14a JK |
472 | git hash-object -w --stdin -t tree) && |
473 | git fsck 2>out && | |
674ba340 | 474 | test_i18ngrep "warning.*null sha1" out |
c479d14a JK |
475 | ) |
476 | ' | |
477 | ||
478 | test_expect_success 'fsck notices submodule entry pointing to null sha1' ' | |
479 | (git init null-commit && | |
480 | cd null-commit && | |
8de6b383 | 481 | sha=$(printf "160000 submodule$_bz$_bzoid" | |
c479d14a JK |
482 | git hash-object -w --stdin -t tree) && |
483 | git fsck 2>out && | |
674ba340 | 484 | test_i18ngrep "warning.*null sha1" out |
c479d14a JK |
485 | ) |
486 | ' | |
487 | ||
a18fcc9f | 488 | while read name path pretty; do |
450870cb | 489 | while read mode type; do |
a18fcc9f JK |
490 | : ${pretty:=$path} |
491 | test_expect_success "fsck notices $pretty as $type" ' | |
450870cb JK |
492 | ( |
493 | git init $name-$type && | |
494 | cd $name-$type && | |
e1d911dd | 495 | git config core.protectNTFS false && |
450870cb JK |
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 && | |
d08c13b9 | 503 | bad_tree=$(git mktree <bad) && |
450870cb | 504 | git fsck 2>out && |
674ba340 | 505 | test_i18ngrep "warning.*tree $bad_tree" out |
450870cb JK |
506 | )' |
507 | done <<-\EOF | |
508 | 100644 blob | |
509 | 040000 tree | |
510 | EOF | |
a18fcc9f | 511 | done <<-EOF |
450870cb JK |
512 | dot . |
513 | dotdot .. | |
514 | dotgit .git | |
76e86fc6 | 515 | dotgit-case .GIT |
a18fcc9f | 516 | dotgit-unicode .gI${u200c}T .gI{u200c}T |
d08c13b9 JS |
517 | dotgit-case2 .Git |
518 | git-tilde1 git~1 | |
519 | dotgitdot .git. | |
520 | dot-backslash-case .\\\\.GIT\\\\foobar | |
521 | dotgit-case-backslash .git\\\\foobar | |
450870cb | 522 | EOF |
5c17f512 | 523 | |
6aaf956b JK |
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 | ||
6d2d780f JH |
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 && | |
674ba340 | 551 | test_i18ngrep nulInCommit warn.1 && |
6d2d780f | 552 | git fsck 2>warn.2 && |
674ba340 | 553 | test_i18ngrep nulInCommit warn.2 |
6d2d780f JH |
554 | ) |
555 | ' | |
556 | ||
30d1038d JK |
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 | ||
02976bf8 JS |
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 && | |
3e3f8bd6 JK |
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. | |
8de6b383 | 632 | empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) && |
3e3f8bd6 | 633 | blob=$(echo unrelated | git hash-object -w --stdin) && |
c20d4d70 | 634 | mv -f $(sha1_file $blob) $empty && |
3e3f8bd6 | 635 | |
02976bf8 JS |
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 | ||
3e3f8bd6 JK |
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 | ||
90cf590f JS |
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 && | |
0b20f1a2 | 668 | remove_object $(git rev-parse julius:caesar.t) && |
90cf590f JS |
669 | test_must_fail git fsck --name-objects >out && |
670 | tree=$(git rev-parse --verify julius:) && | |
a59cfb32 | 671 | test_i18ngrep "$tree (refs/tags/julius:" out |
90cf590f JS |
672 | ) |
673 | ' | |
674 | ||
771e7d57 JK |
675 | test_expect_success 'alternate objects are correctly blamed' ' |
676 | test_when_finished "rm -rf alt.git .git/objects/info/alternates" && | |
8de6b383 | 677 | name=$(test_oid numeric) && |
678 | path=$(test_oid_to_path "$name") && | |
771e7d57 JK |
679 | git init --bare alt.git && |
680 | echo "../../alt.git/objects" >.git/objects/info/alternates && | |
8de6b383 | 681 | mkdir alt.git/objects/$(dirname $path) && |
682 | >alt.git/objects/$(dirname $path)/$(basename $path) && | |
771e7d57 | 683 | test_must_fail git fsck >out 2>&1 && |
674ba340 | 684 | test_i18ngrep alt.git out |
771e7d57 JK |
685 | ' |
686 | ||
118e6cea JK |
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 && | |
674ba340 NTND |
703 | test_i18ngrep "error in commit $one.* - bad name" out && |
704 | test_i18ngrep "error in commit $two.* - bad name" out && | |
118e6cea JK |
705 | ! grep corrupt out |
706 | ' | |
707 | ||
a7c28a21 JT |
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 && | |
dc156bc3 | 716 | printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 && |
a7c28a21 JT |
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 | ||
c68b489e JK |
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 | ||
cce044df JK |
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 | ||
5632baf2 | 752 | test_expect_success 'fsck detects trailing loose garbage (large blob)' ' |
cce044df JK |
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" && | |
5632baf2 | 758 | test_must_fail git -c core.bigfilethreshold=5 fsck 2>out && |
cce044df JK |
759 | test_i18ngrep "garbage.*$blob" out |
760 | ' | |
761 | ||
ccdc4819 JK |
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 | |
18ad13e5 | 765 | test-tool genrandom truncate 4096 >file && |
ccdc4819 JK |
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 | ||
b4584e4f JK |
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. | |
8d8c2a5a | 784 | test_expect_success 'create dangling-object repository' ' |
b4584e4f JK |
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 | ||
8d8c2a5a | 795 | cat >expect <<-EOF |
b4584e4f JK |
796 | dangling blob $dblob |
797 | dangling commit $dcommit | |
798 | dangling tree $dtree | |
799 | EOF | |
8d8c2a5a JK |
800 | ) |
801 | ' | |
b4584e4f | 802 | |
8d8c2a5a JK |
803 | test_expect_success 'fsck notices dangling objects' ' |
804 | ( | |
805 | cd dangling && | |
b4584e4f JK |
806 | git fsck >actual && |
807 | # the output order is non-deterministic, as it comes from a hash | |
808 | sort <actual >actual.sorted && | |
674ba340 | 809 | test_i18ncmp expect actual.sorted |
b4584e4f JK |
810 | ) |
811 | ' | |
812 | ||
8d8c2a5a JK |
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 | ||
c6c7b16d JK |
823 | test_expect_success 'fsck $name notices bogus $name' ' |
824 | test_must_fail git fsck bogus && | |
8125a58b | 825 | test_must_fail git fsck $ZERO_OID |
c6c7b16d JK |
826 | ' |
827 | ||
c3271a0e JK |
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 && | |
8125a58b | 835 | test_must_fail git fsck $ZERO_OID >out 2>&1 && |
c3271a0e JK |
836 | ! grep $blob out |
837 | ' | |
838 | ||
4d9bc37f JH |
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. | |
a33fc72f JH |
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" && | |
4d9bc37f JH |
865 | corrupt_index_checksum && |
866 | test_must_fail git fsck --cache 2>errors && | |
9d0a9e90 | 867 | test_i18ngrep "bad index file" errors |
a33fc72f JH |
868 | ' |
869 | ||
469e2ebf | 870 | test_done |