]>
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 && | |
dbedf8bf JN |
19 | git reflog expire --expire=now --all && |
20 | >empty | |
469e2ebf JH |
21 | ' |
22 | ||
e15ef669 JH |
23 | test_expect_success 'loose objects borrowed from alternate are not missing' ' |
24 | mkdir another && | |
25 | ( | |
26 | cd another && | |
27 | git init && | |
28 | echo ../../../.git/objects >.git/objects/info/alternates && | |
29 | test_commit C fileC one && | |
c6a13b2c | 30 | git fsck --no-dangling >../actual 2>&1 |
dbedf8bf | 31 | ) && |
dbedf8bf | 32 | test_cmp empty actual |
e15ef669 JH |
33 | ' |
34 | ||
dbedf8bf JN |
35 | test_expect_success 'HEAD is part of refs, valid objects appear valid' ' |
36 | git fsck >actual 2>&1 && | |
37 | test_cmp empty actual | |
0adc6a3d JN |
38 | ' |
39 | ||
02a6552c TR |
40 | # Corruption tests follow. Make sure to remove all traces of the |
41 | # specific corruption you test afterwards, lest a later test trip over | |
42 | # it. | |
43 | ||
dbedf8bf JN |
44 | test_expect_success 'setup: helpers for corruption tests' ' |
45 | sha1_file() { | |
46 | echo "$*" | sed "s#..#.git/objects/&/#" | |
47 | } && | |
48 | ||
49 | remove_object() { | |
50 | file=$(sha1_file "$*") && | |
51 | test -e "$file" && | |
52 | rm -f "$file" | |
53 | } | |
54 | ' | |
55 | ||
02a6552c TR |
56 | test_expect_success 'object with bad sha1' ' |
57 | sha=$(echo blob | git hash-object -w --stdin) && | |
02a6552c TR |
58 | old=$(echo $sha | sed "s+^..+&/+") && |
59 | new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff && | |
a48fcd83 | 60 | sha="$(dirname $new)$(basename $new)" && |
02a6552c | 61 | mv .git/objects/$old .git/objects/$new && |
dbedf8bf | 62 | test_when_finished "remove_object $sha" && |
02a6552c | 63 | git update-index --add --cacheinfo 100644 $sha foo && |
dbedf8bf | 64 | test_when_finished "git read-tree -u --reset HEAD" && |
02a6552c | 65 | tree=$(git write-tree) && |
dbedf8bf | 66 | test_when_finished "remove_object $tree" && |
02a6552c | 67 | cmt=$(echo bogus | git commit-tree $tree) && |
dbedf8bf | 68 | test_when_finished "remove_object $cmt" && |
02a6552c | 69 | git update-ref refs/heads/bogus $cmt && |
dbedf8bf JN |
70 | test_when_finished "git update-ref -d refs/heads/bogus" && |
71 | ||
2e770fe4 | 72 | test_must_fail git fsck 2>out && |
dbedf8bf JN |
73 | cat out && |
74 | grep "$sha.*corrupt" out | |
02a6552c TR |
75 | ' |
76 | ||
77 | test_expect_success 'branch pointing to non-commit' ' | |
dbedf8bf JN |
78 | git rev-parse HEAD^{tree} >.git/refs/heads/invalid && |
79 | test_when_finished "git update-ref -d refs/heads/invalid" && | |
122f76f5 | 80 | test_must_fail git fsck 2>out && |
dbedf8bf JN |
81 | cat out && |
82 | grep "not a commit" out | |
02a6552c TR |
83 | ' |
84 | ||
122f76f5 JH |
85 | test_expect_success 'HEAD link pointing at a funny object' ' |
86 | test_when_finished "mv .git/SAVED_HEAD .git/HEAD" && | |
87 | mv .git/HEAD .git/SAVED_HEAD && | |
88 | echo 0000000000000000000000000000000000000000 >.git/HEAD && | |
89 | # avoid corrupt/broken HEAD from interfering with repo discovery | |
90 | test_must_fail env GIT_DIR=.git git fsck 2>out && | |
91 | cat out && | |
92 | grep "detached HEAD points" out | |
93 | ' | |
94 | ||
95 | test_expect_success 'HEAD link pointing at a funny place' ' | |
96 | test_when_finished "mv .git/SAVED_HEAD .git/HEAD" && | |
97 | mv .git/HEAD .git/SAVED_HEAD && | |
98 | echo "ref: refs/funny/place" >.git/HEAD && | |
99 | # avoid corrupt/broken HEAD from interfering with repo discovery | |
100 | test_must_fail env GIT_DIR=.git git fsck 2>out && | |
101 | cat out && | |
102 | grep "HEAD points to something strange" out | |
103 | ' | |
104 | ||
daae1922 JN |
105 | test_expect_success 'email without @ is okay' ' |
106 | git cat-file commit HEAD >basis && | |
107 | sed "s/@/AT/" basis >okay && | |
108 | new=$(git hash-object -t commit -w --stdin <okay) && | |
dbedf8bf | 109 | test_when_finished "remove_object $new" && |
daae1922 | 110 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 111 | test_when_finished "git update-ref -d refs/heads/bogus" && |
daae1922 JN |
112 | git fsck 2>out && |
113 | cat out && | |
dbedf8bf | 114 | ! grep "commit $new" out |
daae1922 | 115 | ' |
daae1922 | 116 | |
daae1922 JN |
117 | test_expect_success 'email with embedded > is not okay' ' |
118 | git cat-file commit HEAD >basis && | |
119 | sed "s/@[a-z]/&>/" basis >bad-email && | |
120 | new=$(git hash-object -t commit -w --stdin <bad-email) && | |
dbedf8bf | 121 | test_when_finished "remove_object $new" && |
daae1922 | 122 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 123 | test_when_finished "git update-ref -d refs/heads/bogus" && |
2e770fe4 | 124 | test_must_fail git fsck 2>out && |
daae1922 JN |
125 | cat out && |
126 | grep "error in commit $new" out | |
127 | ' | |
02a6552c | 128 | |
53f53cff | 129 | test_expect_success 'missing < email delimiter is reported nicely' ' |
e3c98120 DI |
130 | git cat-file commit HEAD >basis && |
131 | sed "s/<//" basis >bad-email-2 && | |
132 | new=$(git hash-object -t commit -w --stdin <bad-email-2) && | |
133 | test_when_finished "remove_object $new" && | |
134 | git update-ref refs/heads/bogus "$new" && | |
135 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 136 | test_must_fail git fsck 2>out && |
e3c98120 DI |
137 | cat out && |
138 | grep "error in commit $new.* - bad name" out | |
139 | ' | |
140 | ||
53f53cff | 141 | test_expect_success 'missing email is reported nicely' ' |
e3c98120 DI |
142 | git cat-file commit HEAD >basis && |
143 | sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 && | |
144 | new=$(git hash-object -t commit -w --stdin <bad-email-3) && | |
145 | test_when_finished "remove_object $new" && | |
146 | git update-ref refs/heads/bogus "$new" && | |
147 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 148 | test_must_fail git fsck 2>out && |
e3c98120 DI |
149 | cat out && |
150 | grep "error in commit $new.* - missing email" out | |
151 | ' | |
152 | ||
53f53cff | 153 | test_expect_success '> in name is reported' ' |
e3c98120 DI |
154 | git cat-file commit HEAD >basis && |
155 | sed "s/ </> </" basis >bad-email-4 && | |
156 | new=$(git hash-object -t commit -w --stdin <bad-email-4) && | |
157 | test_when_finished "remove_object $new" && | |
158 | git update-ref refs/heads/bogus "$new" && | |
159 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 160 | test_must_fail git fsck 2>out && |
e3c98120 DI |
161 | cat out && |
162 | grep "error in commit $new" out | |
163 | ' | |
164 | ||
d4b8de04 JK |
165 | # date is 2^64 + 1 |
166 | test_expect_success 'integer overflow in timestamps is reported' ' | |
167 | git cat-file commit HEAD >basis && | |
168 | sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \ | |
169 | <basis >bad-timestamp && | |
170 | new=$(git hash-object -t commit -w --stdin <bad-timestamp) && | |
171 | test_when_finished "remove_object $new" && | |
172 | git update-ref refs/heads/bogus "$new" && | |
173 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 174 | test_must_fail git fsck 2>out && |
d4b8de04 JK |
175 | cat out && |
176 | grep "error in commit $new.*integer overflow" out | |
177 | ' | |
178 | ||
2e770fe4 JK |
179 | test_expect_success 'malformatted tree object' ' |
180 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
181 | test_when_finished "remove_object \$T" && | |
182 | T=$( | |
183 | GIT_INDEX_FILE=test-index && | |
184 | export GIT_INDEX_FILE && | |
185 | rm -f test-index && | |
186 | >x && | |
187 | git add x && | |
188 | T=$(git write-tree) && | |
189 | ( | |
190 | git cat-file tree $T && | |
191 | git cat-file tree $T | |
192 | ) | | |
193 | git hash-object -w -t tree --stdin | |
194 | ) && | |
195 | test_must_fail git fsck 2>out && | |
196 | grep "error in tree .*contains duplicate file entries" out | |
197 | ' | |
198 | ||
4551d035 | 199 | test_expect_success 'tag pointing to nonexistent' ' |
a48fcd83 | 200 | cat >invalid-tag <<-\EOF && |
dbedf8bf JN |
201 | object ffffffffffffffffffffffffffffffffffffffff |
202 | type commit | |
203 | tag invalid | |
204 | tagger T A Gger <tagger@example.com> 1234567890 -0000 | |
205 | ||
206 | This is an invalid tag. | |
207 | EOF | |
208 | ||
209 | tag=$(git hash-object -t tag -w --stdin <invalid-tag) && | |
210 | test_when_finished "remove_object $tag" && | |
211 | echo $tag >.git/refs/tags/invalid && | |
212 | test_when_finished "git update-ref -d refs/tags/invalid" && | |
4551d035 | 213 | test_must_fail git fsck --tags >out && |
02a6552c | 214 | cat out && |
dbedf8bf | 215 | grep "broken link" out |
02a6552c TR |
216 | ' |
217 | ||
4551d035 | 218 | test_expect_success 'tag pointing to something else than its type' ' |
dbedf8bf JN |
219 | sha=$(echo blob | git hash-object -w --stdin) && |
220 | test_when_finished "remove_object $sha" && | |
221 | cat >wrong-tag <<-EOF && | |
222 | object $sha | |
223 | type commit | |
224 | tag wrong | |
225 | tagger T A Gger <tagger@example.com> 1234567890 -0000 | |
226 | ||
227 | This is an invalid tag. | |
228 | EOF | |
229 | ||
230 | tag=$(git hash-object -t tag -w --stdin <wrong-tag) && | |
231 | test_when_finished "remove_object $tag" && | |
232 | echo $tag >.git/refs/tags/wrong && | |
233 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
9dad83be | 234 | test_must_fail git fsck --tags |
02a6552c TR |
235 | ' |
236 | ||
90e3e5f0 JS |
237 | test_expect_success 'tag with incorrect tag name & missing tagger' ' |
238 | sha=$(git rev-parse HEAD) && | |
239 | cat >wrong-tag <<-EOF && | |
240 | object $sha | |
241 | type commit | |
242 | tag wrong name format | |
243 | ||
244 | This is an invalid tag. | |
245 | EOF | |
246 | ||
247 | tag=$(git hash-object -t tag -w --stdin <wrong-tag) && | |
248 | test_when_finished "remove_object $tag" && | |
249 | echo $tag >.git/refs/tags/wrong && | |
250 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
251 | git fsck --tags 2>out && | |
7add4419 JK |
252 | |
253 | cat >expect <<-EOF && | |
71ab8fa8 JS |
254 | warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format |
255 | warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line | |
7add4419 JK |
256 | EOF |
257 | test_cmp expect out | |
90e3e5f0 JS |
258 | ' |
259 | ||
b659605d JH |
260 | test_expect_success 'tag with bad tagger' ' |
261 | sha=$(git rev-parse HEAD) && | |
262 | cat >wrong-tag <<-EOF && | |
263 | object $sha | |
264 | type commit | |
265 | tag not-quite-wrong | |
266 | tagger Bad Tagger Name | |
267 | ||
268 | This is an invalid tag. | |
269 | EOF | |
270 | ||
271 | tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) && | |
272 | test_when_finished "remove_object $tag" && | |
273 | echo $tag >.git/refs/tags/wrong && | |
274 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
275 | test_must_fail git fsck --tags 2>out && | |
276 | grep "error in tag .*: invalid author/committer" out | |
277 | ' | |
278 | ||
dbedf8bf JN |
279 | test_expect_success 'cleaned up' ' |
280 | git fsck >actual 2>&1 && | |
281 | test_cmp empty actual | |
282 | ' | |
02a6552c | 283 | |
cb8da705 CB |
284 | test_expect_success 'rev-list --verify-objects' ' |
285 | git rev-list --verify-objects --all >/dev/null 2>out && | |
286 | test_cmp empty out | |
287 | ' | |
288 | ||
289 | test_expect_success 'rev-list --verify-objects with bad sha1' ' | |
290 | sha=$(echo blob | git hash-object -w --stdin) && | |
291 | old=$(echo $sha | sed "s+^..+&/+") && | |
292 | new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff && | |
293 | sha="$(dirname $new)$(basename $new)" && | |
294 | mv .git/objects/$old .git/objects/$new && | |
295 | test_when_finished "remove_object $sha" && | |
296 | git update-index --add --cacheinfo 100644 $sha foo && | |
297 | test_when_finished "git read-tree -u --reset HEAD" && | |
298 | tree=$(git write-tree) && | |
299 | test_when_finished "remove_object $tree" && | |
300 | cmt=$(echo bogus | git commit-tree $tree) && | |
301 | test_when_finished "remove_object $cmt" && | |
302 | git update-ref refs/heads/bogus $cmt && | |
303 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
304 | ||
305 | test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out && | |
306 | cat out && | |
307 | grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out | |
308 | ' | |
309 | ||
2becf00f JS |
310 | test_expect_success 'force fsck to ignore double author' ' |
311 | git cat-file commit HEAD >basis && | |
312 | sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors && | |
313 | new=$(git hash-object -t commit -w --stdin <multiple-authors) && | |
314 | test_when_finished "remove_object $new" && | |
315 | git update-ref refs/heads/bogus "$new" && | |
316 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
317 | test_must_fail git fsck && | |
318 | git -c fsck.multipleAuthors=ignore fsck | |
319 | ' | |
320 | ||
c479d14a JK |
321 | _bz='\0' |
322 | _bz5="$_bz$_bz$_bz$_bz$_bz" | |
323 | _bz20="$_bz5$_bz5$_bz5$_bz5" | |
324 | ||
325 | test_expect_success 'fsck notices blob entry pointing to null sha1' ' | |
326 | (git init null-blob && | |
327 | cd null-blob && | |
328 | sha=$(printf "100644 file$_bz$_bz20" | | |
329 | git hash-object -w --stdin -t tree) && | |
330 | git fsck 2>out && | |
331 | cat out && | |
332 | grep "warning.*null sha1" out | |
333 | ) | |
334 | ' | |
335 | ||
336 | test_expect_success 'fsck notices submodule entry pointing to null sha1' ' | |
337 | (git init null-commit && | |
338 | cd null-commit && | |
339 | sha=$(printf "160000 submodule$_bz$_bz20" | | |
340 | git hash-object -w --stdin -t tree) && | |
341 | git fsck 2>out && | |
342 | cat out && | |
343 | grep "warning.*null sha1" out | |
344 | ) | |
345 | ' | |
346 | ||
a18fcc9f | 347 | while read name path pretty; do |
450870cb | 348 | while read mode type; do |
a18fcc9f JK |
349 | : ${pretty:=$path} |
350 | test_expect_success "fsck notices $pretty as $type" ' | |
450870cb JK |
351 | ( |
352 | git init $name-$type && | |
353 | cd $name-$type && | |
354 | echo content >file && | |
355 | git add file && | |
356 | git commit -m base && | |
357 | blob=$(git rev-parse :file) && | |
358 | tree=$(git rev-parse HEAD^{tree}) && | |
359 | value=$(eval "echo \$$type") && | |
360 | printf "$mode $type %s\t%s" "$value" "$path" >bad && | |
d08c13b9 | 361 | bad_tree=$(git mktree <bad) && |
450870cb JK |
362 | git fsck 2>out && |
363 | cat out && | |
d08c13b9 | 364 | grep "warning.*tree $bad_tree" out |
450870cb JK |
365 | )' |
366 | done <<-\EOF | |
367 | 100644 blob | |
368 | 040000 tree | |
369 | EOF | |
a18fcc9f | 370 | done <<-EOF |
450870cb JK |
371 | dot . |
372 | dotdot .. | |
373 | dotgit .git | |
76e86fc6 | 374 | dotgit-case .GIT |
a18fcc9f | 375 | dotgit-unicode .gI${u200c}T .gI{u200c}T |
d08c13b9 JS |
376 | dotgit-case2 .Git |
377 | git-tilde1 git~1 | |
378 | dotgitdot .git. | |
379 | dot-backslash-case .\\\\.GIT\\\\foobar | |
380 | dotgit-case-backslash .git\\\\foobar | |
450870cb | 381 | EOF |
5c17f512 | 382 | |
6aaf956b JK |
383 | test_expect_success 'fsck allows .Ňit' ' |
384 | ( | |
385 | git init not-dotgit && | |
386 | cd not-dotgit && | |
387 | echo content >file && | |
388 | git add file && | |
389 | git commit -m base && | |
390 | blob=$(git rev-parse :file) && | |
391 | printf "100644 blob $blob\t.\\305\\207it" >tree && | |
392 | tree=$(git mktree <tree) && | |
393 | git fsck 2>err && | |
394 | test_line_count = 0 err | |
395 | ) | |
396 | ' | |
397 | ||
30d1038d JK |
398 | # create a static test repo which is broken by omitting |
399 | # one particular object ($1, which is looked up via rev-parse | |
400 | # in the new repository). | |
401 | create_repo_missing () { | |
402 | rm -rf missing && | |
403 | git init missing && | |
404 | ( | |
405 | cd missing && | |
406 | git commit -m one --allow-empty && | |
407 | mkdir subdir && | |
408 | echo content >subdir/file && | |
409 | git add subdir/file && | |
410 | git commit -m two && | |
411 | unrelated=$(echo unrelated | git hash-object --stdin -w) && | |
412 | git tag -m foo tag $unrelated && | |
413 | sha1=$(git rev-parse --verify "$1") && | |
414 | path=$(echo $sha1 | sed 's|..|&/|') && | |
415 | rm .git/objects/$path | |
416 | ) | |
417 | } | |
418 | ||
419 | test_expect_success 'fsck notices missing blob' ' | |
420 | create_repo_missing HEAD:subdir/file && | |
421 | test_must_fail git -C missing fsck | |
422 | ' | |
423 | ||
424 | test_expect_success 'fsck notices missing subtree' ' | |
425 | create_repo_missing HEAD:subdir && | |
426 | test_must_fail git -C missing fsck | |
427 | ' | |
428 | ||
429 | test_expect_success 'fsck notices missing root tree' ' | |
430 | create_repo_missing HEAD^{tree} && | |
431 | test_must_fail git -C missing fsck | |
432 | ' | |
433 | ||
434 | test_expect_success 'fsck notices missing parent' ' | |
435 | create_repo_missing HEAD^ && | |
436 | test_must_fail git -C missing fsck | |
437 | ' | |
438 | ||
439 | test_expect_success 'fsck notices missing tagged object' ' | |
440 | create_repo_missing tag^{blob} && | |
441 | test_must_fail git -C missing fsck | |
442 | ' | |
443 | ||
444 | test_expect_success 'fsck notices ref pointing to missing commit' ' | |
445 | create_repo_missing HEAD && | |
446 | test_must_fail git -C missing fsck | |
447 | ' | |
448 | ||
449 | test_expect_success 'fsck notices ref pointing to missing tag' ' | |
450 | create_repo_missing tag && | |
451 | test_must_fail git -C missing fsck | |
452 | ' | |
453 | ||
02976bf8 JS |
454 | test_expect_success 'fsck --connectivity-only' ' |
455 | rm -rf connectivity-only && | |
456 | git init connectivity-only && | |
457 | ( | |
458 | cd connectivity-only && | |
459 | touch empty && | |
460 | git add empty && | |
461 | test_commit empty && | |
462 | empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 && | |
463 | rm -f $empty && | |
464 | echo invalid >$empty && | |
465 | test_must_fail git fsck --strict && | |
466 | git fsck --strict --connectivity-only && | |
467 | tree=$(git rev-parse HEAD:) && | |
468 | suffix=${tree#??} && | |
469 | tree=.git/objects/${tree%$suffix}/$suffix && | |
470 | rm -f $tree && | |
471 | echo invalid >$tree && | |
472 | test_must_fail git fsck --strict --connectivity-only | |
473 | ) | |
474 | ' | |
475 | ||
469e2ebf | 476 | test_done |