Merge branch 'sb/describe-long'
[git] / t / t5516-fetch-push.sh
1 #!/bin/sh
2
3 test_description='fetching and pushing, with or without wildcard'
4
5 . ./test-lib.sh
6
7 D=`pwd`
8
9 mk_empty () {
10         rm -fr testrepo &&
11         mkdir testrepo &&
12         (
13                 cd testrepo &&
14                 git init &&
15                 mv .git/hooks .git/hooks-disabled
16         )
17 }
18
19 mk_test () {
20         mk_empty &&
21         (
22                 for ref in "$@"
23                 do
24                         git push testrepo $the_first_commit:refs/$ref || {
25                                 echo "Oops, push refs/$ref failure"
26                                 exit 1
27                         }
28                 done &&
29                 cd testrepo &&
30                 for ref in "$@"
31                 do
32                         r=$(git show-ref -s --verify refs/$ref) &&
33                         test "z$r" = "z$the_first_commit" || {
34                                 echo "Oops, refs/$ref is wrong"
35                                 exit 1
36                         }
37                 done &&
38                 git fsck --full
39         )
40 }
41
42 check_push_result () {
43         (
44                 cd testrepo &&
45                 it="$1" &&
46                 shift
47                 for ref in "$@"
48                 do
49                         r=$(git show-ref -s --verify refs/$ref) &&
50                         test "z$r" = "z$it" || {
51                                 echo "Oops, refs/$ref is wrong"
52                                 exit 1
53                         }
54                 done &&
55                 git fsck --full
56         )
57 }
58
59 test_expect_success setup '
60
61         : >path1 &&
62         git add path1 &&
63         test_tick &&
64         git commit -a -m repo &&
65         the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
66
67         : >path2 &&
68         git add path2 &&
69         test_tick &&
70         git commit -a -m second &&
71         the_commit=$(git show-ref -s --verify refs/heads/master)
72
73 '
74
75 test_expect_success 'fetch without wildcard' '
76         mk_empty &&
77         (
78                 cd testrepo &&
79                 git fetch .. refs/heads/master:refs/remotes/origin/master &&
80
81                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
82                 test "z$r" = "z$the_commit" &&
83
84                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
85         )
86 '
87
88 test_expect_success 'fetch with wildcard' '
89         mk_empty &&
90         (
91                 cd testrepo &&
92                 git config remote.up.url .. &&
93                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
94                 git fetch up &&
95
96                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
97                 test "z$r" = "z$the_commit" &&
98
99                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
100         )
101 '
102
103 test_expect_success 'fetch with insteadOf' '
104         mk_empty &&
105         (
106                 TRASH=$(pwd) &&
107                 cd testrepo &&
108                 git config url./$TRASH/.insteadOf trash/
109                 git config remote.up.url trash/. &&
110                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
111                 git fetch up &&
112
113                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
114                 test "z$r" = "z$the_commit" &&
115
116                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
117         )
118 '
119
120 test_expect_success 'push without wildcard' '
121         mk_empty &&
122
123         git push testrepo refs/heads/master:refs/remotes/origin/master &&
124         (
125                 cd testrepo &&
126                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
127                 test "z$r" = "z$the_commit" &&
128
129                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
130         )
131 '
132
133 test_expect_success 'push with wildcard' '
134         mk_empty &&
135
136         git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
137         (
138                 cd testrepo &&
139                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
140                 test "z$r" = "z$the_commit" &&
141
142                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
143         )
144 '
145
146 test_expect_success 'push with insteadOf' '
147         mk_empty &&
148         TRASH=$(pwd) &&
149         git config url./$TRASH/.insteadOf trash/ &&
150         git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
151         (
152                 cd testrepo &&
153                 r=$(git show-ref -s --verify refs/remotes/origin/master) &&
154                 test "z$r" = "z$the_commit" &&
155
156                 test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
157         )
158 '
159
160 test_expect_success 'push with matching heads' '
161
162         mk_test heads/master &&
163         git push testrepo &&
164         check_push_result $the_commit heads/master
165
166 '
167
168 test_expect_success 'push with no ambiguity (1)' '
169
170         mk_test heads/master &&
171         git push testrepo master:master &&
172         check_push_result $the_commit heads/master
173
174 '
175
176 test_expect_success 'push with no ambiguity (2)' '
177
178         mk_test remotes/origin/master &&
179         git push testrepo master:origin/master &&
180         check_push_result $the_commit remotes/origin/master
181
182 '
183
184 test_expect_success 'push with colon-less refspec, no ambiguity' '
185
186         mk_test heads/master heads/t/master &&
187         git branch -f t/master master &&
188         git push testrepo master &&
189         check_push_result $the_commit heads/master &&
190         check_push_result $the_first_commit heads/t/master
191
192 '
193
194 test_expect_success 'push with weak ambiguity (1)' '
195
196         mk_test heads/master remotes/origin/master &&
197         git push testrepo master:master &&
198         check_push_result $the_commit heads/master &&
199         check_push_result $the_first_commit remotes/origin/master
200
201 '
202
203 test_expect_success 'push with weak ambiguity (2)' '
204
205         mk_test heads/master remotes/origin/master remotes/another/master &&
206         git push testrepo master:master &&
207         check_push_result $the_commit heads/master &&
208         check_push_result $the_first_commit remotes/origin/master remotes/another/master
209
210 '
211
212 test_expect_success 'push with ambiguity (1)' '
213
214         mk_test remotes/origin/master remotes/frotz/master &&
215         if git push testrepo master:master
216         then
217                 echo "Oops, should have failed"
218                 false
219         else
220                 check_push_result $the_first_commit remotes/origin/master remotes/frotz/master
221         fi
222 '
223
224 test_expect_success 'push with ambiguity (2)' '
225
226         mk_test heads/frotz tags/frotz &&
227         if git push testrepo master:frotz
228         then
229                 echo "Oops, should have failed"
230                 false
231         else
232                 check_push_result $the_first_commit heads/frotz tags/frotz
233         fi
234
235 '
236
237 test_expect_success 'push with colon-less refspec (1)' '
238
239         mk_test heads/frotz tags/frotz &&
240         git branch -f frotz master &&
241         git push testrepo frotz &&
242         check_push_result $the_commit heads/frotz &&
243         check_push_result $the_first_commit tags/frotz
244
245 '
246
247 test_expect_success 'push with colon-less refspec (2)' '
248
249         mk_test heads/frotz tags/frotz &&
250         if git show-ref --verify -q refs/heads/frotz
251         then
252                 git branch -D frotz
253         fi &&
254         git tag -f frotz &&
255         git push testrepo frotz &&
256         check_push_result $the_commit tags/frotz &&
257         check_push_result $the_first_commit heads/frotz
258
259 '
260
261 test_expect_success 'push with colon-less refspec (3)' '
262
263         mk_test &&
264         if git show-ref --verify -q refs/tags/frotz
265         then
266                 git tag -d frotz
267         fi &&
268         git branch -f frotz master &&
269         git push testrepo frotz &&
270         check_push_result $the_commit heads/frotz &&
271         test 1 = $( cd testrepo && git show-ref | wc -l )
272 '
273
274 test_expect_success 'push with colon-less refspec (4)' '
275
276         mk_test &&
277         if git show-ref --verify -q refs/heads/frotz
278         then
279                 git branch -D frotz
280         fi &&
281         git tag -f frotz &&
282         git push testrepo frotz &&
283         check_push_result $the_commit tags/frotz &&
284         test 1 = $( cd testrepo && git show-ref | wc -l )
285
286 '
287
288 test_expect_success 'push with HEAD' '
289
290         mk_test heads/master &&
291         git checkout master &&
292         git push testrepo HEAD &&
293         check_push_result $the_commit heads/master
294
295 '
296
297 test_expect_success 'push with HEAD nonexisting at remote' '
298
299         mk_test heads/master &&
300         git checkout -b local master &&
301         git push testrepo HEAD &&
302         check_push_result $the_commit heads/local
303 '
304
305 test_expect_success 'push with +HEAD' '
306
307         mk_test heads/master &&
308         git checkout master &&
309         git branch -D local &&
310         git checkout -b local &&
311         git push testrepo master local &&
312         check_push_result $the_commit heads/master &&
313         check_push_result $the_commit heads/local &&
314
315         # Without force rewinding should fail
316         git reset --hard HEAD^ &&
317         ! git push testrepo HEAD &&
318         check_push_result $the_commit heads/local &&
319
320         # With force rewinding should succeed
321         git push testrepo +HEAD &&
322         check_push_result $the_first_commit heads/local
323
324 '
325
326 test_expect_success 'push with config remote.*.push = HEAD' '
327
328         mk_test heads/local &&
329         git checkout master &&
330         git branch -f local $the_commit &&
331         (
332                 cd testrepo &&
333                 git checkout local &&
334                 git reset --hard $the_first_commit
335         ) &&
336         git config remote.there.url testrepo &&
337         git config remote.there.push HEAD &&
338         git config branch.master.remote there &&
339         git push &&
340         check_push_result $the_commit heads/master &&
341         check_push_result $the_first_commit heads/local
342 '
343
344 # clean up the cruft left with the previous one
345 git config --remove-section remote.there
346 git config --remove-section branch.master
347
348 test_expect_success 'push with dry-run' '
349
350         mk_test heads/master &&
351         (cd testrepo &&
352          old_commit=$(git show-ref -s --verify refs/heads/master)) &&
353         git push --dry-run testrepo &&
354         check_push_result $old_commit heads/master
355 '
356
357 test_expect_success 'push updates local refs' '
358
359         rm -rf parent child &&
360         mkdir parent &&
361         (cd parent && git init &&
362                 echo one >foo && git add foo && git commit -m one) &&
363         git clone parent child &&
364         (cd child &&
365                 echo two >foo && git commit -a -m two &&
366                 git push &&
367         test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
368
369 '
370
371 test_expect_success 'push does not update local refs on failure' '
372
373         rm -rf parent child &&
374         mkdir parent &&
375         (cd parent && git init &&
376                 echo one >foo && git add foo && git commit -m one &&
377                 echo exit 1 >.git/hooks/pre-receive &&
378                 chmod +x .git/hooks/pre-receive) &&
379         git clone parent child &&
380         (cd child &&
381                 echo two >foo && git commit -a -m two &&
382                 ! git push &&
383                 test $(git rev-parse master) != \
384                         $(git rev-parse remotes/origin/master))
385
386 '
387
388 test_expect_success 'allow deleting an invalid remote ref' '
389
390         pwd &&
391         rm -f testrepo/.git/objects/??/* &&
392         git push testrepo :refs/heads/master &&
393         (cd testrepo && ! git rev-parse --verify refs/heads/master)
394
395 '
396
397 test_done