Merge branch 'ma/sha1-is-a-hash'
[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                         test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
1264                 )
1265         '
1266 done
1267
1268 test_expect_success 'fetch follows tags by default' '
1269         mk_test testrepo heads/master &&
1270         rm -fr src dst &&
1271         git init src &&
1272         (
1273                 cd src &&
1274                 git pull ../testrepo master &&
1275                 git tag -m "annotated" tag &&
1276                 git for-each-ref >tmp1 &&
1277                 (
1278                         cat tmp1
1279                         sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1280                 ) |
1281                 sort -k 3 >../expect
1282         ) &&
1283         git init dst &&
1284         (
1285                 cd dst &&
1286                 git remote add origin ../src &&
1287                 git config branch.master.remote origin &&
1288                 git config branch.master.merge refs/heads/master &&
1289                 git pull &&
1290                 git for-each-ref >../actual
1291         ) &&
1292         test_cmp expect actual
1293 '
1294
1295 test_expect_success 'peeled advertisements are not considered ref tips' '
1296         mk_empty testrepo &&
1297         git -C testrepo commit --allow-empty -m one &&
1298         git -C testrepo commit --allow-empty -m two &&
1299         git -C testrepo tag -m foo mytag HEAD^ &&
1300         oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1301         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1302                 git fetch testrepo $oid 2>err &&
1303         test_i18ngrep "Server does not allow request for unadvertised object" err
1304 '
1305
1306 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1307         mk_test testrepo heads/master &&
1308         rm -fr src dst &&
1309         git init src &&
1310         git init --bare dst &&
1311         (
1312                 cd src &&
1313                 git pull ../testrepo master &&
1314                 git branch next &&
1315                 git config remote.dst.url ../dst &&
1316                 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1317                 git push dst master &&
1318                 git show-ref refs/heads/master |
1319                 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1320         ) &&
1321         (
1322                 cd dst &&
1323                 test_must_fail git show-ref refs/heads/next &&
1324                 test_must_fail git show-ref refs/heads/master &&
1325                 git show-ref refs/remotes/src/master >actual
1326         ) &&
1327         test_cmp dst/expect dst/actual
1328 '
1329
1330 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1331         mk_test testrepo heads/master &&
1332         rm -fr src dst &&
1333         git init src &&
1334         git init --bare dst &&
1335         (
1336                 cd src &&
1337                 git pull ../testrepo master &&
1338                 git branch next &&
1339                 git config remote.dst.url ../dst &&
1340                 git config push.default matching &&
1341                 git push dst master &&
1342                 git show-ref refs/heads/master >../dst/expect
1343         ) &&
1344         (
1345                 cd dst &&
1346                 test_must_fail git show-ref refs/heads/next &&
1347                 git show-ref refs/heads/master >actual
1348         ) &&
1349         test_cmp dst/expect dst/actual
1350 '
1351
1352 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1353         mk_test testrepo heads/master &&
1354         rm -fr src dst &&
1355         git init src &&
1356         git init --bare dst &&
1357         (
1358                 cd src &&
1359                 git pull ../testrepo master &&
1360                 git branch next &&
1361                 git config remote.dst.url ../dst &&
1362                 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1363                 git config push.default upstream &&
1364
1365                 git config branch.master.merge refs/heads/trunk &&
1366                 git config branch.master.remote dst &&
1367
1368                 git push dst master &&
1369                 git show-ref refs/heads/master |
1370                 sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1371         ) &&
1372         (
1373                 cd dst &&
1374                 test_must_fail git show-ref refs/heads/master &&
1375                 test_must_fail git show-ref refs/heads/next &&
1376                 git show-ref refs/heads/trunk >actual
1377         ) &&
1378         test_cmp dst/expect dst/actual
1379 '
1380
1381 test_expect_success 'push does not follow tags by default' '
1382         mk_test testrepo heads/master &&
1383         rm -fr src dst &&
1384         git init src &&
1385         git init --bare dst &&
1386         (
1387                 cd src &&
1388                 git pull ../testrepo master &&
1389                 git tag -m "annotated" tag &&
1390                 git checkout -b another &&
1391                 git commit --allow-empty -m "future commit" &&
1392                 git tag -m "future" future &&
1393                 git checkout master &&
1394                 git for-each-ref refs/heads/master >../expect &&
1395                 git push ../dst master
1396         ) &&
1397         (
1398                 cd dst &&
1399                 git for-each-ref >../actual
1400         ) &&
1401         test_cmp expect actual
1402 '
1403
1404 test_expect_success 'push --follow-tags only pushes relevant tags' '
1405         mk_test testrepo heads/master &&
1406         rm -fr src dst &&
1407         git init src &&
1408         git init --bare dst &&
1409         (
1410                 cd src &&
1411                 git pull ../testrepo master &&
1412                 git tag -m "annotated" tag &&
1413                 git checkout -b another &&
1414                 git commit --allow-empty -m "future commit" &&
1415                 git tag -m "future" future &&
1416                 git checkout master &&
1417                 git for-each-ref refs/heads/master refs/tags/tag >../expect &&
1418                 git push --follow-tags ../dst master
1419         ) &&
1420         (
1421                 cd dst &&
1422                 git for-each-ref >../actual
1423         ) &&
1424         test_cmp expect actual
1425 '
1426
1427 test_expect_success 'push --no-thin must produce non-thin pack' '
1428         cat >>path1 <<\EOF &&
1429 keep base version of path1 big enough, compared to the new changes
1430 later, in order to pass size heuristics in
1431 builtin/pack-objects.c:try_delta()
1432 EOF
1433         git commit -am initial &&
1434         git init no-thin &&
1435         git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1436         git push no-thin/.git refs/heads/master:refs/heads/foo &&
1437         echo modified >> path1 &&
1438         git commit -am modified &&
1439         git repack -adf &&
1440         rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1441         git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/master:refs/heads/foo
1442 '
1443
1444 test_expect_success 'pushing a tag pushes the tagged object' '
1445         rm -rf dst.git &&
1446         blob=$(echo unreferenced | git hash-object -w --stdin) &&
1447         git tag -m foo tag-of-blob $blob &&
1448         git init --bare dst.git &&
1449         git push dst.git tag-of-blob &&
1450         # the receiving index-pack should have noticed
1451         # any problems, but we double check
1452         echo unreferenced >expect &&
1453         git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1454         test_cmp expect actual
1455 '
1456
1457 test_expect_success 'push into bare respects core.logallrefupdates' '
1458         rm -rf dst.git &&
1459         git init --bare dst.git &&
1460         git -C dst.git config core.logallrefupdates true &&
1461
1462         # double push to test both with and without
1463         # the actual pack transfer
1464         git push dst.git master:one &&
1465         echo "one@{0} push" >expect &&
1466         git -C dst.git log -g --format="%gd %gs" one >actual &&
1467         test_cmp expect actual &&
1468
1469         git push dst.git master:two &&
1470         echo "two@{0} push" >expect &&
1471         git -C dst.git log -g --format="%gd %gs" two >actual &&
1472         test_cmp expect actual
1473 '
1474
1475 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1476         rm -rf dst.git &&
1477         git init --bare dst.git &&
1478         (
1479                 cd dst.git &&
1480                 git config core.logallrefupdates true &&
1481
1482                 # as above, we double-fetch to test both
1483                 # with and without pack transfer
1484                 git fetch .. master:one &&
1485                 echo "one@{0} fetch .. master:one: storing head" >expect &&
1486                 git log -g --format="%gd %gs" one >actual &&
1487                 test_cmp expect actual &&
1488
1489                 git fetch .. master:two &&
1490                 echo "two@{0} fetch .. master:two: storing head" >expect &&
1491                 git log -g --format="%gd %gs" two >actual &&
1492                 test_cmp expect actual
1493         )
1494 '
1495
1496 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1497         git push testrepo master &&
1498         (
1499                 cd testrepo &&
1500                 git reset --hard &&
1501                 git config receive.denyCurrentBranch updateInstead
1502         ) &&
1503         test_commit third path2 &&
1504
1505         # Try pushing into a repository with pristine working tree
1506         git push testrepo master &&
1507         (
1508                 cd testrepo &&
1509                 git update-index -q --refresh &&
1510                 git diff-files --quiet -- &&
1511                 git diff-index --quiet --cached HEAD -- &&
1512                 test third = "$(cat path2)" &&
1513                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1514         ) &&
1515
1516         # Try pushing into a repository with working tree needing a refresh
1517         (
1518                 cd testrepo &&
1519                 git reset --hard HEAD^ &&
1520                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1521                 test-tool chmtime +100 path1
1522         ) &&
1523         git push testrepo master &&
1524         (
1525                 cd testrepo &&
1526                 git update-index -q --refresh &&
1527                 git diff-files --quiet -- &&
1528                 git diff-index --quiet --cached HEAD -- &&
1529                 test_cmp ../path1 path1 &&
1530                 test third = "$(cat path2)" &&
1531                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1532         ) &&
1533
1534         # Update what is to be pushed
1535         test_commit fourth path2 &&
1536
1537         # Try pushing into a repository with a dirty working tree
1538         # (1) the working tree updated
1539         (
1540                 cd testrepo &&
1541                 echo changed >path1
1542         ) &&
1543         test_must_fail git push testrepo master &&
1544         (
1545                 cd testrepo &&
1546                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1547                 git diff --quiet --cached &&
1548                 test changed = "$(cat path1)"
1549         ) &&
1550
1551         # (2) the index updated
1552         (
1553                 cd testrepo &&
1554                 echo changed >path1 &&
1555                 git add path1
1556         ) &&
1557         test_must_fail git push testrepo master &&
1558         (
1559                 cd testrepo &&
1560                 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1561                 git diff --quiet &&
1562                 test changed = "$(cat path1)"
1563         ) &&
1564
1565         # Introduce a new file in the update
1566         test_commit fifth path3 &&
1567
1568         # (3) the working tree has an untracked file that would interfere
1569         (
1570                 cd testrepo &&
1571                 git reset --hard &&
1572                 echo changed >path3
1573         ) &&
1574         test_must_fail git push testrepo master &&
1575         (
1576                 cd testrepo &&
1577                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1578                 git diff --quiet &&
1579                 git diff --quiet --cached &&
1580                 test changed = "$(cat path3)"
1581         ) &&
1582
1583         # (4) the target changes to what gets pushed but it still is a change
1584         (
1585                 cd testrepo &&
1586                 git reset --hard &&
1587                 echo fifth >path3 &&
1588                 git add path3
1589         ) &&
1590         test_must_fail git push testrepo master &&
1591         (
1592                 cd testrepo &&
1593                 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1594                 git diff --quiet &&
1595                 test fifth = "$(cat path3)"
1596         ) &&
1597
1598         # (5) push into void
1599         rm -fr void &&
1600         git init void &&
1601         (
1602                 cd void &&
1603                 git config receive.denyCurrentBranch updateInstead
1604         ) &&
1605         git push void master &&
1606         (
1607                 cd void &&
1608                 test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1609                 git diff --quiet &&
1610                 git diff --cached --quiet
1611         ) &&
1612
1613         # (6) updateInstead intervened by fast-forward check
1614         test_must_fail git push void master^:master &&
1615         test $(git -C void rev-parse HEAD) = $(git rev-parse master) &&
1616         git -C void diff --quiet &&
1617         git -C void diff --cached --quiet
1618 '
1619
1620 test_expect_success 'updateInstead with push-to-checkout hook' '
1621         rm -fr testrepo &&
1622         git init testrepo &&
1623         (
1624                 cd testrepo &&
1625                 git pull .. master &&
1626                 git reset --hard HEAD^^ &&
1627                 git tag initial &&
1628                 git config receive.denyCurrentBranch updateInstead &&
1629                 write_script .git/hooks/push-to-checkout <<-\EOF
1630                 echo >&2 updating from $(git rev-parse HEAD)
1631                 echo >&2 updating to "$1"
1632
1633                 git update-index -q --refresh &&
1634                 git read-tree -u -m HEAD "$1" || {
1635                         status=$?
1636                         echo >&2 read-tree failed
1637                         exit $status
1638                 }
1639                 EOF
1640         ) &&
1641
1642         # Try pushing into a pristine
1643         git push testrepo master &&
1644         (
1645                 cd testrepo &&
1646                 git diff --quiet &&
1647                 git diff HEAD --quiet &&
1648                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1649         ) &&
1650
1651         # Try pushing into a repository with conflicting change
1652         (
1653                 cd testrepo &&
1654                 git reset --hard initial &&
1655                 echo conflicting >path2
1656         ) &&
1657         test_must_fail git push testrepo master &&
1658         (
1659                 cd testrepo &&
1660                 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1661                 test conflicting = "$(cat path2)" &&
1662                 git diff-index --quiet --cached HEAD
1663         ) &&
1664
1665         # Try pushing into a repository with unrelated change
1666         (
1667                 cd testrepo &&
1668                 git reset --hard initial &&
1669                 echo unrelated >path1 &&
1670                 echo irrelevant >path5 &&
1671                 git add path5
1672         ) &&
1673         git push testrepo master &&
1674         (
1675                 cd testrepo &&
1676                 test "$(cat path1)" = unrelated &&
1677                 test "$(cat path5)" = irrelevant &&
1678                 test "$(git diff --name-only --cached HEAD)" = path5 &&
1679                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1680         ) &&
1681
1682         # push into void
1683         rm -fr void &&
1684         git init void &&
1685         (
1686                 cd void &&
1687                 git config receive.denyCurrentBranch updateInstead &&
1688                 write_script .git/hooks/push-to-checkout <<-\EOF
1689                 if git rev-parse --quiet --verify HEAD
1690                 then
1691                         has_head=yes
1692                         echo >&2 updating from $(git rev-parse HEAD)
1693                 else
1694                         has_head=no
1695                         echo >&2 pushing into void
1696                 fi
1697                 echo >&2 updating to "$1"
1698
1699                 git update-index -q --refresh &&
1700                 case "$has_head" in
1701                 yes)
1702                         git read-tree -u -m HEAD "$1" ;;
1703                 no)
1704                         git read-tree -u -m "$1" ;;
1705                 esac || {
1706                         status=$?
1707                         echo >&2 read-tree failed
1708                         exit $status
1709                 }
1710                 EOF
1711         ) &&
1712
1713         git push void master &&
1714         (
1715                 cd void &&
1716                 git diff --quiet &&
1717                 git diff --cached --quiet &&
1718                 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1719         )
1720 '
1721
1722 test_expect_success 'denyCurrentBranch and worktrees' '
1723         git worktree add new-wt &&
1724         git clone . cloned &&
1725         test_commit -C cloned first &&
1726         test_config receive.denyCurrentBranch refuse &&
1727         test_must_fail git -C cloned push origin HEAD:new-wt &&
1728         test_config receive.denyCurrentBranch updateInstead &&
1729         git -C cloned push origin HEAD:new-wt &&
1730         test_must_fail git -C cloned push --delete origin new-wt
1731 '
1732
1733 test_done