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 &&
33 grep -v dangling out >actual ||
39 test_expect_success 'HEAD is part of refs, valid objects appear valid' '
40 git fsck >actual 2>&1 &&
44 # Corruption tests follow. Make sure to remove all traces of the
45 # specific corruption you test afterwards, lest a later test trip over
48 test_expect_success 'setup: helpers for corruption tests' '
50 echo "$*" | sed "s#..#.git/objects/&/#"
54 file=$(sha1_file "$*") &&
60 test_expect_success 'object with bad sha1' '
61 sha=$(echo blob | git hash-object -w --stdin) &&
62 old=$(echo $sha | sed "s+^..+&/+") &&
63 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
64 sha="$(dirname $new)$(basename $new)" &&
65 mv .git/objects/$old .git/objects/$new &&
66 test_when_finished "remove_object $sha" &&
67 git update-index --add --cacheinfo 100644 $sha foo &&
68 test_when_finished "git read-tree -u --reset HEAD" &&
69 tree=$(git write-tree) &&
70 test_when_finished "remove_object $tree" &&
71 cmt=$(echo bogus | git commit-tree $tree) &&
72 test_when_finished "remove_object $cmt" &&
73 git update-ref refs/heads/bogus $cmt &&
74 test_when_finished "git update-ref -d refs/heads/bogus" &&
76 test_might_fail git fsck 2>out &&
78 grep "$sha.*corrupt" out
81 test_expect_success 'branch pointing to non-commit' '
82 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
83 test_when_finished "git update-ref -d refs/heads/invalid" &&
86 grep "not a commit" out
89 test_expect_success 'email without @ is okay' '
90 git cat-file commit HEAD >basis &&
91 sed "s/@/AT/" basis >okay &&
92 new=$(git hash-object -t commit -w --stdin <okay) &&
93 test_when_finished "remove_object $new" &&
94 git update-ref refs/heads/bogus "$new" &&
95 test_when_finished "git update-ref -d refs/heads/bogus" &&
98 ! grep "commit $new" out
101 test_expect_success 'email with embedded > is not okay' '
102 git cat-file commit HEAD >basis &&
103 sed "s/@[a-z]/&>/" basis >bad-email &&
104 new=$(git hash-object -t commit -w --stdin <bad-email) &&
105 test_when_finished "remove_object $new" &&
106 git update-ref refs/heads/bogus "$new" &&
107 test_when_finished "git update-ref -d refs/heads/bogus" &&
110 grep "error in commit $new" out
113 test_expect_success 'missing < email delimiter is reported nicely' '
114 git cat-file commit HEAD >basis &&
115 sed "s/<//" basis >bad-email-2 &&
116 new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
117 test_when_finished "remove_object $new" &&
118 git update-ref refs/heads/bogus "$new" &&
119 test_when_finished "git update-ref -d refs/heads/bogus" &&
122 grep "error in commit $new.* - bad name" out
125 test_expect_success 'missing email is reported nicely' '
126 git cat-file commit HEAD >basis &&
127 sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
128 new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
129 test_when_finished "remove_object $new" &&
130 git update-ref refs/heads/bogus "$new" &&
131 test_when_finished "git update-ref -d refs/heads/bogus" &&
134 grep "error in commit $new.* - missing email" out
137 test_expect_success '> in name is reported' '
138 git cat-file commit HEAD >basis &&
139 sed "s/ </> </" basis >bad-email-4 &&
140 new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
141 test_when_finished "remove_object $new" &&
142 git update-ref refs/heads/bogus "$new" &&
143 test_when_finished "git update-ref -d refs/heads/bogus" &&
146 grep "error in commit $new" out
149 test_expect_success 'tag pointing to nonexistent' '
150 cat >invalid-tag <<-\EOF &&
151 object ffffffffffffffffffffffffffffffffffffffff
154 tagger T A Gger <tagger@example.com> 1234567890 -0000
156 This is an invalid tag.
159 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
160 test_when_finished "remove_object $tag" &&
161 echo $tag >.git/refs/tags/invalid &&
162 test_when_finished "git update-ref -d refs/tags/invalid" &&
163 test_must_fail git fsck --tags >out &&
165 grep "broken link" out
168 test_expect_success 'tag pointing to something else than its type' '
169 sha=$(echo blob | git hash-object -w --stdin) &&
170 test_when_finished "remove_object $sha" &&
171 cat >wrong-tag <<-EOF &&
175 tagger T A Gger <tagger@example.com> 1234567890 -0000
177 This is an invalid tag.
180 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
181 test_when_finished "remove_object $tag" &&
182 echo $tag >.git/refs/tags/wrong &&
183 test_when_finished "git update-ref -d refs/tags/wrong" &&
184 test_must_fail git fsck --tags 2>out &&
186 grep "error in tag.*broken links" out
189 test_expect_success 'cleaned up' '
190 git fsck >actual 2>&1 &&
191 test_cmp empty actual
194 test_expect_success 'rev-list --verify-objects' '
195 git rev-list --verify-objects --all >/dev/null 2>out &&
199 test_expect_success 'rev-list --verify-objects with bad sha1' '
200 sha=$(echo blob | git hash-object -w --stdin) &&
201 old=$(echo $sha | sed "s+^..+&/+") &&
202 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
203 sha="$(dirname $new)$(basename $new)" &&
204 mv .git/objects/$old .git/objects/$new &&
205 test_when_finished "remove_object $sha" &&
206 git update-index --add --cacheinfo 100644 $sha foo &&
207 test_when_finished "git read-tree -u --reset HEAD" &&
208 tree=$(git write-tree) &&
209 test_when_finished "remove_object $tree" &&
210 cmt=$(echo bogus | git commit-tree $tree) &&
211 test_when_finished "remove_object $cmt" &&
212 git update-ref refs/heads/bogus $cmt &&
213 test_when_finished "git update-ref -d refs/heads/bogus" &&
215 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
217 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out