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