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