push: do not turn --delete '' into a matching push
[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 * reflogs
15 '
16
17 . ./test-lib.sh
18
19 D=$(pwd)
20
21 mk_empty () {
22         repo_name="$1"
23         rm -fr "$repo_name" &&
24         mkdir "$repo_name" &&
25         (
26                 cd "$repo_name" &&
27                 git init &&
28                 git config receive.denyCurrentBranch warn &&
29                 mv .git/hooks .git/hooks-disabled
30         )
31 }
32
33 mk_test () {
34         repo_name="$1"
35         shift
36
37         mk_empty "$repo_name" &&
38         (
39                 for ref in "$@"
40                 do
41                         git push "$repo_name" $the_first_commit:refs/$ref ||
42                         exit
43                 done &&
44                 cd "$repo_name" &&
45                 for ref in "$@"
46                 do
47                         echo "$the_first_commit" >expect &&
48                         git show-ref -s --verify refs/$ref >actual &&
49                         test_cmp expect actual ||
50                         exit
51                 done &&
52                 git fsck --full
53         )
54 }
55
56 mk_test_with_hooks() {
57         repo_name=$1
58         mk_test "$@" &&
59         (
60                 cd "$repo_name" &&
61                 mkdir .git/hooks &&
62                 cd .git/hooks &&
63
64                 cat >pre-receive <<-'EOF' &&
65                 #!/bin/sh
66                 cat - >>pre-receive.actual
67                 EOF
68
69                 cat >update <<-'EOF' &&
70                 #!/bin/sh
71                 printf "%s %s %s\n" "$@" >>update.actual
72                 EOF
73
74                 cat >post-receive <<-'EOF' &&
75                 #!/bin/sh
76                 cat - >>post-receive.actual
77                 EOF
78
79                 cat >post-update <<-'EOF' &&
80                 #!/bin/sh
81                 for ref in "$@"
82                 do
83                         printf "%s\n" "$ref" >>post-update.actual
84                 done
85                 EOF
86
87                 chmod +x pre-receive update post-receive post-update
88         )
89 }
90
91 mk_child() {
92         rm -rf "$2" &&
93         git clone "$1" "$2"
94 }
95
96 check_push_result () {
97         test $# -ge 3 ||
98         BUG "check_push_result requires at least 3 parameters"
99
100         repo_name="$1"
101         shift
102
103         (
104                 cd "$repo_name" &&
105                 echo "$1" >expect &&
106                 shift &&
107                 for ref in "$@"
108                 do
109                         git show-ref -s --verify refs/$ref >actual &&
110                         test_cmp expect actual ||
111                         exit
112                 done &&
113                 git fsck --full
114         )
115 }
116
117 test_expect_success setup '
118
119         >path1 &&
120         git add path1 &&
121         test_tick &&
122         git commit -a -m repo &&
123         the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
124
125         >path2 &&
126         git add path2 &&
127         test_tick &&
128         git commit -a -m second &&
129         the_commit=$(git show-ref -s --verify refs/heads/master)
130
131 '
132
133 test_expect_success 'fetch without wildcard' '
134         mk_empty testrepo &&
135         (
136                 cd testrepo &&
137                 git fetch .. refs/heads/master:refs/remotes/origin/master &&
138
139                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
140                 git for-each-ref refs/remotes/origin >actual &&
141                 test_cmp expect actual
142         )
143 '
144
145 test_expect_success 'fetch with wildcard' '
146         mk_empty testrepo &&
147         (
148                 cd testrepo &&
149                 git config remote.up.url .. &&
150                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
151                 git fetch up &&
152
153                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
154                 git for-each-ref refs/remotes/origin >actual &&
155                 test_cmp expect actual
156         )
157 '
158
159 test_expect_success 'fetch with insteadOf' '
160         mk_empty testrepo &&
161         (
162                 TRASH=$(pwd)/ &&
163                 cd testrepo &&
164                 git config "url.$TRASH.insteadOf" trash/ &&
165                 git config remote.up.url trash/. &&
166                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
167                 git fetch up &&
168
169                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
170                 git for-each-ref refs/remotes/origin >actual &&
171                 test_cmp expect actual
172         )
173 '
174
175 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
176         mk_empty testrepo &&
177         (
178                 TRASH=$(pwd)/ &&
179                 cd testrepo &&
180                 git config "url.trash/.pushInsteadOf" "$TRASH" &&
181                 git config remote.up.url "$TRASH." &&
182                 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
183                 git fetch up &&
184
185                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
186                 git for-each-ref refs/remotes/origin >actual &&
187                 test_cmp expect actual
188         )
189 '
190
191 test_expect_success 'push without wildcard' '
192         mk_empty testrepo &&
193
194         git push testrepo refs/heads/master:refs/remotes/origin/master &&
195         (
196                 cd testrepo &&
197                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
198                 git for-each-ref refs/remotes/origin >actual &&
199                 test_cmp expect actual
200         )
201 '
202
203 test_expect_success 'push with wildcard' '
204         mk_empty testrepo &&
205
206         git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
207         (
208                 cd testrepo &&
209                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
210                 git for-each-ref refs/remotes/origin >actual &&
211                 test_cmp expect actual
212         )
213 '
214
215 test_expect_success 'push with insteadOf' '
216         mk_empty testrepo &&
217         TRASH="$(pwd)/" &&
218         test_config "url.$TRASH.insteadOf" trash/ &&
219         git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
220         (
221                 cd testrepo &&
222                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
223                 git for-each-ref refs/remotes/origin >actual &&
224                 test_cmp expect actual
225         )
226 '
227
228 test_expect_success 'push with pushInsteadOf' '
229         mk_empty testrepo &&
230         TRASH="$(pwd)/" &&
231         test_config "url.$TRASH.pushInsteadOf" trash/ &&
232         git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
233         (
234                 cd testrepo &&
235                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
236                 git for-each-ref refs/remotes/origin >actual &&
237                 test_cmp expect actual
238         )
239 '
240
241 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
242         mk_empty testrepo &&
243         test_config "url.trash2/.pushInsteadOf" testrepo/ &&
244         test_config "url.trash3/.pushInsteadOf" trash/wrong &&
245         test_config remote.r.url trash/wrong &&
246         test_config remote.r.pushurl "testrepo/" &&
247         git push r refs/heads/master:refs/remotes/origin/master &&
248         (
249                 cd testrepo &&
250                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
251                 git for-each-ref refs/remotes/origin >actual &&
252                 test_cmp expect actual
253         )
254 '
255
256 test_expect_success 'push with matching heads' '
257
258         mk_test testrepo heads/master &&
259         git push testrepo : &&
260         check_push_result testrepo $the_commit heads/master
261
262 '
263
264 test_expect_success 'push with matching heads on the command line' '
265
266         mk_test testrepo heads/master &&
267         git push testrepo : &&
268         check_push_result testrepo $the_commit heads/master
269
270 '
271
272 test_expect_success 'failed (non-fast-forward) push with matching heads' '
273
274         mk_test testrepo heads/master &&
275         git push testrepo : &&
276         git commit --amend -massaged &&
277         test_must_fail git push testrepo &&
278         check_push_result testrepo $the_commit heads/master &&
279         git reset --hard $the_commit
280
281 '
282
283 test_expect_success 'push --force with matching heads' '
284
285         mk_test testrepo heads/master &&
286         git push testrepo : &&
287         git commit --amend -massaged &&
288         git push --force testrepo : &&
289         ! check_push_result testrepo $the_commit heads/master &&
290         git reset --hard $the_commit
291
292 '
293
294 test_expect_success 'push with matching heads and forced update' '
295
296         mk_test testrepo heads/master &&
297         git push testrepo : &&
298         git commit --amend -massaged &&
299         git push testrepo +: &&
300         ! check_push_result testrepo $the_commit heads/master &&
301         git reset --hard $the_commit
302
303 '
304
305 test_expect_success 'push with no ambiguity (1)' '
306
307         mk_test testrepo heads/master &&
308         git push testrepo master:master &&
309         check_push_result testrepo $the_commit heads/master
310
311 '
312
313 test_expect_success 'push with no ambiguity (2)' '
314
315         mk_test testrepo remotes/origin/master &&
316         git push testrepo master:origin/master &&
317         check_push_result testrepo $the_commit remotes/origin/master
318
319 '
320
321 test_expect_success 'push with colon-less refspec, no ambiguity' '
322
323         mk_test testrepo heads/master heads/t/master &&
324         git branch -f t/master master &&
325         git push testrepo master &&
326         check_push_result testrepo $the_commit heads/master &&
327         check_push_result testrepo $the_first_commit heads/t/master
328
329 '
330
331 test_expect_success 'push with weak ambiguity (1)' '
332
333         mk_test testrepo heads/master remotes/origin/master &&
334         git push testrepo master:master &&
335         check_push_result testrepo $the_commit heads/master &&
336         check_push_result testrepo $the_first_commit remotes/origin/master
337
338 '
339
340 test_expect_success 'push with weak ambiguity (2)' '
341
342         mk_test testrepo heads/master remotes/origin/master remotes/another/master &&
343         git push testrepo master:master &&
344         check_push_result testrepo $the_commit heads/master &&
345         check_push_result testrepo $the_first_commit remotes/origin/master remotes/another/master
346
347 '
348
349 test_expect_success 'push with ambiguity' '
350
351         mk_test testrepo heads/frotz tags/frotz &&
352         test_must_fail git push testrepo master:frotz &&
353         check_push_result testrepo $the_first_commit heads/frotz tags/frotz
354
355 '
356
357 test_expect_success 'push with colon-less refspec (1)' '
358
359         mk_test testrepo heads/frotz tags/frotz &&
360         git branch -f frotz master &&
361         git push testrepo frotz &&
362         check_push_result testrepo $the_commit heads/frotz &&
363         check_push_result testrepo $the_first_commit tags/frotz
364
365 '
366
367 test_expect_success 'push with colon-less refspec (2)' '
368
369         mk_test testrepo heads/frotz tags/frotz &&
370         if git show-ref --verify -q refs/heads/frotz
371         then
372                 git branch -D frotz
373         fi &&
374         git tag -f frotz &&
375         git push -f testrepo frotz &&
376         check_push_result testrepo $the_commit tags/frotz &&
377         check_push_result testrepo $the_first_commit heads/frotz
378
379 '
380
381 test_expect_success 'push with colon-less refspec (3)' '
382
383         mk_test testrepo &&
384         if git show-ref --verify -q refs/tags/frotz
385         then
386                 git tag -d frotz
387         fi &&
388         git branch -f frotz master &&
389         git push testrepo frotz &&
390         check_push_result testrepo $the_commit heads/frotz &&
391         test 1 = $( cd testrepo && git show-ref | wc -l )
392 '
393
394 test_expect_success 'push with colon-less refspec (4)' '
395
396         mk_test testrepo &&
397         if git show-ref --verify -q refs/heads/frotz
398         then
399                 git branch -D frotz
400         fi &&
401         git tag -f frotz &&
402         git push testrepo frotz &&
403         check_push_result testrepo $the_commit tags/frotz &&
404         test 1 = $( cd testrepo && git show-ref | wc -l )
405
406 '
407
408 test_expect_success 'push head with non-existent, incomplete dest' '
409
410         mk_test testrepo &&
411         git push testrepo master:branch &&
412         check_push_result testrepo $the_commit heads/branch
413
414 '
415
416 test_expect_success 'push tag with non-existent, incomplete dest' '
417
418         mk_test testrepo &&
419         git tag -f v1.0 &&
420         git push testrepo v1.0:tag &&
421         check_push_result testrepo $the_commit tags/tag
422
423 '
424
425 test_expect_success 'push sha1 with non-existent, incomplete dest' '
426
427         mk_test testrepo &&
428         test_must_fail git push testrepo $(git rev-parse master):foo
429
430 '
431
432 test_expect_success 'push ref expression with non-existent, incomplete dest' '
433
434         mk_test testrepo &&
435         test_must_fail git push testrepo master^:branch
436
437 '
438
439 test_expect_success 'push with HEAD' '
440
441         mk_test testrepo heads/master &&
442         git checkout master &&
443         git push testrepo HEAD &&
444         check_push_result testrepo $the_commit heads/master
445
446 '
447
448 test_expect_success 'push with HEAD nonexisting at remote' '
449
450         mk_test testrepo heads/master &&
451         git checkout -b local master &&
452         git push testrepo HEAD &&
453         check_push_result testrepo $the_commit heads/local
454 '
455
456 test_expect_success 'push with +HEAD' '
457
458         mk_test testrepo heads/master &&
459         git checkout master &&
460         git branch -D local &&
461         git checkout -b local &&
462         git push testrepo master local &&
463         check_push_result testrepo $the_commit heads/master &&
464         check_push_result testrepo $the_commit heads/local &&
465
466         # Without force rewinding should fail
467         git reset --hard HEAD^ &&
468         test_must_fail git push testrepo HEAD &&
469         check_push_result testrepo $the_commit heads/local &&
470
471         # With force rewinding should succeed
472         git push testrepo +HEAD &&
473         check_push_result testrepo $the_first_commit heads/local
474
475 '
476
477 test_expect_success 'push HEAD with non-existent, incomplete dest' '
478
479         mk_test testrepo &&
480         git checkout master &&
481         git push testrepo HEAD:branch &&
482         check_push_result testrepo $the_commit heads/branch
483
484 '
485
486 test_expect_success 'push with config remote.*.push = HEAD' '
487
488         mk_test testrepo heads/local &&
489         git checkout master &&
490         git branch -f local $the_commit &&
491         (
492                 cd testrepo &&
493                 git checkout local &&
494                 git reset --hard $the_first_commit
495         ) &&
496         test_config remote.there.url testrepo &&
497         test_config remote.there.push HEAD &&
498         test_config branch.master.remote there &&
499         git push &&
500         check_push_result testrepo $the_commit heads/master &&
501         check_push_result testrepo $the_first_commit heads/local
502 '
503
504 test_expect_success 'push with remote.pushdefault' '
505         mk_test up_repo heads/master &&
506         mk_test down_repo heads/master &&
507         test_config remote.up.url up_repo &&
508         test_config remote.down.url down_repo &&
509         test_config branch.master.remote up &&
510         test_config remote.pushdefault down &&
511         test_config push.default matching &&
512         git push &&
513         check_push_result up_repo $the_first_commit heads/master &&
514         check_push_result down_repo $the_commit heads/master
515 '
516
517 test_expect_success 'push with config remote.*.pushurl' '
518
519         mk_test testrepo heads/master &&
520         git checkout master &&
521         test_config remote.there.url test2repo &&
522         test_config remote.there.pushurl testrepo &&
523         git push there : &&
524         check_push_result testrepo $the_commit heads/master
525 '
526
527 test_expect_success 'push with config branch.*.pushremote' '
528         mk_test up_repo heads/master &&
529         mk_test side_repo heads/master &&
530         mk_test down_repo heads/master &&
531         test_config remote.up.url up_repo &&
532         test_config remote.pushdefault side_repo &&
533         test_config remote.down.url down_repo &&
534         test_config branch.master.remote up &&
535         test_config branch.master.pushremote down &&
536         test_config push.default matching &&
537         git push &&
538         check_push_result up_repo $the_first_commit heads/master &&
539         check_push_result side_repo $the_first_commit heads/master &&
540         check_push_result down_repo $the_commit heads/master
541 '
542
543 test_expect_success 'branch.*.pushremote config order is irrelevant' '
544         mk_test one_repo heads/master &&
545         mk_test two_repo heads/master &&
546         test_config remote.one.url one_repo &&
547         test_config remote.two.url two_repo &&
548         test_config branch.master.pushremote two_repo &&
549         test_config remote.pushdefault one_repo &&
550         test_config push.default matching &&
551         git push &&
552         check_push_result one_repo $the_first_commit heads/master &&
553         check_push_result two_repo $the_commit heads/master
554 '
555
556 test_expect_success 'push with dry-run' '
557
558         mk_test testrepo heads/master &&
559         old_commit=$(git -C testrepo show-ref -s --verify refs/heads/master) &&
560         git push --dry-run testrepo : &&
561         check_push_result testrepo $old_commit heads/master
562 '
563
564 test_expect_success 'push updates local refs' '
565
566         mk_test testrepo heads/master &&
567         mk_child testrepo child &&
568         (
569                 cd child &&
570                 git pull .. 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 updates up-to-date local refs' '
579
580         mk_test testrepo heads/master &&
581         mk_child testrepo child1 &&
582         mk_child testrepo child2 &&
583         (cd child1 && git pull .. master && git push) &&
584         (
585                 cd child2 &&
586                 git pull ../child1 master &&
587                 git push &&
588                 test $(git rev-parse master) = \
589                         $(git rev-parse remotes/origin/master)
590         )
591
592 '
593
594 test_expect_success 'push preserves up-to-date packed refs' '
595
596         mk_test testrepo heads/master &&
597         mk_child testrepo child &&
598         (
599                 cd child &&
600                 git push &&
601                 ! test -f .git/refs/remotes/origin/master
602         )
603
604 '
605
606 test_expect_success 'push does not update local refs on failure' '
607
608         mk_test testrepo heads/master &&
609         mk_child testrepo child &&
610         mkdir testrepo/.git/hooks &&
611         echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
612         chmod +x testrepo/.git/hooks/pre-receive &&
613         (
614                 cd child &&
615                 git pull .. master &&
616                 test_must_fail git push &&
617                 test $(git rev-parse master) != \
618                         $(git rev-parse remotes/origin/master)
619         )
620
621 '
622
623 test_expect_success 'allow deleting an invalid remote ref' '
624
625         mk_test testrepo heads/master &&
626         rm -f testrepo/.git/objects/??/* &&
627         git push testrepo :refs/heads/master &&
628         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
629
630 '
631
632 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
633         mk_test_with_hooks testrepo heads/master heads/next &&
634         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
635         newmaster=$(git show-ref -s --verify refs/heads/master) &&
636         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
637         newnext=$ZERO_OID &&
638         git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
639         (
640                 cd testrepo/.git &&
641                 cat >pre-receive.expect <<-EOF &&
642                 $orgmaster $newmaster refs/heads/master
643                 $orgnext $newnext refs/heads/next
644                 EOF
645
646                 cat >update.expect <<-EOF &&
647                 refs/heads/master $orgmaster $newmaster
648                 refs/heads/next $orgnext $newnext
649                 EOF
650
651                 cat >post-receive.expect <<-EOF &&
652                 $orgmaster $newmaster refs/heads/master
653                 $orgnext $newnext refs/heads/next
654                 EOF
655
656                 cat >post-update.expect <<-EOF &&
657                 refs/heads/master
658                 refs/heads/next
659                 EOF
660
661                 test_cmp pre-receive.expect pre-receive.actual &&
662                 test_cmp update.expect update.actual &&
663                 test_cmp post-receive.expect post-receive.actual &&
664                 test_cmp post-update.expect post-update.actual
665         )
666 '
667
668 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
669         mk_test_with_hooks testrepo heads/master &&
670         rm -f testrepo/.git/objects/??/* &&
671         git push testrepo :refs/heads/master &&
672         (
673                 cd testrepo/.git &&
674                 cat >pre-receive.expect <<-EOF &&
675                 $ZERO_OID $ZERO_OID refs/heads/master
676                 EOF
677
678                 cat >update.expect <<-EOF &&
679                 refs/heads/master $ZERO_OID $ZERO_OID
680                 EOF
681
682                 cat >post-receive.expect <<-EOF &&
683                 $ZERO_OID $ZERO_OID refs/heads/master
684                 EOF
685
686                 cat >post-update.expect <<-EOF &&
687                 refs/heads/master
688                 EOF
689
690                 test_cmp pre-receive.expect pre-receive.actual &&
691                 test_cmp update.expect update.actual &&
692                 test_cmp post-receive.expect post-receive.actual &&
693                 test_cmp post-update.expect post-update.actual
694         )
695 '
696
697 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
698         mk_test_with_hooks testrepo heads/master &&
699         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
700         newmaster=$(git show-ref -s --verify refs/heads/master) &&
701         git push testrepo master :refs/heads/nonexistent &&
702         (
703                 cd testrepo/.git &&
704                 cat >pre-receive.expect <<-EOF &&
705                 $orgmaster $newmaster refs/heads/master
706                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
707                 EOF
708
709                 cat >update.expect <<-EOF &&
710                 refs/heads/master $orgmaster $newmaster
711                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
712                 EOF
713
714                 cat >post-receive.expect <<-EOF &&
715                 $orgmaster $newmaster refs/heads/master
716                 EOF
717
718                 cat >post-update.expect <<-EOF &&
719                 refs/heads/master
720                 EOF
721
722                 test_cmp pre-receive.expect pre-receive.actual &&
723                 test_cmp update.expect update.actual &&
724                 test_cmp post-receive.expect post-receive.actual &&
725                 test_cmp post-update.expect post-update.actual
726         )
727 '
728
729 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
730         mk_test_with_hooks testrepo heads/master &&
731         git push testrepo :refs/heads/nonexistent &&
732         (
733                 cd testrepo/.git &&
734                 cat >pre-receive.expect <<-EOF &&
735                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
736                 EOF
737
738                 cat >update.expect <<-EOF &&
739                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
740                 EOF
741
742                 test_cmp pre-receive.expect pre-receive.actual &&
743                 test_cmp update.expect update.actual &&
744                 test_path_is_missing post-receive.actual &&
745                 test_path_is_missing post-update.actual
746         )
747 '
748
749 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
750         mk_test_with_hooks testrepo heads/master heads/next heads/seen &&
751         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
752         newmaster=$(git show-ref -s --verify refs/heads/master) &&
753         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
754         newnext=$ZERO_OID &&
755         orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
756         newseen=$(git show-ref -s --verify refs/heads/master) &&
757         git push testrepo refs/heads/master:refs/heads/master \
758             refs/heads/master:refs/heads/seen :refs/heads/next \
759             :refs/heads/nonexistent &&
760         (
761                 cd testrepo/.git &&
762                 cat >pre-receive.expect <<-EOF &&
763                 $orgmaster $newmaster refs/heads/master
764                 $orgnext $newnext refs/heads/next
765                 $orgseen $newseen refs/heads/seen
766                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
767                 EOF
768
769                 cat >update.expect <<-EOF &&
770                 refs/heads/master $orgmaster $newmaster
771                 refs/heads/next $orgnext $newnext
772                 refs/heads/seen $orgseen $newseen
773                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
774                 EOF
775
776                 cat >post-receive.expect <<-EOF &&
777                 $orgmaster $newmaster refs/heads/master
778                 $orgnext $newnext refs/heads/next
779                 $orgseen $newseen refs/heads/seen
780                 EOF
781
782                 cat >post-update.expect <<-EOF &&
783                 refs/heads/master
784                 refs/heads/next
785                 refs/heads/seen
786                 EOF
787
788                 test_cmp pre-receive.expect pre-receive.actual &&
789                 test_cmp update.expect update.actual &&
790                 test_cmp post-receive.expect post-receive.actual &&
791                 test_cmp post-update.expect post-update.actual
792         )
793 '
794
795 test_expect_success 'allow deleting a ref using --delete' '
796         mk_test testrepo heads/master &&
797         (cd testrepo && git config receive.denyDeleteCurrent warn) &&
798         git push testrepo --delete master &&
799         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
800 '
801
802 test_expect_success 'allow deleting a tag using --delete' '
803         mk_test testrepo heads/master &&
804         git tag -a -m dummy_message deltag heads/master &&
805         git push testrepo --tags &&
806         (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
807         git push testrepo --delete tag deltag &&
808         (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
809 '
810
811 test_expect_success 'push --delete without args aborts' '
812         mk_test testrepo heads/master &&
813         test_must_fail git push testrepo --delete
814 '
815
816 test_expect_success 'push --delete refuses src:dest refspecs' '
817         mk_test testrepo heads/master &&
818         test_must_fail git push testrepo --delete master:foo
819 '
820
821 test_expect_success 'push --delete refuses empty string' '
822         mk_test testrepo heads/master &&
823         test_must_fail git push testrepo --delete ""
824 '
825
826 test_expect_success 'warn on push to HEAD of non-bare repository' '
827         mk_test testrepo heads/master &&
828         (
829                 cd testrepo &&
830                 git checkout master &&
831                 git config receive.denyCurrentBranch warn
832         ) &&
833         git push testrepo master 2>stderr &&
834         grep "warning: updating the current branch" stderr
835 '
836
837 test_expect_success 'deny push to HEAD of non-bare repository' '
838         mk_test testrepo heads/master &&
839         (
840                 cd testrepo &&
841                 git checkout master &&
842                 git config receive.denyCurrentBranch true
843         ) &&
844         test_must_fail git push testrepo master
845 '
846
847 test_expect_success 'allow push to HEAD of bare repository (bare)' '
848         mk_test testrepo heads/master &&
849         (
850                 cd testrepo &&
851                 git checkout master &&
852                 git config receive.denyCurrentBranch true &&
853                 git config core.bare true
854         ) &&
855         git push testrepo master 2>stderr &&
856         ! grep "warning: updating the current branch" stderr
857 '
858
859 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
860         mk_test testrepo heads/master &&
861         (
862                 cd testrepo &&
863                 git checkout master &&
864                 git config receive.denyCurrentBranch false
865         ) &&
866         git push testrepo master 2>stderr &&
867         ! grep "warning: updating the current branch" stderr
868 '
869
870 test_expect_success 'fetch with branches' '
871         mk_empty testrepo &&
872         git branch second $the_first_commit &&
873         git checkout second &&
874         echo ".." > testrepo/.git/branches/branch1 &&
875         (
876                 cd testrepo &&
877                 git fetch branch1 &&
878                 echo "$the_commit commit        refs/heads/branch1" >expect &&
879                 git for-each-ref refs/heads >actual &&
880                 test_cmp expect actual
881         ) &&
882         git checkout master
883 '
884
885 test_expect_success 'fetch with branches containing #' '
886         mk_empty testrepo &&
887         echo "..#second" > testrepo/.git/branches/branch2 &&
888         (
889                 cd testrepo &&
890                 git fetch branch2 &&
891                 echo "$the_first_commit commit  refs/heads/branch2" >expect &&
892                 git for-each-ref refs/heads >actual &&
893                 test_cmp expect actual
894         ) &&
895         git checkout master
896 '
897
898 test_expect_success 'push with branches' '
899         mk_empty testrepo &&
900         git checkout second &&
901         echo "testrepo" > .git/branches/branch1 &&
902         git push branch1 &&
903         (
904                 cd testrepo &&
905                 echo "$the_first_commit commit  refs/heads/master" >expect &&
906                 git for-each-ref refs/heads >actual &&
907                 test_cmp expect actual
908         )
909 '
910
911 test_expect_success 'push with branches containing #' '
912         mk_empty testrepo &&
913         echo "testrepo#branch3" > .git/branches/branch2 &&
914         git push branch2 &&
915         (
916                 cd testrepo &&
917                 echo "$the_first_commit commit  refs/heads/branch3" >expect &&
918                 git for-each-ref refs/heads >actual &&
919                 test_cmp expect actual
920         ) &&
921         git checkout master
922 '
923
924 test_expect_success 'push into aliased refs (consistent)' '
925         mk_test testrepo heads/master &&
926         mk_child testrepo child1 &&
927         mk_child testrepo child2 &&
928         (
929                 cd child1 &&
930                 git branch foo &&
931                 git symbolic-ref refs/heads/bar refs/heads/foo &&
932                 git config receive.denyCurrentBranch false
933         ) &&
934         (
935                 cd child2 &&
936                 >path2 &&
937                 git add path2 &&
938                 test_tick &&
939                 git commit -a -m child2 &&
940                 git branch foo &&
941                 git branch bar &&
942                 git push ../child1 foo bar
943         )
944 '
945
946 test_expect_success 'push into aliased refs (inconsistent)' '
947         mk_test testrepo heads/master &&
948         mk_child testrepo child1 &&
949         mk_child testrepo child2 &&
950         (
951                 cd child1 &&
952                 git branch foo &&
953                 git symbolic-ref refs/heads/bar refs/heads/foo &&
954                 git config receive.denyCurrentBranch false
955         ) &&
956         (
957                 cd child2 &&
958                 >path2 &&
959                 git add path2 &&
960                 test_tick &&
961                 git commit -a -m child2 &&
962                 git branch foo &&
963                 >path3 &&
964                 git add path3 &&
965                 test_tick &&
966                 git commit -a -m child2 &&
967                 git branch bar &&
968                 test_must_fail git push ../child1 foo bar 2>stderr &&
969                 grep "refusing inconsistent update" stderr
970         )
971 '
972
973 test_force_push_tag () {
974         tag_type_description=$1
975         tag_args=$2
976
977         test_expect_success "force pushing required to update $tag_type_description" "
978                 mk_test testrepo heads/master &&
979                 mk_child testrepo child1 &&
980                 mk_child testrepo child2 &&
981                 (
982                         cd child1 &&
983                         git tag testTag &&
984                         git push ../child2 testTag &&
985                         >file1 &&
986                         git add file1 &&
987                         git commit -m 'file1' &&
988                         git tag $tag_args testTag &&
989                         test_must_fail git push ../child2 testTag &&
990                         git push --force ../child2 testTag &&
991                         git tag $tag_args testTag HEAD~ &&
992                         test_must_fail git push ../child2 testTag &&
993                         git push --force ../child2 testTag &&
994
995                         # Clobbering without + in refspec needs --force
996                         git tag -f testTag &&
997                         test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
998                         git push --force ../child2 'refs/tags/*:refs/tags/*' &&
999
1000                         # Clobbering with + in refspec does not need --force
1001                         git tag -f testTag HEAD~ &&
1002                         git push ../child2 '+refs/tags/*:refs/tags/*' &&
1003
1004                         # Clobbering with --no-force still obeys + in refspec
1005                         git tag -f testTag &&
1006                         git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1007
1008                         # Clobbering with/without --force and 'tag <name>' format
1009                         git tag -f testTag HEAD~ &&
1010                         test_must_fail git push ../child2 tag testTag &&
1011                         git push --force ../child2 tag testTag
1012                 )
1013         "
1014 }
1015
1016 test_force_push_tag "lightweight tag" "-f"
1017 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1018
1019 test_force_fetch_tag () {
1020         tag_type_description=$1
1021         tag_args=$2
1022
1023         test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1024                 mk_test testrepo heads/master &&
1025                 mk_child testrepo child1 &&
1026                 mk_child testrepo child2 &&
1027                 (
1028                         cd testrepo &&
1029                         git tag testTag &&
1030                         git -C ../child1 fetch origin tag testTag &&
1031                         >file1 &&
1032                         git add file1 &&
1033                         git commit -m 'file1' &&
1034                         git tag $tag_args testTag &&
1035                         test_must_fail git -C ../child1 fetch origin tag testTag &&
1036                         git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1037                 )
1038         "
1039 }
1040
1041 test_force_fetch_tag "lightweight tag" "-f"
1042 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1043
1044 test_expect_success 'push --porcelain' '
1045         mk_empty testrepo &&
1046         echo >.git/foo  "To testrepo" &&
1047         echo >>.git/foo "*      refs/heads/master:refs/remotes/origin/master    [new reference]"  &&
1048         echo >>.git/foo "Done" &&
1049         git push >.git/bar --porcelain  testrepo refs/heads/master:refs/remotes/origin/master &&
1050         (
1051                 cd testrepo &&
1052                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
1053                 git for-each-ref refs/remotes/origin >actual &&
1054                 test_cmp expect actual
1055         ) &&
1056         test_cmp .git/foo .git/bar
1057 '
1058
1059 test_expect_success 'push --porcelain bad url' '
1060         mk_empty testrepo &&
1061         test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
1062         ! grep -q Done .git/bar
1063 '
1064
1065 test_expect_success 'push --porcelain rejected' '
1066         mk_empty testrepo &&
1067         git push testrepo refs/heads/master:refs/remotes/origin/master &&
1068         (cd testrepo &&
1069                 git reset --hard origin/master^ &&
1070                 git config receive.denyCurrentBranch true) &&
1071
1072         echo >.git/foo  "To testrepo"  &&
1073         echo >>.git/foo "!      refs/heads/master:refs/heads/master     [remote rejected] (branch is currently checked out)" &&
1074         echo >>.git/foo "Done" &&
1075
1076         test_must_fail git push >.git/bar --porcelain  testrepo refs/heads/master:refs/heads/master &&
1077         test_cmp .git/foo .git/bar
1078 '
1079
1080 test_expect_success 'push --porcelain --dry-run rejected' '
1081         mk_empty testrepo &&
1082         git push testrepo refs/heads/master:refs/remotes/origin/master &&
1083         (cd testrepo &&
1084                 git reset --hard origin/master &&
1085                 git config receive.denyCurrentBranch true) &&
1086
1087         echo >.git/foo  "To testrepo"  &&
1088         echo >>.git/foo "!      refs/heads/master^:refs/heads/master    [rejected] (non-fast-forward)" &&
1089         echo >>.git/foo "Done" &&
1090
1091         test_must_fail git push >.git/bar --porcelain  --dry-run testrepo refs/heads/master^:refs/heads/master &&
1092         test_cmp .git/foo .git/bar
1093 '
1094
1095 test_expect_success 'push --prune' '
1096         mk_test testrepo heads/master heads/second heads/foo heads/bar &&
1097         git push --prune testrepo : &&
1098         check_push_result testrepo $the_commit heads/master &&
1099         check_push_result testrepo $the_first_commit heads/second &&
1100         ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1101 '
1102
1103 test_expect_success 'push --prune refspec' '
1104         mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
1105         git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1106         check_push_result testrepo $the_commit tmp/master &&
1107         check_push_result testrepo $the_first_commit tmp/second &&
1108         ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1109 '
1110
1111 for configsection in transfer receive
1112 do
1113         test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1114                 mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
1115                 (
1116                         cd testrepo &&
1117                         git config $configsection.hiderefs refs/hidden
1118                 ) &&
1119
1120                 # push to unhidden ref succeeds normally
1121                 git push testrepo master:refs/heads/master &&
1122                 check_push_result testrepo $the_commit heads/master &&
1123
1124                 # push to update a hidden ref should fail
1125                 test_must_fail git push testrepo master:refs/hidden/one &&
1126                 check_push_result testrepo $the_first_commit hidden/one &&
1127
1128                 # push to delete a hidden ref should fail
1129                 test_must_fail git push testrepo :refs/hidden/two &&
1130                 check_push_result testrepo $the_first_commit hidden/two &&
1131
1132                 # idempotent push to update a hidden ref should fail
1133                 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1134                 check_push_result testrepo $the_first_commit hidden/three
1135         '
1136 done
1137
1138 test_expect_success 'fetch exact SHA1' '
1139         mk_test testrepo heads/master hidden/one &&
1140         git push testrepo master:refs/hidden/one &&
1141         (
1142                 cd testrepo &&
1143                 git config transfer.hiderefs refs/hidden
1144         ) &&
1145         check_push_result testrepo $the_commit hidden/one &&
1146
1147         mk_child testrepo child &&
1148         (
1149                 cd child &&
1150
1151                 # make sure $the_commit does not exist here
1152                 git repack -a -d &&
1153                 git prune &&
1154                 test_must_fail git cat-file -t $the_commit &&
1155
1156                 # Some protocol versions (e.g. 2) support fetching
1157                 # unadvertised objects, so restrict this test to v0.
1158
1159                 # fetching the hidden object should fail by default
1160                 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1161                         git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1162                 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1163                 test_must_fail git rev-parse --verify refs/heads/copy &&
1164
1165                 # the server side can allow it to succeed
1166                 (
1167                         cd ../testrepo &&
1168                         git config uploadpack.allowtipsha1inwant true
1169                 ) &&
1170
1171                 git fetch -v ../testrepo $the_commit:refs/heads/copy master:refs/heads/extra &&
1172                 cat >expect <<-EOF &&
1173                 $the_commit
1174                 $the_first_commit
1175                 EOF
1176                 {
1177                         git rev-parse --verify refs/heads/copy &&
1178                         git rev-parse --verify refs/heads/extra
1179                 } >actual &&
1180                 test_cmp expect actual
1181         )
1182 '
1183
1184 test_expect_success 'fetch exact SHA1 in protocol v2' '
1185         mk_test testrepo heads/master hidden/one &&
1186         git push testrepo master:refs/hidden/one &&
1187         git -C testrepo config transfer.hiderefs refs/hidden &&
1188         check_push_result testrepo $the_commit hidden/one &&
1189
1190         mk_child testrepo child &&
1191         git -C child config protocol.version 2 &&
1192
1193         # make sure $the_commit does not exist here
1194         git -C child repack -a -d &&
1195         git -C child prune &&
1196         test_must_fail git -C child cat-file -t $the_commit &&
1197
1198         # fetching the hidden object succeeds by default
1199         # NEEDSWORK: should this match the v0 behavior instead?
1200         git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1201 '
1202
1203 for configallowtipsha1inwant in true false
1204 do
1205         test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1206                 mk_empty testrepo &&
1207                 (
1208                         cd testrepo &&
1209                         git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1210                         git commit --allow-empty -m foo &&
1211                         git commit --allow-empty -m bar
1212                 ) &&
1213                 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1214                 mk_empty shallow &&
1215                 (
1216                         cd shallow &&
1217                         # Some protocol versions (e.g. 2) support fetching
1218                         # unadvertised objects, so restrict this test to v0.
1219                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1220                                 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1221                         git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1222                         git fetch --depth=1 ../testrepo/.git $SHA1 &&
1223                         git cat-file commit $SHA1
1224                 )
1225         '
1226
1227         test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1228                 mk_empty testrepo &&
1229                 (
1230                         cd testrepo &&
1231                         git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1232                         git commit --allow-empty -m foo &&
1233                         git commit --allow-empty -m bar &&
1234                         git commit --allow-empty -m xyz
1235                 ) &&
1236                 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1237                 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1238                 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1239                 (
1240                         cd testrepo &&
1241                         git reset --hard $SHA1_2 &&
1242                         git cat-file commit $SHA1_1 &&
1243                         git cat-file commit $SHA1_3
1244                 ) &&
1245                 mk_empty shallow &&
1246                 (
1247                         cd shallow &&
1248                         # Some protocol versions (e.g. 2) support fetching
1249                         # unadvertised objects, so restrict this test to v0.
1250                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1251                                 git fetch ../testrepo/.git $SHA1_3 &&
1252                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1253                                 git fetch ../testrepo/.git $SHA1_1 &&
1254                         git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1255                         git fetch ../testrepo/.git $SHA1_1 &&
1256                         git cat-file commit $SHA1_1 &&
1257                         test_must_fail git cat-file commit $SHA1_2 &&
1258                         git fetch ../testrepo/.git $SHA1_2 &&
1259                         git cat-file commit $SHA1_2 &&
1260                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1261                                 git fetch ../testrepo/.git $SHA1_3 2>err &&
1262                         test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
1263                 )
1264         '
1265 done
1266
1267 test_expect_success 'fetch follows tags by default' '
1268         mk_test testrepo heads/master &&
1269         rm -fr src dst &&
1270         git init src &&
1271         (
1272                 cd src &&
1273                 git pull ../testrepo master &&
1274                 git tag -m "annotated" tag &&
1275                 git for-each-ref >tmp1 &&
1276                 (
1277                         cat tmp1
1278                         sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1279                 ) |
1280                 sort -k 3 >../expect
1281         ) &&
1282         git init dst &&
1283         (
1284                 cd dst &&
1285                 git remote add origin ../src &&
1286                 git config branch.master.remote origin &&
1287                 git config branch.master.merge refs/heads/master &&
1288                 git pull &&
1289                 git for-each-ref >../actual
1290         ) &&
1291         test_cmp expect actual
1292 '
1293
1294 test_expect_success 'peeled advertisements are not considered ref tips' '
1295         mk_empty testrepo &&
1296         git -C testrepo commit --allow-empty -m one &&
1297         git -C testrepo commit --allow-empty -m two &&
1298         git -C testrepo tag -m foo mytag HEAD^ &&
1299         oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1300         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1301                 git fetch testrepo $oid 2>err &&
1302         test_i18ngrep "Server does not allow request for unadvertised object" err
1303 '
1304
1305 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1306         mk_test testrepo heads/master &&
1307         rm -fr src dst &&
1308         git init src &&
1309         git init --bare dst &&
1310         (
1311                 cd src &&
1312                 git pull ../testrepo master &&
1313                 git branch next &&
1314                 git config remote.dst.url ../dst &&
1315                 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1316                 git push dst master &&
1317                 git show-ref refs/heads/master |
1318                 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1319         ) &&
1320         (
1321                 cd dst &&
1322                 test_must_fail git show-ref refs/heads/next &&
1323                 test_must_fail git show-ref refs/heads/master &&
1324                 git show-ref refs/remotes/src/master >actual
1325         ) &&
1326         test_cmp dst/expect dst/actual
1327 '
1328
1329 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1330         mk_test testrepo heads/master &&
1331         rm -fr src dst &&
1332         git init src &&
1333         git init --bare dst &&
1334         (
1335                 cd src &&
1336                 git pull ../testrepo master &&
1337                 git branch next &&
1338                 git config remote.dst.url ../dst &&
1339                 git config push.default matching &&
1340                 git push dst master &&
1341                 git show-ref refs/heads/master >../dst/expect
1342         ) &&
1343         (
1344                 cd dst &&
1345                 test_must_fail git show-ref refs/heads/next &&
1346                 git show-ref refs/heads/master >actual
1347         ) &&
1348         test_cmp dst/expect dst/actual
1349 '
1350
1351 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1352         mk_test testrepo heads/master &&
1353         rm -fr src dst &&
1354         git init src &&
1355         git init --bare dst &&
1356         (
1357                 cd src &&
1358                 git pull ../testrepo master &&
1359                 git branch next &&
1360                 git config remote.dst.url ../dst &&
1361                 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1362                 git config push.default upstream &&
1363
1364                 git config branch.master.merge refs/heads/trunk &&
1365                 git config branch.master.remote dst &&
1366
1367                 git push dst master &&
1368                 git show-ref refs/heads/master |
1369                 sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1370         ) &&
1371         (
1372                 cd dst &&
1373                 test_must_fail git show-ref refs/heads/master &&
1374                 test_must_fail git show-ref refs/heads/next &&
1375                 git show-ref refs/heads/trunk >actual
1376         ) &&
1377         test_cmp dst/expect dst/actual
1378 '
1379
1380 test_expect_success 'push does not follow tags by default' '
1381         mk_test testrepo heads/master &&
1382         rm -fr src dst &&
1383         git init src &&
1384         git init --bare dst &&
1385         (
1386                 cd src &&
1387                 git pull ../testrepo master &&
1388                 git tag -m "annotated" tag &&
1389                 git checkout -b another &&
1390                 git commit --allow-empty -m "future commit" &&
1391                 git tag -m "future" future &&
1392                 git checkout master &&
1393                 git for-each-ref refs/heads/master >../expect &&
1394                 git push ../dst master
1395         ) &&
1396         (
1397                 cd dst &&
1398                 git for-each-ref >../actual
1399         ) &&
1400         test_cmp expect actual
1401 '
1402
1403 test_expect_success 'push --follow-tags only pushes relevant tags' '
1404         mk_test testrepo heads/master &&
1405         rm -fr src dst &&
1406         git init src &&
1407         git init --bare dst &&
1408         (
1409                 cd src &&
1410                 git pull ../testrepo master &&
1411                 git tag -m "annotated" tag &&
1412                 git checkout -b another &&
1413                 git commit --allow-empty -m "future commit" &&
1414                 git tag -m "future" future &&
1415                 git checkout master &&
1416                 git for-each-ref refs/heads/master refs/tags/tag >../expect &&
1417                 git push --follow-tags ../dst master
1418         ) &&
1419         (
1420                 cd dst &&
1421                 git for-each-ref >../actual
1422         ) &&
1423         test_cmp expect actual
1424 '
1425
1426 test_expect_success 'push --no-thin must produce non-thin pack' '
1427         cat >>path1 <<\EOF &&
1428 keep base version of path1 big enough, compared to the new changes
1429 later, in order to pass size heuristics in
1430 builtin/pack-objects.c:try_delta()
1431 EOF
1432         git commit -am initial &&
1433         git init no-thin &&
1434         git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1435         git push no-thin/.git refs/heads/master:refs/heads/foo &&
1436         echo modified >> path1 &&
1437         git commit -am modified &&
1438         git repack -adf &&
1439         rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1440         git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/master:refs/heads/foo
1441 '
1442
1443 test_expect_success 'pushing a tag pushes the tagged object' '
1444         rm -rf dst.git &&
1445         blob=$(echo unreferenced | git hash-object -w --stdin) &&
1446         git tag -m foo tag-of-blob $blob &&
1447         git init --bare dst.git &&
1448         git push dst.git tag-of-blob &&
1449         # the receiving index-pack should have noticed
1450         # any problems, but we double check
1451         echo unreferenced >expect &&
1452         git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1453         test_cmp expect actual
1454 '
1455
1456 test_expect_success 'push into bare respects core.logallrefupdates' '
1457         rm -rf dst.git &&
1458         git init --bare dst.git &&
1459         git -C dst.git config core.logallrefupdates true &&
1460
1461         # double push to test both with and without
1462         # the actual pack transfer
1463         git push dst.git master:one &&
1464         echo "one@{0} push" >expect &&
1465         git -C dst.git log -g --format="%gd %gs" one >actual &&
1466         test_cmp expect actual &&
1467
1468         git push dst.git master:two &&
1469         echo "two@{0} push" >expect &&
1470         git -C dst.git log -g --format="%gd %gs" two >actual &&
1471         test_cmp expect actual
1472 '
1473
1474 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1475         rm -rf dst.git &&
1476         git init --bare dst.git &&
1477         (
1478                 cd dst.git &&
1479                 git config core.logallrefupdates true &&
1480
1481                 # as above, we double-fetch to test both
1482                 # with and without pack transfer
1483                 git fetch .. master:one &&
1484                 echo "one@{0} fetch .. master:one: storing head" >expect &&
1485                 git log -g --format="%gd %gs" one >actual &&
1486                 test_cmp expect actual &&
1487
1488                 git fetch .. master:two &&
1489                 echo "two@{0} fetch .. master:two: storing head" >expect &&
1490                 git log -g --format="%gd %gs" two >actual &&
1491                 test_cmp expect actual
1492         )
1493 '
1494
1495 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1496         git push testrepo master &&
1497         (
1498                 cd testrepo &&
1499                 git reset --hard &&
1500                 git config receive.denyCurrentBranch updateInstead
1501         ) &&
1502         test_commit third path2 &&
1503
1504         # Try pushing into a repository with pristine working tree
1505         git push testrepo master &&
1506         (
1507                 cd testrepo &&
1508                 git update-index -q --refresh &&
1509                 git diff-files --quiet -- &&
1510                 git diff-index --quiet --cached HEAD -- &&
1511                 test third = "$(cat path2)" &&
1512                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1513         ) &&
1514
1515         # Try pushing into a repository with working tree needing a refresh
1516         (
1517                 cd testrepo &&
1518                 git reset --hard HEAD^ &&
1519                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1520                 test-tool chmtime +100 path1
1521         ) &&
1522         git push testrepo master &&
1523         (
1524                 cd testrepo &&
1525                 git update-index -q --refresh &&
1526                 git diff-files --quiet -- &&
1527                 git diff-index --quiet --cached HEAD -- &&
1528                 test_cmp ../path1 path1 &&
1529                 test third = "$(cat path2)" &&
1530                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1531         ) &&
1532
1533         # Update what is to be pushed
1534         test_commit fourth path2 &&
1535
1536         # Try pushing into a repository with a dirty working tree
1537         # (1) the working tree updated
1538         (
1539                 cd testrepo &&
1540                 echo changed >path1
1541         ) &&
1542         test_must_fail git push testrepo master &&
1543         (
1544                 cd testrepo &&
1545                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1546                 git diff --quiet --cached &&
1547                 test changed = "$(cat path1)"
1548         ) &&
1549
1550         # (2) the index updated
1551         (
1552                 cd testrepo &&
1553                 echo changed >path1 &&
1554                 git add path1
1555         ) &&
1556         test_must_fail git push testrepo master &&
1557         (
1558                 cd testrepo &&
1559                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1560                 git diff --quiet &&
1561                 test changed = "$(cat path1)"
1562         ) &&
1563
1564         # Introduce a new file in the update
1565         test_commit fifth path3 &&
1566
1567         # (3) the working tree has an untracked file that would interfere
1568         (
1569                 cd testrepo &&
1570                 git reset --hard &&
1571                 echo changed >path3
1572         ) &&
1573         test_must_fail git push testrepo master &&
1574         (
1575                 cd testrepo &&
1576                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1577                 git diff --quiet &&
1578                 git diff --quiet --cached &&
1579                 test changed = "$(cat path3)"
1580         ) &&
1581
1582         # (4) the target changes to what gets pushed but it still is a change
1583         (
1584                 cd testrepo &&
1585                 git reset --hard &&
1586                 echo fifth >path3 &&
1587                 git add path3
1588         ) &&
1589         test_must_fail git push testrepo master &&
1590         (
1591                 cd testrepo &&
1592                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1593                 git diff --quiet &&
1594                 test fifth = "$(cat path3)"
1595         ) &&
1596
1597         # (5) push into void
1598         rm -fr void &&
1599         git init void &&
1600         (
1601                 cd void &&
1602                 git config receive.denyCurrentBranch updateInstead
1603         ) &&
1604         git push void master &&
1605         (
1606                 cd void &&
1607                 test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1608                 git diff --quiet &&
1609                 git diff --cached --quiet
1610         ) &&
1611
1612         # (6) updateInstead intervened by fast-forward check
1613         test_must_fail git push void master^:master &&
1614         test $(git -C void rev-parse HEAD) = $(git rev-parse master) &&
1615         git -C void diff --quiet &&
1616         git -C void diff --cached --quiet
1617 '
1618
1619 test_expect_success 'updateInstead with push-to-checkout hook' '
1620         rm -fr testrepo &&
1621         git init testrepo &&
1622         (
1623                 cd testrepo &&
1624                 git pull .. master &&
1625                 git reset --hard HEAD^^ &&
1626                 git tag initial &&
1627                 git config receive.denyCurrentBranch updateInstead &&
1628                 write_script .git/hooks/push-to-checkout <<-\EOF
1629                 echo >&2 updating from $(git rev-parse HEAD)
1630                 echo >&2 updating to "$1"
1631
1632                 git update-index -q --refresh &&
1633                 git read-tree -u -m HEAD "$1" || {
1634                         status=$?
1635                         echo >&2 read-tree failed
1636                         exit $status
1637                 }
1638                 EOF
1639         ) &&
1640
1641         # Try pushing into a pristine
1642         git push testrepo master &&
1643         (
1644                 cd testrepo &&
1645                 git diff --quiet &&
1646                 git diff HEAD --quiet &&
1647                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1648         ) &&
1649
1650         # Try pushing into a repository with conflicting change
1651         (
1652                 cd testrepo &&
1653                 git reset --hard initial &&
1654                 echo conflicting >path2
1655         ) &&
1656         test_must_fail git push testrepo master &&
1657         (
1658                 cd testrepo &&
1659                 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1660                 test conflicting = "$(cat path2)" &&
1661                 git diff-index --quiet --cached HEAD
1662         ) &&
1663
1664         # Try pushing into a repository with unrelated change
1665         (
1666                 cd testrepo &&
1667                 git reset --hard initial &&
1668                 echo unrelated >path1 &&
1669                 echo irrelevant >path5 &&
1670                 git add path5
1671         ) &&
1672         git push testrepo master &&
1673         (
1674                 cd testrepo &&
1675                 test "$(cat path1)" = unrelated &&
1676                 test "$(cat path5)" = irrelevant &&
1677                 test "$(git diff --name-only --cached HEAD)" = path5 &&
1678                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1679         ) &&
1680
1681         # push into void
1682         rm -fr void &&
1683         git init void &&
1684         (
1685                 cd void &&
1686                 git config receive.denyCurrentBranch updateInstead &&
1687                 write_script .git/hooks/push-to-checkout <<-\EOF
1688                 if git rev-parse --quiet --verify HEAD
1689                 then
1690                         has_head=yes
1691                         echo >&2 updating from $(git rev-parse HEAD)
1692                 else
1693                         has_head=no
1694                         echo >&2 pushing into void
1695                 fi
1696                 echo >&2 updating to "$1"
1697
1698                 git update-index -q --refresh &&
1699                 case "$has_head" in
1700                 yes)
1701                         git read-tree -u -m HEAD "$1" ;;
1702                 no)
1703                         git read-tree -u -m "$1" ;;
1704                 esac || {
1705                         status=$?
1706                         echo >&2 read-tree failed
1707                         exit $status
1708                 }
1709                 EOF
1710         ) &&
1711
1712         git push void master &&
1713         (
1714                 cd void &&
1715                 git diff --quiet &&
1716                 git diff --cached --quiet &&
1717                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1718         )
1719 '
1720
1721 test_expect_success 'denyCurrentBranch and worktrees' '
1722         git worktree add new-wt &&
1723         git clone . cloned &&
1724         test_commit -C cloned first &&
1725         test_config receive.denyCurrentBranch refuse &&
1726         test_must_fail git -C cloned push origin HEAD:new-wt &&
1727         test_config receive.denyCurrentBranch updateInstead &&
1728         git -C cloned push origin HEAD:new-wt &&
1729         test_must_fail git -C cloned push --delete origin new-wt
1730 '
1731
1732 test_done