t5510: harden the way verify-pack is used
[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 matching heads on the command line' '
169
170         mk_test heads/master &&
171         git push testrepo : &&
172         check_push_result $the_commit heads/master
173
174 '
175
176 test_expect_success 'failed (non-fast-forward) push with matching heads' '
177
178         mk_test heads/master &&
179         git push testrepo : &&
180         git commit --amend -massaged &&
181         test_must_fail git push testrepo &&
182         check_push_result $the_commit heads/master &&
183         git reset --hard $the_commit
184
185 '
186
187 test_expect_success 'push --force with matching heads' '
188
189         mk_test heads/master &&
190         git push testrepo : &&
191         git commit --amend -massaged &&
192         git push --force testrepo &&
193         ! check_push_result $the_commit heads/master &&
194         git reset --hard $the_commit
195
196 '
197
198 test_expect_success 'push with matching heads and forced update' '
199
200         mk_test heads/master &&
201         git push testrepo : &&
202         git commit --amend -massaged &&
203         git push testrepo +: &&
204         ! check_push_result $the_commit heads/master &&
205         git reset --hard $the_commit
206
207 '
208
209 test_expect_success 'push with no ambiguity (1)' '
210
211         mk_test heads/master &&
212         git push testrepo master:master &&
213         check_push_result $the_commit heads/master
214
215 '
216
217 test_expect_success 'push with no ambiguity (2)' '
218
219         mk_test remotes/origin/master &&
220         git push testrepo master:origin/master &&
221         check_push_result $the_commit remotes/origin/master
222
223 '
224
225 test_expect_success 'push with colon-less refspec, no ambiguity' '
226
227         mk_test heads/master heads/t/master &&
228         git branch -f t/master master &&
229         git push testrepo master &&
230         check_push_result $the_commit heads/master &&
231         check_push_result $the_first_commit heads/t/master
232
233 '
234
235 test_expect_success 'push with weak ambiguity (1)' '
236
237         mk_test heads/master remotes/origin/master &&
238         git push testrepo master:master &&
239         check_push_result $the_commit heads/master &&
240         check_push_result $the_first_commit remotes/origin/master
241
242 '
243
244 test_expect_success 'push with weak ambiguity (2)' '
245
246         mk_test heads/master remotes/origin/master remotes/another/master &&
247         git push testrepo master:master &&
248         check_push_result $the_commit heads/master &&
249         check_push_result $the_first_commit remotes/origin/master remotes/another/master
250
251 '
252
253 test_expect_success 'push with ambiguity' '
254
255         mk_test heads/frotz tags/frotz &&
256         if git push testrepo master:frotz
257         then
258                 echo "Oops, should have failed"
259                 false
260         else
261                 check_push_result $the_first_commit heads/frotz tags/frotz
262         fi
263
264 '
265
266 test_expect_success 'push with colon-less refspec (1)' '
267
268         mk_test heads/frotz tags/frotz &&
269         git branch -f frotz master &&
270         git push testrepo frotz &&
271         check_push_result $the_commit heads/frotz &&
272         check_push_result $the_first_commit tags/frotz
273
274 '
275
276 test_expect_success 'push with colon-less refspec (2)' '
277
278         mk_test heads/frotz tags/frotz &&
279         if git show-ref --verify -q refs/heads/frotz
280         then
281                 git branch -D frotz
282         fi &&
283         git tag -f frotz &&
284         git push testrepo frotz &&
285         check_push_result $the_commit tags/frotz &&
286         check_push_result $the_first_commit heads/frotz
287
288 '
289
290 test_expect_success 'push with colon-less refspec (3)' '
291
292         mk_test &&
293         if git show-ref --verify -q refs/tags/frotz
294         then
295                 git tag -d frotz
296         fi &&
297         git branch -f frotz master &&
298         git push testrepo frotz &&
299         check_push_result $the_commit heads/frotz &&
300         test 1 = $( cd testrepo && git show-ref | wc -l )
301 '
302
303 test_expect_success 'push with colon-less refspec (4)' '
304
305         mk_test &&
306         if git show-ref --verify -q refs/heads/frotz
307         then
308                 git branch -D frotz
309         fi &&
310         git tag -f frotz &&
311         git push testrepo frotz &&
312         check_push_result $the_commit tags/frotz &&
313         test 1 = $( cd testrepo && git show-ref | wc -l )
314
315 '
316
317 test_expect_success 'push head with non-existant, incomplete dest' '
318
319         mk_test &&
320         git push testrepo master:branch &&
321         check_push_result $the_commit heads/branch
322
323 '
324
325 test_expect_success 'push tag with non-existant, incomplete dest' '
326
327         mk_test &&
328         git tag -f v1.0 &&
329         git push testrepo v1.0:tag &&
330         check_push_result $the_commit tags/tag
331
332 '
333
334 test_expect_success 'push sha1 with non-existant, incomplete dest' '
335
336         mk_test &&
337         test_must_fail git push testrepo `git rev-parse master`:foo
338
339 '
340
341 test_expect_success 'push ref expression with non-existant, incomplete dest' '
342
343         mk_test &&
344         test_must_fail git push testrepo master^:branch
345
346 '
347
348 test_expect_success 'push with HEAD' '
349
350         mk_test heads/master &&
351         git checkout master &&
352         git push testrepo HEAD &&
353         check_push_result $the_commit heads/master
354
355 '
356
357 test_expect_success 'push with HEAD nonexisting at remote' '
358
359         mk_test heads/master &&
360         git checkout -b local master &&
361         git push testrepo HEAD &&
362         check_push_result $the_commit heads/local
363 '
364
365 test_expect_success 'push with +HEAD' '
366
367         mk_test heads/master &&
368         git checkout master &&
369         git branch -D local &&
370         git checkout -b local &&
371         git push testrepo master local &&
372         check_push_result $the_commit heads/master &&
373         check_push_result $the_commit heads/local &&
374
375         # Without force rewinding should fail
376         git reset --hard HEAD^ &&
377         test_must_fail git push testrepo HEAD &&
378         check_push_result $the_commit heads/local &&
379
380         # With force rewinding should succeed
381         git push testrepo +HEAD &&
382         check_push_result $the_first_commit heads/local
383
384 '
385
386 test_expect_success 'push HEAD with non-existant, incomplete dest' '
387
388         mk_test &&
389         git checkout master &&
390         git push testrepo HEAD:branch &&
391         check_push_result $the_commit heads/branch
392
393 '
394
395 test_expect_success 'push with config remote.*.push = HEAD' '
396
397         mk_test heads/local &&
398         git checkout master &&
399         git branch -f local $the_commit &&
400         (
401                 cd testrepo &&
402                 git checkout local &&
403                 git reset --hard $the_first_commit
404         ) &&
405         git config remote.there.url testrepo &&
406         git config remote.there.push HEAD &&
407         git config branch.master.remote there &&
408         git push &&
409         check_push_result $the_commit heads/master &&
410         check_push_result $the_first_commit heads/local
411 '
412
413 # clean up the cruft left with the previous one
414 git config --remove-section remote.there
415 git config --remove-section branch.master
416
417 test_expect_success 'push with dry-run' '
418
419         mk_test heads/master &&
420         (cd testrepo &&
421          old_commit=$(git show-ref -s --verify refs/heads/master)) &&
422         git push --dry-run testrepo &&
423         check_push_result $old_commit heads/master
424 '
425
426 test_expect_success 'push updates local refs' '
427
428         rm -rf parent child &&
429         mkdir parent &&
430         (cd parent && git init &&
431                 echo one >foo && git add foo && git commit -m one) &&
432         git clone parent child &&
433         (cd child &&
434                 echo two >foo && git commit -a -m two &&
435                 git push &&
436         test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
437
438 '
439
440 test_expect_success 'push updates up-to-date local refs' '
441
442         rm -rf parent child &&
443         mkdir parent &&
444         (cd parent && git init &&
445                 echo one >foo && git add foo && git commit -m one) &&
446         git clone parent child1 &&
447         git clone parent child2 &&
448         (cd child1 &&
449                 echo two >foo && git commit -a -m two &&
450                 git push) &&
451         (cd child2 &&
452                 git pull ../child1 master &&
453                 git push &&
454         test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
455
456 '
457
458 test_expect_success 'push preserves up-to-date packed refs' '
459
460         rm -rf parent child &&
461         mkdir parent &&
462         (cd parent && git init &&
463                 echo one >foo && git add foo && git commit -m one) &&
464         git clone parent child &&
465         (cd child &&
466                 git push &&
467         ! test -f .git/refs/remotes/origin/master)
468
469 '
470
471 test_expect_success 'push does not update local refs on failure' '
472
473         rm -rf parent child &&
474         mkdir parent &&
475         (cd parent && git init &&
476                 echo one >foo && git add foo && git commit -m one &&
477                 echo exit 1 >.git/hooks/pre-receive &&
478                 chmod +x .git/hooks/pre-receive) &&
479         git clone parent child &&
480         (cd child &&
481                 echo two >foo && git commit -a -m two &&
482                 test_must_fail git push &&
483                 test $(git rev-parse master) != \
484                         $(git rev-parse remotes/origin/master))
485
486 '
487
488 test_expect_success 'allow deleting an invalid remote ref' '
489
490         pwd &&
491         rm -f testrepo/.git/objects/??/* &&
492         git push testrepo :refs/heads/master &&
493         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
494
495 '
496
497 test_expect_success 'fetch with branches' '
498         mk_empty &&
499         git branch second $the_first_commit &&
500         git checkout second &&
501         echo ".." > testrepo/.git/branches/branch1 &&
502         (cd testrepo &&
503                 git fetch branch1 &&
504                 r=$(git show-ref -s --verify refs/heads/branch1) &&
505                 test "z$r" = "z$the_commit" &&
506                 test 1 = $(git for-each-ref refs/heads | wc -l)
507         ) &&
508         git checkout master
509 '
510
511 test_expect_success 'fetch with branches containing #' '
512         mk_empty &&
513         echo "..#second" > testrepo/.git/branches/branch2 &&
514         (cd testrepo &&
515                 git fetch branch2 &&
516                 r=$(git show-ref -s --verify refs/heads/branch2) &&
517                 test "z$r" = "z$the_first_commit" &&
518                 test 1 = $(git for-each-ref refs/heads | wc -l)
519         ) &&
520         git checkout master
521 '
522
523 test_expect_success 'push with branches' '
524         mk_empty &&
525         git checkout second &&
526         echo "testrepo" > .git/branches/branch1 &&
527         git push branch1 &&
528         (cd testrepo &&
529                 r=$(git show-ref -s --verify refs/heads/master) &&
530                 test "z$r" = "z$the_first_commit" &&
531                 test 1 = $(git for-each-ref refs/heads | wc -l)
532         )
533 '
534
535 test_expect_success 'push with branches containing #' '
536         mk_empty &&
537         echo "testrepo#branch3" > .git/branches/branch2 &&
538         git push branch2 &&
539         (cd testrepo &&
540                 r=$(git show-ref -s --verify refs/heads/branch3) &&
541                 test "z$r" = "z$the_first_commit" &&
542                 test 1 = $(git for-each-ref refs/heads | wc -l)
543         ) &&
544         git checkout master
545 '
546
547 test_done