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
194 test_expect_success
'push without wildcard' '
197 git push testrepo refs/heads/main:refs/remotes/origin/main &&
200 echo "$the_commit commit refs/remotes/origin/main" >expect &&
201 git for-each-ref refs/remotes/origin >actual &&
202 test_cmp expect actual
206 test_expect_success
'push with wildcard' '
209 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
212 echo "$the_commit commit refs/remotes/origin/main" >expect &&
213 git for-each-ref refs/remotes/origin >actual &&
214 test_cmp expect actual
218 test_expect_success
'push with insteadOf' '
221 test_config "url.$TRASH.insteadOf" trash/ &&
222 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
225 echo "$the_commit commit refs/remotes/origin/main" >expect &&
226 git for-each-ref refs/remotes/origin >actual &&
227 test_cmp expect actual
231 test_expect_success
'push with pushInsteadOf' '
234 test_config "url.$TRASH.pushInsteadOf" trash/ &&
235 git push trash/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 pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
246 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
247 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
248 test_config remote.r.url trash/wrong &&
249 test_config remote.r.pushurl "testrepo/" &&
250 git push r refs/heads/main:refs/remotes/origin/main &&
253 echo "$the_commit commit refs/remotes/origin/main" >expect &&
254 git for-each-ref refs/remotes/origin >actual &&
255 test_cmp expect actual
259 test_expect_success
'push with matching heads' '
261 mk_test testrepo heads/main &&
262 git push testrepo : &&
263 check_push_result testrepo $the_commit heads/main
267 test_expect_success
'push with matching heads on the command line' '
269 mk_test testrepo heads/main &&
270 git push testrepo : &&
271 check_push_result testrepo $the_commit heads/main
275 test_expect_success
'failed (non-fast-forward) push with matching heads' '
277 mk_test testrepo heads/main &&
278 git push testrepo : &&
279 git commit --amend -massaged &&
280 test_must_fail git push testrepo &&
281 check_push_result testrepo $the_commit heads/main &&
282 git reset --hard $the_commit
286 test_expect_success
'push --force with matching heads' '
288 mk_test testrepo heads/main &&
289 git push testrepo : &&
290 git commit --amend -massaged &&
291 git push --force testrepo : &&
292 ! check_push_result testrepo $the_commit heads/main &&
293 git reset --hard $the_commit
297 test_expect_success
'push with matching heads and forced update' '
299 mk_test testrepo heads/main &&
300 git push testrepo : &&
301 git commit --amend -massaged &&
302 git push testrepo +: &&
303 ! check_push_result testrepo $the_commit heads/main &&
304 git reset --hard $the_commit
308 test_expect_success
'push with no ambiguity (1)' '
310 mk_test testrepo heads/main &&
311 git push testrepo main:main &&
312 check_push_result testrepo $the_commit heads/main
316 test_expect_success
'push with no ambiguity (2)' '
318 mk_test testrepo remotes/origin/main &&
319 git push testrepo main:origin/main &&
320 check_push_result testrepo $the_commit remotes/origin/main
324 test_expect_success
'push with colon-less refspec, no ambiguity' '
326 mk_test testrepo heads/main heads/t/main &&
327 git branch -f t/main main &&
328 git push testrepo main &&
329 check_push_result testrepo $the_commit heads/main &&
330 check_push_result testrepo $the_first_commit heads/t/main
334 test_expect_success
'push with weak ambiguity (1)' '
336 mk_test testrepo heads/main remotes/origin/main &&
337 git push testrepo main:main &&
338 check_push_result testrepo $the_commit heads/main &&
339 check_push_result testrepo $the_first_commit remotes/origin/main
343 test_expect_success
'push with weak ambiguity (2)' '
345 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
346 git push testrepo main:main &&
347 check_push_result testrepo $the_commit heads/main &&
348 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
352 test_expect_success
'push with ambiguity' '
354 mk_test testrepo heads/frotz tags/frotz &&
355 test_must_fail git push testrepo main:frotz &&
356 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
360 test_expect_success
'push with colon-less refspec (1)' '
362 mk_test testrepo heads/frotz tags/frotz &&
363 git branch -f frotz main &&
364 git push testrepo frotz &&
365 check_push_result testrepo $the_commit heads/frotz &&
366 check_push_result testrepo $the_first_commit tags/frotz
370 test_expect_success
'push with colon-less refspec (2)' '
372 mk_test testrepo heads/frotz tags/frotz &&
373 if git show-ref --verify -q refs/heads/frotz
378 git push -f testrepo frotz &&
379 check_push_result testrepo $the_commit tags/frotz &&
380 check_push_result testrepo $the_first_commit heads/frotz
384 test_expect_success
'push with colon-less refspec (3)' '
387 if git show-ref --verify -q refs/tags/frotz
391 git branch -f frotz main &&
392 git push testrepo frotz &&
393 check_push_result testrepo $the_commit heads/frotz &&
394 test 1 = $( cd testrepo && git show-ref | wc -l )
397 test_expect_success
'push with colon-less refspec (4)' '
400 if git show-ref --verify -q refs/heads/frotz
405 git push testrepo frotz &&
406 check_push_result testrepo $the_commit tags/frotz &&
407 test 1 = $( cd testrepo && git show-ref | wc -l )
411 test_expect_success
'push head with non-existent, incomplete dest' '
414 git push testrepo main:branch &&
415 check_push_result testrepo $the_commit heads/branch
419 test_expect_success
'push tag with non-existent, incomplete dest' '
423 git push testrepo v1.0:tag &&
424 check_push_result testrepo $the_commit tags/tag
428 test_expect_success
'push sha1 with non-existent, incomplete dest' '
431 test_must_fail git push testrepo $(git rev-parse main):foo
435 test_expect_success
'push ref expression with non-existent, incomplete dest' '
438 test_must_fail git push testrepo main^:branch
445 test_expect_success
"push with $head" '
446 mk_test testrepo heads/main &&
448 git push testrepo $head &&
449 check_push_result testrepo $the_commit heads/main
452 test_expect_success
"push with $head nonexisting at remote" '
453 mk_test testrepo heads/main &&
454 git checkout -b local main &&
455 test_when_finished "git checkout main; git branch -D local" &&
456 git push testrepo $head &&
457 check_push_result testrepo $the_commit heads/local
460 test_expect_success
"push with +$head" '
461 mk_test testrepo heads/main &&
462 git checkout -b local main &&
463 test_when_finished "git checkout main; git branch -D local" &&
464 git push testrepo main local &&
465 check_push_result testrepo $the_commit heads/main &&
466 check_push_result testrepo $the_commit heads/local &&
468 # Without force rewinding should fail
469 git reset --hard $head^ &&
470 test_must_fail git push testrepo $head &&
471 check_push_result testrepo $the_commit heads/local &&
473 # With force rewinding should succeed
474 git push testrepo +$head &&
475 check_push_result testrepo $the_first_commit heads/local
478 test_expect_success
"push $head with non-existent, incomplete dest" '
481 git push testrepo $head:branch &&
482 check_push_result testrepo $the_commit heads/branch
486 test_expect_success
"push with config remote.*.push = $head" '
487 mk_test testrepo heads/local &&
489 git branch -f local $the_commit &&
490 test_when_finished "git branch -D local" &&
493 git checkout local &&
494 git reset --hard $the_first_commit
496 test_config remote.there.url testrepo &&
497 test_config remote.there.push $head &&
498 test_config branch.main.remote there &&
500 check_push_result testrepo $the_commit heads/main &&
501 check_push_result testrepo $the_first_commit heads/local
506 test_expect_success
'push with remote.pushdefault' '
507 mk_test up_repo heads/main &&
508 mk_test down_repo heads/main &&
509 test_config remote.up.url up_repo &&
510 test_config remote.down.url down_repo &&
511 test_config branch.main.remote up &&
512 test_config remote.pushdefault down &&
513 test_config push.default matching &&
515 check_push_result up_repo $the_first_commit heads/main &&
516 check_push_result down_repo $the_commit heads/main
519 test_expect_success
'push with config remote.*.pushurl' '
521 mk_test testrepo heads/main &&
523 test_config remote.there.url test2repo &&
524 test_config remote.there.pushurl testrepo &&
526 check_push_result testrepo $the_commit heads/main
529 test_expect_success
'push with config branch.*.pushremote' '
530 mk_test up_repo heads/main &&
531 mk_test side_repo heads/main &&
532 mk_test down_repo heads/main &&
533 test_config remote.up.url up_repo &&
534 test_config remote.pushdefault side_repo &&
535 test_config remote.down.url down_repo &&
536 test_config branch.main.remote up &&
537 test_config branch.main.pushremote down &&
538 test_config push.default matching &&
540 check_push_result up_repo $the_first_commit heads/main &&
541 check_push_result side_repo $the_first_commit heads/main &&
542 check_push_result down_repo $the_commit heads/main
545 test_expect_success
'branch.*.pushremote config order is irrelevant' '
546 mk_test one_repo heads/main &&
547 mk_test two_repo heads/main &&
548 test_config remote.one.url one_repo &&
549 test_config remote.two.url two_repo &&
550 test_config branch.main.pushremote two_repo &&
551 test_config remote.pushdefault one_repo &&
552 test_config push.default matching &&
554 check_push_result one_repo $the_first_commit heads/main &&
555 check_push_result two_repo $the_commit heads/main
558 test_expect_success
'push with dry-run' '
560 mk_test testrepo heads/main &&
561 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
562 git push --dry-run testrepo : &&
563 check_push_result testrepo $old_commit heads/main
566 test_expect_success
'push updates local refs' '
568 mk_test testrepo heads/main &&
569 mk_child testrepo child &&
574 test $(git rev-parse main) = \
575 $(git rev-parse remotes/origin/main)
580 test_expect_success
'push updates up-to-date local refs' '
582 mk_test testrepo heads/main &&
583 mk_child testrepo child1 &&
584 mk_child testrepo child2 &&
585 (cd child1 && git pull .. main && git push) &&
588 git pull ../child1 main &&
590 test $(git rev-parse main) = \
591 $(git rev-parse remotes/origin/main)
596 test_expect_success
'push preserves up-to-date packed refs' '
598 mk_test testrepo heads/main &&
599 mk_child testrepo child &&
603 ! test -f .git/refs/remotes/origin/main
608 test_expect_success
'push does not update local refs on failure' '
610 mk_test testrepo heads/main &&
611 mk_child testrepo child &&
612 mkdir testrepo/.git/hooks &&
613 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
614 chmod +x testrepo/.git/hooks/pre-receive &&
618 test_must_fail git push &&
619 test $(git rev-parse main) != \
620 $(git rev-parse remotes/origin/main)
625 test_expect_success
'allow deleting an invalid remote ref' '
627 mk_test testrepo heads/main &&
628 rm -f testrepo/.git/objects/??/* &&
629 git push testrepo :refs/heads/main &&
630 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
634 test_expect_success
'pushing valid refs triggers post-receive and post-update hooks' '
635 mk_test_with_hooks testrepo heads/main heads/next &&
636 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
637 newmain=$(git show-ref -s --verify refs/heads/main) &&
638 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
640 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
643 cat >pre-receive.expect <<-EOF &&
644 $orgmain $newmain refs/heads/main
645 $orgnext $newnext refs/heads/next
648 cat >update.expect <<-EOF &&
649 refs/heads/main $orgmain $newmain
650 refs/heads/next $orgnext $newnext
653 cat >post-receive.expect <<-EOF &&
654 $orgmain $newmain refs/heads/main
655 $orgnext $newnext refs/heads/next
658 cat >post-update.expect <<-EOF &&
663 test_cmp pre-receive.expect pre-receive.actual &&
664 test_cmp update.expect update.actual &&
665 test_cmp post-receive.expect post-receive.actual &&
666 test_cmp post-update.expect post-update.actual
670 test_expect_success
'deleting dangling ref triggers hooks with correct args' '
671 mk_test_with_hooks testrepo heads/main &&
672 rm -f testrepo/.git/objects/??/* &&
673 git push testrepo :refs/heads/main &&
676 cat >pre-receive.expect <<-EOF &&
677 $ZERO_OID $ZERO_OID refs/heads/main
680 cat >update.expect <<-EOF &&
681 refs/heads/main $ZERO_OID $ZERO_OID
684 cat >post-receive.expect <<-EOF &&
685 $ZERO_OID $ZERO_OID refs/heads/main
688 cat >post-update.expect <<-EOF &&
692 test_cmp pre-receive.expect pre-receive.actual &&
693 test_cmp update.expect update.actual &&
694 test_cmp post-receive.expect post-receive.actual &&
695 test_cmp post-update.expect post-update.actual
699 test_expect_success
'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
700 mk_test_with_hooks testrepo heads/main &&
701 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
702 newmain=$(git show-ref -s --verify refs/heads/main) &&
703 git push testrepo main :refs/heads/nonexistent &&
706 cat >pre-receive.expect <<-EOF &&
707 $orgmain $newmain refs/heads/main
708 $ZERO_OID $ZERO_OID refs/heads/nonexistent
711 cat >update.expect <<-EOF &&
712 refs/heads/main $orgmain $newmain
713 refs/heads/nonexistent $ZERO_OID $ZERO_OID
716 cat >post-receive.expect <<-EOF &&
717 $orgmain $newmain refs/heads/main
720 cat >post-update.expect <<-EOF &&
724 test_cmp pre-receive.expect pre-receive.actual &&
725 test_cmp update.expect update.actual &&
726 test_cmp post-receive.expect post-receive.actual &&
727 test_cmp post-update.expect post-update.actual
731 test_expect_success
'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
732 mk_test_with_hooks testrepo heads/main &&
733 git push testrepo :refs/heads/nonexistent &&
736 cat >pre-receive.expect <<-EOF &&
737 $ZERO_OID $ZERO_OID refs/heads/nonexistent
740 cat >update.expect <<-EOF &&
741 refs/heads/nonexistent $ZERO_OID $ZERO_OID
744 test_cmp pre-receive.expect pre-receive.actual &&
745 test_cmp update.expect update.actual &&
746 test_path_is_missing post-receive.actual &&
747 test_path_is_missing post-update.actual
751 test_expect_success
'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
752 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
753 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
754 newmain=$(git show-ref -s --verify refs/heads/main) &&
755 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
757 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
758 newseen=$(git show-ref -s --verify refs/heads/main) &&
759 git push testrepo refs/heads/main:refs/heads/main \
760 refs/heads/main:refs/heads/seen :refs/heads/next \
761 :refs/heads/nonexistent &&
764 cat >pre-receive.expect <<-EOF &&
765 $orgmain $newmain refs/heads/main
766 $orgnext $newnext refs/heads/next
767 $orgseen $newseen refs/heads/seen
768 $ZERO_OID $ZERO_OID refs/heads/nonexistent
771 cat >update.expect <<-EOF &&
772 refs/heads/main $orgmain $newmain
773 refs/heads/next $orgnext $newnext
774 refs/heads/seen $orgseen $newseen
775 refs/heads/nonexistent $ZERO_OID $ZERO_OID
778 cat >post-receive.expect <<-EOF &&
779 $orgmain $newmain refs/heads/main
780 $orgnext $newnext refs/heads/next
781 $orgseen $newseen refs/heads/seen
784 cat >post-update.expect <<-EOF &&
790 test_cmp pre-receive.expect pre-receive.actual &&
791 test_cmp update.expect update.actual &&
792 test_cmp post-receive.expect post-receive.actual &&
793 test_cmp post-update.expect post-update.actual
797 test_expect_success
'allow deleting a ref using --delete' '
798 mk_test testrepo heads/main &&
799 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
800 git push testrepo --delete main &&
801 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
804 test_expect_success
'allow deleting a tag using --delete' '
805 mk_test testrepo heads/main &&
806 git tag -a -m dummy_message deltag heads/main &&
807 git push testrepo --tags &&
808 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
809 git push testrepo --delete tag deltag &&
810 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
813 test_expect_success
'push --delete without args aborts' '
814 mk_test testrepo heads/main &&
815 test_must_fail git push testrepo --delete
818 test_expect_success
'push --delete refuses src:dest refspecs' '
819 mk_test testrepo heads/main &&
820 test_must_fail git push testrepo --delete main:foo
823 test_expect_success
'push --delete refuses empty string' '
824 mk_test testrepo heads/master &&
825 test_must_fail git push testrepo --delete ""
828 test_expect_success
'warn on push to HEAD of non-bare repository' '
829 mk_test testrepo heads/main &&
833 git config receive.denyCurrentBranch warn
835 git push testrepo main 2>stderr &&
836 grep "warning: updating the current branch" stderr
839 test_expect_success
'deny push to HEAD of non-bare repository' '
840 mk_test testrepo heads/main &&
844 git config receive.denyCurrentBranch true
846 test_must_fail git push testrepo main
849 test_expect_success
'allow push to HEAD of bare repository (bare)' '
850 mk_test testrepo heads/main &&
854 git config receive.denyCurrentBranch true &&
855 git config core.bare true
857 git push testrepo main 2>stderr &&
858 ! grep "warning: updating the current branch" stderr
861 test_expect_success
'allow push to HEAD of non-bare repository (config)' '
862 mk_test testrepo heads/main &&
866 git config receive.denyCurrentBranch false
868 git push testrepo main 2>stderr &&
869 ! grep "warning: updating the current branch" stderr
872 test_expect_success
'fetch with branches' '
874 git branch second $the_first_commit &&
875 git checkout second &&
876 echo ".." > testrepo/.git/branches/branch1 &&
880 echo "$the_commit commit refs/heads/branch1" >expect &&
881 git for-each-ref refs/heads >actual &&
882 test_cmp expect actual
887 test_expect_success
'fetch with branches containing #' '
889 echo "..#second" > testrepo/.git/branches/branch2 &&
893 echo "$the_first_commit commit refs/heads/branch2" >expect &&
894 git for-each-ref refs/heads >actual &&
895 test_cmp expect actual
900 test_expect_success
'push with branches' '
902 git checkout second &&
903 echo "testrepo" > .git/branches/branch1 &&
907 echo "$the_first_commit commit refs/heads/main" >expect &&
908 git for-each-ref refs/heads >actual &&
909 test_cmp expect actual
913 test_expect_success
'push with branches containing #' '
915 echo "testrepo#branch3" > .git/branches/branch2 &&
919 echo "$the_first_commit commit refs/heads/branch3" >expect &&
920 git for-each-ref refs/heads >actual &&
921 test_cmp expect actual
926 test_expect_success
'push into aliased refs (consistent)' '
927 mk_test testrepo heads/main &&
928 mk_child testrepo child1 &&
929 mk_child testrepo child2 &&
933 git symbolic-ref refs/heads/bar refs/heads/foo &&
934 git config receive.denyCurrentBranch false
941 git commit -a -m child2 &&
944 git push ../child1 foo bar
948 test_expect_success
'push into aliased refs (inconsistent)' '
949 mk_test testrepo heads/main &&
950 mk_child testrepo child1 &&
951 mk_child testrepo child2 &&
955 git symbolic-ref refs/heads/bar refs/heads/foo &&
956 git config receive.denyCurrentBranch false
963 git commit -a -m child2 &&
968 git commit -a -m child2 &&
970 test_must_fail git push ../child1 foo bar 2>stderr &&
971 grep "refusing inconsistent update" stderr
975 test_force_push_tag
() {
976 tag_type_description
=$1
979 test_expect_success
"force pushing required to update $tag_type_description" "
980 mk_test testrepo heads/main &&
981 mk_child testrepo child1 &&
982 mk_child testrepo child2 &&
986 git push ../child2 testTag &&
989 git commit -m 'file1' &&
990 git tag $tag_args testTag &&
991 test_must_fail git push ../child2 testTag &&
992 git push --force ../child2 testTag &&
993 git tag $tag_args testTag HEAD~ &&
994 test_must_fail git push ../child2 testTag &&
995 git push --force ../child2 testTag &&
997 # Clobbering without + in refspec needs --force
998 git tag -f testTag &&
999 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1000 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1002 # Clobbering with + in refspec does not need --force
1003 git tag -f testTag HEAD~ &&
1004 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1006 # Clobbering with --no-force still obeys + in refspec
1007 git tag -f testTag &&
1008 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1010 # Clobbering with/without --force and 'tag <name>' format
1011 git tag -f testTag HEAD~ &&
1012 test_must_fail git push ../child2 tag testTag &&
1013 git push --force ../child2 tag testTag
1018 test_force_push_tag
"lightweight tag" "-f"
1019 test_force_push_tag
"annotated tag" "-f -a -m'tag message'"
1021 test_force_fetch_tag
() {
1022 tag_type_description
=$1
1025 test_expect_success
"fetch will not clobber an existing $tag_type_description without --force" "
1026 mk_test testrepo heads/main &&
1027 mk_child testrepo child1 &&
1028 mk_child testrepo child2 &&
1032 git -C ../child1 fetch origin tag testTag &&
1035 git commit -m 'file1' &&
1036 git tag $tag_args testTag &&
1037 test_must_fail git -C ../child1 fetch origin tag testTag &&
1038 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1043 test_force_fetch_tag
"lightweight tag" "-f"
1044 test_force_fetch_tag
"annotated tag" "-f -a -m'tag message'"
1046 test_expect_success
'push --porcelain' '
1047 mk_empty testrepo &&
1048 echo >.git/foo "To testrepo" &&
1049 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1050 echo >>.git/foo "Done" &&
1051 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1054 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1055 git for-each-ref refs/remotes/origin >actual &&
1056 test_cmp expect actual
1058 test_cmp .git/foo .git/bar
1061 test_expect_success
'push --porcelain bad url' '
1062 mk_empty testrepo &&
1063 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1064 ! grep -q Done .git/bar
1067 test_expect_success
'push --porcelain rejected' '
1068 mk_empty testrepo &&
1069 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1071 git reset --hard origin/main^ &&
1072 git config receive.denyCurrentBranch true) &&
1074 echo >.git/foo "To testrepo" &&
1075 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1076 echo >>.git/foo "Done" &&
1078 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1079 test_cmp .git/foo .git/bar
1082 test_expect_success
'push --porcelain --dry-run rejected' '
1083 mk_empty testrepo &&
1084 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1086 git reset --hard origin/main &&
1087 git config receive.denyCurrentBranch true) &&
1089 echo >.git/foo "To testrepo" &&
1090 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1091 echo >>.git/foo "Done" &&
1093 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1094 test_cmp .git/foo .git/bar
1097 test_expect_success
'push --prune' '
1098 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1099 git push --prune testrepo : &&
1100 check_push_result testrepo $the_commit heads/main &&
1101 check_push_result testrepo $the_first_commit heads/second &&
1102 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1105 test_expect_success
'push --prune refspec' '
1106 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1107 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1108 check_push_result testrepo $the_commit tmp/main &&
1109 check_push_result testrepo $the_first_commit tmp/second &&
1110 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1113 for configsection
in transfer receive
1115 test_expect_success
"push to update a ref hidden by $configsection.hiderefs" '
1116 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1119 git config $configsection.hiderefs refs/hidden
1122 # push to unhidden ref succeeds normally
1123 git push testrepo main:refs/heads/main &&
1124 check_push_result testrepo $the_commit heads/main &&
1126 # push to update a hidden ref should fail
1127 test_must_fail git push testrepo main:refs/hidden/one &&
1128 check_push_result testrepo $the_first_commit hidden/one &&
1130 # push to delete a hidden ref should fail
1131 test_must_fail git push testrepo :refs/hidden/two &&
1132 check_push_result testrepo $the_first_commit hidden/two &&
1134 # idempotent push to update a hidden ref should fail
1135 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1136 check_push_result testrepo $the_first_commit hidden/three
1140 test_expect_success
'fetch exact SHA1' '
1141 mk_test testrepo heads/main hidden/one &&
1142 git push testrepo main:refs/hidden/one &&
1145 git config transfer.hiderefs refs/hidden
1147 check_push_result testrepo $the_commit hidden/one &&
1149 mk_child testrepo child &&
1153 # make sure $the_commit does not exist here
1156 test_must_fail git cat-file -t $the_commit &&
1158 # Some protocol versions (e.g. 2) support fetching
1159 # unadvertised objects, so restrict this test to v0.
1161 # fetching the hidden object should fail by default
1162 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1163 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1164 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1165 test_must_fail git rev-parse --verify refs/heads/copy &&
1167 # the server side can allow it to succeed
1170 git config uploadpack.allowtipsha1inwant true
1173 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1174 cat >expect <<-EOF &&
1179 git rev-parse --verify refs/heads/copy &&
1180 git rev-parse --verify refs/heads/extra
1182 test_cmp expect actual
1186 test_expect_success
'fetch exact SHA1 in protocol v2' '
1187 mk_test testrepo heads/main hidden/one &&
1188 git push testrepo main:refs/hidden/one &&
1189 git -C testrepo config transfer.hiderefs refs/hidden &&
1190 check_push_result testrepo $the_commit hidden/one &&
1192 mk_child testrepo child &&
1193 git -C child config protocol.version 2 &&
1195 # make sure $the_commit does not exist here
1196 git -C child repack -a -d &&
1197 git -C child prune &&
1198 test_must_fail git -C child cat-file -t $the_commit &&
1200 # fetching the hidden object succeeds by default
1201 # NEEDSWORK: should this match the v0 behavior instead?
1202 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1205 for configallowtipsha1inwant
in true false
1207 test_expect_success
"shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1208 mk_empty testrepo &&
1211 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1212 git commit --allow-empty -m foo &&
1213 git commit --allow-empty -m bar
1215 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1219 # Some protocol versions (e.g. 2) support fetching
1220 # unadvertised objects, so restrict this test to v0.
1221 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1222 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1223 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1224 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1225 git cat-file commit $SHA1
1229 test_expect_success
"deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1230 mk_empty testrepo &&
1233 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1234 git commit --allow-empty -m foo &&
1235 git commit --allow-empty -m bar &&
1236 git commit --allow-empty -m xyz
1238 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1239 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1240 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1243 git reset --hard $SHA1_2 &&
1244 git cat-file commit $SHA1_1 &&
1245 git cat-file commit $SHA1_3
1250 # Some protocol versions (e.g. 2) support fetching
1251 # unadvertised objects, so restrict this test to v0.
1252 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1253 git fetch ../testrepo/.git $SHA1_3 &&
1254 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1255 git fetch ../testrepo/.git $SHA1_1 &&
1256 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1257 git fetch ../testrepo/.git $SHA1_1 &&
1258 git cat-file commit $SHA1_1 &&
1259 test_must_fail git cat-file commit $SHA1_2 &&
1260 git fetch ../testrepo/.git $SHA1_2 &&
1261 git cat-file commit $SHA1_2 &&
1262 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1263 git fetch ../testrepo/.git $SHA1_3 2>err &&
1264 # ideally we would insist this be on a "remote error:"
1265 # line, but it is racy; see the commit message
1266 test_i18ngrep "not our ref.*$SHA1_3\$" err
1271 test_expect_success
'fetch follows tags by default' '
1272 mk_test testrepo heads/main &&
1277 git pull ../testrepo main &&
1278 git tag -m "annotated" tag &&
1279 git for-each-ref >tmp1 &&
1282 sed -n "s|refs/heads/main$|refs/remotes/origin/main|p" tmp1
1284 sort -k 3 >../expect
1289 git remote add origin ../src &&
1290 git config branch.main.remote origin &&
1291 git config branch.main.merge refs/heads/main &&
1293 git for-each-ref >../actual
1295 test_cmp expect actual
1298 test_expect_success
'peeled advertisements are not considered ref tips' '
1299 mk_empty testrepo &&
1300 git -C testrepo commit --allow-empty -m one &&
1301 git -C testrepo commit --allow-empty -m two &&
1302 git -C testrepo tag -m foo mytag HEAD^ &&
1303 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1304 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1305 git fetch testrepo $oid 2>err &&
1306 test_i18ngrep "Server does not allow request for unadvertised object" err
1309 test_expect_success
'pushing a specific ref applies remote.$name.push as refmap' '
1310 mk_test testrepo heads/main &&
1313 git init --bare dst &&
1316 git pull ../testrepo main &&
1318 git config remote.dst.url ../dst &&
1319 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1320 git push dst main &&
1321 git show-ref refs/heads/main |
1322 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1326 test_must_fail git show-ref refs/heads/next &&
1327 test_must_fail git show-ref refs/heads/main &&
1328 git show-ref refs/remotes/src/main >actual
1330 test_cmp dst/expect dst/actual
1333 test_expect_success
'with no remote.$name.push, it is not used as refmap' '
1334 mk_test testrepo heads/main &&
1337 git init --bare dst &&
1340 git pull ../testrepo main &&
1342 git config remote.dst.url ../dst &&
1343 git config push.default matching &&
1344 git push dst main &&
1345 git show-ref refs/heads/main >../dst/expect
1349 test_must_fail git show-ref refs/heads/next &&
1350 git show-ref refs/heads/main >actual
1352 test_cmp dst/expect dst/actual
1355 test_expect_success
'with no remote.$name.push, upstream mapping is used' '
1356 mk_test testrepo heads/main &&
1359 git init --bare dst &&
1362 git pull ../testrepo main &&
1364 git config remote.dst.url ../dst &&
1365 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1366 git config push.default upstream &&
1368 git config branch.main.merge refs/heads/trunk &&
1369 git config branch.main.remote dst &&
1371 git push dst main &&
1372 git show-ref refs/heads/main |
1373 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1377 test_must_fail git show-ref refs/heads/main &&
1378 test_must_fail git show-ref refs/heads/next &&
1379 git show-ref refs/heads/trunk >actual
1381 test_cmp dst/expect dst/actual
1384 test_expect_success
'push does not follow tags by default' '
1385 mk_test testrepo heads/main &&
1388 git init --bare dst &&
1391 git pull ../testrepo main &&
1392 git tag -m "annotated" tag &&
1393 git checkout -b another &&
1394 git commit --allow-empty -m "future commit" &&
1395 git tag -m "future" future &&
1396 git checkout main &&
1397 git for-each-ref refs/heads/main >../expect &&
1398 git push ../dst main
1402 git for-each-ref >../actual
1404 test_cmp expect actual
1407 test_expect_success
'push --follow-tags only pushes relevant tags' '
1408 mk_test testrepo heads/main &&
1411 git init --bare dst &&
1414 git pull ../testrepo main &&
1415 git tag -m "annotated" tag &&
1416 git checkout -b another &&
1417 git commit --allow-empty -m "future commit" &&
1418 git tag -m "future" future &&
1419 git checkout main &&
1420 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1421 git push --follow-tags ../dst main
1425 git for-each-ref >../actual
1427 test_cmp expect actual
1430 test_expect_success
'push --no-thin must produce non-thin pack' '
1431 cat >>path1 <<\EOF &&
1432 keep base version of path1 big enough, compared to the new changes
1433 later, in order to pass size heuristics in
1434 builtin/pack-objects.c:try_delta()
1436 git commit -am initial &&
1438 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1439 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1440 echo modified >> path1 &&
1441 git commit -am modified &&
1443 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1444 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1447 test_expect_success
'pushing a tag pushes the tagged object' '
1449 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1450 git tag -m foo tag-of-blob $blob &&
1451 git init --bare dst.git &&
1452 git push dst.git tag-of-blob &&
1453 # the receiving index-pack should have noticed
1454 # any problems, but we double check
1455 echo unreferenced >expect &&
1456 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1457 test_cmp expect actual
1460 test_expect_success
'push into bare respects core.logallrefupdates' '
1462 git init --bare dst.git &&
1463 git -C dst.git config core.logallrefupdates true &&
1465 # double push to test both with and without
1466 # the actual pack transfer
1467 git push dst.git main:one &&
1468 echo "one@{0} push" >expect &&
1469 git -C dst.git log -g --format="%gd %gs" one >actual &&
1470 test_cmp expect actual &&
1472 git push dst.git main:two &&
1473 echo "two@{0} push" >expect &&
1474 git -C dst.git log -g --format="%gd %gs" two >actual &&
1475 test_cmp expect actual
1478 test_expect_success
'fetch into bare respects core.logallrefupdates' '
1480 git init --bare dst.git &&
1483 git config core.logallrefupdates true &&
1485 # as above, we double-fetch to test both
1486 # with and without pack transfer
1487 git fetch .. main:one &&
1488 echo "one@{0} fetch .. main:one: storing head" >expect &&
1489 git log -g --format="%gd %gs" one >actual &&
1490 test_cmp expect actual &&
1492 git fetch .. main:two &&
1493 echo "two@{0} fetch .. main:two: storing head" >expect &&
1494 git log -g --format="%gd %gs" two >actual &&
1495 test_cmp expect actual
1499 test_expect_success
'receive.denyCurrentBranch = updateInstead' '
1500 git push testrepo main &&
1504 git config receive.denyCurrentBranch updateInstead
1506 test_commit third path2 &&
1508 # Try pushing into a repository with pristine working tree
1509 git push testrepo main &&
1512 git update-index -q --refresh &&
1513 git diff-files --quiet -- &&
1514 git diff-index --quiet --cached HEAD -- &&
1515 test third = "$(cat path2)" &&
1516 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1519 # Try pushing into a repository with working tree needing a refresh
1522 git reset --hard HEAD^ &&
1523 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1524 test-tool chmtime +100 path1
1526 git push testrepo main &&
1529 git update-index -q --refresh &&
1530 git diff-files --quiet -- &&
1531 git diff-index --quiet --cached HEAD -- &&
1532 test_cmp ../path1 path1 &&
1533 test third = "$(cat path2)" &&
1534 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1537 # Update what is to be pushed
1538 test_commit fourth path2 &&
1540 # Try pushing into a repository with a dirty working tree
1541 # (1) the working tree updated
1546 test_must_fail git push testrepo main &&
1549 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1550 git diff --quiet --cached &&
1551 test changed = "$(cat path1)"
1554 # (2) the index updated
1557 echo changed >path1 &&
1560 test_must_fail git push testrepo main &&
1563 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1565 test changed = "$(cat path1)"
1568 # Introduce a new file in the update
1569 test_commit fifth path3 &&
1571 # (3) the working tree has an untracked file that would interfere
1577 test_must_fail git push testrepo main &&
1580 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1582 git diff --quiet --cached &&
1583 test changed = "$(cat path3)"
1586 # (4) the target changes to what gets pushed but it still is a change
1590 echo fifth >path3 &&
1593 test_must_fail git push testrepo main &&
1596 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1598 test fifth = "$(cat path3)"
1601 # (5) push into void
1606 git config receive.denyCurrentBranch updateInstead
1608 git push void main &&
1611 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1613 git diff --cached --quiet
1616 # (6) updateInstead intervened by fast-forward check
1617 test_must_fail git push void main^:main &&
1618 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1619 git -C void diff --quiet &&
1620 git -C void diff --cached --quiet
1623 test_expect_success
'updateInstead with push-to-checkout hook' '
1625 git init testrepo &&
1629 git reset --hard HEAD^^ &&
1631 git config receive.denyCurrentBranch updateInstead &&
1632 write_script .git/hooks/push-to-checkout <<-\EOF
1633 echo >&2 updating from $(git rev-parse HEAD)
1634 echo >&2 updating to "$1"
1636 git update-index -q --refresh &&
1637 git read-tree -u -m HEAD "$1" || {
1639 echo >&2 read-tree failed
1645 # Try pushing into a pristine
1646 git push testrepo main &&
1650 git diff HEAD --quiet &&
1651 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1654 # Try pushing into a repository with conflicting change
1657 git reset --hard initial &&
1658 echo conflicting >path2
1660 test_must_fail git push testrepo main &&
1663 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1664 test conflicting = "$(cat path2)" &&
1665 git diff-index --quiet --cached HEAD
1668 # Try pushing into a repository with unrelated change
1671 git reset --hard initial &&
1672 echo unrelated >path1 &&
1673 echo irrelevant >path5 &&
1676 git push testrepo main &&
1679 test "$(cat path1)" = unrelated &&
1680 test "$(cat path5)" = irrelevant &&
1681 test "$(git diff --name-only --cached HEAD)" = path5 &&
1682 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1690 git config receive.denyCurrentBranch updateInstead &&
1691 write_script .git/hooks/push-to-checkout <<-\EOF
1692 if git rev-parse --quiet --verify HEAD
1695 echo >&2 updating from $(git rev-parse HEAD)
1698 echo >&2 pushing into void
1700 echo >&2 updating to "$1"
1702 git update-index -q --refresh &&
1705 git read-tree -u -m HEAD "$1" ;;
1707 git read-tree -u -m "$1" ;;
1710 echo >&2 read-tree failed
1716 git push void main &&
1720 git diff --cached --quiet &&
1721 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1725 test_expect_success
'denyCurrentBranch and worktrees' '
1726 git worktree add new-wt &&
1727 git clone . cloned &&
1728 test_commit -C cloned first &&
1729 test_config receive.denyCurrentBranch refuse &&
1730 test_must_fail git -C cloned push origin HEAD:new-wt &&
1731 test_config receive.denyCurrentBranch updateInstead &&
1732 git -C cloned push origin HEAD:new-wt &&
1733 test_must_fail git -C cloned push --delete origin new-wt