tests: push: improve cleanup of HEAD tests
[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         test_when_finished "git checkout master; git branch -D local" &&
453         git push testrepo HEAD &&
454         check_push_result testrepo $the_commit heads/local
455 '
456
457 test_expect_success 'push with +HEAD' '
458
459         mk_test testrepo heads/master &&
460         git checkout master &&
461         git checkout -b local &&
462         test_when_finished "git checkout master; git branch -D local" &&
463         git push testrepo master local &&
464         check_push_result testrepo $the_commit heads/master &&
465         check_push_result testrepo $the_commit heads/local &&
466
467         # Without force rewinding should fail
468         git reset --hard HEAD^ &&
469         test_must_fail git push testrepo HEAD &&
470         check_push_result testrepo $the_commit heads/local &&
471
472         # With force rewinding should succeed
473         git push testrepo +HEAD &&
474         check_push_result testrepo $the_first_commit heads/local
475
476 '
477
478 test_expect_success 'push HEAD with non-existent, incomplete dest' '
479
480         mk_test testrepo &&
481         git checkout master &&
482         git push testrepo HEAD:branch &&
483         check_push_result testrepo $the_commit heads/branch
484
485 '
486
487 test_expect_success 'push with config remote.*.push = HEAD' '
488
489         mk_test testrepo heads/local &&
490         git checkout master &&
491         git branch -f local $the_commit &&
492         test_when_finished "git branch -D local" &&
493         (
494                 cd testrepo &&
495                 git checkout local &&
496                 git reset --hard $the_first_commit
497         ) &&
498         test_config remote.there.url testrepo &&
499         test_config remote.there.push HEAD &&
500         test_config branch.master.remote there &&
501         git push &&
502         check_push_result testrepo $the_commit heads/master &&
503         check_push_result testrepo $the_first_commit heads/local
504 '
505
506 test_expect_success 'push with remote.pushdefault' '
507         mk_test up_repo heads/master &&
508         mk_test down_repo heads/master &&
509         test_config remote.up.url up_repo &&
510         test_config remote.down.url down_repo &&
511         test_config branch.master.remote up &&
512         test_config remote.pushdefault down &&
513         test_config push.default matching &&
514         git push &&
515         check_push_result up_repo $the_first_commit heads/master &&
516         check_push_result down_repo $the_commit heads/master
517 '
518
519 test_expect_success 'push with config remote.*.pushurl' '
520
521         mk_test testrepo heads/master &&
522         git checkout master &&
523         test_config remote.there.url test2repo &&
524         test_config remote.there.pushurl testrepo &&
525         git push there : &&
526         check_push_result testrepo $the_commit heads/master
527 '
528
529 test_expect_success 'push with config branch.*.pushremote' '
530         mk_test up_repo heads/master &&
531         mk_test side_repo heads/master &&
532         mk_test down_repo heads/master &&
533         test_config remote.up.url up_repo &&
534         test_config remote.pushdefault side_repo &&
535         test_config remote.down.url down_repo &&
536         test_config branch.master.remote up &&
537         test_config branch.master.pushremote down &&
538         test_config push.default matching &&
539         git push &&
540         check_push_result up_repo $the_first_commit heads/master &&
541         check_push_result side_repo $the_first_commit heads/master &&
542         check_push_result down_repo $the_commit heads/master
543 '
544
545 test_expect_success 'branch.*.pushremote config order is irrelevant' '
546         mk_test one_repo heads/master &&
547         mk_test two_repo heads/master &&
548         test_config remote.one.url one_repo &&
549         test_config remote.two.url two_repo &&
550         test_config branch.master.pushremote two_repo &&
551         test_config remote.pushdefault one_repo &&
552         test_config push.default matching &&
553         git push &&
554         check_push_result one_repo $the_first_commit heads/master &&
555         check_push_result two_repo $the_commit heads/master
556 '
557
558 test_expect_success 'push with dry-run' '
559
560         mk_test testrepo heads/master &&
561         old_commit=$(git -C testrepo show-ref -s --verify refs/heads/master) &&
562         git push --dry-run testrepo : &&
563         check_push_result testrepo $old_commit heads/master
564 '
565
566 test_expect_success 'push updates local refs' '
567
568         mk_test testrepo heads/master &&
569         mk_child testrepo child &&
570         (
571                 cd child &&
572                 git pull .. master &&
573                 git push &&
574                 test $(git rev-parse master) = \
575                         $(git rev-parse remotes/origin/master)
576         )
577
578 '
579
580 test_expect_success 'push updates up-to-date local refs' '
581
582         mk_test testrepo heads/master &&
583         mk_child testrepo child1 &&
584         mk_child testrepo child2 &&
585         (cd child1 && git pull .. master && git push) &&
586         (
587                 cd child2 &&
588                 git pull ../child1 master &&
589                 git push &&
590                 test $(git rev-parse master) = \
591                         $(git rev-parse remotes/origin/master)
592         )
593
594 '
595
596 test_expect_success 'push preserves up-to-date packed refs' '
597
598         mk_test testrepo heads/master &&
599         mk_child testrepo child &&
600         (
601                 cd child &&
602                 git push &&
603                 ! test -f .git/refs/remotes/origin/master
604         )
605
606 '
607
608 test_expect_success 'push does not update local refs on failure' '
609
610         mk_test testrepo heads/master &&
611         mk_child testrepo child &&
612         mkdir testrepo/.git/hooks &&
613         echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
614         chmod +x testrepo/.git/hooks/pre-receive &&
615         (
616                 cd child &&
617                 git pull .. master &&
618                 test_must_fail git push &&
619                 test $(git rev-parse master) != \
620                         $(git rev-parse remotes/origin/master)
621         )
622
623 '
624
625 test_expect_success 'allow deleting an invalid remote ref' '
626
627         mk_test testrepo heads/master &&
628         rm -f testrepo/.git/objects/??/* &&
629         git push testrepo :refs/heads/master &&
630         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
631
632 '
633
634 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
635         mk_test_with_hooks testrepo heads/master heads/next &&
636         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
637         newmaster=$(git show-ref -s --verify refs/heads/master) &&
638         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
639         newnext=$ZERO_OID &&
640         git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
641         (
642                 cd testrepo/.git &&
643                 cat >pre-receive.expect <<-EOF &&
644                 $orgmaster $newmaster refs/heads/master
645                 $orgnext $newnext refs/heads/next
646                 EOF
647
648                 cat >update.expect <<-EOF &&
649                 refs/heads/master $orgmaster $newmaster
650                 refs/heads/next $orgnext $newnext
651                 EOF
652
653                 cat >post-receive.expect <<-EOF &&
654                 $orgmaster $newmaster refs/heads/master
655                 $orgnext $newnext refs/heads/next
656                 EOF
657
658                 cat >post-update.expect <<-EOF &&
659                 refs/heads/master
660                 refs/heads/next
661                 EOF
662
663                 test_cmp pre-receive.expect pre-receive.actual &&
664                 test_cmp update.expect update.actual &&
665                 test_cmp post-receive.expect post-receive.actual &&
666                 test_cmp post-update.expect post-update.actual
667         )
668 '
669
670 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
671         mk_test_with_hooks testrepo heads/master &&
672         rm -f testrepo/.git/objects/??/* &&
673         git push testrepo :refs/heads/master &&
674         (
675                 cd testrepo/.git &&
676                 cat >pre-receive.expect <<-EOF &&
677                 $ZERO_OID $ZERO_OID refs/heads/master
678                 EOF
679
680                 cat >update.expect <<-EOF &&
681                 refs/heads/master $ZERO_OID $ZERO_OID
682                 EOF
683
684                 cat >post-receive.expect <<-EOF &&
685                 $ZERO_OID $ZERO_OID refs/heads/master
686                 EOF
687
688                 cat >post-update.expect <<-EOF &&
689                 refs/heads/master
690                 EOF
691
692                 test_cmp pre-receive.expect pre-receive.actual &&
693                 test_cmp update.expect update.actual &&
694                 test_cmp post-receive.expect post-receive.actual &&
695                 test_cmp post-update.expect post-update.actual
696         )
697 '
698
699 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
700         mk_test_with_hooks testrepo heads/master &&
701         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
702         newmaster=$(git show-ref -s --verify refs/heads/master) &&
703         git push testrepo master :refs/heads/nonexistent &&
704         (
705                 cd testrepo/.git &&
706                 cat >pre-receive.expect <<-EOF &&
707                 $orgmaster $newmaster refs/heads/master
708                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
709                 EOF
710
711                 cat >update.expect <<-EOF &&
712                 refs/heads/master $orgmaster $newmaster
713                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
714                 EOF
715
716                 cat >post-receive.expect <<-EOF &&
717                 $orgmaster $newmaster refs/heads/master
718                 EOF
719
720                 cat >post-update.expect <<-EOF &&
721                 refs/heads/master
722                 EOF
723
724                 test_cmp pre-receive.expect pre-receive.actual &&
725                 test_cmp update.expect update.actual &&
726                 test_cmp post-receive.expect post-receive.actual &&
727                 test_cmp post-update.expect post-update.actual
728         )
729 '
730
731 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
732         mk_test_with_hooks testrepo heads/master &&
733         git push testrepo :refs/heads/nonexistent &&
734         (
735                 cd testrepo/.git &&
736                 cat >pre-receive.expect <<-EOF &&
737                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
738                 EOF
739
740                 cat >update.expect <<-EOF &&
741                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
742                 EOF
743
744                 test_cmp pre-receive.expect pre-receive.actual &&
745                 test_cmp update.expect update.actual &&
746                 test_path_is_missing post-receive.actual &&
747                 test_path_is_missing post-update.actual
748         )
749 '
750
751 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
752         mk_test_with_hooks testrepo heads/master heads/next heads/seen &&
753         orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
754         newmaster=$(git show-ref -s --verify refs/heads/master) &&
755         orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
756         newnext=$ZERO_OID &&
757         orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
758         newseen=$(git show-ref -s --verify refs/heads/master) &&
759         git push testrepo refs/heads/master:refs/heads/master \
760             refs/heads/master:refs/heads/seen :refs/heads/next \
761             :refs/heads/nonexistent &&
762         (
763                 cd testrepo/.git &&
764                 cat >pre-receive.expect <<-EOF &&
765                 $orgmaster $newmaster refs/heads/master
766                 $orgnext $newnext refs/heads/next
767                 $orgseen $newseen refs/heads/seen
768                 $ZERO_OID $ZERO_OID refs/heads/nonexistent
769                 EOF
770
771                 cat >update.expect <<-EOF &&
772                 refs/heads/master $orgmaster $newmaster
773                 refs/heads/next $orgnext $newnext
774                 refs/heads/seen $orgseen $newseen
775                 refs/heads/nonexistent $ZERO_OID $ZERO_OID
776                 EOF
777
778                 cat >post-receive.expect <<-EOF &&
779                 $orgmaster $newmaster refs/heads/master
780                 $orgnext $newnext refs/heads/next
781                 $orgseen $newseen refs/heads/seen
782                 EOF
783
784                 cat >post-update.expect <<-EOF &&
785                 refs/heads/master
786                 refs/heads/next
787                 refs/heads/seen
788                 EOF
789
790                 test_cmp pre-receive.expect pre-receive.actual &&
791                 test_cmp update.expect update.actual &&
792                 test_cmp post-receive.expect post-receive.actual &&
793                 test_cmp post-update.expect post-update.actual
794         )
795 '
796
797 test_expect_success 'allow deleting a ref using --delete' '
798         mk_test testrepo heads/master &&
799         (cd testrepo && git config receive.denyDeleteCurrent warn) &&
800         git push testrepo --delete master &&
801         (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
802 '
803
804 test_expect_success 'allow deleting a tag using --delete' '
805         mk_test testrepo heads/master &&
806         git tag -a -m dummy_message deltag heads/master &&
807         git push testrepo --tags &&
808         (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
809         git push testrepo --delete tag deltag &&
810         (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
811 '
812
813 test_expect_success 'push --delete without args aborts' '
814         mk_test testrepo heads/master &&
815         test_must_fail git push testrepo --delete
816 '
817
818 test_expect_success 'push --delete refuses src:dest refspecs' '
819         mk_test testrepo heads/master &&
820         test_must_fail git push testrepo --delete master:foo
821 '
822
823 test_expect_success 'warn on push to HEAD of non-bare repository' '
824         mk_test testrepo heads/master &&
825         (
826                 cd testrepo &&
827                 git checkout master &&
828                 git config receive.denyCurrentBranch warn
829         ) &&
830         git push testrepo master 2>stderr &&
831         grep "warning: updating the current branch" stderr
832 '
833
834 test_expect_success 'deny push to HEAD of non-bare repository' '
835         mk_test testrepo heads/master &&
836         (
837                 cd testrepo &&
838                 git checkout master &&
839                 git config receive.denyCurrentBranch true
840         ) &&
841         test_must_fail git push testrepo master
842 '
843
844 test_expect_success 'allow push to HEAD of bare repository (bare)' '
845         mk_test testrepo heads/master &&
846         (
847                 cd testrepo &&
848                 git checkout master &&
849                 git config receive.denyCurrentBranch true &&
850                 git config core.bare true
851         ) &&
852         git push testrepo master 2>stderr &&
853         ! grep "warning: updating the current branch" stderr
854 '
855
856 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
857         mk_test testrepo heads/master &&
858         (
859                 cd testrepo &&
860                 git checkout master &&
861                 git config receive.denyCurrentBranch false
862         ) &&
863         git push testrepo master 2>stderr &&
864         ! grep "warning: updating the current branch" stderr
865 '
866
867 test_expect_success 'fetch with branches' '
868         mk_empty testrepo &&
869         git branch second $the_first_commit &&
870         git checkout second &&
871         echo ".." > testrepo/.git/branches/branch1 &&
872         (
873                 cd testrepo &&
874                 git fetch branch1 &&
875                 echo "$the_commit commit        refs/heads/branch1" >expect &&
876                 git for-each-ref refs/heads >actual &&
877                 test_cmp expect actual
878         ) &&
879         git checkout master
880 '
881
882 test_expect_success 'fetch with branches containing #' '
883         mk_empty testrepo &&
884         echo "..#second" > testrepo/.git/branches/branch2 &&
885         (
886                 cd testrepo &&
887                 git fetch branch2 &&
888                 echo "$the_first_commit commit  refs/heads/branch2" >expect &&
889                 git for-each-ref refs/heads >actual &&
890                 test_cmp expect actual
891         ) &&
892         git checkout master
893 '
894
895 test_expect_success 'push with branches' '
896         mk_empty testrepo &&
897         git checkout second &&
898         echo "testrepo" > .git/branches/branch1 &&
899         git push branch1 &&
900         (
901                 cd testrepo &&
902                 echo "$the_first_commit commit  refs/heads/master" >expect &&
903                 git for-each-ref refs/heads >actual &&
904                 test_cmp expect actual
905         )
906 '
907
908 test_expect_success 'push with branches containing #' '
909         mk_empty testrepo &&
910         echo "testrepo#branch3" > .git/branches/branch2 &&
911         git push branch2 &&
912         (
913                 cd testrepo &&
914                 echo "$the_first_commit commit  refs/heads/branch3" >expect &&
915                 git for-each-ref refs/heads >actual &&
916                 test_cmp expect actual
917         ) &&
918         git checkout master
919 '
920
921 test_expect_success 'push into aliased refs (consistent)' '
922         mk_test testrepo heads/master &&
923         mk_child testrepo child1 &&
924         mk_child testrepo child2 &&
925         (
926                 cd child1 &&
927                 git branch foo &&
928                 git symbolic-ref refs/heads/bar refs/heads/foo &&
929                 git config receive.denyCurrentBranch false
930         ) &&
931         (
932                 cd child2 &&
933                 >path2 &&
934                 git add path2 &&
935                 test_tick &&
936                 git commit -a -m child2 &&
937                 git branch foo &&
938                 git branch bar &&
939                 git push ../child1 foo bar
940         )
941 '
942
943 test_expect_success 'push into aliased refs (inconsistent)' '
944         mk_test testrepo heads/master &&
945         mk_child testrepo child1 &&
946         mk_child testrepo child2 &&
947         (
948                 cd child1 &&
949                 git branch foo &&
950                 git symbolic-ref refs/heads/bar refs/heads/foo &&
951                 git config receive.denyCurrentBranch false
952         ) &&
953         (
954                 cd child2 &&
955                 >path2 &&
956                 git add path2 &&
957                 test_tick &&
958                 git commit -a -m child2 &&
959                 git branch foo &&
960                 >path3 &&
961                 git add path3 &&
962                 test_tick &&
963                 git commit -a -m child2 &&
964                 git branch bar &&
965                 test_must_fail git push ../child1 foo bar 2>stderr &&
966                 grep "refusing inconsistent update" stderr
967         )
968 '
969
970 test_force_push_tag () {
971         tag_type_description=$1
972         tag_args=$2
973
974         test_expect_success "force pushing required to update $tag_type_description" "
975                 mk_test testrepo heads/master &&
976                 mk_child testrepo child1 &&
977                 mk_child testrepo child2 &&
978                 (
979                         cd child1 &&
980                         git tag testTag &&
981                         git push ../child2 testTag &&
982                         >file1 &&
983                         git add file1 &&
984                         git commit -m 'file1' &&
985                         git tag $tag_args testTag &&
986                         test_must_fail git push ../child2 testTag &&
987                         git push --force ../child2 testTag &&
988                         git tag $tag_args testTag HEAD~ &&
989                         test_must_fail git push ../child2 testTag &&
990                         git push --force ../child2 testTag &&
991
992                         # Clobbering without + in refspec needs --force
993                         git tag -f testTag &&
994                         test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
995                         git push --force ../child2 'refs/tags/*:refs/tags/*' &&
996
997                         # Clobbering with + in refspec does not need --force
998                         git tag -f testTag HEAD~ &&
999                         git push ../child2 '+refs/tags/*:refs/tags/*' &&
1000
1001                         # Clobbering with --no-force still obeys + in refspec
1002                         git tag -f testTag &&
1003                         git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1004
1005                         # Clobbering with/without --force and 'tag <name>' format
1006                         git tag -f testTag HEAD~ &&
1007                         test_must_fail git push ../child2 tag testTag &&
1008                         git push --force ../child2 tag testTag
1009                 )
1010         "
1011 }
1012
1013 test_force_push_tag "lightweight tag" "-f"
1014 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1015
1016 test_force_fetch_tag () {
1017         tag_type_description=$1
1018         tag_args=$2
1019
1020         test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1021                 mk_test testrepo heads/master &&
1022                 mk_child testrepo child1 &&
1023                 mk_child testrepo child2 &&
1024                 (
1025                         cd testrepo &&
1026                         git tag testTag &&
1027                         git -C ../child1 fetch origin tag testTag &&
1028                         >file1 &&
1029                         git add file1 &&
1030                         git commit -m 'file1' &&
1031                         git tag $tag_args testTag &&
1032                         test_must_fail git -C ../child1 fetch origin tag testTag &&
1033                         git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1034                 )
1035         "
1036 }
1037
1038 test_force_fetch_tag "lightweight tag" "-f"
1039 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1040
1041 test_expect_success 'push --porcelain' '
1042         mk_empty testrepo &&
1043         echo >.git/foo  "To testrepo" &&
1044         echo >>.git/foo "*      refs/heads/master:refs/remotes/origin/master    [new reference]"  &&
1045         echo >>.git/foo "Done" &&
1046         git push >.git/bar --porcelain  testrepo refs/heads/master:refs/remotes/origin/master &&
1047         (
1048                 cd testrepo &&
1049                 echo "$the_commit commit        refs/remotes/origin/master" >expect &&
1050                 git for-each-ref refs/remotes/origin >actual &&
1051                 test_cmp expect actual
1052         ) &&
1053         test_cmp .git/foo .git/bar
1054 '
1055
1056 test_expect_success 'push --porcelain bad url' '
1057         mk_empty testrepo &&
1058         test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
1059         ! grep -q Done .git/bar
1060 '
1061
1062 test_expect_success 'push --porcelain rejected' '
1063         mk_empty testrepo &&
1064         git push testrepo refs/heads/master:refs/remotes/origin/master &&
1065         (cd testrepo &&
1066                 git reset --hard origin/master^ &&
1067                 git config receive.denyCurrentBranch true) &&
1068
1069         echo >.git/foo  "To testrepo"  &&
1070         echo >>.git/foo "!      refs/heads/master:refs/heads/master     [remote rejected] (branch is currently checked out)" &&
1071         echo >>.git/foo "Done" &&
1072
1073         test_must_fail git push >.git/bar --porcelain  testrepo refs/heads/master:refs/heads/master &&
1074         test_cmp .git/foo .git/bar
1075 '
1076
1077 test_expect_success 'push --porcelain --dry-run rejected' '
1078         mk_empty testrepo &&
1079         git push testrepo refs/heads/master:refs/remotes/origin/master &&
1080         (cd testrepo &&
1081                 git reset --hard origin/master &&
1082                 git config receive.denyCurrentBranch true) &&
1083
1084         echo >.git/foo  "To testrepo"  &&
1085         echo >>.git/foo "!      refs/heads/master^:refs/heads/master    [rejected] (non-fast-forward)" &&
1086         echo >>.git/foo "Done" &&
1087
1088         test_must_fail git push >.git/bar --porcelain  --dry-run testrepo refs/heads/master^:refs/heads/master &&
1089         test_cmp .git/foo .git/bar
1090 '
1091
1092 test_expect_success 'push --prune' '
1093         mk_test testrepo heads/master heads/second heads/foo heads/bar &&
1094         git push --prune testrepo : &&
1095         check_push_result testrepo $the_commit heads/master &&
1096         check_push_result testrepo $the_first_commit heads/second &&
1097         ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1098 '
1099
1100 test_expect_success 'push --prune refspec' '
1101         mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
1102         git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1103         check_push_result testrepo $the_commit tmp/master &&
1104         check_push_result testrepo $the_first_commit tmp/second &&
1105         ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1106 '
1107
1108 for configsection in transfer receive
1109 do
1110         test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1111                 mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
1112                 (
1113                         cd testrepo &&
1114                         git config $configsection.hiderefs refs/hidden
1115                 ) &&
1116
1117                 # push to unhidden ref succeeds normally
1118                 git push testrepo master:refs/heads/master &&
1119                 check_push_result testrepo $the_commit heads/master &&
1120
1121                 # push to update a hidden ref should fail
1122                 test_must_fail git push testrepo master:refs/hidden/one &&
1123                 check_push_result testrepo $the_first_commit hidden/one &&
1124
1125                 # push to delete a hidden ref should fail
1126                 test_must_fail git push testrepo :refs/hidden/two &&
1127                 check_push_result testrepo $the_first_commit hidden/two &&
1128
1129                 # idempotent push to update a hidden ref should fail
1130                 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1131                 check_push_result testrepo $the_first_commit hidden/three
1132         '
1133 done
1134
1135 test_expect_success 'fetch exact SHA1' '
1136         mk_test testrepo heads/master hidden/one &&
1137         git push testrepo master:refs/hidden/one &&
1138         (
1139                 cd testrepo &&
1140                 git config transfer.hiderefs refs/hidden
1141         ) &&
1142         check_push_result testrepo $the_commit hidden/one &&
1143
1144         mk_child testrepo child &&
1145         (
1146                 cd child &&
1147
1148                 # make sure $the_commit does not exist here
1149                 git repack -a -d &&
1150                 git prune &&
1151                 test_must_fail git cat-file -t $the_commit &&
1152
1153                 # Some protocol versions (e.g. 2) support fetching
1154                 # unadvertised objects, so restrict this test to v0.
1155
1156                 # fetching the hidden object should fail by default
1157                 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1158                         git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1159                 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1160                 test_must_fail git rev-parse --verify refs/heads/copy &&
1161
1162                 # the server side can allow it to succeed
1163                 (
1164                         cd ../testrepo &&
1165                         git config uploadpack.allowtipsha1inwant true
1166                 ) &&
1167
1168                 git fetch -v ../testrepo $the_commit:refs/heads/copy master:refs/heads/extra &&
1169                 cat >expect <<-EOF &&
1170                 $the_commit
1171                 $the_first_commit
1172                 EOF
1173                 {
1174                         git rev-parse --verify refs/heads/copy &&
1175                         git rev-parse --verify refs/heads/extra
1176                 } >actual &&
1177                 test_cmp expect actual
1178         )
1179 '
1180
1181 test_expect_success 'fetch exact SHA1 in protocol v2' '
1182         mk_test testrepo heads/master hidden/one &&
1183         git push testrepo master:refs/hidden/one &&
1184         git -C testrepo config transfer.hiderefs refs/hidden &&
1185         check_push_result testrepo $the_commit hidden/one &&
1186
1187         mk_child testrepo child &&
1188         git -C child config protocol.version 2 &&
1189
1190         # make sure $the_commit does not exist here
1191         git -C child repack -a -d &&
1192         git -C child prune &&
1193         test_must_fail git -C child cat-file -t $the_commit &&
1194
1195         # fetching the hidden object succeeds by default
1196         # NEEDSWORK: should this match the v0 behavior instead?
1197         git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1198 '
1199
1200 for configallowtipsha1inwant in true false
1201 do
1202         test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1203                 mk_empty testrepo &&
1204                 (
1205                         cd testrepo &&
1206                         git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1207                         git commit --allow-empty -m foo &&
1208                         git commit --allow-empty -m bar
1209                 ) &&
1210                 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1211                 mk_empty shallow &&
1212                 (
1213                         cd shallow &&
1214                         # Some protocol versions (e.g. 2) support fetching
1215                         # unadvertised objects, so restrict this test to v0.
1216                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1217                                 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1218                         git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1219                         git fetch --depth=1 ../testrepo/.git $SHA1 &&
1220                         git cat-file commit $SHA1
1221                 )
1222         '
1223
1224         test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1225                 mk_empty testrepo &&
1226                 (
1227                         cd testrepo &&
1228                         git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1229                         git commit --allow-empty -m foo &&
1230                         git commit --allow-empty -m bar &&
1231                         git commit --allow-empty -m xyz
1232                 ) &&
1233                 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1234                 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1235                 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1236                 (
1237                         cd testrepo &&
1238                         git reset --hard $SHA1_2 &&
1239                         git cat-file commit $SHA1_1 &&
1240                         git cat-file commit $SHA1_3
1241                 ) &&
1242                 mk_empty shallow &&
1243                 (
1244                         cd shallow &&
1245                         # Some protocol versions (e.g. 2) support fetching
1246                         # unadvertised objects, so restrict this test to v0.
1247                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1248                                 git fetch ../testrepo/.git $SHA1_3 &&
1249                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1250                                 git fetch ../testrepo/.git $SHA1_1 &&
1251                         git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1252                         git fetch ../testrepo/.git $SHA1_1 &&
1253                         git cat-file commit $SHA1_1 &&
1254                         test_must_fail git cat-file commit $SHA1_2 &&
1255                         git fetch ../testrepo/.git $SHA1_2 &&
1256                         git cat-file commit $SHA1_2 &&
1257                         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1258                                 git fetch ../testrepo/.git $SHA1_3 2>err &&
1259                         test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
1260                 )
1261         '
1262 done
1263
1264 test_expect_success 'fetch follows tags by default' '
1265         mk_test testrepo heads/master &&
1266         rm -fr src dst &&
1267         git init src &&
1268         (
1269                 cd src &&
1270                 git pull ../testrepo master &&
1271                 git tag -m "annotated" tag &&
1272                 git for-each-ref >tmp1 &&
1273                 (
1274                         cat tmp1
1275                         sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1276                 ) |
1277                 sort -k 3 >../expect
1278         ) &&
1279         git init dst &&
1280         (
1281                 cd dst &&
1282                 git remote add origin ../src &&
1283                 git config branch.master.remote origin &&
1284                 git config branch.master.merge refs/heads/master &&
1285                 git pull &&
1286                 git for-each-ref >../actual
1287         ) &&
1288         test_cmp expect actual
1289 '
1290
1291 test_expect_success 'peeled advertisements are not considered ref tips' '
1292         mk_empty testrepo &&
1293         git -C testrepo commit --allow-empty -m one &&
1294         git -C testrepo commit --allow-empty -m two &&
1295         git -C testrepo tag -m foo mytag HEAD^ &&
1296         oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1297         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1298                 git fetch testrepo $oid 2>err &&
1299         test_i18ngrep "Server does not allow request for unadvertised object" err
1300 '
1301
1302 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1303         mk_test testrepo heads/master &&
1304         rm -fr src dst &&
1305         git init src &&
1306         git init --bare dst &&
1307         (
1308                 cd src &&
1309                 git pull ../testrepo master &&
1310                 git branch next &&
1311                 git config remote.dst.url ../dst &&
1312                 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1313                 git push dst master &&
1314                 git show-ref refs/heads/master |
1315                 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1316         ) &&
1317         (
1318                 cd dst &&
1319                 test_must_fail git show-ref refs/heads/next &&
1320                 test_must_fail git show-ref refs/heads/master &&
1321                 git show-ref refs/remotes/src/master >actual
1322         ) &&
1323         test_cmp dst/expect dst/actual
1324 '
1325
1326 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1327         mk_test testrepo heads/master &&
1328         rm -fr src dst &&
1329         git init src &&
1330         git init --bare dst &&
1331         (
1332                 cd src &&
1333                 git pull ../testrepo master &&
1334                 git branch next &&
1335                 git config remote.dst.url ../dst &&
1336                 git config push.default matching &&
1337                 git push dst master &&
1338                 git show-ref refs/heads/master >../dst/expect
1339         ) &&
1340         (
1341                 cd dst &&
1342                 test_must_fail git show-ref refs/heads/next &&
1343                 git show-ref refs/heads/master >actual
1344         ) &&
1345         test_cmp dst/expect dst/actual
1346 '
1347
1348 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1349         mk_test testrepo heads/master &&
1350         rm -fr src dst &&
1351         git init src &&
1352         git init --bare dst &&
1353         (
1354                 cd src &&
1355                 git pull ../testrepo master &&
1356                 git branch next &&
1357                 git config remote.dst.url ../dst &&
1358                 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1359                 git config push.default upstream &&
1360
1361                 git config branch.master.merge refs/heads/trunk &&
1362                 git config branch.master.remote dst &&
1363
1364                 git push dst master &&
1365                 git show-ref refs/heads/master |
1366                 sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1367         ) &&
1368         (
1369                 cd dst &&
1370                 test_must_fail git show-ref refs/heads/master &&
1371                 test_must_fail git show-ref refs/heads/next &&
1372                 git show-ref refs/heads/trunk >actual
1373         ) &&
1374         test_cmp dst/expect dst/actual
1375 '
1376
1377 test_expect_success 'push does not follow tags by default' '
1378         mk_test testrepo heads/master &&
1379         rm -fr src dst &&
1380         git init src &&
1381         git init --bare dst &&
1382         (
1383                 cd src &&
1384                 git pull ../testrepo master &&
1385                 git tag -m "annotated" tag &&
1386                 git checkout -b another &&
1387                 git commit --allow-empty -m "future commit" &&
1388                 git tag -m "future" future &&
1389                 git checkout master &&
1390                 git for-each-ref refs/heads/master >../expect &&
1391                 git push ../dst master
1392         ) &&
1393         (
1394                 cd dst &&
1395                 git for-each-ref >../actual
1396         ) &&
1397         test_cmp expect actual
1398 '
1399
1400 test_expect_success 'push --follow-tags only pushes relevant tags' '
1401         mk_test testrepo heads/master &&
1402         rm -fr src dst &&
1403         git init src &&
1404         git init --bare dst &&
1405         (
1406                 cd src &&
1407                 git pull ../testrepo master &&
1408                 git tag -m "annotated" tag &&
1409                 git checkout -b another &&
1410                 git commit --allow-empty -m "future commit" &&
1411                 git tag -m "future" future &&
1412                 git checkout master &&
1413                 git for-each-ref refs/heads/master refs/tags/tag >../expect &&
1414                 git push --follow-tags ../dst master
1415         ) &&
1416         (
1417                 cd dst &&
1418                 git for-each-ref >../actual
1419         ) &&
1420         test_cmp expect actual
1421 '
1422
1423 test_expect_success 'push --no-thin must produce non-thin pack' '
1424         cat >>path1 <<\EOF &&
1425 keep base version of path1 big enough, compared to the new changes
1426 later, in order to pass size heuristics in
1427 builtin/pack-objects.c:try_delta()
1428 EOF
1429         git commit -am initial &&
1430         git init no-thin &&
1431         git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1432         git push no-thin/.git refs/heads/master:refs/heads/foo &&
1433         echo modified >> path1 &&
1434         git commit -am modified &&
1435         git repack -adf &&
1436         rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1437         git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/master:refs/heads/foo
1438 '
1439
1440 test_expect_success 'pushing a tag pushes the tagged object' '
1441         rm -rf dst.git &&
1442         blob=$(echo unreferenced | git hash-object -w --stdin) &&
1443         git tag -m foo tag-of-blob $blob &&
1444         git init --bare dst.git &&
1445         git push dst.git tag-of-blob &&
1446         # the receiving index-pack should have noticed
1447         # any problems, but we double check
1448         echo unreferenced >expect &&
1449         git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1450         test_cmp expect actual
1451 '
1452
1453 test_expect_success 'push into bare respects core.logallrefupdates' '
1454         rm -rf dst.git &&
1455         git init --bare dst.git &&
1456         git -C dst.git config core.logallrefupdates true &&
1457
1458         # double push to test both with and without
1459         # the actual pack transfer
1460         git push dst.git master:one &&
1461         echo "one@{0} push" >expect &&
1462         git -C dst.git log -g --format="%gd %gs" one >actual &&
1463         test_cmp expect actual &&
1464
1465         git push dst.git master:two &&
1466         echo "two@{0} push" >expect &&
1467         git -C dst.git log -g --format="%gd %gs" two >actual &&
1468         test_cmp expect actual
1469 '
1470
1471 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1472         rm -rf dst.git &&
1473         git init --bare dst.git &&
1474         (
1475                 cd dst.git &&
1476                 git config core.logallrefupdates true &&
1477
1478                 # as above, we double-fetch to test both
1479                 # with and without pack transfer
1480                 git fetch .. master:one &&
1481                 echo "one@{0} fetch .. master:one: storing head" >expect &&
1482                 git log -g --format="%gd %gs" one >actual &&
1483                 test_cmp expect actual &&
1484
1485                 git fetch .. master:two &&
1486                 echo "two@{0} fetch .. master:two: storing head" >expect &&
1487                 git log -g --format="%gd %gs" two >actual &&
1488                 test_cmp expect actual
1489         )
1490 '
1491
1492 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1493         git push testrepo master &&
1494         (
1495                 cd testrepo &&
1496                 git reset --hard &&
1497                 git config receive.denyCurrentBranch updateInstead
1498         ) &&
1499         test_commit third path2 &&
1500
1501         # Try pushing into a repository with pristine working tree
1502         git push testrepo master &&
1503         (
1504                 cd testrepo &&
1505                 git update-index -q --refresh &&
1506                 git diff-files --quiet -- &&
1507                 git diff-index --quiet --cached HEAD -- &&
1508                 test third = "$(cat path2)" &&
1509                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1510         ) &&
1511
1512         # Try pushing into a repository with working tree needing a refresh
1513         (
1514                 cd testrepo &&
1515                 git reset --hard HEAD^ &&
1516                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1517                 test-tool chmtime +100 path1
1518         ) &&
1519         git push testrepo master &&
1520         (
1521                 cd testrepo &&
1522                 git update-index -q --refresh &&
1523                 git diff-files --quiet -- &&
1524                 git diff-index --quiet --cached HEAD -- &&
1525                 test_cmp ../path1 path1 &&
1526                 test third = "$(cat path2)" &&
1527                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1528         ) &&
1529
1530         # Update what is to be pushed
1531         test_commit fourth path2 &&
1532
1533         # Try pushing into a repository with a dirty working tree
1534         # (1) the working tree updated
1535         (
1536                 cd testrepo &&
1537                 echo changed >path1
1538         ) &&
1539         test_must_fail git push testrepo master &&
1540         (
1541                 cd testrepo &&
1542                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1543                 git diff --quiet --cached &&
1544                 test changed = "$(cat path1)"
1545         ) &&
1546
1547         # (2) the index updated
1548         (
1549                 cd testrepo &&
1550                 echo changed >path1 &&
1551                 git add path1
1552         ) &&
1553         test_must_fail git push testrepo master &&
1554         (
1555                 cd testrepo &&
1556                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1557                 git diff --quiet &&
1558                 test changed = "$(cat path1)"
1559         ) &&
1560
1561         # Introduce a new file in the update
1562         test_commit fifth path3 &&
1563
1564         # (3) the working tree has an untracked file that would interfere
1565         (
1566                 cd testrepo &&
1567                 git reset --hard &&
1568                 echo changed >path3
1569         ) &&
1570         test_must_fail git push testrepo master &&
1571         (
1572                 cd testrepo &&
1573                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1574                 git diff --quiet &&
1575                 git diff --quiet --cached &&
1576                 test changed = "$(cat path3)"
1577         ) &&
1578
1579         # (4) the target changes to what gets pushed but it still is a change
1580         (
1581                 cd testrepo &&
1582                 git reset --hard &&
1583                 echo fifth >path3 &&
1584                 git add path3
1585         ) &&
1586         test_must_fail git push testrepo master &&
1587         (
1588                 cd testrepo &&
1589                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1590                 git diff --quiet &&
1591                 test fifth = "$(cat path3)"
1592         ) &&
1593
1594         # (5) push into void
1595         rm -fr void &&
1596         git init void &&
1597         (
1598                 cd void &&
1599                 git config receive.denyCurrentBranch updateInstead
1600         ) &&
1601         git push void master &&
1602         (
1603                 cd void &&
1604                 test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1605                 git diff --quiet &&
1606                 git diff --cached --quiet
1607         ) &&
1608
1609         # (6) updateInstead intervened by fast-forward check
1610         test_must_fail git push void master^:master &&
1611         test $(git -C void rev-parse HEAD) = $(git rev-parse master) &&
1612         git -C void diff --quiet &&
1613         git -C void diff --cached --quiet
1614 '
1615
1616 test_expect_success 'updateInstead with push-to-checkout hook' '
1617         rm -fr testrepo &&
1618         git init testrepo &&
1619         (
1620                 cd testrepo &&
1621                 git pull .. master &&
1622                 git reset --hard HEAD^^ &&
1623                 git tag initial &&
1624                 git config receive.denyCurrentBranch updateInstead &&
1625                 write_script .git/hooks/push-to-checkout <<-\EOF
1626                 echo >&2 updating from $(git rev-parse HEAD)
1627                 echo >&2 updating to "$1"
1628
1629                 git update-index -q --refresh &&
1630                 git read-tree -u -m HEAD "$1" || {
1631                         status=$?
1632                         echo >&2 read-tree failed
1633                         exit $status
1634                 }
1635                 EOF
1636         ) &&
1637
1638         # Try pushing into a pristine
1639         git push testrepo master &&
1640         (
1641                 cd testrepo &&
1642                 git diff --quiet &&
1643                 git diff HEAD --quiet &&
1644                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1645         ) &&
1646
1647         # Try pushing into a repository with conflicting change
1648         (
1649                 cd testrepo &&
1650                 git reset --hard initial &&
1651                 echo conflicting >path2
1652         ) &&
1653         test_must_fail git push testrepo master &&
1654         (
1655                 cd testrepo &&
1656                 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1657                 test conflicting = "$(cat path2)" &&
1658                 git diff-index --quiet --cached HEAD
1659         ) &&
1660
1661         # Try pushing into a repository with unrelated change
1662         (
1663                 cd testrepo &&
1664                 git reset --hard initial &&
1665                 echo unrelated >path1 &&
1666                 echo irrelevant >path5 &&
1667                 git add path5
1668         ) &&
1669         git push testrepo master &&
1670         (
1671                 cd testrepo &&
1672                 test "$(cat path1)" = unrelated &&
1673                 test "$(cat path5)" = irrelevant &&
1674                 test "$(git diff --name-only --cached HEAD)" = path5 &&
1675                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1676         ) &&
1677
1678         # push into void
1679         rm -fr void &&
1680         git init void &&
1681         (
1682                 cd void &&
1683                 git config receive.denyCurrentBranch updateInstead &&
1684                 write_script .git/hooks/push-to-checkout <<-\EOF
1685                 if git rev-parse --quiet --verify HEAD
1686                 then
1687                         has_head=yes
1688                         echo >&2 updating from $(git rev-parse HEAD)
1689                 else
1690                         has_head=no
1691                         echo >&2 pushing into void
1692                 fi
1693                 echo >&2 updating to "$1"
1694
1695                 git update-index -q --refresh &&
1696                 case "$has_head" in
1697                 yes)
1698                         git read-tree -u -m HEAD "$1" ;;
1699                 no)
1700                         git read-tree -u -m "$1" ;;
1701                 esac || {
1702                         status=$?
1703                         echo >&2 read-tree failed
1704                         exit $status
1705                 }
1706                 EOF
1707         ) &&
1708
1709         git push void master &&
1710         (
1711                 cd void &&
1712                 git diff --quiet &&
1713                 git diff --cached --quiet &&
1714                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1715         )
1716 '
1717
1718 test_expect_success 'denyCurrentBranch and worktrees' '
1719         git worktree add new-wt &&
1720         git clone . cloned &&
1721         test_commit -C cloned first &&
1722         test_config receive.denyCurrentBranch refuse &&
1723         test_must_fail git -C cloned push origin HEAD:new-wt &&
1724         test_config receive.denyCurrentBranch updateInstead &&
1725         git -C cloned push origin HEAD:new-wt &&
1726         test_must_fail git -C cloned push --delete origin new-wt
1727 '
1728
1729 test_done