filter_ref: avoid overwriting ref->old_sha1 with garbage
[git.git] / t / t1450-fsck.sh
blob793aee9f0bbd535ddef38249a088d102b15b6479
1 #!/bin/sh
3 test_description='git fsck random collection of tests
5 * (HEAD) B
6 * (master) A
9 . ./test-lib.sh
11 test_expect_success setup '
12 git config gc.auto 0 &&
13 git config i18n.commitencoding ISO-8859-1 &&
14 test_commit A fileA one &&
15 git config --unset i18n.commitencoding &&
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 git tag -d A B &&
19 git reflog expire --expire=now --all &&
20 >empty
23 test_expect_success 'loose objects borrowed from alternate are not missing' '
24 mkdir another &&
26 cd another &&
27 git init &&
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
30 git fsck --no-dangling >../actual 2>&1
31 ) &&
32 test_cmp empty actual
35 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
37 test_cmp empty actual
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.
44 test_expect_success 'setup: helpers for corruption tests' '
45 sha1_file() {
46 echo "$*" | sed "s#..#.git/objects/&/#"
47 } &&
49 remove_object() {
50 file=$(sha1_file "$*") &&
51 test -e "$file" &&
52 rm -f "$file"
56 test_expect_success 'object with bad sha1' '
57 sha=$(echo blob | git hash-object -w --stdin) &&
58 old=$(echo $sha | sed "s+^..+&/+") &&
59 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
60 sha="$(dirname $new)$(basename $new)" &&
61 mv .git/objects/$old .git/objects/$new &&
62 test_when_finished "remove_object $sha" &&
63 git update-index --add --cacheinfo 100644 $sha foo &&
64 test_when_finished "git read-tree -u --reset HEAD" &&
65 tree=$(git write-tree) &&
66 test_when_finished "remove_object $tree" &&
67 cmt=$(echo bogus | git commit-tree $tree) &&
68 test_when_finished "remove_object $cmt" &&
69 git update-ref refs/heads/bogus $cmt &&
70 test_when_finished "git update-ref -d refs/heads/bogus" &&
72 test_must_fail git fsck 2>out &&
73 cat out &&
74 grep "$sha.*corrupt" out
77 test_expect_success 'branch pointing to non-commit' '
78 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79 test_when_finished "git update-ref -d refs/heads/invalid" &&
80 git fsck 2>out &&
81 cat out &&
82 grep "not a commit" out
85 test_expect_success 'email without @ is okay' '
86 git cat-file commit HEAD >basis &&
87 sed "s/@/AT/" basis >okay &&
88 new=$(git hash-object -t commit -w --stdin <okay) &&
89 test_when_finished "remove_object $new" &&
90 git update-ref refs/heads/bogus "$new" &&
91 test_when_finished "git update-ref -d refs/heads/bogus" &&
92 git fsck 2>out &&
93 cat out &&
94 ! grep "commit $new" out
97 test_expect_success 'email with embedded > is not okay' '
98 git cat-file commit HEAD >basis &&
99 sed "s/@[a-z]/&>/" basis >bad-email &&
100 new=$(git hash-object -t commit -w --stdin <bad-email) &&
101 test_when_finished "remove_object $new" &&
102 git update-ref refs/heads/bogus "$new" &&
103 test_when_finished "git update-ref -d refs/heads/bogus" &&
104 test_must_fail git fsck 2>out &&
105 cat out &&
106 grep "error in commit $new" out
109 test_expect_success 'missing < email delimiter is reported nicely' '
110 git cat-file commit HEAD >basis &&
111 sed "s/<//" basis >bad-email-2 &&
112 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
113 test_when_finished "remove_object $new" &&
114 git update-ref refs/heads/bogus "$new" &&
115 test_when_finished "git update-ref -d refs/heads/bogus" &&
116 test_must_fail git fsck 2>out &&
117 cat out &&
118 grep "error in commit $new.* - bad name" out
121 test_expect_success 'missing email is reported nicely' '
122 git cat-file commit HEAD >basis &&
123 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
124 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
125 test_when_finished "remove_object $new" &&
126 git update-ref refs/heads/bogus "$new" &&
127 test_when_finished "git update-ref -d refs/heads/bogus" &&
128 test_must_fail git fsck 2>out &&
129 cat out &&
130 grep "error in commit $new.* - missing email" out
133 test_expect_success '> in name is reported' '
134 git cat-file commit HEAD >basis &&
135 sed "s/ </> </" basis >bad-email-4 &&
136 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
137 test_when_finished "remove_object $new" &&
138 git update-ref refs/heads/bogus "$new" &&
139 test_when_finished "git update-ref -d refs/heads/bogus" &&
140 test_must_fail git fsck 2>out &&
141 cat out &&
142 grep "error in commit $new" out
145 # date is 2^64 + 1
146 test_expect_success 'integer overflow in timestamps is reported' '
147 git cat-file commit HEAD >basis &&
148 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
149 <basis >bad-timestamp &&
150 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
151 test_when_finished "remove_object $new" &&
152 git update-ref refs/heads/bogus "$new" &&
153 test_when_finished "git update-ref -d refs/heads/bogus" &&
154 test_must_fail git fsck 2>out &&
155 cat out &&
156 grep "error in commit $new.*integer overflow" out
159 test_expect_success 'malformatted tree object' '
160 test_when_finished "git update-ref -d refs/tags/wrong" &&
161 test_when_finished "remove_object \$T" &&
162 T=$(
163 GIT_INDEX_FILE=test-index &&
164 export GIT_INDEX_FILE &&
165 rm -f test-index &&
166 >x &&
167 git add x &&
168 T=$(git write-tree) &&
170 git cat-file tree $T &&
171 git cat-file tree $T
173 git hash-object -w -t tree --stdin
174 ) &&
175 test_must_fail git fsck 2>out &&
176 grep "error in tree .*contains duplicate file entries" out
179 test_expect_success 'tag pointing to nonexistent' '
180 cat >invalid-tag <<-\EOF &&
181 object ffffffffffffffffffffffffffffffffffffffff
182 type commit
183 tag invalid
184 tagger T A Gger <tagger@example.com> 1234567890 -0000
186 This is an invalid tag.
189 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
190 test_when_finished "remove_object $tag" &&
191 echo $tag >.git/refs/tags/invalid &&
192 test_when_finished "git update-ref -d refs/tags/invalid" &&
193 test_must_fail git fsck --tags >out &&
194 cat out &&
195 grep "broken link" out
198 test_expect_success 'tag pointing to something else than its type' '
199 sha=$(echo blob | git hash-object -w --stdin) &&
200 test_when_finished "remove_object $sha" &&
201 cat >wrong-tag <<-EOF &&
202 object $sha
203 type commit
204 tag wrong
205 tagger T A Gger <tagger@example.com> 1234567890 -0000
207 This is an invalid tag.
210 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
211 test_when_finished "remove_object $tag" &&
212 echo $tag >.git/refs/tags/wrong &&
213 test_when_finished "git update-ref -d refs/tags/wrong" &&
214 test_must_fail git fsck --tags
217 test_expect_success 'tag with incorrect tag name & missing tagger' '
218 sha=$(git rev-parse HEAD) &&
219 cat >wrong-tag <<-EOF &&
220 object $sha
221 type commit
222 tag wrong name format
224 This is an invalid tag.
227 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
228 test_when_finished "remove_object $tag" &&
229 echo $tag >.git/refs/tags/wrong &&
230 test_when_finished "git update-ref -d refs/tags/wrong" &&
231 git fsck --tags 2>out &&
232 grep "invalid .tag. name" out &&
233 grep "expected .tagger. line" out
236 test_expect_success 'tag with bad tagger' '
237 sha=$(git rev-parse HEAD) &&
238 cat >wrong-tag <<-EOF &&
239 object $sha
240 type commit
241 tag not-quite-wrong
242 tagger Bad Tagger Name
244 This is an invalid tag.
247 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
248 test_when_finished "remove_object $tag" &&
249 echo $tag >.git/refs/tags/wrong &&
250 test_when_finished "git update-ref -d refs/tags/wrong" &&
251 test_must_fail git fsck --tags 2>out &&
252 grep "error in tag .*: invalid author/committer" out
255 test_expect_success 'cleaned up' '
256 git fsck >actual 2>&1 &&
257 test_cmp empty actual
260 test_expect_success 'rev-list --verify-objects' '
261 git rev-list --verify-objects --all >/dev/null 2>out &&
262 test_cmp empty out
265 test_expect_success 'rev-list --verify-objects with bad sha1' '
266 sha=$(echo blob | git hash-object -w --stdin) &&
267 old=$(echo $sha | sed "s+^..+&/+") &&
268 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
269 sha="$(dirname $new)$(basename $new)" &&
270 mv .git/objects/$old .git/objects/$new &&
271 test_when_finished "remove_object $sha" &&
272 git update-index --add --cacheinfo 100644 $sha foo &&
273 test_when_finished "git read-tree -u --reset HEAD" &&
274 tree=$(git write-tree) &&
275 test_when_finished "remove_object $tree" &&
276 cmt=$(echo bogus | git commit-tree $tree) &&
277 test_when_finished "remove_object $cmt" &&
278 git update-ref refs/heads/bogus $cmt &&
279 test_when_finished "git update-ref -d refs/heads/bogus" &&
281 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
282 cat out &&
283 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
286 _bz='\0'
287 _bz5="$_bz$_bz$_bz$_bz$_bz"
288 _bz20="$_bz5$_bz5$_bz5$_bz5"
290 test_expect_success 'fsck notices blob entry pointing to null sha1' '
291 (git init null-blob &&
292 cd null-blob &&
293 sha=$(printf "100644 file$_bz$_bz20" |
294 git hash-object -w --stdin -t tree) &&
295 git fsck 2>out &&
296 cat out &&
297 grep "warning.*null sha1" out
301 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
302 (git init null-commit &&
303 cd null-commit &&
304 sha=$(printf "160000 submodule$_bz$_bz20" |
305 git hash-object -w --stdin -t tree) &&
306 git fsck 2>out &&
307 cat out &&
308 grep "warning.*null sha1" out
312 while read name path pretty; do
313 while read mode type; do
314 : ${pretty:=$path}
315 test_expect_success "fsck notices $pretty as $type" '
317 git init $name-$type &&
318 cd $name-$type &&
319 echo content >file &&
320 git add file &&
321 git commit -m base &&
322 blob=$(git rev-parse :file) &&
323 tree=$(git rev-parse HEAD^{tree}) &&
324 value=$(eval "echo \$$type") &&
325 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
326 bad_tree=$(git mktree <bad) &&
327 git fsck 2>out &&
328 cat out &&
329 grep "warning.*tree $bad_tree" out
331 done <<-\EOF
332 100644 blob
333 040000 tree
335 done <<-EOF
336 dot .
337 dotdot ..
338 dotgit .git
339 dotgit-case .GIT
340 dotgit-unicode .gI${u200c}T .gI{u200c}T
341 dotgit-case2 .Git
342 git-tilde1 git~1
343 dotgitdot .git.
344 dot-backslash-case .\\\\.GIT\\\\foobar
345 dotgit-case-backslash .git\\\\foobar
348 test_expect_success 'fsck allows .Ňit' '
350 git init not-dotgit &&
351 cd not-dotgit &&
352 echo content >file &&
353 git add file &&
354 git commit -m base &&
355 blob=$(git rev-parse :file) &&
356 printf "100644 blob $blob\t.\\305\\207it" >tree &&
357 tree=$(git mktree <tree) &&
358 git fsck 2>err &&
359 test_line_count = 0 err
363 # create a static test repo which is broken by omitting
364 # one particular object ($1, which is looked up via rev-parse
365 # in the new repository).
366 create_repo_missing () {
367 rm -rf missing &&
368 git init missing &&
370 cd missing &&
371 git commit -m one --allow-empty &&
372 mkdir subdir &&
373 echo content >subdir/file &&
374 git add subdir/file &&
375 git commit -m two &&
376 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
377 git tag -m foo tag $unrelated &&
378 sha1=$(git rev-parse --verify "$1") &&
379 path=$(echo $sha1 | sed 's|..|&/|') &&
380 rm .git/objects/$path
384 test_expect_success 'fsck notices missing blob' '
385 create_repo_missing HEAD:subdir/file &&
386 test_must_fail git -C missing fsck
389 test_expect_success 'fsck notices missing subtree' '
390 create_repo_missing HEAD:subdir &&
391 test_must_fail git -C missing fsck
394 test_expect_success 'fsck notices missing root tree' '
395 create_repo_missing HEAD^{tree} &&
396 test_must_fail git -C missing fsck
399 test_expect_success 'fsck notices missing parent' '
400 create_repo_missing HEAD^ &&
401 test_must_fail git -C missing fsck
404 test_expect_success 'fsck notices missing tagged object' '
405 create_repo_missing tag^{blob} &&
406 test_must_fail git -C missing fsck
409 test_expect_success 'fsck notices ref pointing to missing commit' '
410 create_repo_missing HEAD &&
411 test_must_fail git -C missing fsck
414 test_expect_success 'fsck notices ref pointing to missing tag' '
415 create_repo_missing tag &&
416 test_must_fail git -C missing fsck
419 test_done