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