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