]>
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" && | |
02a6552c | 80 | git fsck 2>out && |
dbedf8bf JN |
81 | cat out && |
82 | grep "not a commit" out | |
02a6552c TR |
83 | ' |
84 | ||
daae1922 JN |
85 | test_expect_success 'email without @ is okay' ' |
86 | git cat-file commit HEAD >basis && | |
87 | sed "s/@/AT/" basis >okay && | |
88 | new=$(git hash-object -t commit -w --stdin <okay) && | |
dbedf8bf | 89 | test_when_finished "remove_object $new" && |
daae1922 | 90 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 91 | test_when_finished "git update-ref -d refs/heads/bogus" && |
daae1922 JN |
92 | git fsck 2>out && |
93 | cat out && | |
dbedf8bf | 94 | ! grep "commit $new" out |
daae1922 | 95 | ' |
daae1922 | 96 | |
daae1922 JN |
97 | test_expect_success 'email with embedded > is not okay' ' |
98 | git cat-file commit HEAD >basis && | |
99 | sed "s/@[a-z]/&>/" basis >bad-email && | |
100 | new=$(git hash-object -t commit -w --stdin <bad-email) && | |
dbedf8bf | 101 | test_when_finished "remove_object $new" && |
daae1922 | 102 | git update-ref refs/heads/bogus "$new" && |
dbedf8bf | 103 | test_when_finished "git update-ref -d refs/heads/bogus" && |
2e770fe4 | 104 | test_must_fail git fsck 2>out && |
daae1922 JN |
105 | cat out && |
106 | grep "error in commit $new" out | |
107 | ' | |
02a6552c | 108 | |
53f53cff | 109 | test_expect_success 'missing < email delimiter is reported nicely' ' |
e3c98120 DI |
110 | git cat-file commit HEAD >basis && |
111 | sed "s/<//" basis >bad-email-2 && | |
112 | new=$(git hash-object -t commit -w --stdin <bad-email-2) && | |
113 | test_when_finished "remove_object $new" && | |
114 | git update-ref refs/heads/bogus "$new" && | |
115 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 116 | test_must_fail git fsck 2>out && |
e3c98120 DI |
117 | cat out && |
118 | grep "error in commit $new.* - bad name" out | |
119 | ' | |
120 | ||
53f53cff | 121 | test_expect_success 'missing email is reported nicely' ' |
e3c98120 DI |
122 | git cat-file commit HEAD >basis && |
123 | sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 && | |
124 | new=$(git hash-object -t commit -w --stdin <bad-email-3) && | |
125 | test_when_finished "remove_object $new" && | |
126 | git update-ref refs/heads/bogus "$new" && | |
127 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 128 | test_must_fail git fsck 2>out && |
e3c98120 DI |
129 | cat out && |
130 | grep "error in commit $new.* - missing email" out | |
131 | ' | |
132 | ||
53f53cff | 133 | test_expect_success '> in name is reported' ' |
e3c98120 DI |
134 | git cat-file commit HEAD >basis && |
135 | sed "s/ </> </" basis >bad-email-4 && | |
136 | new=$(git hash-object -t commit -w --stdin <bad-email-4) && | |
137 | test_when_finished "remove_object $new" && | |
138 | git update-ref refs/heads/bogus "$new" && | |
139 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
2e770fe4 | 140 | test_must_fail git fsck 2>out && |
e3c98120 DI |
141 | cat out && |
142 | grep "error in commit $new" out | |
143 | ' | |
144 | ||
d4b8de04 JK |
145 | # date is 2^64 + 1 |
146 | test_expect_success 'integer overflow in timestamps is reported' ' | |
147 | git cat-file commit HEAD >basis && | |
148 | sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \ | |
149 | <basis >bad-timestamp && | |
150 | new=$(git hash-object -t commit -w --stdin <bad-timestamp) && | |
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" && | |
2e770fe4 | 154 | test_must_fail git fsck 2>out && |
d4b8de04 JK |
155 | cat out && |
156 | grep "error in commit $new.*integer overflow" out | |
157 | ' | |
158 | ||
2e770fe4 JK |
159 | test_expect_success 'malformatted tree object' ' |
160 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
161 | test_when_finished "remove_object \$T" && | |
162 | T=$( | |
163 | GIT_INDEX_FILE=test-index && | |
164 | export GIT_INDEX_FILE && | |
165 | rm -f test-index && | |
166 | >x && | |
167 | git add x && | |
168 | T=$(git write-tree) && | |
169 | ( | |
170 | git cat-file tree $T && | |
171 | git cat-file tree $T | |
172 | ) | | |
173 | git hash-object -w -t tree --stdin | |
174 | ) && | |
175 | test_must_fail git fsck 2>out && | |
176 | grep "error in tree .*contains duplicate file entries" out | |
177 | ' | |
178 | ||
4551d035 | 179 | test_expect_success 'tag pointing to nonexistent' ' |
a48fcd83 | 180 | cat >invalid-tag <<-\EOF && |
dbedf8bf JN |
181 | object ffffffffffffffffffffffffffffffffffffffff |
182 | type commit | |
183 | tag invalid | |
184 | tagger T A Gger <tagger@example.com> 1234567890 -0000 | |
185 | ||
186 | This is an invalid tag. | |
187 | EOF | |
188 | ||
189 | tag=$(git hash-object -t tag -w --stdin <invalid-tag) && | |
190 | test_when_finished "remove_object $tag" && | |
191 | echo $tag >.git/refs/tags/invalid && | |
192 | test_when_finished "git update-ref -d refs/tags/invalid" && | |
4551d035 | 193 | test_must_fail git fsck --tags >out && |
02a6552c | 194 | cat out && |
dbedf8bf | 195 | grep "broken link" out |
02a6552c TR |
196 | ' |
197 | ||
4551d035 | 198 | test_expect_success 'tag pointing to something else than its type' ' |
dbedf8bf JN |
199 | sha=$(echo blob | git hash-object -w --stdin) && |
200 | test_when_finished "remove_object $sha" && | |
201 | cat >wrong-tag <<-EOF && | |
202 | object $sha | |
203 | type commit | |
204 | tag wrong | |
205 | tagger T A Gger <tagger@example.com> 1234567890 -0000 | |
206 | ||
207 | This is an invalid tag. | |
208 | EOF | |
209 | ||
210 | tag=$(git hash-object -t tag -w --stdin <wrong-tag) && | |
211 | test_when_finished "remove_object $tag" && | |
212 | echo $tag >.git/refs/tags/wrong && | |
213 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
9dad83be | 214 | test_must_fail git fsck --tags |
02a6552c TR |
215 | ' |
216 | ||
90e3e5f0 JS |
217 | test_expect_success 'tag with incorrect tag name & missing tagger' ' |
218 | sha=$(git rev-parse HEAD) && | |
219 | cat >wrong-tag <<-EOF && | |
220 | object $sha | |
221 | type commit | |
222 | tag wrong name format | |
223 | ||
224 | This is an invalid tag. | |
225 | EOF | |
226 | ||
227 | tag=$(git hash-object -t tag -w --stdin <wrong-tag) && | |
228 | test_when_finished "remove_object $tag" && | |
229 | echo $tag >.git/refs/tags/wrong && | |
230 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
231 | git fsck --tags 2>out && | |
7add4419 JK |
232 | |
233 | cat >expect <<-EOF && | |
234 | warning in tag $tag: invalid '\''tag'\'' name: wrong name format | |
235 | warning in tag $tag: invalid format - expected '\''tagger'\'' line | |
236 | EOF | |
237 | test_cmp expect out | |
90e3e5f0 JS |
238 | ' |
239 | ||
b659605d JH |
240 | test_expect_success 'tag with bad tagger' ' |
241 | sha=$(git rev-parse HEAD) && | |
242 | cat >wrong-tag <<-EOF && | |
243 | object $sha | |
244 | type commit | |
245 | tag not-quite-wrong | |
246 | tagger Bad Tagger Name | |
247 | ||
248 | This is an invalid tag. | |
249 | EOF | |
250 | ||
251 | tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) && | |
252 | test_when_finished "remove_object $tag" && | |
253 | echo $tag >.git/refs/tags/wrong && | |
254 | test_when_finished "git update-ref -d refs/tags/wrong" && | |
255 | test_must_fail git fsck --tags 2>out && | |
256 | grep "error in tag .*: invalid author/committer" out | |
257 | ' | |
258 | ||
dbedf8bf JN |
259 | test_expect_success 'cleaned up' ' |
260 | git fsck >actual 2>&1 && | |
261 | test_cmp empty actual | |
262 | ' | |
02a6552c | 263 | |
cb8da705 CB |
264 | test_expect_success 'rev-list --verify-objects' ' |
265 | git rev-list --verify-objects --all >/dev/null 2>out && | |
266 | test_cmp empty out | |
267 | ' | |
268 | ||
269 | test_expect_success 'rev-list --verify-objects with bad sha1' ' | |
270 | sha=$(echo blob | git hash-object -w --stdin) && | |
271 | old=$(echo $sha | sed "s+^..+&/+") && | |
272 | new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff && | |
273 | sha="$(dirname $new)$(basename $new)" && | |
274 | mv .git/objects/$old .git/objects/$new && | |
275 | test_when_finished "remove_object $sha" && | |
276 | git update-index --add --cacheinfo 100644 $sha foo && | |
277 | test_when_finished "git read-tree -u --reset HEAD" && | |
278 | tree=$(git write-tree) && | |
279 | test_when_finished "remove_object $tree" && | |
280 | cmt=$(echo bogus | git commit-tree $tree) && | |
281 | test_when_finished "remove_object $cmt" && | |
282 | git update-ref refs/heads/bogus $cmt && | |
283 | test_when_finished "git update-ref -d refs/heads/bogus" && | |
284 | ||
285 | test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out && | |
286 | cat out && | |
287 | grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out | |
288 | ' | |
289 | ||
c479d14a JK |
290 | _bz='\0' |
291 | _bz5="$_bz$_bz$_bz$_bz$_bz" | |
292 | _bz20="$_bz5$_bz5$_bz5$_bz5" | |
293 | ||
294 | test_expect_success 'fsck notices blob entry pointing to null sha1' ' | |
295 | (git init null-blob && | |
296 | cd null-blob && | |
297 | sha=$(printf "100644 file$_bz$_bz20" | | |
298 | git hash-object -w --stdin -t tree) && | |
299 | git fsck 2>out && | |
300 | cat out && | |
301 | grep "warning.*null sha1" out | |
302 | ) | |
303 | ' | |
304 | ||
305 | test_expect_success 'fsck notices submodule entry pointing to null sha1' ' | |
306 | (git init null-commit && | |
307 | cd null-commit && | |
308 | sha=$(printf "160000 submodule$_bz$_bz20" | | |
309 | git hash-object -w --stdin -t tree) && | |
310 | git fsck 2>out && | |
311 | cat out && | |
312 | grep "warning.*null sha1" out | |
313 | ) | |
314 | ' | |
315 | ||
a18fcc9f | 316 | while read name path pretty; do |
450870cb | 317 | while read mode type; do |
a18fcc9f JK |
318 | : ${pretty:=$path} |
319 | test_expect_success "fsck notices $pretty as $type" ' | |
450870cb JK |
320 | ( |
321 | git init $name-$type && | |
322 | cd $name-$type && | |
323 | echo content >file && | |
324 | git add file && | |
325 | git commit -m base && | |
326 | blob=$(git rev-parse :file) && | |
327 | tree=$(git rev-parse HEAD^{tree}) && | |
328 | value=$(eval "echo \$$type") && | |
329 | printf "$mode $type %s\t%s" "$value" "$path" >bad && | |
d08c13b9 | 330 | bad_tree=$(git mktree <bad) && |
450870cb JK |
331 | git fsck 2>out && |
332 | cat out && | |
d08c13b9 | 333 | grep "warning.*tree $bad_tree" out |
450870cb JK |
334 | )' |
335 | done <<-\EOF | |
336 | 100644 blob | |
337 | 040000 tree | |
338 | EOF | |
a18fcc9f | 339 | done <<-EOF |
450870cb JK |
340 | dot . |
341 | dotdot .. | |
342 | dotgit .git | |
76e86fc6 | 343 | dotgit-case .GIT |
a18fcc9f | 344 | dotgit-unicode .gI${u200c}T .gI{u200c}T |
d08c13b9 JS |
345 | dotgit-case2 .Git |
346 | git-tilde1 git~1 | |
347 | dotgitdot .git. | |
348 | dot-backslash-case .\\\\.GIT\\\\foobar | |
349 | dotgit-case-backslash .git\\\\foobar | |
450870cb | 350 | EOF |
5c17f512 | 351 | |
6aaf956b JK |
352 | test_expect_success 'fsck allows .Ňit' ' |
353 | ( | |
354 | git init not-dotgit && | |
355 | cd not-dotgit && | |
356 | echo content >file && | |
357 | git add file && | |
358 | git commit -m base && | |
359 | blob=$(git rev-parse :file) && | |
360 | printf "100644 blob $blob\t.\\305\\207it" >tree && | |
361 | tree=$(git mktree <tree) && | |
362 | git fsck 2>err && | |
363 | test_line_count = 0 err | |
364 | ) | |
365 | ' | |
366 | ||
30d1038d JK |
367 | # create a static test repo which is broken by omitting |
368 | # one particular object ($1, which is looked up via rev-parse | |
369 | # in the new repository). | |
370 | create_repo_missing () { | |
371 | rm -rf missing && | |
372 | git init missing && | |
373 | ( | |
374 | cd missing && | |
375 | git commit -m one --allow-empty && | |
376 | mkdir subdir && | |
377 | echo content >subdir/file && | |
378 | git add subdir/file && | |
379 | git commit -m two && | |
380 | unrelated=$(echo unrelated | git hash-object --stdin -w) && | |
381 | git tag -m foo tag $unrelated && | |
382 | sha1=$(git rev-parse --verify "$1") && | |
383 | path=$(echo $sha1 | sed 's|..|&/|') && | |
384 | rm .git/objects/$path | |
385 | ) | |
386 | } | |
387 | ||
388 | test_expect_success 'fsck notices missing blob' ' | |
389 | create_repo_missing HEAD:subdir/file && | |
390 | test_must_fail git -C missing fsck | |
391 | ' | |
392 | ||
393 | test_expect_success 'fsck notices missing subtree' ' | |
394 | create_repo_missing HEAD:subdir && | |
395 | test_must_fail git -C missing fsck | |
396 | ' | |
397 | ||
398 | test_expect_success 'fsck notices missing root tree' ' | |
399 | create_repo_missing HEAD^{tree} && | |
400 | test_must_fail git -C missing fsck | |
401 | ' | |
402 | ||
403 | test_expect_success 'fsck notices missing parent' ' | |
404 | create_repo_missing HEAD^ && | |
405 | test_must_fail git -C missing fsck | |
406 | ' | |
407 | ||
408 | test_expect_success 'fsck notices missing tagged object' ' | |
409 | create_repo_missing tag^{blob} && | |
410 | test_must_fail git -C missing fsck | |
411 | ' | |
412 | ||
413 | test_expect_success 'fsck notices ref pointing to missing commit' ' | |
414 | create_repo_missing HEAD && | |
415 | test_must_fail git -C missing fsck | |
416 | ' | |
417 | ||
418 | test_expect_success 'fsck notices ref pointing to missing tag' ' | |
419 | create_repo_missing tag && | |
420 | test_must_fail git -C missing fsck | |
421 | ' | |
422 | ||
469e2ebf | 423 | test_done |