Merge branch 'fc/remote-helpers-test-updates'
[git] / t / t5516-fetch-push.sh
1 #!/bin/sh
2
3 test_description='Basic fetch/push functionality.
4
5 This test checks the following functionality:
6
7 * command-line syntax
8 * refspecs
9 * fast-forward detection, and overriding it
10 * configuration
11 * hooks
12 * --porcelain output format
13 * hiderefs
14 '
15
16 . ./test-lib.sh
17
18 D=`pwd`
19
20 mk_empty () {
21         repo_name="$1"
22         rm -fr "$repo_name" &&
23         mkdir "$repo_name" &&
24         (
25                 cd "$repo_name" &&
26                 git init &&
27                 git config receive.denyCurrentBranch warn &&
28                 mv .git/hooks .git/hooks-disabled
29         )
30 }
31
32 mk_test () {
33         repo_name="$1"
34         shift
35
36         mk_empty "$repo_name" &&
37         (
38                 for ref in "$@"
39                 do
40                         git push "$repo_name" $the_first_commit:refs/$ref ||
41                         exit
42                 done &&
43                 cd "$repo_name" &&
44                 for ref in "$@"
45                 do
46                         echo "$the_first_commit" >expect &&
47                         git show-ref -s --verify refs/$ref >actual &&
48                         test_cmp expect actual ||
49                         exit
50                 done &&
51                 git fsck --full
52         )
53 }
54
55 mk_test_with_hooks() {
56         repo_name=$1
57         mk_test "$@" &&
58         (
59                 cd "$repo_name" &&
60                 mkdir .git/hooks &&
61                 cd .git/hooks &&
62
63                 cat >pre-receive <<-'EOF' &&
64                 #!/bin/sh
65                 cat - >>pre-receive.actual
66                 EOF
67
68                 cat >update <<-'EOF' &&
69                 #!/bin/sh
70                 printf "%s %s %s\n" "$@" >>update.actual
71                 EOF
72
73                 cat >post-receive <<-'EOF' &&
74                 #!/bin/sh
75                 cat - >>post-receive.actual
76                 EOF
77
78                 cat >post-update <<-'EOF' &&
79                 #!/bin/sh
80                 for ref in "$@"
81                 do
82                         printf "%s\n" "$ref" >>post-update.actual
83                 done
84                 EOF
85
86                 chmod +x pre-receive update post-receive post-update
87         )
88 }
89
90 mk_child() {
91         rm -rf "$2" &&
92         git clone "$1" "$2"
93 }
94
95 check_push_result () {
96         repo_name="$1"
97         shift
98
99         (
100                 cd "$repo_name" &&
101                 echo "$1" >expect &&
102                 shift &&
103                 for ref in "$@"
104                 do
105                         git show-ref -s --verify refs/$ref >actual &&
106                         test_cmp expect actual ||
107                         exit
108                 done &&
109                 git fsck --full
110         )
111 }
112
113 test_expect_success setup '
114
115         >path1 &&
116         git add path1 &&
117         test_tick &&
118         git commit -a -m repo &&
119         the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
120
121         >path2 &&
122         git add path2 &&
123         test_tick &&
124         git commit -a -m second &&
125         the_commit=$(git show-ref -s --verify refs/heads/master)
126
127 '
128
129 test_expect_success 'fetch without wildcard' '
130         mk_empty testrepo &&
131         (
132                 cd testrepo &&
133                 git fetch .. refs/heads/master:refs/remotes/origin/master &&
134
135                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
136                 git for-each-ref refs/remotes/origin >actual &&
137                 test_cmp expect actual
138         )
139 '
140
141 test_expect_success 'fetch with wildcard' '
142         mk_empty testrepo &&
143         (
144                 cd testrepo &&
145                 git config remote.up.url .. &&
146                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
147                 git fetch up &&
148
149                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
150                 git for-each-ref refs/remotes/origin >actual &&
151                 test_cmp expect actual
152         )
153 '
154
155 test_expect_success 'fetch with insteadOf' '
156         mk_empty testrepo &&
157         (
158                 TRASH=$(pwd)/ &&
159                 cd testrepo &&
160                 git config "url.$TRASH.insteadOf" trash/ &&
161                 git config remote.up.url trash/. &&
162                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
163                 git fetch up &&
164
165                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
166                 git for-each-ref refs/remotes/origin >actual &&
167                 test_cmp expect actual
168         )
169 '
170
171 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
172         mk_empty testrepo &&
173         (
174                 TRASH=$(pwd)/ &&
175                 cd testrepo &&
176                 git config "url.trash/.pushInsteadOf" "$TRASH" &&
177                 git config remote.up.url "$TRASH." &&
178                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
179                 git fetch up &&
180
181                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
182                 git for-each-ref refs/remotes/origin >actual &&
183                 test_cmp expect actual
184         )
185 '
186
187 test_expect_success 'push without wildcard' '
188         mk_empty testrepo &&
189
190         git push testrepo refs/heads/master:refs/remotes/origin/master &&
191         (
192                 cd testrepo &&
193                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
194                 git for-each-ref refs/remotes/origin >actual &&
195                 test_cmp expect actual
196         )
197 '
198
199 test_expect_success 'push with wildcard' '
200         mk_empty testrepo &&
201
202         git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
203         (
204                 cd testrepo &&
205                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
206                 git for-each-ref refs/remotes/origin >actual &&
207                 test_cmp expect actual
208         )
209 '
210
211 test_expect_success 'push with insteadOf' '
212         mk_empty testrepo &&
213         TRASH="$(pwd)/" &&
214         test_config "url.$TRASH.insteadOf" trash/ &&
215         git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
216         (
217                 cd testrepo &&
218                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
219                 git for-each-ref refs/remotes/origin >actual &&
220                 test_cmp expect actual
221         )
222 '
223
224 test_expect_success 'push with pushInsteadOf' '
225         mk_empty testrepo &&
226         TRASH="$(pwd)/" &&
227         test_config "url.$TRASH.pushInsteadOf" trash/ &&
228         git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
229         (
230                 cd testrepo &&
231                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
232                 git for-each-ref refs/remotes/origin >actual &&
233                 test_cmp expect actual
234         )
235 '
236
237 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
238         mk_empty testrepo &&
239         test_config "url.trash2/.pushInsteadOf" testrepo/ &&
240         test_config "url.trash3/.pusnInsteadOf" trash/wrong &&
241         test_config remote.r.url trash/wrong &&
242         test_config remote.r.pushurl "testrepo/" &&
243         git push r refs/heads/master:refs/remotes/origin/master &&
244         (
245                 cd testrepo &&
246                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
247                 git for-each-ref refs/remotes/origin >actual &&
248                 test_cmp expect actual
249         )
250 '
251
252 test_expect_success 'push with matching heads' '
253
254         mk_test testrepo heads/master &&
255         git push testrepo &&
256         check_push_result testrepo $the_commit heads/master
257
258 '
259
260 test_expect_success 'push with matching heads on the command line' '
261
262         mk_test testrepo heads/master &&
263         git push testrepo : &&
264         check_push_result testrepo $the_commit heads/master
265
266 '
267
268 test_expect_success 'failed (non-fast-forward) push with matching heads' '
269
270         mk_test testrepo heads/master &&
271         git push testrepo : &&
272         git commit --amend -massaged &&
273         test_must_fail git push testrepo &&
274         check_push_result testrepo $the_commit heads/master &&
275         git reset --hard $the_commit
276
277 '
278
279 test_expect_success 'push --force with matching heads' '
280
281         mk_test testrepo heads/master &&
282         git push testrepo : &&
283         git commit --amend -massaged &&
284         git push --force testrepo &&
285         ! check_push_result testrepo $the_commit heads/master &&
286         git reset --hard $the_commit
287
288 '
289
290 test_expect_success 'push with matching heads and forced update' '
291
292         mk_test testrepo heads/master &&
293         git push testrepo : &&
294         git commit --amend -massaged &&
295         git push testrepo +: &&
296         ! check_push_result testrepo $the_commit heads/master &&
297         git reset --hard $the_commit
298
299 '
300
301 test_expect_success 'push with no ambiguity (1)' '
302
303         mk_test testrepo heads/master &&
304         git push testrepo master:master &&
305         check_push_result testrepo $the_commit heads/master
306
307 '
308
309 test_expect_success 'push with no ambiguity (2)' '
310
311         mk_test testrepo remotes/origin/master &&
312         git push testrepo master:origin/master &&
313         check_push_result testrepo $the_commit remotes/origin/master
314
315 '
316
317 test_expect_success 'push with colon-less refspec, no ambiguity' '
318
319         mk_test testrepo heads/master heads/t/master &&
320         git branch -f t/master master &&
321         git push testrepo master &&
322         check_push_result testrepo $the_commit heads/master &&
323         check_push_result testrepo $the_first_commit heads/t/master
324
325 '
326
327 test_expect_success 'push with weak ambiguity (1)' '
328
329         mk_test testrepo heads/master remotes/origin/master &&
330         git push testrepo master:master &&
331         check_push_result testrepo $the_commit heads/master &&
332         check_push_result testrepo $the_first_commit remotes/origin/master
333
334 '
335
336 test_expect_success 'push with weak ambiguity (2)' '
337
338         mk_test testrepo heads/master remotes/origin/master remotes/another/master &&
339         git push testrepo master:master &&
340         check_push_result testrepo $the_commit heads/master &&
341         check_push_result testrepo $the_first_commit remotes/origin/master remotes/another/master
342
343 '
344
345 test_expect_success 'push with ambiguity' '
346
347         mk_test testrepo heads/frotz tags/frotz &&
348         test_must_fail git push testrepo master:frotz &&
349         check_push_result testrepo $the_first_commit heads/frotz tags/frotz
350
351 '
352
353 test_expect_success 'push with colon-less refspec (1)' '
354
355         mk_test testrepo heads/frotz tags/frotz &&
356         git branch -f frotz master &&
357         git push testrepo frotz &&
358         check_push_result testrepo $the_commit heads/frotz &&
359         check_push_result testrepo $the_first_commit tags/frotz
360
361 '
362
363 test_expect_success 'push with colon-less refspec (2)' '
364
365         mk_test testrepo heads/frotz tags/frotz &&
366         if git show-ref --verify -q refs/heads/frotz
367         then
368                 git branch -D frotz
369         fi &&
370         git tag -f frotz &&
371         git push -f testrepo frotz &&
372         check_push_result testrepo $the_commit tags/frotz &&
373         check_push_result testrepo $the_first_commit heads/frotz
374
375 '
376
377 test_expect_success 'push with colon-less refspec (3)' '
378
379         mk_test testrepo &&
380         if git show-ref --verify -q refs/tags/frotz
381         then
382                 git tag -d frotz
383         fi &&
384         git branch -f frotz master &&
385         git push testrepo frotz &&
386         check_push_result testrepo $the_commit heads/frotz &&
387         test 1 = $( cd testrepo && git show-ref | wc -l )
388 '
389
390 test_expect_success 'push with colon-less refspec (4)' '
391
392         mk_test testrepo &&
393         if git show-ref --verify -q refs/heads/frotz
394         then
395                 git branch -D frotz
396         fi &&
397         git tag -f frotz &&
398         git push testrepo frotz &&
399         check_push_result testrepo $the_commit tags/frotz &&
400         test 1 = $( cd testrepo && git show-ref | wc -l )
401
402 '
403
404 test_expect_success 'push head with non-existent, incomplete dest' '
405
406         mk_test testrepo &&
407         git push testrepo master:branch &&
408         check_push_result testrepo $the_commit heads/branch
409
410 '
411
412 test_expect_success 'push tag with non-existent, incomplete dest' '
413
414         mk_test testrepo &&
415         git tag -f v1.0 &&
416         git push testrepo v1.0:tag &&
417         check_push_result testrepo $the_commit tags/tag
418
419 '
420
421 test_expect_success 'push sha1 with non-existent, incomplete dest' '
422
423         mk_test testrepo &&
424         test_must_fail git push testrepo `git rev-parse master`:foo
425
426 '
427
428 test_expect_success 'push ref expression with non-existent, incomplete dest' '
429
430         mk_test testrepo &&
431         test_must_fail git push testrepo master^:branch
432
433 '
434
435 test_expect_success 'push with HEAD' '
436
437         mk_test testrepo heads/master &&
438         git checkout master &&
439         git push testrepo HEAD &&
440         check_push_result testrepo $the_commit heads/master
441
442 '
443
444 test_expect_success 'push with HEAD nonexisting at remote' '
445
446         mk_test testrepo heads/master &&
447         git checkout -b local master &&
448         git push testrepo HEAD &&
449         check_push_result testrepo $the_commit heads/local
450 '
451
452 test_expect_success 'push with +HEAD' '
453
454         mk_test testrepo heads/master &&
455         git checkout master &&
456         git branch -D local &&
457         git checkout -b local &&
458         git push testrepo master local &&
459         check_push_result testrepo $the_commit heads/master &&
460         check_push_result testrepo $the_commit heads/local &&
461
462         # Without force rewinding should fail
463         git reset --hard HEAD^ &&
464         test_must_fail git push testrepo HEAD &&
465         check_push_result testrepo $the_commit heads/local &&
466
467         # With force rewinding should succeed
468         git push testrepo +HEAD &&
469         check_push_result testrepo $the_first_commit heads/local
470
471 '
472
473 test_expect_success 'push HEAD with non-existent, incomplete dest' '
474
475         mk_test testrepo &&
476         git checkout master &&
477         git push testrepo HEAD:branch &&
478         check_push_result testrepo $the_commit heads/branch
479
480 '
481
482 test_expect_success 'push with config remote.*.push = HEAD' '
483
484         mk_test testrepo heads/local &&
485         git checkout master &&
486         git branch -f local $the_commit &&
487         (
488                 cd testrepo &&
489                 git checkout local &&
490                 git reset --hard $the_first_commit
491         ) &&
492         test_config remote.there.url testrepo &&
493         test_config remote.there.push HEAD &&
494         test_config branch.master.remote there &&
495         git push &&
496         check_push_result testrepo $the_commit heads/master &&
497         check_push_result testrepo $the_first_commit heads/local
498 '
499
500 test_expect_success 'push with remote.pushdefault' '
501         mk_test up_repo heads/master &&
502         mk_test down_repo heads/master &&
503         test_config remote.up.url up_repo &&
504         test_config remote.down.url down_repo &&
505         test_config branch.master.remote up &&
506         test_config remote.pushdefault down &&
507         git push &&
508         check_push_result up_repo $the_first_commit heads/master &&
509         check_push_result down_repo $the_commit heads/master
510 '
511
512 test_expect_success 'push with config remote.*.pushurl' '
513
514         mk_test testrepo heads/master &&
515         git checkout master &&
516         test_config remote.there.url test2repo &&
517         test_config remote.there.pushurl testrepo &&
518         git push there &&
519         check_push_result testrepo $the_commit heads/master
520 '
521
522 test_expect_success 'push with config branch.*.pushremote' '
523         mk_test up_repo heads/master &&
524         mk_test side_repo heads/master &&
525         mk_test down_repo heads/master &&
526         test_config remote.up.url up_repo &&
527         test_config remote.pushdefault side_repo &&
528         test_config remote.down.url down_repo &&
529         test_config branch.master.remote up &&
530         test_config branch.master.pushremote down &&
531         git push &&
532         check_push_result up_repo $the_first_commit heads/master &&
533         check_push_result side_repo $the_first_commit heads/master &&
534         check_push_result down_repo $the_commit heads/master
535 '
536
537 test_expect_success 'push with dry-run' '
538
539         mk_test testrepo heads/master &&
540         (
541                 cd testrepo &&
542                 old_commit=$(git show-ref -s --verify refs/heads/master)
543         ) &&
544         git push --dry-run testrepo &&
545         check_push_result testrepo $old_commit heads/master
546 '
547
548 test_expect_success 'push updates local refs' '
549
550         mk_test testrepo heads/master &&
551         mk_child testrepo child &&
552         (
553                 cd child &&
554                 git pull .. master &&
555                 git push &&
556                 test $(git rev-parse master) = \
557                         $(git rev-parse remotes/origin/master)
558         )
559
560 '
561
562 test_expect_success 'push updates up-to-date local refs' '
563
564         mk_test testrepo heads/master &&
565         mk_child testrepo child1 &&
566         mk_child testrepo child2 &&
567         (cd child1 && git pull .. master && git push) &&
568         (
569                 cd child2 &&
570                 git pull ../child1 master &&
571                 git push &&
572                 test $(git rev-parse master) = \
573                         $(git rev-parse remotes/origin/master)
574         )
575
576 '
577
578 test_expect_success 'push preserves up-to-date packed refs' '
579
580         mk_test testrepo heads/master &&
581         mk_child testrepo child &&
582         (
583                 cd child &&
584                 git push &&
585                 ! test -f .git/refs/remotes/origin/master
586         )
587
588 '
589
590 test_expect_success 'push does not update local refs on failure' '
591
592         mk_test testrepo heads/master &&
593         mk_child testrepo child &&
594         mkdir testrepo/.git/hooks &&
595         echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
596         chmod +x testrepo/.git/hooks/pre-receive &&
597         (
598                 cd child &&
599                 git pull .. master
600                 test_must_fail git push &&
601                 test $(git rev-parse master) != \
602                         $(git rev-parse remotes/origin/master)
603         )
604
605 '
606
607 test_expect_success 'allow deleting an invalid remote ref' '
608
609         mk_test testrepo heads/master &&
610         rm -f testrepo/.git/objects/??/* &&
611         git push testrepo :refs/heads/master &&
612         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
613
614 '
615
616 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
617         mk_test_with_hooks testrepo heads/master heads/next &&
618         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
619         newmaster=$(git show-ref -s --verify refs/heads/master) &&
620         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
621         newnext=$_z40 &&
622         git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
623         (
624                 cd testrepo/.git &&
625                 cat >pre-receive.expect <<-EOF &&
626                 $orgmaster $newmaster refs/heads/master
627                 $orgnext $newnext refs/heads/next
628                 EOF
629
630                 cat >update.expect <<-EOF &&
631                 refs/heads/master $orgmaster $newmaster
632                 refs/heads/next $orgnext $newnext
633                 EOF
634
635                 cat >post-receive.expect <<-EOF &&
636                 $orgmaster $newmaster refs/heads/master
637                 $orgnext $newnext refs/heads/next
638                 EOF
639
640                 cat >post-update.expect <<-EOF &&
641                 refs/heads/master
642                 refs/heads/next
643                 EOF
644
645                 test_cmp pre-receive.expect pre-receive.actual &&
646                 test_cmp update.expect update.actual &&
647                 test_cmp post-receive.expect post-receive.actual &&
648                 test_cmp post-update.expect post-update.actual
649         )
650 '
651
652 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
653         mk_test_with_hooks testrepo heads/master &&
654         rm -f testrepo/.git/objects/??/* &&
655         git push testrepo :refs/heads/master &&
656         (
657                 cd testrepo/.git &&
658                 cat >pre-receive.expect <<-EOF &&
659                 $_z40 $_z40 refs/heads/master
660                 EOF
661
662                 cat >update.expect <<-EOF &&
663                 refs/heads/master $_z40 $_z40
664                 EOF
665
666                 cat >post-receive.expect <<-EOF &&
667                 $_z40 $_z40 refs/heads/master
668                 EOF
669
670                 cat >post-update.expect <<-EOF &&
671                 refs/heads/master
672                 EOF
673
674                 test_cmp pre-receive.expect pre-receive.actual &&
675                 test_cmp update.expect update.actual &&
676                 test_cmp post-receive.expect post-receive.actual &&
677                 test_cmp post-update.expect post-update.actual
678         )
679 '
680
681 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
682         mk_test_with_hooks testrepo heads/master &&
683         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
684         newmaster=$(git show-ref -s --verify refs/heads/master) &&
685         git push testrepo master :refs/heads/nonexistent &&
686         (
687                 cd testrepo/.git &&
688                 cat >pre-receive.expect <<-EOF &&
689                 $orgmaster $newmaster refs/heads/master
690                 $_z40 $_z40 refs/heads/nonexistent
691                 EOF
692
693                 cat >update.expect <<-EOF &&
694                 refs/heads/master $orgmaster $newmaster
695                 refs/heads/nonexistent $_z40 $_z40
696                 EOF
697
698                 cat >post-receive.expect <<-EOF &&
699                 $orgmaster $newmaster refs/heads/master
700                 EOF
701
702                 cat >post-update.expect <<-EOF &&
703                 refs/heads/master
704                 EOF
705
706                 test_cmp pre-receive.expect pre-receive.actual &&
707                 test_cmp update.expect update.actual &&
708                 test_cmp post-receive.expect post-receive.actual &&
709                 test_cmp post-update.expect post-update.actual
710         )
711 '
712
713 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
714         mk_test_with_hooks testrepo heads/master &&
715         git push testrepo :refs/heads/nonexistent &&
716         (
717                 cd testrepo/.git &&
718                 cat >pre-receive.expect <<-EOF &&
719                 $_z40 $_z40 refs/heads/nonexistent
720                 EOF
721
722                 cat >update.expect <<-EOF &&
723                 refs/heads/nonexistent $_z40 $_z40
724                 EOF
725
726                 test_cmp pre-receive.expect pre-receive.actual &&
727                 test_cmp update.expect update.actual &&
728                 test_path_is_missing post-receive.actual &&
729                 test_path_is_missing post-update.actual
730         )
731 '
732
733 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
734         mk_test_with_hooks testrepo heads/master heads/next heads/pu &&
735         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
736         newmaster=$(git show-ref -s --verify refs/heads/master) &&
737         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
738         newnext=$_z40 &&
739         orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
740         newpu=$(git show-ref -s --verify refs/heads/master) &&
741         git push testrepo refs/heads/master:refs/heads/master \
742             refs/heads/master:refs/heads/pu :refs/heads/next \
743             :refs/heads/nonexistent &&
744         (
745                 cd testrepo/.git &&
746                 cat >pre-receive.expect <<-EOF &&
747                 $orgmaster $newmaster refs/heads/master
748                 $orgnext $newnext refs/heads/next
749                 $orgpu $newpu refs/heads/pu
750                 $_z40 $_z40 refs/heads/nonexistent
751                 EOF
752
753                 cat >update.expect <<-EOF &&
754                 refs/heads/master $orgmaster $newmaster
755                 refs/heads/next $orgnext $newnext
756                 refs/heads/pu $orgpu $newpu
757                 refs/heads/nonexistent $_z40 $_z40
758                 EOF
759
760                 cat >post-receive.expect <<-EOF &&
761                 $orgmaster $newmaster refs/heads/master
762                 $orgnext $newnext refs/heads/next
763                 $orgpu $newpu refs/heads/pu
764                 EOF
765
766                 cat >post-update.expect <<-EOF &&
767                 refs/heads/master
768                 refs/heads/next
769                 refs/heads/pu
770                 EOF
771
772                 test_cmp pre-receive.expect pre-receive.actual &&
773                 test_cmp update.expect update.actual &&
774                 test_cmp post-receive.expect post-receive.actual &&
775                 test_cmp post-update.expect post-update.actual
776         )
777 '
778
779 test_expect_success 'allow deleting a ref using --delete' '
780         mk_test testrepo heads/master &&
781         (cd testrepo && git config receive.denyDeleteCurrent warn) &&
782         git push testrepo --delete master &&
783         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
784 '
785
786 test_expect_success 'allow deleting a tag using --delete' '
787         mk_test testrepo heads/master &&
788         git tag -a -m dummy_message deltag heads/master &&
789         git push testrepo --tags &&
790         (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
791         git push testrepo --delete tag deltag &&
792         (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
793 '
794
795 test_expect_success 'push --delete without args aborts' '
796         mk_test testrepo heads/master &&
797         test_must_fail git push testrepo --delete
798 '
799
800 test_expect_success 'push --delete refuses src:dest refspecs' '
801         mk_test testrepo heads/master &&
802         test_must_fail git push testrepo --delete master:foo
803 '
804
805 test_expect_success 'warn on push to HEAD of non-bare repository' '
806         mk_test testrepo heads/master &&
807         (
808                 cd testrepo &&
809                 git checkout master &&
810                 git config receive.denyCurrentBranch warn
811         ) &&
812         git push testrepo master 2>stderr &&
813         grep "warning: updating the current branch" stderr
814 '
815
816 test_expect_success 'deny push to HEAD of non-bare repository' '
817         mk_test testrepo heads/master &&
818         (
819                 cd testrepo &&
820                 git checkout master &&
821                 git config receive.denyCurrentBranch true
822         ) &&
823         test_must_fail git push testrepo master
824 '
825
826 test_expect_success 'allow push to HEAD of bare repository (bare)' '
827         mk_test testrepo heads/master &&
828         (
829                 cd testrepo &&
830                 git checkout master &&
831                 git config receive.denyCurrentBranch true &&
832                 git config core.bare true
833         ) &&
834         git push testrepo master 2>stderr &&
835         ! grep "warning: updating the current branch" stderr
836 '
837
838 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
839         mk_test testrepo heads/master &&
840         (
841                 cd testrepo &&
842                 git checkout master &&
843                 git config receive.denyCurrentBranch false
844         ) &&
845         git push testrepo master 2>stderr &&
846         ! grep "warning: updating the current branch" stderr
847 '
848
849 test_expect_success 'fetch with branches' '
850         mk_empty testrepo &&
851         git branch second $the_first_commit &&
852         git checkout second &&
853         echo ".." > testrepo/.git/branches/branch1 &&
854         (
855                 cd testrepo &&
856                 git fetch branch1 &&
857                 echo "$the_commit commit        refs/heads/branch1" >expect &&
858                 git for-each-ref refs/heads >actual &&
859                 test_cmp expect actual
860         ) &&
861         git checkout master
862 '
863
864 test_expect_success 'fetch with branches containing #' '
865         mk_empty testrepo &&
866         echo "..#second" > testrepo/.git/branches/branch2 &&
867         (
868                 cd testrepo &&
869                 git fetch branch2 &&
870                 echo "$the_first_commit commit  refs/heads/branch2" >expect &&
871                 git for-each-ref refs/heads >actual &&
872                 test_cmp expect actual
873         ) &&
874         git checkout master
875 '
876
877 test_expect_success 'push with branches' '
878         mk_empty testrepo &&
879         git checkout second &&
880         echo "testrepo" > .git/branches/branch1 &&
881         git push branch1 &&
882         (
883                 cd testrepo &&
884                 echo "$the_first_commit commit  refs/heads/master" >expect &&
885                 git for-each-ref refs/heads >actual &&
886                 test_cmp expect actual
887         )
888 '
889
890 test_expect_success 'push with branches containing #' '
891         mk_empty testrepo &&
892         echo "testrepo#branch3" > .git/branches/branch2 &&
893         git push branch2 &&
894         (
895                 cd testrepo &&
896                 echo "$the_first_commit commit  refs/heads/branch3" >expect &&
897                 git for-each-ref refs/heads >actual &&
898                 test_cmp expect actual
899         ) &&
900         git checkout master
901 '
902
903 test_expect_success 'push into aliased refs (consistent)' '
904         mk_test testrepo heads/master &&
905         mk_child testrepo child1 &&
906         mk_child testrepo child2 &&
907         (
908                 cd child1 &&
909                 git branch foo &&
910                 git symbolic-ref refs/heads/bar refs/heads/foo
911                 git config receive.denyCurrentBranch false
912         ) &&
913         (
914                 cd child2 &&
915                 >path2 &&
916                 git add path2 &&
917                 test_tick &&
918                 git commit -a -m child2 &&
919                 git branch foo &&
920                 git branch bar &&
921                 git push ../child1 foo bar
922         )
923 '
924
925 test_expect_success 'push into aliased refs (inconsistent)' '
926         mk_test testrepo heads/master &&
927         mk_child testrepo child1 &&
928         mk_child testrepo child2 &&
929         (
930                 cd child1 &&
931                 git branch foo &&
932                 git symbolic-ref refs/heads/bar refs/heads/foo
933                 git config receive.denyCurrentBranch false
934         ) &&
935         (
936                 cd child2 &&
937                 >path2 &&
938                 git add path2 &&
939                 test_tick &&
940                 git commit -a -m child2 &&
941                 git branch foo &&
942                 >path3 &&
943                 git add path3 &&
944                 test_tick &&
945                 git commit -a -m child2 &&
946                 git branch bar &&
947                 test_must_fail git push ../child1 foo bar 2>stderr &&
948                 grep "refusing inconsistent update" stderr
949         )
950 '
951
952 test_expect_success 'push requires --force to update lightweight tag' '
953         mk_test testrepo heads/master &&
954         mk_child testrepo child1 &&
955         mk_child testrepo child2 &&
956         (
957                 cd child1 &&
958                 git tag Tag &&
959                 git push ../child2 Tag &&
960                 git push ../child2 Tag &&
961                 >file1 &&
962                 git add file1 &&
963                 git commit -m "file1" &&
964                 git tag -f Tag &&
965                 test_must_fail git push ../child2 Tag &&
966                 git push --force ../child2 Tag &&
967                 git tag -f Tag &&
968                 test_must_fail git push ../child2 Tag HEAD~ &&
969                 git push --force ../child2 Tag
970         )
971 '
972
973 test_expect_success 'push --porcelain' '
974         mk_empty testrepo &&
975         echo >.git/foo  "To testrepo" &&
976         echo >>.git/foo "*      refs/heads/master:refs/remotes/origin/master    [new branch]"  &&
977         echo >>.git/foo "Done" &&
978         git push >.git/bar --porcelain  testrepo refs/heads/master:refs/remotes/origin/master &&
979         (
980                 cd testrepo &&
981                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
982                 git for-each-ref refs/remotes/origin >actual &&
983                 test_cmp expect actual
984         ) &&
985         test_cmp .git/foo .git/bar
986 '
987
988 test_expect_success 'push --porcelain bad url' '
989         mk_empty testrepo &&
990         test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
991         test_must_fail grep -q Done .git/bar
992 '
993
994 test_expect_success 'push --porcelain rejected' '
995         mk_empty testrepo &&
996         git push testrepo refs/heads/master:refs/remotes/origin/master &&
997         (cd testrepo &&
998                 git reset --hard origin/master^
999                 git config receive.denyCurrentBranch true) &&
1000
1001         echo >.git/foo  "To testrepo"  &&
1002         echo >>.git/foo "!      refs/heads/master:refs/heads/master     [remote rejected] (branch is currently checked out)" &&
1003
1004         test_must_fail git push >.git/bar --porcelain  testrepo refs/heads/master:refs/heads/master &&
1005         test_cmp .git/foo .git/bar
1006 '
1007
1008 test_expect_success 'push --porcelain --dry-run rejected' '
1009         mk_empty testrepo &&
1010         git push testrepo refs/heads/master:refs/remotes/origin/master &&
1011         (cd testrepo &&
1012                 git reset --hard origin/master
1013                 git config receive.denyCurrentBranch true) &&
1014
1015         echo >.git/foo  "To testrepo"  &&
1016         echo >>.git/foo "!      refs/heads/master^:refs/heads/master    [rejected] (non-fast-forward)" &&
1017         echo >>.git/foo "Done" &&
1018
1019         test_must_fail git push >.git/bar --porcelain  --dry-run testrepo refs/heads/master^:refs/heads/master &&
1020         test_cmp .git/foo .git/bar
1021 '
1022
1023 test_expect_success 'push --prune' '
1024         mk_test testrepo heads/master heads/second heads/foo heads/bar &&
1025         git push --prune testrepo &&
1026         check_push_result testrepo $the_commit heads/master &&
1027         check_push_result testrepo $the_first_commit heads/second &&
1028         ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1029 '
1030
1031 test_expect_success 'push --prune refspec' '
1032         mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
1033         git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1034         check_push_result testrepo $the_commit tmp/master &&
1035         check_push_result testrepo $the_first_commit tmp/second &&
1036         ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1037 '
1038
1039 for configsection in transfer receive
1040 do
1041         test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1042                 mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
1043                 (
1044                         cd testrepo &&
1045                         git config $configsection.hiderefs refs/hidden
1046                 ) &&
1047
1048                 # push to unhidden ref succeeds normally
1049                 git push testrepo master:refs/heads/master &&
1050                 check_push_result testrepo $the_commit heads/master &&
1051
1052                 # push to update a hidden ref should fail
1053                 test_must_fail git push testrepo master:refs/hidden/one &&
1054                 check_push_result testrepo $the_first_commit hidden/one &&
1055
1056                 # push to delete a hidden ref should fail
1057                 test_must_fail git push testrepo :refs/hidden/two &&
1058                 check_push_result testrepo $the_first_commit hidden/two &&
1059
1060                 # idempotent push to update a hidden ref should fail
1061                 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1062                 check_push_result testrepo $the_first_commit hidden/three
1063         '
1064 done
1065
1066 test_expect_success 'fetch exact SHA1' '
1067         mk_test testrepo heads/master hidden/one &&
1068         git push testrepo master:refs/hidden/one &&
1069         (
1070                 cd testrepo &&
1071                 git config transfer.hiderefs refs/hidden
1072         ) &&
1073         check_push_result testrepo $the_commit hidden/one &&
1074
1075         mk_child testrepo child &&
1076         (
1077                 cd child &&
1078
1079                 # make sure $the_commit does not exist here
1080                 git repack -a -d &&
1081                 git prune &&
1082                 test_must_fail git cat-file -t $the_commit &&
1083
1084                 # fetching the hidden object should fail by default
1085                 test_must_fail git fetch -v ../testrepo $the_commit:refs/heads/copy &&
1086                 test_must_fail git rev-parse --verify refs/heads/copy &&
1087
1088                 # the server side can allow it to succeed
1089                 (
1090                         cd ../testrepo &&
1091                         git config uploadpack.allowtipsha1inwant true
1092                 ) &&
1093
1094                 git fetch -v ../testrepo $the_commit:refs/heads/copy &&
1095                 result=$(git rev-parse --verify refs/heads/copy) &&
1096                 test "$the_commit" = "$result"
1097         )
1098 '
1099
1100 test_expect_success 'fetch follows tags by default' '
1101         mk_test testrepo heads/master &&
1102         rm -fr src dst &&
1103         git init src &&
1104         (
1105                 cd src &&
1106                 git pull ../testrepo master &&
1107                 git tag -m "annotated" tag &&
1108                 git for-each-ref >tmp1 &&
1109                 (
1110                         cat tmp1
1111                         sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1112                 ) |
1113                 sort -k 3 >../expect
1114         ) &&
1115         git init dst &&
1116         (
1117                 cd dst &&
1118                 git remote add origin ../src &&
1119                 git config branch.master.remote origin &&
1120                 git config branch.master.merge refs/heads/master &&
1121                 git pull &&
1122                 git for-each-ref >../actual
1123         ) &&
1124         test_cmp expect actual
1125 '
1126
1127 test_expect_success 'push does not follow tags by default' '
1128         mk_test testrepo heads/master &&
1129         rm -fr src dst &&
1130         git init src &&
1131         git init --bare dst &&
1132         (
1133                 cd src &&
1134                 git pull ../testrepo master &&
1135                 git tag -m "annotated" tag &&
1136                 git checkout -b another &&
1137                 git commit --allow-empty -m "future commit" &&
1138                 git tag -m "future" future &&
1139                 git checkout master &&
1140                 git for-each-ref refs/heads/master >../expect &&
1141                 git push ../dst master
1142         ) &&
1143         (
1144                 cd dst &&
1145                 git for-each-ref >../actual
1146         ) &&
1147         test_cmp expect actual
1148 '
1149
1150 test_expect_success 'push --follow-tag only pushes relevant tags' '
1151         mk_test testrepo heads/master &&
1152         rm -fr src dst &&
1153         git init src &&
1154         git init --bare dst &&
1155         (
1156                 cd src &&
1157                 git pull ../testrepo master &&
1158                 git tag -m "annotated" tag &&
1159                 git checkout -b another &&
1160                 git commit --allow-empty -m "future commit" &&
1161                 git tag -m "future" future &&
1162                 git checkout master &&
1163                 git for-each-ref refs/heads/master refs/tags/tag >../expect
1164                 git push --follow-tag ../dst master
1165         ) &&
1166         (
1167                 cd dst &&
1168                 git for-each-ref >../actual
1169         ) &&
1170         test_cmp expect actual
1171 '
1172
1173 test_done