3 test_description
='Basic fetch/push functionality.
5 This test checks the following functionality:
9 * fast-forward detection, and overriding it
12 * --porcelain output format
17 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
18 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
26 rm -fr "$repo_name" &&
31 git config receive.denyCurrentBranch warn
&&
32 mv .git
/hooks .git
/hooks-disabled
40 mk_empty
"$repo_name" &&
44 git push
"$repo_name" $the_first_commit:refs
/$ref ||
50 echo "$the_first_commit" >expect
&&
51 git show-ref
-s --verify refs
/$ref >actual
&&
52 test_cmp expect actual ||
59 mk_test_with_hooks
() {
67 cat >pre-receive
<<-'EOF' &&
69 cat - >>pre-receive.actual
72 cat >update
<<-'EOF' &&
74 printf "%s %s %s\n" "$@" >>update.actual
77 cat >post-receive
<<-'EOF' &&
79 cat - >>post-receive.actual
82 cat >post-update
<<-'EOF' &&
86 printf "%s\n" "$ref" >>post-update.actual
90 chmod +x pre-receive update post-receive post-update
99 check_push_result
() {
101 BUG
"check_push_result requires at least 3 parameters"
112 git show-ref
-s --verify refs
/$ref >actual
&&
113 test_cmp expect actual ||
120 test_expect_success setup
'
125 git commit -a -m repo &&
126 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
131 git commit -a -m second &&
132 the_commit=$(git show-ref -s --verify refs/heads/main)
136 test_expect_success
'fetch without wildcard' '
140 git fetch .. refs/heads/main:refs/remotes/origin/main &&
142 echo "$the_commit commit refs/remotes/origin/main" >expect &&
143 git for-each-ref refs/remotes/origin >actual &&
144 test_cmp expect actual
148 test_expect_success
'fetch with wildcard' '
152 git config remote.up.url .. &&
153 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
156 echo "$the_commit commit refs/remotes/origin/main" >expect &&
157 git for-each-ref refs/remotes/origin >actual &&
158 test_cmp expect actual
162 test_expect_success
'fetch with insteadOf' '
167 git config "url.$TRASH.insteadOf" trash/ &&
168 git config remote.up.url trash/. &&
169 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
172 echo "$the_commit commit refs/remotes/origin/main" >expect &&
173 git for-each-ref refs/remotes/origin >actual &&
174 test_cmp expect actual
178 test_expect_success
'fetch with pushInsteadOf (should not rewrite)' '
183 git config "url.trash/.pushInsteadOf" "$TRASH" &&
184 git config remote.up.url "$TRASH." &&
185 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
188 echo "$the_commit commit refs/remotes/origin/main" >expect &&
189 git for-each-ref refs/remotes/origin >actual &&
190 test_cmp expect actual
197 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
200 test_expect_success
'push with negotiation' '
201 # Without negotiation
203 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
204 test_commit -C testrepo unrelated_commit &&
205 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
206 echo now pushing without negotiation &&
207 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
208 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
210 # Same commands, but with negotiation
213 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
214 test_commit -C testrepo unrelated_commit &&
215 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
216 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
217 grep_wrote 2 event # 1 commit, 1 tree
220 test_expect_success
'push with negotiation proceeds anyway even if negotiation fails' '
223 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
224 test_commit -C testrepo unrelated_commit &&
225 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
226 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
227 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
228 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
229 test_i18ngrep "push negotiation failed" err
232 test_expect_success
'push without wildcard' '
235 git push testrepo refs/heads/main:refs/remotes/origin/main &&
238 echo "$the_commit commit refs/remotes/origin/main" >expect &&
239 git for-each-ref refs/remotes/origin >actual &&
240 test_cmp expect actual
244 test_expect_success
'push with wildcard' '
247 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
250 echo "$the_commit commit refs/remotes/origin/main" >expect &&
251 git for-each-ref refs/remotes/origin >actual &&
252 test_cmp expect actual
256 test_expect_success
'push with insteadOf' '
259 test_config "url.$TRASH.insteadOf" trash/ &&
260 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
263 echo "$the_commit commit refs/remotes/origin/main" >expect &&
264 git for-each-ref refs/remotes/origin >actual &&
265 test_cmp expect actual
269 test_expect_success
'push with pushInsteadOf' '
272 test_config "url.$TRASH.pushInsteadOf" trash/ &&
273 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
276 echo "$the_commit commit refs/remotes/origin/main" >expect &&
277 git for-each-ref refs/remotes/origin >actual &&
278 test_cmp expect actual
282 test_expect_success
'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
284 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
285 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
286 test_config remote.r.url trash/wrong &&
287 test_config remote.r.pushurl "testrepo/" &&
288 git push r refs/heads/main:refs/remotes/origin/main &&
291 echo "$the_commit commit refs/remotes/origin/main" >expect &&
292 git for-each-ref refs/remotes/origin >actual &&
293 test_cmp expect actual
297 test_expect_success
'push with matching heads' '
299 mk_test testrepo heads/main &&
300 git push testrepo : &&
301 check_push_result testrepo $the_commit heads/main
305 test_expect_success
'push with matching heads on the command line' '
307 mk_test testrepo heads/main &&
308 git push testrepo : &&
309 check_push_result testrepo $the_commit heads/main
313 test_expect_success
'failed (non-fast-forward) push with matching heads' '
315 mk_test testrepo heads/main &&
316 git push testrepo : &&
317 git commit --amend -massaged &&
318 test_must_fail git push testrepo &&
319 check_push_result testrepo $the_commit heads/main &&
320 git reset --hard $the_commit
324 test_expect_success
'push --force with matching heads' '
326 mk_test testrepo heads/main &&
327 git push testrepo : &&
328 git commit --amend -massaged &&
329 git push --force testrepo : &&
330 ! check_push_result testrepo $the_commit heads/main &&
331 git reset --hard $the_commit
335 test_expect_success
'push with matching heads and forced update' '
337 mk_test testrepo heads/main &&
338 git push testrepo : &&
339 git commit --amend -massaged &&
340 git push testrepo +: &&
341 ! check_push_result testrepo $the_commit heads/main &&
342 git reset --hard $the_commit
346 test_expect_success
'push with no ambiguity (1)' '
348 mk_test testrepo heads/main &&
349 git push testrepo main:main &&
350 check_push_result testrepo $the_commit heads/main
354 test_expect_success
'push with no ambiguity (2)' '
356 mk_test testrepo remotes/origin/main &&
357 git push testrepo main:origin/main &&
358 check_push_result testrepo $the_commit remotes/origin/main
362 test_expect_success
'push with colon-less refspec, no ambiguity' '
364 mk_test testrepo heads/main heads/t/main &&
365 git branch -f t/main main &&
366 git push testrepo main &&
367 check_push_result testrepo $the_commit heads/main &&
368 check_push_result testrepo $the_first_commit heads/t/main
372 test_expect_success
'push with weak ambiguity (1)' '
374 mk_test testrepo heads/main remotes/origin/main &&
375 git push testrepo main:main &&
376 check_push_result testrepo $the_commit heads/main &&
377 check_push_result testrepo $the_first_commit remotes/origin/main
381 test_expect_success
'push with weak ambiguity (2)' '
383 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
384 git push testrepo main:main &&
385 check_push_result testrepo $the_commit heads/main &&
386 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
390 test_expect_success
'push with ambiguity' '
392 mk_test testrepo heads/frotz tags/frotz &&
393 test_must_fail git push testrepo main:frotz &&
394 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
398 test_expect_success
'push with colon-less refspec (1)' '
400 mk_test testrepo heads/frotz tags/frotz &&
401 git branch -f frotz main &&
402 git push testrepo frotz &&
403 check_push_result testrepo $the_commit heads/frotz &&
404 check_push_result testrepo $the_first_commit tags/frotz
408 test_expect_success
'push with colon-less refspec (2)' '
410 mk_test testrepo heads/frotz tags/frotz &&
411 if git show-ref --verify -q refs/heads/frotz
416 git push -f testrepo frotz &&
417 check_push_result testrepo $the_commit tags/frotz &&
418 check_push_result testrepo $the_first_commit heads/frotz
422 test_expect_success
'push with colon-less refspec (3)' '
425 if git show-ref --verify -q refs/tags/frotz
429 git branch -f frotz main &&
430 git push testrepo frotz &&
431 check_push_result testrepo $the_commit heads/frotz &&
432 test 1 = $( cd testrepo && git show-ref | wc -l )
435 test_expect_success
'push with colon-less refspec (4)' '
438 if git show-ref --verify -q refs/heads/frotz
443 git push testrepo frotz &&
444 check_push_result testrepo $the_commit tags/frotz &&
445 test 1 = $( cd testrepo && git show-ref | wc -l )
449 test_expect_success
'push head with non-existent, incomplete dest' '
452 git push testrepo main:branch &&
453 check_push_result testrepo $the_commit heads/branch
457 test_expect_success
'push tag with non-existent, incomplete dest' '
461 git push testrepo v1.0:tag &&
462 check_push_result testrepo $the_commit tags/tag
466 test_expect_success
'push sha1 with non-existent, incomplete dest' '
469 test_must_fail git push testrepo $(git rev-parse main):foo
473 test_expect_success
'push ref expression with non-existent, incomplete dest' '
476 test_must_fail git push testrepo main^:branch
483 test_expect_success
"push with $head" '
484 mk_test testrepo heads/main &&
486 git push testrepo $head &&
487 check_push_result testrepo $the_commit heads/main
490 test_expect_success
"push with $head nonexisting at remote" '
491 mk_test testrepo heads/main &&
492 git checkout -b local main &&
493 test_when_finished "git checkout main; git branch -D local" &&
494 git push testrepo $head &&
495 check_push_result testrepo $the_commit heads/local
498 test_expect_success
"push with +$head" '
499 mk_test testrepo heads/main &&
500 git checkout -b local main &&
501 test_when_finished "git checkout main; git branch -D local" &&
502 git push testrepo main local &&
503 check_push_result testrepo $the_commit heads/main &&
504 check_push_result testrepo $the_commit heads/local &&
506 # Without force rewinding should fail
507 git reset --hard $head^ &&
508 test_must_fail git push testrepo $head &&
509 check_push_result testrepo $the_commit heads/local &&
511 # With force rewinding should succeed
512 git push testrepo +$head &&
513 check_push_result testrepo $the_first_commit heads/local
516 test_expect_success
"push $head with non-existent, incomplete dest" '
519 git push testrepo $head:branch &&
520 check_push_result testrepo $the_commit heads/branch
524 test_expect_success
"push with config remote.*.push = $head" '
525 mk_test testrepo heads/local &&
527 git branch -f local $the_commit &&
528 test_when_finished "git branch -D local" &&
531 git checkout local &&
532 git reset --hard $the_first_commit
534 test_config remote.there.url testrepo &&
535 test_config remote.there.push $head &&
536 test_config branch.main.remote there &&
538 check_push_result testrepo $the_commit heads/main &&
539 check_push_result testrepo $the_first_commit heads/local
544 test_expect_success
'push with remote.pushdefault' '
545 mk_test up_repo heads/main &&
546 mk_test down_repo heads/main &&
547 test_config remote.up.url up_repo &&
548 test_config remote.down.url down_repo &&
549 test_config branch.main.remote up &&
550 test_config remote.pushdefault down &&
551 test_config push.default matching &&
553 check_push_result up_repo $the_first_commit heads/main &&
554 check_push_result down_repo $the_commit heads/main
557 test_expect_success
'push with config remote.*.pushurl' '
559 mk_test testrepo heads/main &&
561 test_config remote.there.url test2repo &&
562 test_config remote.there.pushurl testrepo &&
564 check_push_result testrepo $the_commit heads/main
567 test_expect_success
'push with config branch.*.pushremote' '
568 mk_test up_repo heads/main &&
569 mk_test side_repo heads/main &&
570 mk_test down_repo heads/main &&
571 test_config remote.up.url up_repo &&
572 test_config remote.pushdefault side_repo &&
573 test_config remote.down.url down_repo &&
574 test_config branch.main.remote up &&
575 test_config branch.main.pushremote down &&
576 test_config push.default matching &&
578 check_push_result up_repo $the_first_commit heads/main &&
579 check_push_result side_repo $the_first_commit heads/main &&
580 check_push_result down_repo $the_commit heads/main
583 test_expect_success
'branch.*.pushremote config order is irrelevant' '
584 mk_test one_repo heads/main &&
585 mk_test two_repo heads/main &&
586 test_config remote.one.url one_repo &&
587 test_config remote.two.url two_repo &&
588 test_config branch.main.pushremote two_repo &&
589 test_config remote.pushdefault one_repo &&
590 test_config push.default matching &&
592 check_push_result one_repo $the_first_commit heads/main &&
593 check_push_result two_repo $the_commit heads/main
596 test_expect_success
'push with dry-run' '
598 mk_test testrepo heads/main &&
599 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
600 git push --dry-run testrepo : &&
601 check_push_result testrepo $old_commit heads/main
604 test_expect_success
'push updates local refs' '
606 mk_test testrepo heads/main &&
607 mk_child testrepo child &&
612 test $(git rev-parse main) = \
613 $(git rev-parse remotes/origin/main)
618 test_expect_success
'push updates up-to-date local refs' '
620 mk_test testrepo heads/main &&
621 mk_child testrepo child1 &&
622 mk_child testrepo child2 &&
623 (cd child1 && git pull .. main && git push) &&
626 git pull ../child1 main &&
628 test $(git rev-parse main) = \
629 $(git rev-parse remotes/origin/main)
634 test_expect_success
'push preserves up-to-date packed refs' '
636 mk_test testrepo heads/main &&
637 mk_child testrepo child &&
641 ! test -f .git/refs/remotes/origin/main
646 test_expect_success
'push does not update local refs on failure' '
648 mk_test testrepo heads/main &&
649 mk_child testrepo child &&
650 mkdir testrepo/.git/hooks &&
651 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
652 chmod +x testrepo/.git/hooks/pre-receive &&
656 test_must_fail git push &&
657 test $(git rev-parse main) != \
658 $(git rev-parse remotes/origin/main)
663 test_expect_success
'allow deleting an invalid remote ref' '
665 mk_test testrepo heads/branch &&
666 rm -f testrepo/.git/objects/??/* &&
667 git push testrepo :refs/heads/branch &&
668 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
672 test_expect_success
'pushing valid refs triggers post-receive and post-update hooks' '
673 mk_test_with_hooks testrepo heads/main heads/next &&
674 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
675 newmain=$(git show-ref -s --verify refs/heads/main) &&
676 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
678 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
681 cat >pre-receive.expect <<-EOF &&
682 $orgmain $newmain refs/heads/main
683 $orgnext $newnext refs/heads/next
686 cat >update.expect <<-EOF &&
687 refs/heads/main $orgmain $newmain
688 refs/heads/next $orgnext $newnext
691 cat >post-receive.expect <<-EOF &&
692 $orgmain $newmain refs/heads/main
693 $orgnext $newnext refs/heads/next
696 cat >post-update.expect <<-EOF &&
701 test_cmp pre-receive.expect pre-receive.actual &&
702 test_cmp update.expect update.actual &&
703 test_cmp post-receive.expect post-receive.actual &&
704 test_cmp post-update.expect post-update.actual
708 test_expect_success
'deleting dangling ref triggers hooks with correct args' '
709 mk_test_with_hooks testrepo heads/branch &&
710 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
711 rm -f testrepo/.git/objects/??/* &&
712 git push testrepo :refs/heads/branch &&
715 cat >pre-receive.expect <<-EOF &&
716 $orig $ZERO_OID refs/heads/branch
719 cat >update.expect <<-EOF &&
720 refs/heads/branch $orig $ZERO_OID
723 cat >post-receive.expect <<-EOF &&
724 $orig $ZERO_OID refs/heads/branch
727 cat >post-update.expect <<-EOF &&
731 test_cmp pre-receive.expect pre-receive.actual &&
732 test_cmp update.expect update.actual &&
733 test_cmp post-receive.expect post-receive.actual &&
734 test_cmp post-update.expect post-update.actual
738 test_expect_success
'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
739 mk_test_with_hooks testrepo heads/main &&
740 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
741 newmain=$(git show-ref -s --verify refs/heads/main) &&
742 git push testrepo main :refs/heads/nonexistent &&
745 cat >pre-receive.expect <<-EOF &&
746 $orgmain $newmain refs/heads/main
747 $ZERO_OID $ZERO_OID refs/heads/nonexistent
750 cat >update.expect <<-EOF &&
751 refs/heads/main $orgmain $newmain
752 refs/heads/nonexistent $ZERO_OID $ZERO_OID
755 cat >post-receive.expect <<-EOF &&
756 $orgmain $newmain refs/heads/main
759 cat >post-update.expect <<-EOF &&
763 test_cmp pre-receive.expect pre-receive.actual &&
764 test_cmp update.expect update.actual &&
765 test_cmp post-receive.expect post-receive.actual &&
766 test_cmp post-update.expect post-update.actual
770 test_expect_success
'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
771 mk_test_with_hooks testrepo heads/main &&
772 git push testrepo :refs/heads/nonexistent &&
775 cat >pre-receive.expect <<-EOF &&
776 $ZERO_OID $ZERO_OID refs/heads/nonexistent
779 cat >update.expect <<-EOF &&
780 refs/heads/nonexistent $ZERO_OID $ZERO_OID
783 test_cmp pre-receive.expect pre-receive.actual &&
784 test_cmp update.expect update.actual &&
785 test_path_is_missing post-receive.actual &&
786 test_path_is_missing post-update.actual
790 test_expect_success
'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
791 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
792 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
793 newmain=$(git show-ref -s --verify refs/heads/main) &&
794 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
796 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
797 newseen=$(git show-ref -s --verify refs/heads/main) &&
798 git push testrepo refs/heads/main:refs/heads/main \
799 refs/heads/main:refs/heads/seen :refs/heads/next \
800 :refs/heads/nonexistent &&
803 cat >pre-receive.expect <<-EOF &&
804 $orgmain $newmain refs/heads/main
805 $orgnext $newnext refs/heads/next
806 $orgseen $newseen refs/heads/seen
807 $ZERO_OID $ZERO_OID refs/heads/nonexistent
810 cat >update.expect <<-EOF &&
811 refs/heads/main $orgmain $newmain
812 refs/heads/next $orgnext $newnext
813 refs/heads/seen $orgseen $newseen
814 refs/heads/nonexistent $ZERO_OID $ZERO_OID
817 cat >post-receive.expect <<-EOF &&
818 $orgmain $newmain refs/heads/main
819 $orgnext $newnext refs/heads/next
820 $orgseen $newseen refs/heads/seen
823 cat >post-update.expect <<-EOF &&
829 test_cmp pre-receive.expect pre-receive.actual &&
830 test_cmp update.expect update.actual &&
831 test_cmp post-receive.expect post-receive.actual &&
832 test_cmp post-update.expect post-update.actual
836 test_expect_success
'allow deleting a ref using --delete' '
837 mk_test testrepo heads/main &&
838 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
839 git push testrepo --delete main &&
840 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
843 test_expect_success
'allow deleting a tag using --delete' '
844 mk_test testrepo heads/main &&
845 git tag -a -m dummy_message deltag heads/main &&
846 git push testrepo --tags &&
847 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
848 git push testrepo --delete tag deltag &&
849 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
852 test_expect_success
'push --delete without args aborts' '
853 mk_test testrepo heads/main &&
854 test_must_fail git push testrepo --delete
857 test_expect_success
'push --delete refuses src:dest refspecs' '
858 mk_test testrepo heads/main &&
859 test_must_fail git push testrepo --delete main:foo
862 test_expect_success
'push --delete refuses empty string' '
863 mk_test testrepo heads/master &&
864 test_must_fail git push testrepo --delete ""
867 test_expect_success
'warn on push to HEAD of non-bare repository' '
868 mk_test testrepo heads/main &&
872 git config receive.denyCurrentBranch warn
874 git push testrepo main 2>stderr &&
875 grep "warning: updating the current branch" stderr
878 test_expect_success
'deny push to HEAD of non-bare repository' '
879 mk_test testrepo heads/main &&
883 git config receive.denyCurrentBranch true
885 test_must_fail git push testrepo main
888 test_expect_success
'allow push to HEAD of bare repository (bare)' '
889 mk_test testrepo heads/main &&
893 git config receive.denyCurrentBranch true &&
894 git config core.bare true
896 git push testrepo main 2>stderr &&
897 ! grep "warning: updating the current branch" stderr
900 test_expect_success
'allow push to HEAD of non-bare repository (config)' '
901 mk_test testrepo heads/main &&
905 git config receive.denyCurrentBranch false
907 git push testrepo main 2>stderr &&
908 ! grep "warning: updating the current branch" stderr
911 test_expect_success
'fetch with branches' '
913 git branch second $the_first_commit &&
914 git checkout second &&
915 echo ".." > testrepo/.git/branches/branch1 &&
919 echo "$the_commit commit refs/heads/branch1" >expect &&
920 git for-each-ref refs/heads >actual &&
921 test_cmp expect actual
926 test_expect_success
'fetch with branches containing #' '
928 echo "..#second" > testrepo/.git/branches/branch2 &&
932 echo "$the_first_commit commit refs/heads/branch2" >expect &&
933 git for-each-ref refs/heads >actual &&
934 test_cmp expect actual
939 test_expect_success
'push with branches' '
941 git checkout second &&
942 echo "testrepo" > .git/branches/branch1 &&
946 echo "$the_first_commit commit refs/heads/main" >expect &&
947 git for-each-ref refs/heads >actual &&
948 test_cmp expect actual
952 test_expect_success
'push with branches containing #' '
954 echo "testrepo#branch3" > .git/branches/branch2 &&
958 echo "$the_first_commit commit refs/heads/branch3" >expect &&
959 git for-each-ref refs/heads >actual &&
960 test_cmp expect actual
965 test_expect_success
'push into aliased refs (consistent)' '
966 mk_test testrepo heads/main &&
967 mk_child testrepo child1 &&
968 mk_child testrepo child2 &&
972 git symbolic-ref refs/heads/bar refs/heads/foo &&
973 git config receive.denyCurrentBranch false
980 git commit -a -m child2 &&
983 git push ../child1 foo bar
987 test_expect_success
'push into aliased refs (inconsistent)' '
988 mk_test testrepo heads/main &&
989 mk_child testrepo child1 &&
990 mk_child testrepo child2 &&
994 git symbolic-ref refs/heads/bar refs/heads/foo &&
995 git config receive.denyCurrentBranch false
1002 git commit -a -m child2 &&
1007 git commit -a -m child2 &&
1009 test_must_fail git push ../child1 foo bar 2>stderr &&
1010 grep "refusing inconsistent update" stderr
1014 test_force_push_tag
() {
1015 tag_type_description
=$1
1018 test_expect_success
"force pushing required to update $tag_type_description" "
1019 mk_test testrepo heads/main &&
1020 mk_child testrepo child1 &&
1021 mk_child testrepo child2 &&
1025 git push ../child2 testTag &&
1028 git commit -m 'file1' &&
1029 git tag $tag_args testTag &&
1030 test_must_fail git push ../child2 testTag &&
1031 git push --force ../child2 testTag &&
1032 git tag $tag_args testTag HEAD~ &&
1033 test_must_fail git push ../child2 testTag &&
1034 git push --force ../child2 testTag &&
1036 # Clobbering without + in refspec needs --force
1037 git tag -f testTag &&
1038 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1039 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1041 # Clobbering with + in refspec does not need --force
1042 git tag -f testTag HEAD~ &&
1043 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1045 # Clobbering with --no-force still obeys + in refspec
1046 git tag -f testTag &&
1047 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1049 # Clobbering with/without --force and 'tag <name>' format
1050 git tag -f testTag HEAD~ &&
1051 test_must_fail git push ../child2 tag testTag &&
1052 git push --force ../child2 tag testTag
1057 test_force_push_tag
"lightweight tag" "-f"
1058 test_force_push_tag
"annotated tag" "-f -a -m'tag message'"
1060 test_force_fetch_tag
() {
1061 tag_type_description
=$1
1064 test_expect_success
"fetch will not clobber an existing $tag_type_description without --force" "
1065 mk_test testrepo heads/main &&
1066 mk_child testrepo child1 &&
1067 mk_child testrepo child2 &&
1071 git -C ../child1 fetch origin tag testTag &&
1074 git commit -m 'file1' &&
1075 git tag $tag_args testTag &&
1076 test_must_fail git -C ../child1 fetch origin tag testTag &&
1077 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1082 test_force_fetch_tag
"lightweight tag" "-f"
1083 test_force_fetch_tag
"annotated tag" "-f -a -m'tag message'"
1085 test_expect_success
'push --porcelain' '
1086 mk_empty testrepo &&
1087 echo >.git/foo "To testrepo" &&
1088 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1089 echo >>.git/foo "Done" &&
1090 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1093 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1094 git for-each-ref refs/remotes/origin >actual &&
1095 test_cmp expect actual
1097 test_cmp .git/foo .git/bar
1100 test_expect_success
'push --porcelain bad url' '
1101 mk_empty testrepo &&
1102 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1103 ! grep -q Done .git/bar
1106 test_expect_success
'push --porcelain rejected' '
1107 mk_empty testrepo &&
1108 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1110 git reset --hard origin/main^ &&
1111 git config receive.denyCurrentBranch true) &&
1113 echo >.git/foo "To testrepo" &&
1114 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1115 echo >>.git/foo "Done" &&
1117 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1118 test_cmp .git/foo .git/bar
1121 test_expect_success
'push --porcelain --dry-run rejected' '
1122 mk_empty testrepo &&
1123 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1125 git reset --hard origin/main &&
1126 git config receive.denyCurrentBranch true) &&
1128 echo >.git/foo "To testrepo" &&
1129 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1130 echo >>.git/foo "Done" &&
1132 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1133 test_cmp .git/foo .git/bar
1136 test_expect_success
'push --prune' '
1137 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1138 git push --prune testrepo : &&
1139 check_push_result testrepo $the_commit heads/main &&
1140 check_push_result testrepo $the_first_commit heads/second &&
1141 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1144 test_expect_success
'push --prune refspec' '
1145 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1146 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1147 check_push_result testrepo $the_commit tmp/main &&
1148 check_push_result testrepo $the_first_commit tmp/second &&
1149 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1152 for configsection
in transfer receive
1154 test_expect_success
"push to update a ref hidden by $configsection.hiderefs" '
1155 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1158 git config $configsection.hiderefs refs/hidden
1161 # push to unhidden ref succeeds normally
1162 git push testrepo main:refs/heads/main &&
1163 check_push_result testrepo $the_commit heads/main &&
1165 # push to update a hidden ref should fail
1166 test_must_fail git push testrepo main:refs/hidden/one &&
1167 check_push_result testrepo $the_first_commit hidden/one &&
1169 # push to delete a hidden ref should fail
1170 test_must_fail git push testrepo :refs/hidden/two &&
1171 check_push_result testrepo $the_first_commit hidden/two &&
1173 # idempotent push to update a hidden ref should fail
1174 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1175 check_push_result testrepo $the_first_commit hidden/three
1179 test_expect_success
'fetch exact SHA1' '
1180 mk_test testrepo heads/main hidden/one &&
1181 git push testrepo main:refs/hidden/one &&
1184 git config transfer.hiderefs refs/hidden
1186 check_push_result testrepo $the_commit hidden/one &&
1188 mk_child testrepo child &&
1192 # make sure $the_commit does not exist here
1195 test_must_fail git cat-file -t $the_commit &&
1197 # Some protocol versions (e.g. 2) support fetching
1198 # unadvertised objects, so restrict this test to v0.
1200 # fetching the hidden object should fail by default
1201 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1202 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1203 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1204 test_must_fail git rev-parse --verify refs/heads/copy &&
1206 # the server side can allow it to succeed
1209 git config uploadpack.allowtipsha1inwant true
1212 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1213 cat >expect <<-EOF &&
1218 git rev-parse --verify refs/heads/copy &&
1219 git rev-parse --verify refs/heads/extra
1221 test_cmp expect actual
1225 test_expect_success
'fetch exact SHA1 in protocol v2' '
1226 mk_test testrepo heads/main hidden/one &&
1227 git push testrepo main:refs/hidden/one &&
1228 git -C testrepo config transfer.hiderefs refs/hidden &&
1229 check_push_result testrepo $the_commit hidden/one &&
1231 mk_child testrepo child &&
1232 git -C child config protocol.version 2 &&
1234 # make sure $the_commit does not exist here
1235 git -C child repack -a -d &&
1236 git -C child prune &&
1237 test_must_fail git -C child cat-file -t $the_commit &&
1239 # fetching the hidden object succeeds by default
1240 # NEEDSWORK: should this match the v0 behavior instead?
1241 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1244 for configallowtipsha1inwant
in true false
1246 test_expect_success
"shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1247 mk_empty testrepo &&
1250 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1251 git commit --allow-empty -m foo &&
1252 git commit --allow-empty -m bar
1254 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1258 # Some protocol versions (e.g. 2) support fetching
1259 # unadvertised objects, so restrict this test to v0.
1260 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1261 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1262 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1263 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1264 git cat-file commit $SHA1
1268 test_expect_success
"deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1269 mk_empty testrepo &&
1272 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1273 git commit --allow-empty -m foo &&
1274 git commit --allow-empty -m bar &&
1275 git commit --allow-empty -m xyz
1277 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1278 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1279 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1282 git reset --hard $SHA1_2 &&
1283 git cat-file commit $SHA1_1 &&
1284 git cat-file commit $SHA1_3
1289 # Some protocol versions (e.g. 2) support fetching
1290 # unadvertised objects, so restrict this test to v0.
1291 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1292 git fetch ../testrepo/.git $SHA1_3 &&
1293 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1294 git fetch ../testrepo/.git $SHA1_1 &&
1295 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1296 git fetch ../testrepo/.git $SHA1_1 &&
1297 git cat-file commit $SHA1_1 &&
1298 test_must_fail git cat-file commit $SHA1_2 &&
1299 git fetch ../testrepo/.git $SHA1_2 &&
1300 git cat-file commit $SHA1_2 &&
1301 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1302 git fetch ../testrepo/.git $SHA1_3 2>err &&
1303 # ideally we would insist this be on a "remote error:"
1304 # line, but it is racy; see the commit message
1305 test_i18ngrep "not our ref.*$SHA1_3\$" err
1310 test_expect_success
'fetch follows tags by default' '
1311 mk_test testrepo heads/main &&
1316 git pull ../testrepo main &&
1317 git tag -m "annotated" tag &&
1318 git for-each-ref >tmp1 &&
1321 sed -n "s|refs/heads/main$|refs/remotes/origin/main|p" tmp1
1323 sort -k 3 >../expect
1328 git remote add origin ../src &&
1329 git config branch.main.remote origin &&
1330 git config branch.main.merge refs/heads/main &&
1332 git for-each-ref >../actual
1334 test_cmp expect actual
1337 test_expect_success
'peeled advertisements are not considered ref tips' '
1338 mk_empty testrepo &&
1339 git -C testrepo commit --allow-empty -m one &&
1340 git -C testrepo commit --allow-empty -m two &&
1341 git -C testrepo tag -m foo mytag HEAD^ &&
1342 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1343 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1344 git fetch testrepo $oid 2>err &&
1345 test_i18ngrep "Server does not allow request for unadvertised object" err
1348 test_expect_success
'pushing a specific ref applies remote.$name.push as refmap' '
1349 mk_test testrepo heads/main &&
1352 git init --bare dst &&
1355 git pull ../testrepo main &&
1357 git config remote.dst.url ../dst &&
1358 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1359 git push dst main &&
1360 git show-ref refs/heads/main |
1361 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1365 test_must_fail git show-ref refs/heads/next &&
1366 test_must_fail git show-ref refs/heads/main &&
1367 git show-ref refs/remotes/src/main >actual
1369 test_cmp dst/expect dst/actual
1372 test_expect_success
'with no remote.$name.push, it is not used as refmap' '
1373 mk_test testrepo heads/main &&
1376 git init --bare dst &&
1379 git pull ../testrepo main &&
1381 git config remote.dst.url ../dst &&
1382 git config push.default matching &&
1383 git push dst main &&
1384 git show-ref refs/heads/main >../dst/expect
1388 test_must_fail git show-ref refs/heads/next &&
1389 git show-ref refs/heads/main >actual
1391 test_cmp dst/expect dst/actual
1394 test_expect_success
'with no remote.$name.push, upstream mapping is used' '
1395 mk_test testrepo heads/main &&
1398 git init --bare dst &&
1401 git pull ../testrepo main &&
1403 git config remote.dst.url ../dst &&
1404 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1405 git config push.default upstream &&
1407 git config branch.main.merge refs/heads/trunk &&
1408 git config branch.main.remote dst &&
1410 git push dst main &&
1411 git show-ref refs/heads/main |
1412 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1416 test_must_fail git show-ref refs/heads/main &&
1417 test_must_fail git show-ref refs/heads/next &&
1418 git show-ref refs/heads/trunk >actual
1420 test_cmp dst/expect dst/actual
1423 test_expect_success
'push does not follow tags by default' '
1424 mk_test testrepo heads/main &&
1427 git init --bare dst &&
1430 git pull ../testrepo main &&
1431 git tag -m "annotated" tag &&
1432 git checkout -b another &&
1433 git commit --allow-empty -m "future commit" &&
1434 git tag -m "future" future &&
1435 git checkout main &&
1436 git for-each-ref refs/heads/main >../expect &&
1437 git push ../dst main
1441 git for-each-ref >../actual
1443 test_cmp expect actual
1446 test_expect_success
'push --follow-tags only pushes relevant tags' '
1447 mk_test testrepo heads/main &&
1450 git init --bare dst &&
1453 git pull ../testrepo main &&
1454 git tag -m "annotated" tag &&
1455 git checkout -b another &&
1456 git commit --allow-empty -m "future commit" &&
1457 git tag -m "future" future &&
1458 git checkout main &&
1459 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1460 git push --follow-tags ../dst main
1464 git for-each-ref >../actual
1466 test_cmp expect actual
1469 test_expect_success
'push --no-thin must produce non-thin pack' '
1470 cat >>path1 <<\EOF &&
1471 keep base version of path1 big enough, compared to the new changes
1472 later, in order to pass size heuristics in
1473 builtin/pack-objects.c:try_delta()
1475 git commit -am initial &&
1477 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1478 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1479 echo modified >> path1 &&
1480 git commit -am modified &&
1482 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1483 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1486 test_expect_success
'pushing a tag pushes the tagged object' '
1488 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1489 git tag -m foo tag-of-blob $blob &&
1490 git init --bare dst.git &&
1491 git push dst.git tag-of-blob &&
1492 # the receiving index-pack should have noticed
1493 # any problems, but we double check
1494 echo unreferenced >expect &&
1495 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1496 test_cmp expect actual
1499 test_expect_success
'push into bare respects core.logallrefupdates' '
1501 git init --bare dst.git &&
1502 git -C dst.git config core.logallrefupdates true &&
1504 # double push to test both with and without
1505 # the actual pack transfer
1506 git push dst.git main:one &&
1507 echo "one@{0} push" >expect &&
1508 git -C dst.git log -g --format="%gd %gs" one >actual &&
1509 test_cmp expect actual &&
1511 git push dst.git main:two &&
1512 echo "two@{0} push" >expect &&
1513 git -C dst.git log -g --format="%gd %gs" two >actual &&
1514 test_cmp expect actual
1517 test_expect_success
'fetch into bare respects core.logallrefupdates' '
1519 git init --bare dst.git &&
1522 git config core.logallrefupdates true &&
1524 # as above, we double-fetch to test both
1525 # with and without pack transfer
1526 git fetch .. main:one &&
1527 echo "one@{0} fetch .. main:one: storing head" >expect &&
1528 git log -g --format="%gd %gs" one >actual &&
1529 test_cmp expect actual &&
1531 git fetch .. main:two &&
1532 echo "two@{0} fetch .. main:two: storing head" >expect &&
1533 git log -g --format="%gd %gs" two >actual &&
1534 test_cmp expect actual
1538 test_expect_success
'receive.denyCurrentBranch = updateInstead' '
1539 git push testrepo main &&
1543 git config receive.denyCurrentBranch updateInstead
1545 test_commit third path2 &&
1547 # Try pushing into a repository with pristine working tree
1548 git push testrepo main &&
1551 git update-index -q --refresh &&
1552 git diff-files --quiet -- &&
1553 git diff-index --quiet --cached HEAD -- &&
1554 test third = "$(cat path2)" &&
1555 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1558 # Try pushing into a repository with working tree needing a refresh
1561 git reset --hard HEAD^ &&
1562 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1563 test-tool chmtime +100 path1
1565 git push testrepo main &&
1568 git update-index -q --refresh &&
1569 git diff-files --quiet -- &&
1570 git diff-index --quiet --cached HEAD -- &&
1571 test_cmp ../path1 path1 &&
1572 test third = "$(cat path2)" &&
1573 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1576 # Update what is to be pushed
1577 test_commit fourth path2 &&
1579 # Try pushing into a repository with a dirty working tree
1580 # (1) the working tree updated
1585 test_must_fail git push testrepo main &&
1588 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1589 git diff --quiet --cached &&
1590 test changed = "$(cat path1)"
1593 # (2) the index updated
1596 echo changed >path1 &&
1599 test_must_fail git push testrepo main &&
1602 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1604 test changed = "$(cat path1)"
1607 # Introduce a new file in the update
1608 test_commit fifth path3 &&
1610 # (3) the working tree has an untracked file that would interfere
1616 test_must_fail git push testrepo main &&
1619 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1621 git diff --quiet --cached &&
1622 test changed = "$(cat path3)"
1625 # (4) the target changes to what gets pushed but it still is a change
1629 echo fifth >path3 &&
1632 test_must_fail git push testrepo main &&
1635 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1637 test fifth = "$(cat path3)"
1640 # (5) push into void
1645 git config receive.denyCurrentBranch updateInstead
1647 git push void main &&
1650 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1652 git diff --cached --quiet
1655 # (6) updateInstead intervened by fast-forward check
1656 test_must_fail git push void main^:main &&
1657 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1658 git -C void diff --quiet &&
1659 git -C void diff --cached --quiet
1662 test_expect_success
'updateInstead with push-to-checkout hook' '
1664 git init testrepo &&
1668 git reset --hard HEAD^^ &&
1670 git config receive.denyCurrentBranch updateInstead &&
1671 write_script .git/hooks/push-to-checkout <<-\EOF
1672 echo >&2 updating from $(git rev-parse HEAD)
1673 echo >&2 updating to "$1"
1675 git update-index -q --refresh &&
1676 git read-tree -u -m HEAD "$1" || {
1678 echo >&2 read-tree failed
1684 # Try pushing into a pristine
1685 git push testrepo main &&
1689 git diff HEAD --quiet &&
1690 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1693 # Try pushing into a repository with conflicting change
1696 git reset --hard initial &&
1697 echo conflicting >path2
1699 test_must_fail git push testrepo main &&
1702 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1703 test conflicting = "$(cat path2)" &&
1704 git diff-index --quiet --cached HEAD
1707 # Try pushing into a repository with unrelated change
1710 git reset --hard initial &&
1711 echo unrelated >path1 &&
1712 echo irrelevant >path5 &&
1715 git push testrepo main &&
1718 test "$(cat path1)" = unrelated &&
1719 test "$(cat path5)" = irrelevant &&
1720 test "$(git diff --name-only --cached HEAD)" = path5 &&
1721 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1729 git config receive.denyCurrentBranch updateInstead &&
1730 write_script .git/hooks/push-to-checkout <<-\EOF
1731 if git rev-parse --quiet --verify HEAD
1734 echo >&2 updating from $(git rev-parse HEAD)
1737 echo >&2 pushing into void
1739 echo >&2 updating to "$1"
1741 git update-index -q --refresh &&
1744 git read-tree -u -m HEAD "$1" ;;
1746 git read-tree -u -m "$1" ;;
1749 echo >&2 read-tree failed
1755 git push void main &&
1759 git diff --cached --quiet &&
1760 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1764 test_expect_success
'denyCurrentBranch and worktrees' '
1765 git worktree add new-wt &&
1766 git clone . cloned &&
1767 test_commit -C cloned first &&
1768 test_config receive.denyCurrentBranch refuse &&
1769 test_must_fail git -C cloned push origin HEAD:new-wt &&
1770 test_config receive.denyCurrentBranch updateInstead &&
1771 git -C cloned push origin HEAD:new-wt &&
1772 test_must_fail git -C cloned push --delete origin new-wt