Merge branch 'fc/fetch-with-import-fix'
[git] / t / t5504-fetch-receive-strict.sh
1 #!/bin/sh
2
3 test_description='fetch/receive strict mode'
4 . ./test-lib.sh
5
6 test_expect_success 'setup and inject "corrupt or missing" object' '
7         echo hello >greetings &&
8         git add greetings &&
9         git commit -m greetings &&
10
11         S=$(git rev-parse :greetings | sed -e "s|^..|&/|") &&
12         X=$(echo bye | git hash-object -w --stdin | sed -e "s|^..|&/|") &&
13         echo $S >S &&
14         echo $X >X &&
15         cp .git/objects/$S .git/objects/$S.back &&
16         mv -f .git/objects/$X .git/objects/$S &&
17
18         test_must_fail git fsck
19 '
20
21 test_expect_success 'fetch without strict' '
22         rm -rf dst &&
23         git init dst &&
24         (
25                 cd dst &&
26                 git config fetch.fsckobjects false &&
27                 git config transfer.fsckobjects false &&
28                 test_must_fail git fetch ../.git master
29         )
30 '
31
32 test_expect_success 'fetch with !fetch.fsckobjects' '
33         rm -rf dst &&
34         git init dst &&
35         (
36                 cd dst &&
37                 git config fetch.fsckobjects false &&
38                 git config transfer.fsckobjects true &&
39                 test_must_fail git fetch ../.git master
40         )
41 '
42
43 test_expect_success 'fetch with fetch.fsckobjects' '
44         rm -rf dst &&
45         git init dst &&
46         (
47                 cd dst &&
48                 git config fetch.fsckobjects true &&
49                 git config transfer.fsckobjects false &&
50                 test_must_fail git fetch ../.git master
51         )
52 '
53
54 test_expect_success 'fetch with transfer.fsckobjects' '
55         rm -rf dst &&
56         git init dst &&
57         (
58                 cd dst &&
59                 git config transfer.fsckobjects true &&
60                 test_must_fail git fetch ../.git master
61         )
62 '
63
64 cat >exp <<EOF
65 To dst
66 !       refs/heads/master:refs/heads/test       [remote rejected] (missing necessary objects)
67 EOF
68
69 test_expect_success 'push without strict' '
70         rm -rf dst &&
71         git init dst &&
72         (
73                 cd dst &&
74                 git config fetch.fsckobjects false &&
75                 git config transfer.fsckobjects false
76         ) &&
77         test_must_fail git push --porcelain dst master:refs/heads/test >act &&
78         test_cmp exp act
79 '
80
81 test_expect_success 'push with !receive.fsckobjects' '
82         rm -rf dst &&
83         git init dst &&
84         (
85                 cd dst &&
86                 git config receive.fsckobjects false &&
87                 git config transfer.fsckobjects true
88         ) &&
89         test_must_fail git push --porcelain dst master:refs/heads/test >act &&
90         test_cmp exp act
91 '
92
93 cat >exp <<EOF
94 To dst
95 !       refs/heads/master:refs/heads/test       [remote rejected] (unpacker error)
96 EOF
97
98 test_expect_success 'push with receive.fsckobjects' '
99         rm -rf dst &&
100         git init dst &&
101         (
102                 cd dst &&
103                 git config receive.fsckobjects true &&
104                 git config transfer.fsckobjects false
105         ) &&
106         test_must_fail git push --porcelain dst master:refs/heads/test >act &&
107         test_cmp exp act
108 '
109
110 test_expect_success 'push with transfer.fsckobjects' '
111         rm -rf dst &&
112         git init dst &&
113         (
114                 cd dst &&
115                 git config transfer.fsckobjects true
116         ) &&
117         test_must_fail git push --porcelain dst master:refs/heads/test >act &&
118         test_cmp exp act
119 '
120
121 test_expect_success 'repair the "corrupt or missing" object' '
122         mv -f .git/objects/$(cat S) .git/objects/$(cat X) &&
123         mv .git/objects/$(cat S).back .git/objects/$(cat S) &&
124         rm -rf .git/objects/$(cat X) &&
125         git fsck
126 '
127
128 cat >bogus-commit <<EOF
129 tree $EMPTY_TREE
130 author Bugs Bunny 1234567890 +0000
131 committer Bugs Bunny <bugs@bun.ni> 1234567890 +0000
132
133 This commit object intentionally broken
134 EOF
135
136 test_expect_success 'setup bogus commit' '
137         commit="$(git hash-object -t commit -w --stdin <bogus-commit)"
138 '
139
140 test_expect_success 'fsck with no skipList input' '
141         test_must_fail git fsck 2>err &&
142         test_i18ngrep "missingEmail" err
143 '
144
145 test_expect_success 'setup sorted and unsorted skipLists' '
146         cat >SKIP.unsorted <<-EOF &&
147         0000000000000000000000000000000000000004
148         0000000000000000000000000000000000000002
149         $commit
150         0000000000000000000000000000000000000001
151         0000000000000000000000000000000000000003
152         EOF
153         sort SKIP.unsorted >SKIP.sorted
154 '
155
156 test_expect_success 'fsck with sorted skipList' '
157         git -c fsck.skipList=SKIP.sorted fsck
158 '
159
160 test_expect_success 'fsck with unsorted skipList' '
161         git -c fsck.skipList=SKIP.unsorted fsck
162 '
163
164 test_expect_success 'fsck with invalid or bogus skipList input' '
165         git -c fsck.skipList=/dev/null -c fsck.missingEmail=ignore fsck &&
166         test_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&
167         test_i18ngrep "Could not open skip list: does-not-exist" err &&
168         test_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&
169         test_i18ngrep "Invalid SHA-1: \[core\]" err
170 '
171
172 test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '
173         cat >SKIP.with-comment <<-EOF &&
174         # Some bad commit
175         0000000000000000000000000000000000000001
176         EOF
177         test_must_fail git -c fsck.skipList=SKIP.with-comment fsck 2>err-with-comment &&
178         test_i18ngrep "missingEmail" err-with-comment &&
179         cat >SKIP.with-empty-line <<-EOF &&
180         0000000000000000000000000000000000000001
181
182         0000000000000000000000000000000000000002
183         EOF
184         test_must_fail git -c fsck.skipList=SKIP.with-empty-line fsck 2>err-with-empty-line &&
185         test_i18ngrep "missingEmail" err-with-empty-line
186 '
187
188 test_expect_success 'fsck no garbage output from comments & empty lines errors' '
189         test_line_count = 1 err-with-comment &&
190         test_line_count = 1 err-with-empty-line
191 '
192
193 test_expect_success 'fsck with invalid abbreviated skipList input' '
194         echo $commit | test_copy_bytes 20 >SKIP.abbreviated &&
195         test_must_fail git -c fsck.skipList=SKIP.abbreviated fsck 2>err-abbreviated &&
196         test_i18ngrep "^fatal: Invalid SHA-1: " err-abbreviated
197 '
198
199 test_expect_success 'fsck with exhaustive accepted skipList input (various types of comments etc.)' '
200         >SKIP.exhaustive &&
201         echo "# A commented line" >>SKIP.exhaustive &&
202         echo "" >>SKIP.exhaustive &&
203         echo " " >>SKIP.exhaustive &&
204         echo " # Comment after whitespace" >>SKIP.exhaustive &&
205         echo "$commit # Our bad commit (with leading whitespace and trailing comment)" >>SKIP.exhaustive &&
206         echo "# Some bad commit (leading whitespace)" >>SKIP.exhaustive &&
207         echo "  0000000000000000000000000000000000000001" >>SKIP.exhaustive &&
208         git -c fsck.skipList=SKIP.exhaustive fsck 2>err &&
209         test_must_be_empty err
210 '
211
212 test_expect_success 'push with receive.fsck.skipList' '
213         git push . $commit:refs/heads/bogus &&
214         rm -rf dst &&
215         git init dst &&
216         git --git-dir=dst/.git config receive.fsckObjects true &&
217         test_must_fail git push --porcelain dst bogus &&
218         echo $commit >dst/.git/SKIP &&
219
220         # receive.fsck.* does not fall back on fsck.*
221         git --git-dir=dst/.git config fsck.skipList SKIP &&
222         test_must_fail git push --porcelain dst bogus &&
223
224         # Invalid and/or bogus skipList input
225         git --git-dir=dst/.git config receive.fsck.skipList /dev/null &&
226         test_must_fail git push --porcelain dst bogus &&
227         git --git-dir=dst/.git config receive.fsck.skipList does-not-exist &&
228         test_must_fail git push --porcelain dst bogus 2>err &&
229         test_i18ngrep "Could not open skip list: does-not-exist" err &&
230         git --git-dir=dst/.git config receive.fsck.skipList config &&
231         test_must_fail git push --porcelain dst bogus 2>err &&
232         test_i18ngrep "Invalid SHA-1: \[core\]" err &&
233
234         git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
235         git push --porcelain dst bogus
236 '
237
238 test_expect_success 'fetch with fetch.fsck.skipList' '
239         refspec=refs/heads/bogus:refs/heads/bogus &&
240         git push . $commit:refs/heads/bogus &&
241         rm -rf dst &&
242         git init dst &&
243         git --git-dir=dst/.git config fetch.fsckObjects true &&
244         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
245         git --git-dir=dst/.git config fetch.fsck.skipList /dev/null &&
246         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
247         echo $commit >dst/.git/SKIP &&
248
249         # fetch.fsck.* does not fall back on fsck.*
250         git --git-dir=dst/.git config fsck.skipList dst/.git/SKIP &&
251         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
252
253         # Invalid and/or bogus skipList input
254         git --git-dir=dst/.git config fetch.fsck.skipList /dev/null &&
255         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
256         git --git-dir=dst/.git config fetch.fsck.skipList does-not-exist &&
257         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
258         test_i18ngrep "Could not open skip list: does-not-exist" err &&
259         git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&
260         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
261         test_i18ngrep "Invalid SHA-1: \[core\]" err &&
262
263         git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&
264         git --git-dir=dst/.git fetch "file://$(pwd)" $refspec
265 '
266
267 test_expect_success 'fsck.<unknownmsg-id> dies' '
268         test_must_fail git -c fsck.whatEver=ignore fsck 2>err &&
269         test_i18ngrep "Unhandled message id: whatever" err
270 '
271
272 test_expect_success 'push with receive.fsck.missingEmail=warn' '
273         git push . $commit:refs/heads/bogus &&
274         rm -rf dst &&
275         git init dst &&
276         git --git-dir=dst/.git config receive.fsckobjects true &&
277         test_must_fail git push --porcelain dst bogus &&
278
279         # receive.fsck.<msg-id> does not fall back on fsck.<msg-id>
280         git --git-dir=dst/.git config fsck.missingEmail warn &&
281         test_must_fail git push --porcelain dst bogus &&
282
283         # receive.fsck.<unknownmsg-id> warns
284         git --git-dir=dst/.git config \
285                 receive.fsck.whatEver error &&
286
287         git --git-dir=dst/.git config \
288                 receive.fsck.missingEmail warn &&
289         git push --porcelain dst bogus >act 2>&1 &&
290         grep "missingEmail" act &&
291         test_i18ngrep "Skipping unknown msg id.*whatever" act &&
292         git --git-dir=dst/.git branch -D bogus &&
293         git --git-dir=dst/.git config --add \
294                 receive.fsck.missingEmail ignore &&
295         git push --porcelain dst bogus >act 2>&1 &&
296         ! grep "missingEmail" act
297 '
298
299 test_expect_success 'fetch with fetch.fsck.missingEmail=warn' '
300         refspec=refs/heads/bogus:refs/heads/bogus &&
301         git push . $commit:refs/heads/bogus &&
302         rm -rf dst &&
303         git init dst &&
304         git --git-dir=dst/.git config fetch.fsckobjects true &&
305         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
306
307         # fetch.fsck.<msg-id> does not fall back on fsck.<msg-id>
308         git --git-dir=dst/.git config fsck.missingEmail warn &&
309         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
310
311         # receive.fsck.<unknownmsg-id> warns
312         git --git-dir=dst/.git config \
313                 fetch.fsck.whatEver error &&
314
315         git --git-dir=dst/.git config \
316                 fetch.fsck.missingEmail warn &&
317         git --git-dir=dst/.git fetch "file://$(pwd)" $refspec >act 2>&1 &&
318         grep "missingEmail" act &&
319         test_i18ngrep "Skipping unknown msg id.*whatever" act &&
320         rm -rf dst &&
321         git init dst &&
322         git --git-dir=dst/.git config fetch.fsckobjects true &&
323         git --git-dir=dst/.git config \
324                 fetch.fsck.missingEmail ignore &&
325         git --git-dir=dst/.git fetch "file://$(pwd)" $refspec >act 2>&1 &&
326         ! grep "missingEmail" act
327 '
328
329 test_expect_success \
330         'receive.fsck.unterminatedHeader=warn triggers error' '
331         rm -rf dst &&
332         git init dst &&
333         git --git-dir=dst/.git config receive.fsckobjects true &&
334         git --git-dir=dst/.git config \
335                 receive.fsck.unterminatedheader warn &&
336         test_must_fail git push --porcelain dst HEAD >act 2>&1 &&
337         grep "Cannot demote unterminatedheader" act
338 '
339
340 test_expect_success \
341         'fetch.fsck.unterminatedHeader=warn triggers error' '
342         rm -rf dst &&
343         git init dst &&
344         git --git-dir=dst/.git config fetch.fsckobjects true &&
345         git --git-dir=dst/.git config \
346                 fetch.fsck.unterminatedheader warn &&
347         test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" HEAD &&
348         grep "Cannot demote unterminatedheader" act
349 '
350
351 test_done