3 test_description='git fsck random collection of tests
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 &&
19 git reflog expire --expire=now --all &&
23 test_expect_success 'loose objects borrowed from alternate are not missing' '
28 echo ../../../.git/objects >.git/objects/info/alternates &&
29 test_commit C fileC one &&
30 git fsck --no-dangling >../actual 2>&1
35 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
40 # Corruption tests follow. Make sure to remove all traces of the
41 # specific corruption you test afterwards, lest a later test trip over
44 test_expect_success 'setup: helpers for corruption tests' '
46 echo "$*" | sed "s#..#.git/objects/&/#"
50 file=$(sha1_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 &&
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 test_must_fail git fsck 2>out &&
82 grep "not a commit" out
85 test_expect_success 'HEAD link pointing at a funny object' '
86 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
87 mv .git/HEAD .git/SAVED_HEAD &&
88 echo 0000000000000000000000000000000000000000 >.git/HEAD &&
89 # avoid corrupt/broken HEAD from interfering with repo discovery
90 test_must_fail env GIT_DIR=.git git fsck 2>out &&
92 grep "detached HEAD points" out
95 test_expect_success 'HEAD link pointing at a funny place' '
96 test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
97 mv .git/HEAD .git/SAVED_HEAD &&
98 echo "ref: refs/funny/place" >.git/HEAD &&
99 # avoid corrupt/broken HEAD from interfering with repo discovery
100 test_must_fail env GIT_DIR=.git git fsck 2>out &&
102 grep "HEAD points to something strange" out
105 test_expect_success 'email without @ is okay' '
106 git cat-file commit HEAD >basis &&
107 sed "s/@/AT/" basis >okay &&
108 new=$(git hash-object -t commit -w --stdin <okay) &&
109 test_when_finished "remove_object $new" &&
110 git update-ref refs/heads/bogus "$new" &&
111 test_when_finished "git update-ref -d refs/heads/bogus" &&
114 ! grep "commit $new" out
117 test_expect_success 'email with embedded > is not okay' '
118 git cat-file commit HEAD >basis &&
119 sed "s/@[a-z]/&>/" basis >bad-email &&
120 new=$(git hash-object -t commit -w --stdin <bad-email) &&
121 test_when_finished "remove_object $new" &&
122 git update-ref refs/heads/bogus "$new" &&
123 test_when_finished "git update-ref -d refs/heads/bogus" &&
124 test_must_fail git fsck 2>out &&
126 grep "error in commit $new" out
129 test_expect_success 'missing < email delimiter is reported nicely' '
130 git cat-file commit HEAD >basis &&
131 sed "s/<//" basis >bad-email-2 &&
132 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
133 test_when_finished "remove_object $new" &&
134 git update-ref refs/heads/bogus "$new" &&
135 test_when_finished "git update-ref -d refs/heads/bogus" &&
136 test_must_fail git fsck 2>out &&
138 grep "error in commit $new.* - bad name" out
141 test_expect_success 'missing email is reported nicely' '
142 git cat-file commit HEAD >basis &&
143 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
144 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
145 test_when_finished "remove_object $new" &&
146 git update-ref refs/heads/bogus "$new" &&
147 test_when_finished "git update-ref -d refs/heads/bogus" &&
148 test_must_fail git fsck 2>out &&
150 grep "error in commit $new.* - missing email" out
153 test_expect_success '> in name is reported' '
154 git cat-file commit HEAD >basis &&
155 sed "s/ </> </" basis >bad-email-4 &&
156 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
157 test_when_finished "remove_object $new" &&
158 git update-ref refs/heads/bogus "$new" &&
159 test_when_finished "git update-ref -d refs/heads/bogus" &&
160 test_must_fail git fsck 2>out &&
162 grep "error in commit $new" out
166 test_expect_success 'integer overflow in timestamps is reported' '
167 git cat-file commit HEAD >basis &&
168 sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
169 <basis >bad-timestamp &&
170 new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
171 test_when_finished "remove_object $new" &&
172 git update-ref refs/heads/bogus "$new" &&
173 test_when_finished "git update-ref -d refs/heads/bogus" &&
174 test_must_fail git fsck 2>out &&
176 grep "error in commit $new.*integer overflow" out
179 test_expect_success 'malformatted tree object' '
180 test_when_finished "git update-ref -d refs/tags/wrong" &&
181 test_when_finished "remove_object \$T" &&
183 GIT_INDEX_FILE=test-index &&
184 export GIT_INDEX_FILE &&
188 T=$(git write-tree) &&
190 git cat-file tree $T &&
193 git hash-object -w -t tree --stdin
195 test_must_fail git fsck 2>out &&
196 grep "error in tree .*contains duplicate file entries" out
199 test_expect_success 'tag pointing to nonexistent' '
200 cat >invalid-tag <<-\EOF &&
201 object ffffffffffffffffffffffffffffffffffffffff
204 tagger T A Gger <tagger@example.com> 1234567890 -0000
206 This is an invalid tag.
209 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
210 test_when_finished "remove_object $tag" &&
211 echo $tag >.git/refs/tags/invalid &&
212 test_when_finished "git update-ref -d refs/tags/invalid" &&
213 test_must_fail git fsck --tags >out &&
215 grep "broken link" out
218 test_expect_success 'tag pointing to something else than its type' '
219 sha=$(echo blob | git hash-object -w --stdin) &&
220 test_when_finished "remove_object $sha" &&
221 cat >wrong-tag <<-EOF &&
225 tagger T A Gger <tagger@example.com> 1234567890 -0000
227 This is an invalid tag.
230 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
231 test_when_finished "remove_object $tag" &&
232 echo $tag >.git/refs/tags/wrong &&
233 test_when_finished "git update-ref -d refs/tags/wrong" &&
234 test_must_fail git fsck --tags
237 test_expect_success 'tag with incorrect tag name & missing tagger' '
238 sha=$(git rev-parse HEAD) &&
239 cat >wrong-tag <<-EOF &&
242 tag wrong name format
244 This is an invalid tag.
247 tag=$(git hash-object -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 git fsck --tags 2>out &&
253 cat >expect <<-EOF &&
254 warning in tag $tag: invalid '\''tag'\'' name: wrong name format
255 warning in tag $tag: invalid format - expected '\''tagger'\'' line
260 test_expect_success 'tag with bad tagger' '
261 sha=$(git rev-parse HEAD) &&
262 cat >wrong-tag <<-EOF &&
266 tagger Bad Tagger Name
268 This is an invalid tag.
271 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
272 test_when_finished "remove_object $tag" &&
273 echo $tag >.git/refs/tags/wrong &&
274 test_when_finished "git update-ref -d refs/tags/wrong" &&
275 test_must_fail git fsck --tags 2>out &&
276 grep "error in tag .*: invalid author/committer" out
279 test_expect_success 'cleaned up' '
280 git fsck >actual 2>&1 &&
281 test_cmp empty actual
284 test_expect_success 'rev-list --verify-objects' '
285 git rev-list --verify-objects --all >/dev/null 2>out &&
289 test_expect_success 'rev-list --verify-objects with bad sha1' '
290 sha=$(echo blob | git hash-object -w --stdin) &&
291 old=$(echo $sha | sed "s+^..+&/+") &&
292 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
293 sha="$(dirname $new)$(basename $new)" &&
294 mv .git/objects/$old .git/objects/$new &&
295 test_when_finished "remove_object $sha" &&
296 git update-index --add --cacheinfo 100644 $sha foo &&
297 test_when_finished "git read-tree -u --reset HEAD" &&
298 tree=$(git write-tree) &&
299 test_when_finished "remove_object $tree" &&
300 cmt=$(echo bogus | git commit-tree $tree) &&
301 test_when_finished "remove_object $cmt" &&
302 git update-ref refs/heads/bogus $cmt &&
303 test_when_finished "git update-ref -d refs/heads/bogus" &&
305 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
307 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
311 _bz5="$_bz$_bz$_bz$_bz$_bz"
312 _bz20="$_bz5$_bz5$_bz5$_bz5"
314 test_expect_success 'fsck notices blob entry pointing to null sha1' '
315 (git init null-blob &&
317 sha=$(printf "100644 file$_bz$_bz20" |
318 git hash-object -w --stdin -t tree) &&
321 grep "warning.*null sha1" out
325 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
326 (git init null-commit &&
328 sha=$(printf "160000 submodule$_bz$_bz20" |
329 git hash-object -w --stdin -t tree) &&
332 grep "warning.*null sha1" out
336 while read name path pretty; do
337 while read mode type; do
339 test_expect_success "fsck notices $pretty as $type" '
341 git init $name-$type &&
343 echo content >file &&
345 git commit -m base &&
346 blob=$(git rev-parse :file) &&
347 tree=$(git rev-parse HEAD^{tree}) &&
348 value=$(eval "echo \$$type") &&
349 printf "$mode $type %s\t%s" "$value" "$path" >bad &&
350 bad_tree=$(git mktree <bad) &&
353 grep "warning.*tree $bad_tree" out
364 dotgit-unicode .gI${u200c}T .gI{u200c}T
368 dot-backslash-case .\\\\.GIT\\\\foobar
369 dotgit-case-backslash .git\\\\foobar
372 test_expect_success 'fsck allows .Ňit' '
374 git init not-dotgit &&
376 echo content >file &&
378 git commit -m base &&
379 blob=$(git rev-parse :file) &&
380 printf "100644 blob $blob\t.\\305\\207it" >tree &&
381 tree=$(git mktree <tree) &&
383 test_line_count = 0 err
387 # create a static test repo which is broken by omitting
388 # one particular object ($1, which is looked up via rev-parse
389 # in the new repository).
390 create_repo_missing () {
395 git commit -m one --allow-empty &&
397 echo content >subdir/file &&
398 git add subdir/file &&
400 unrelated=$(echo unrelated | git hash-object --stdin -w) &&
401 git tag -m foo tag $unrelated &&
402 sha1=$(git rev-parse --verify "$1") &&
403 path=$(echo $sha1 | sed 's|..|&/|') &&
404 rm .git/objects/$path
408 test_expect_success 'fsck notices missing blob' '
409 create_repo_missing HEAD:subdir/file &&
410 test_must_fail git -C missing fsck
413 test_expect_success 'fsck notices missing subtree' '
414 create_repo_missing HEAD:subdir &&
415 test_must_fail git -C missing fsck
418 test_expect_success 'fsck notices missing root tree' '
419 create_repo_missing HEAD^{tree} &&
420 test_must_fail git -C missing fsck
423 test_expect_success 'fsck notices missing parent' '
424 create_repo_missing HEAD^ &&
425 test_must_fail git -C missing fsck
428 test_expect_success 'fsck notices missing tagged object' '
429 create_repo_missing tag^{blob} &&
430 test_must_fail git -C missing fsck
433 test_expect_success 'fsck notices ref pointing to missing commit' '
434 create_repo_missing HEAD &&
435 test_must_fail git -C missing fsck
438 test_expect_success 'fsck notices ref pointing to missing tag' '
439 create_repo_missing tag &&
440 test_must_fail git -C missing fsck