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_might_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" &&
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" &&
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" &&
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" &&
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" &&
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" &&
142 grep "error in commit $new" out
145 test_expect_success 'tag pointing to nonexistent' '
146 cat >invalid-tag <<-\EOF &&
147 object ffffffffffffffffffffffffffffffffffffffff
150 tagger T A Gger <tagger@example.com> 1234567890 -0000
152 This is an invalid tag.
155 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
156 test_when_finished "remove_object $tag" &&
157 echo $tag >.git/refs/tags/invalid &&
158 test_when_finished "git update-ref -d refs/tags/invalid" &&
159 test_must_fail git fsck --tags >out &&
161 grep "broken link" out
164 test_expect_success 'tag pointing to something else than its type' '
165 sha=$(echo blob | git hash-object -w --stdin) &&
166 test_when_finished "remove_object $sha" &&
167 cat >wrong-tag <<-EOF &&
171 tagger T A Gger <tagger@example.com> 1234567890 -0000
173 This is an invalid tag.
176 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
177 test_when_finished "remove_object $tag" &&
178 echo $tag >.git/refs/tags/wrong &&
179 test_when_finished "git update-ref -d refs/tags/wrong" &&
180 test_must_fail git fsck --tags
183 test_expect_success 'cleaned up' '
184 git fsck >actual 2>&1 &&
185 test_cmp empty actual
188 test_expect_success 'rev-list --verify-objects' '
189 git rev-list --verify-objects --all >/dev/null 2>out &&
193 test_expect_success 'rev-list --verify-objects with bad sha1' '
194 sha=$(echo blob | git hash-object -w --stdin) &&
195 old=$(echo $sha | sed "s+^..+&/+") &&
196 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
197 sha="$(dirname $new)$(basename $new)" &&
198 mv .git/objects/$old .git/objects/$new &&
199 test_when_finished "remove_object $sha" &&
200 git update-index --add --cacheinfo 100644 $sha foo &&
201 test_when_finished "git read-tree -u --reset HEAD" &&
202 tree=$(git write-tree) &&
203 test_when_finished "remove_object $tree" &&
204 cmt=$(echo bogus | git commit-tree $tree) &&
205 test_when_finished "remove_object $cmt" &&
206 git update-ref refs/heads/bogus $cmt &&
207 test_when_finished "git update-ref -d refs/heads/bogus" &&
209 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
211 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
215 _bz5="$_bz$_bz$_bz$_bz$_bz"
216 _bz20="$_bz5$_bz5$_bz5$_bz5"
218 test_expect_success 'fsck notices blob entry pointing to null sha1' '
219 (git init null-blob &&
221 sha=$(printf "100644 file$_bz$_bz20" |
222 git hash-object -w --stdin -t tree) &&
225 grep "warning.*null sha1" out
229 test_expect_success 'fsck notices submodule entry pointing to null sha1' '
230 (git init null-commit &&
232 sha=$(printf "160000 submodule$_bz$_bz20" |
233 git hash-object -w --stdin -t tree) &&
236 grep "warning.*null sha1" out