]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1450-fsck.sh
Make sure that index-pack --strict checks tag objects
[thirdparty/git.git] / t / t1450-fsck.sh
CommitLineData
469e2ebf
JH
1#!/bin/sh
2
dbedf8bf
JN
3test_description='git fsck random collection of tests
4
5* (HEAD) B
6* (master) A
7'
469e2ebf
JH
8
9. ./test-lib.sh
10
11test_expect_success setup '
dbedf8bf 12 git config gc.auto 0 &&
0adc6a3d 13 git config i18n.commitencoding ISO-8859-1 &&
469e2ebf 14 test_commit A fileA one &&
0adc6a3d 15 git config --unset i18n.commitencoding &&
469e2ebf
JH
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 git tag -d A B &&
dbedf8bf
JN
19 git reflog expire --expire=now --all &&
20 >empty
469e2ebf
JH
21'
22
e15ef669
JH
23test_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 ) &&
dbedf8bf 32 test_cmp empty actual
e15ef669
JH
33'
34
dbedf8bf
JN
35test_expect_success 'HEAD is part of refs, valid objects appear valid' '
36 git fsck >actual 2>&1 &&
37 test_cmp 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
44test_expect_success 'setup: helpers for corruption tests' '
45 sha1_file() {
46 echo "$*" | sed "s#..#.git/objects/&/#"
47 } &&
48
49 remove_object() {
50 file=$(sha1_file "$*") &&
51 test -e "$file" &&
52 rm -f "$file"
53 }
54'
55
02a6552c
TR
56test_expect_success 'object with bad sha1' '
57 sha=$(echo blob | git hash-object -w --stdin) &&
02a6552c
TR
58 old=$(echo $sha | sed "s+^..+&/+") &&
59 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
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
72 test_might_fail git fsck 2>out &&
73 cat out &&
74 grep "$sha.*corrupt" out
02a6552c
TR
75'
76
77test_expect_success 'branch pointing to non-commit' '
dbedf8bf
JN
78 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79 test_when_finished "git update-ref -d refs/heads/invalid" &&
02a6552c 80 git fsck 2>out &&
dbedf8bf
JN
81 cat out &&
82 grep "not a commit" out
02a6552c
TR
83'
84
daae1922
JN
85test_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) &&
dbedf8bf 89 test_when_finished "remove_object $new" &&
daae1922 90 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 91 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922
JN
92 git fsck 2>out &&
93 cat out &&
dbedf8bf 94 ! grep "commit $new" out
daae1922 95'
daae1922 96
daae1922
JN
97test_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) &&
dbedf8bf 101 test_when_finished "remove_object $new" &&
daae1922 102 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 103 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922
JN
104 git fsck 2>out &&
105 cat out &&
106 grep "error in commit $new" out
107'
02a6552c 108
53f53cff 109test_expect_success 'missing < email delimiter is reported nicely' '
e3c98120
DI
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 git fsck 2>out &&
117 cat out &&
118 grep "error in commit $new.* - bad name" out
119'
120
53f53cff 121test_expect_success 'missing email is reported nicely' '
e3c98120
DI
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 git fsck 2>out &&
129 cat out &&
130 grep "error in commit $new.* - missing email" out
131'
132
53f53cff 133test_expect_success '> in name is reported' '
e3c98120
DI
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 git fsck 2>out &&
141 cat out &&
142 grep "error in commit $new" out
143'
144
d4b8de04
JK
145# date is 2^64 + 1
146test_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 git fsck 2>out &&
155 cat out &&
156 grep "error in commit $new.*integer overflow" out
157'
158
4551d035 159test_expect_success 'tag pointing to nonexistent' '
a48fcd83 160 cat >invalid-tag <<-\EOF &&
dbedf8bf
JN
161 object ffffffffffffffffffffffffffffffffffffffff
162 type commit
163 tag invalid
164 tagger T A Gger <tagger@example.com> 1234567890 -0000
165
166 This is an invalid tag.
167 EOF
168
169 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
170 test_when_finished "remove_object $tag" &&
171 echo $tag >.git/refs/tags/invalid &&
172 test_when_finished "git update-ref -d refs/tags/invalid" &&
4551d035 173 test_must_fail git fsck --tags >out &&
02a6552c 174 cat out &&
dbedf8bf 175 grep "broken link" out
02a6552c
TR
176'
177
4551d035 178test_expect_success 'tag pointing to something else than its type' '
dbedf8bf
JN
179 sha=$(echo blob | git hash-object -w --stdin) &&
180 test_when_finished "remove_object $sha" &&
181 cat >wrong-tag <<-EOF &&
182 object $sha
183 type commit
184 tag wrong
185 tagger T A Gger <tagger@example.com> 1234567890 -0000
186
187 This is an invalid tag.
188 EOF
189
190 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
191 test_when_finished "remove_object $tag" &&
192 echo $tag >.git/refs/tags/wrong &&
193 test_when_finished "git update-ref -d refs/tags/wrong" &&
9dad83be 194 test_must_fail git fsck --tags
02a6552c
TR
195'
196
90e3e5f0
JS
197test_expect_success 'tag with incorrect tag name & missing tagger' '
198 sha=$(git rev-parse HEAD) &&
199 cat >wrong-tag <<-EOF &&
200 object $sha
201 type commit
202 tag wrong name format
203
204 This is an invalid tag.
205 EOF
206
207 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
208 test_when_finished "remove_object $tag" &&
209 echo $tag >.git/refs/tags/wrong &&
210 test_when_finished "git update-ref -d refs/tags/wrong" &&
211 git fsck --tags 2>out &&
212 grep "invalid .tag. name" out &&
213 grep "expected .tagger. line" out
214'
215
dbedf8bf
JN
216test_expect_success 'cleaned up' '
217 git fsck >actual 2>&1 &&
218 test_cmp empty actual
219'
02a6552c 220
cb8da705
CB
221test_expect_success 'rev-list --verify-objects' '
222 git rev-list --verify-objects --all >/dev/null 2>out &&
223 test_cmp empty out
224'
225
226test_expect_success 'rev-list --verify-objects with bad sha1' '
227 sha=$(echo blob | git hash-object -w --stdin) &&
228 old=$(echo $sha | sed "s+^..+&/+") &&
229 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
230 sha="$(dirname $new)$(basename $new)" &&
231 mv .git/objects/$old .git/objects/$new &&
232 test_when_finished "remove_object $sha" &&
233 git update-index --add --cacheinfo 100644 $sha foo &&
234 test_when_finished "git read-tree -u --reset HEAD" &&
235 tree=$(git write-tree) &&
236 test_when_finished "remove_object $tree" &&
237 cmt=$(echo bogus | git commit-tree $tree) &&
238 test_when_finished "remove_object $cmt" &&
239 git update-ref refs/heads/bogus $cmt &&
240 test_when_finished "git update-ref -d refs/heads/bogus" &&
241
242 test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
243 cat out &&
244 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
245'
246
c479d14a
JK
247_bz='\0'
248_bz5="$_bz$_bz$_bz$_bz$_bz"
249_bz20="$_bz5$_bz5$_bz5$_bz5"
250
251test_expect_success 'fsck notices blob entry pointing to null sha1' '
252 (git init null-blob &&
253 cd null-blob &&
254 sha=$(printf "100644 file$_bz$_bz20" |
255 git hash-object -w --stdin -t tree) &&
256 git fsck 2>out &&
257 cat out &&
258 grep "warning.*null sha1" out
259 )
260'
261
262test_expect_success 'fsck notices submodule entry pointing to null sha1' '
263 (git init null-commit &&
264 cd null-commit &&
265 sha=$(printf "160000 submodule$_bz$_bz20" |
266 git hash-object -w --stdin -t tree) &&
267 git fsck 2>out &&
268 cat out &&
269 grep "warning.*null sha1" out
270 )
271'
272
5d34a435
JK
273test_expect_success 'fsck notices "." and ".." in trees' '
274 (
275 git init dots &&
276 cd dots &&
277 blob=$(echo foo | git hash-object -w --stdin) &&
278 tab=$(printf "\\t") &&
279 git mktree <<-EOF &&
280 100644 blob $blob$tab.
281 100644 blob $blob$tab..
282 EOF
283 git fsck 2>out &&
284 cat out &&
285 grep "warning.*\\." out
286 )
287'
288
5c17f512
JK
289test_expect_success 'fsck notices ".git" in trees' '
290 (
291 git init dotgit &&
292 cd dotgit &&
293 blob=$(echo foo | git hash-object -w --stdin) &&
294 tab=$(printf "\\t") &&
295 git mktree <<-EOF &&
296 100644 blob $blob$tab.git
297 EOF
298 git fsck 2>out &&
299 cat out &&
300 grep "warning.*\\.git" out
301 )
302'
303
469e2ebf 304test_done