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
18 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
19 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
21 TEST_CREATE_REPO_NO_TEMPLATE
=1
28 test_when_finished
"rm -rf \"$repo_name\"" &&
29 test_path_is_missing
"$repo_name" &&
30 git init
--template= "$repo_name" &&
31 mkdir
"$repo_name"/.git
/hooks
&&
32 git
-C "$repo_name" config receive.denyCurrentBranch warn
39 mk_empty
"$repo_name" &&
43 git push
"$repo_name" $the_first_commit:refs
/$ref ||
49 echo "$the_first_commit" >expect
&&
50 git show-ref
-s --verify refs
/$ref >actual
&&
51 test_cmp expect actual ||
58 mk_test_with_hooks
() {
61 test_hook
-C "$repo_name" pre-receive
<<-'EOF' &&
62 cat - >>pre-receive.actual
65 test_hook
-C "$repo_name" update
<<-'EOF' &&
66 printf "%s %s %s\n" "$@" >>update.actual
69 test_hook
-C "$repo_name" post-receive
<<-'EOF' &&
70 cat - >>post-receive.actual
73 test_hook
-C "$repo_name" post-update
<<-'EOF'
76 printf "%s\n" "$ref" >>post-update.actual
82 test_when_finished
"rm -rf \"$2\"" &&
83 git clone
--template= "$1" "$2"
86 check_push_result
() {
88 BUG
"check_push_result requires at least 3 parameters"
99 git show-ref
-s --verify refs
/$ref >actual
&&
100 test_cmp expect actual ||
107 test_expect_success setup
'
112 git commit -a -m repo &&
113 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
118 git commit -a -m second &&
119 the_commit=$(git show-ref -s --verify refs/heads/main)
123 for cmd
in push fetch
127 test_expect_success
"reject 'git $cmd --no-$opt'" '
128 test_must_fail git $cmd --no-$opt 2>err &&
129 grep "unknown option .no-$opt" err
134 test_expect_success
'fetch without wildcard' '
138 git fetch .. refs/heads/main:refs/remotes/origin/main &&
140 echo "$the_commit commit refs/remotes/origin/main" >expect &&
141 git for-each-ref refs/remotes/origin >actual &&
142 test_cmp expect actual
146 test_expect_success
'fetch with wildcard' '
150 git config remote.up.url .. &&
151 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
154 echo "$the_commit commit refs/remotes/origin/main" >expect &&
155 git for-each-ref refs/remotes/origin >actual &&
156 test_cmp expect actual
160 test_expect_success
'fetch with insteadOf' '
165 git config "url.$TRASH.insteadOf" trash/ &&
166 git config remote.up.url trash/. &&
167 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
170 echo "$the_commit commit refs/remotes/origin/main" >expect &&
171 git for-each-ref refs/remotes/origin >actual &&
172 test_cmp expect actual
176 test_expect_success
'fetch with pushInsteadOf (should not rewrite)' '
181 git config "url.trash/.pushInsteadOf" "$TRASH" &&
182 git config remote.up.url "$TRASH." &&
183 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
186 echo "$the_commit commit refs/remotes/origin/main" >expect &&
187 git for-each-ref refs/remotes/origin >actual &&
188 test_cmp expect actual
195 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
198 test_expect_success
'push without negotiation' '
200 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
201 test_commit -C testrepo unrelated_commit &&
202 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
203 test_when_finished "rm event" &&
204 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
205 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
208 test_expect_success
'push with negotiation' '
210 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
211 test_commit -C testrepo unrelated_commit &&
212 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
213 test_when_finished "rm event" &&
214 GIT_TRACE2_EVENT="$(pwd)/event" \
215 git -c protocol.version=2 -c push.negotiate=1 \
216 push testrepo refs/heads/main:refs/remotes/origin/main &&
217 grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
218 grep_wrote 2 event # 1 commit, 1 tree
221 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 test_when_finished "rm event" &&
227 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
228 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
229 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
230 test_grep "push negotiation failed" err
233 test_expect_success
'push deletion with negotiation' '
235 git push testrepo $the_first_commit:refs/heads/master &&
236 git -c push.negotiate=1 push testrepo \
237 :master $the_first_commit:refs/heads/next 2>errors-2 &&
238 test_grep ! "negotiate-only needs one or " errors-2 &&
239 git -c push.negotiate=1 push testrepo :next 2>errors-1 &&
240 test_grep ! "negotiate-only needs one or " errors-1
243 test_expect_success
'push with negotiation does not attempt to fetch submodules' '
244 mk_empty submodule_upstream &&
245 test_commit -C submodule_upstream submodule_commit &&
246 test_config_global protocol.file.allow always &&
247 git submodule add ./submodule_upstream submodule &&
249 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
250 test_commit -C testrepo unrelated_commit &&
251 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
252 GIT_TRACE2_EVENT="$(pwd)/event" git -c submodule.recurse=true \
253 -c protocol.version=2 -c push.negotiate=1 \
254 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
255 grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
256 ! grep "Fetching submodule" err
259 test_expect_success
'push without wildcard' '
262 git push testrepo refs/heads/main:refs/remotes/origin/main &&
265 echo "$the_commit commit refs/remotes/origin/main" >expect &&
266 git for-each-ref refs/remotes/origin >actual &&
267 test_cmp expect actual
271 test_expect_success
'push with wildcard' '
274 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
277 echo "$the_commit commit refs/remotes/origin/main" >expect &&
278 git for-each-ref refs/remotes/origin >actual &&
279 test_cmp expect actual
283 test_expect_success
'push with insteadOf' '
286 test_config "url.$TRASH.insteadOf" trash/ &&
287 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
290 echo "$the_commit commit refs/remotes/origin/main" >expect &&
291 git for-each-ref refs/remotes/origin >actual &&
292 test_cmp expect actual
296 test_expect_success
'push with pushInsteadOf' '
299 test_config "url.$TRASH.pushInsteadOf" trash/ &&
300 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
303 echo "$the_commit commit refs/remotes/origin/main" >expect &&
304 git for-each-ref refs/remotes/origin >actual &&
305 test_cmp expect actual
309 test_expect_success
'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
311 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
312 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
313 test_config remote.r.url trash/wrong &&
314 test_config remote.r.pushurl "testrepo/" &&
315 git push r refs/heads/main:refs/remotes/origin/main &&
318 echo "$the_commit commit refs/remotes/origin/main" >expect &&
319 git for-each-ref refs/remotes/origin >actual &&
320 test_cmp expect actual
324 test_expect_success
'push with matching heads' '
326 mk_test testrepo heads/main &&
327 git push testrepo : &&
328 check_push_result testrepo $the_commit heads/main
332 test_expect_success
'push with matching heads on the command line' '
334 mk_test testrepo heads/main &&
335 git push testrepo : &&
336 check_push_result testrepo $the_commit heads/main
340 test_expect_success
'failed (non-fast-forward) push with matching heads' '
342 mk_test testrepo heads/main &&
343 git push testrepo : &&
344 git commit --amend -massaged &&
345 test_must_fail git push testrepo &&
346 check_push_result testrepo $the_commit heads/main &&
347 git reset --hard $the_commit
351 test_expect_success
'push --force with matching heads' '
353 mk_test testrepo heads/main &&
354 git push testrepo : &&
355 git commit --amend -massaged &&
356 git push --force testrepo : &&
357 ! check_push_result testrepo $the_commit heads/main &&
358 git reset --hard $the_commit
362 test_expect_success
'push with matching heads and forced update' '
364 mk_test testrepo heads/main &&
365 git push testrepo : &&
366 git commit --amend -massaged &&
367 git push testrepo +: &&
368 ! check_push_result testrepo $the_commit heads/main &&
369 git reset --hard $the_commit
373 test_expect_success
'push with no ambiguity (1)' '
375 mk_test testrepo heads/main &&
376 git push testrepo main:main &&
377 check_push_result testrepo $the_commit heads/main
381 test_expect_success
'push with no ambiguity (2)' '
383 mk_test testrepo remotes/origin/main &&
384 git push testrepo main:origin/main &&
385 check_push_result testrepo $the_commit remotes/origin/main
389 test_expect_success
'push with colon-less refspec, no ambiguity' '
391 mk_test testrepo heads/main heads/t/main &&
392 git branch -f t/main main &&
393 git push testrepo main &&
394 check_push_result testrepo $the_commit heads/main &&
395 check_push_result testrepo $the_first_commit heads/t/main
399 test_expect_success
'push with weak ambiguity (1)' '
401 mk_test testrepo heads/main remotes/origin/main &&
402 git push testrepo main:main &&
403 check_push_result testrepo $the_commit heads/main &&
404 check_push_result testrepo $the_first_commit remotes/origin/main
408 test_expect_success
'push with weak ambiguity (2)' '
410 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
411 git push testrepo main:main &&
412 check_push_result testrepo $the_commit heads/main &&
413 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
417 test_expect_success
'push with ambiguity' '
419 mk_test testrepo heads/frotz tags/frotz &&
420 test_must_fail git push testrepo main:frotz &&
421 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
425 test_expect_success
'push with onelevel ref' '
426 mk_test testrepo heads/main &&
427 test_must_fail git push testrepo HEAD:refs/onelevel
430 test_expect_success
'push with colon-less refspec (1)' '
432 mk_test testrepo heads/frotz tags/frotz &&
433 git branch -f frotz main &&
434 git push testrepo frotz &&
435 check_push_result testrepo $the_commit heads/frotz &&
436 check_push_result testrepo $the_first_commit tags/frotz
440 test_expect_success
'push with colon-less refspec (2)' '
442 mk_test testrepo heads/frotz tags/frotz &&
443 if git show-ref --verify -q refs/heads/frotz
448 git push -f testrepo frotz &&
449 check_push_result testrepo $the_commit tags/frotz &&
450 check_push_result testrepo $the_first_commit heads/frotz
454 test_expect_success
'push with colon-less refspec (3)' '
457 if git show-ref --verify -q refs/tags/frotz
461 git branch -f frotz main &&
462 git push testrepo frotz &&
463 check_push_result testrepo $the_commit heads/frotz &&
464 test 1 = $( cd testrepo && git show-ref | wc -l )
467 test_expect_success
'push with colon-less refspec (4)' '
470 if git show-ref --verify -q refs/heads/frotz
475 git push testrepo frotz &&
476 check_push_result testrepo $the_commit tags/frotz &&
477 test 1 = $( cd testrepo && git show-ref | wc -l )
481 test_expect_success
'push head with non-existent, incomplete dest' '
484 git push testrepo main:branch &&
485 check_push_result testrepo $the_commit heads/branch
489 test_expect_success
'push tag with non-existent, incomplete dest' '
493 git push testrepo v1.0:tag &&
494 check_push_result testrepo $the_commit tags/tag
498 test_expect_success
'push sha1 with non-existent, incomplete dest' '
501 test_must_fail git push testrepo $(git rev-parse main):foo
505 test_expect_success
'push ref expression with non-existent, incomplete dest' '
508 test_must_fail git push testrepo main^:branch
515 test_expect_success
"push with $head" '
516 mk_test testrepo heads/main &&
518 git push testrepo $head &&
519 check_push_result testrepo $the_commit heads/main
522 test_expect_success
"push with $head nonexisting at remote" '
523 mk_test testrepo heads/main &&
524 git checkout -b local main &&
525 test_when_finished "git checkout main; git branch -D local" &&
526 git push testrepo $head &&
527 check_push_result testrepo $the_commit heads/local
530 test_expect_success
"push with +$head" '
531 mk_test testrepo heads/main &&
532 git checkout -b local main &&
533 test_when_finished "git checkout main; git branch -D local" &&
534 git push testrepo main local &&
535 check_push_result testrepo $the_commit heads/main &&
536 check_push_result testrepo $the_commit heads/local &&
538 # Without force rewinding should fail
539 git reset --hard $head^ &&
540 test_must_fail git push testrepo $head &&
541 check_push_result testrepo $the_commit heads/local &&
543 # With force rewinding should succeed
544 git push testrepo +$head &&
545 check_push_result testrepo $the_first_commit heads/local
548 test_expect_success
"push $head with non-existent, incomplete dest" '
551 git push testrepo $head:branch &&
552 check_push_result testrepo $the_commit heads/branch
556 test_expect_success
"push with config remote.*.push = $head" '
557 mk_test testrepo heads/local &&
559 git branch -f local $the_commit &&
560 test_when_finished "git branch -D local" &&
563 git checkout local &&
564 git reset --hard $the_first_commit
566 test_config remote.there.url testrepo &&
567 test_config remote.there.push $head &&
568 test_config branch.main.remote there &&
570 check_push_result testrepo $the_commit heads/main &&
571 check_push_result testrepo $the_first_commit heads/local
576 test_expect_success
"push to remote with no explicit refspec and config remote.*.push = src:dest" '
577 mk_test testrepo heads/main &&
578 git checkout $the_first_commit &&
579 test_config remote.there.url testrepo &&
580 test_config remote.there.push refs/heads/main:refs/heads/main &&
582 check_push_result testrepo $the_commit heads/main
585 test_expect_success
'push with remote.pushdefault' '
586 mk_test up_repo heads/main &&
587 mk_test down_repo heads/main &&
588 test_config remote.up.url up_repo &&
589 test_config remote.down.url down_repo &&
590 test_config branch.main.remote up &&
591 test_config remote.pushdefault down &&
592 test_config push.default matching &&
594 check_push_result up_repo $the_first_commit heads/main &&
595 check_push_result down_repo $the_commit heads/main
598 test_expect_success
'push with config remote.*.pushurl' '
600 mk_test testrepo heads/main &&
602 test_config remote.there.url test2repo &&
603 test_config remote.there.pushurl testrepo &&
605 check_push_result testrepo $the_commit heads/main
608 test_expect_success
'push with config branch.*.pushremote' '
609 mk_test up_repo heads/main &&
610 mk_test side_repo heads/main &&
611 mk_test down_repo heads/main &&
612 test_config remote.up.url up_repo &&
613 test_config remote.pushdefault side_repo &&
614 test_config remote.down.url down_repo &&
615 test_config branch.main.remote up &&
616 test_config branch.main.pushremote down &&
617 test_config push.default matching &&
619 check_push_result up_repo $the_first_commit heads/main &&
620 check_push_result side_repo $the_first_commit heads/main &&
621 check_push_result down_repo $the_commit heads/main
624 test_expect_success
'branch.*.pushremote config order is irrelevant' '
625 mk_test one_repo heads/main &&
626 mk_test two_repo heads/main &&
627 test_config remote.one.url one_repo &&
628 test_config remote.two.url two_repo &&
629 test_config branch.main.pushremote two_repo &&
630 test_config remote.pushdefault one_repo &&
631 test_config push.default matching &&
633 check_push_result one_repo $the_first_commit heads/main &&
634 check_push_result two_repo $the_commit heads/main
637 test_expect_success
'push rejects empty branch name entries' '
638 mk_test one_repo heads/main &&
639 test_config remote.one.url one_repo &&
640 test_config branch..remote one &&
641 test_config branch..merge refs/heads/ &&
642 test_config branch.main.remote one &&
643 test_config branch.main.merge refs/heads/main &&
644 test_must_fail git push 2>err &&
645 grep "bad config variable .branch\.\." err
648 test_expect_success
'push ignores "branch." config without subsection' '
649 mk_test one_repo heads/main &&
650 test_config remote.one.url one_repo &&
651 test_config branch.autoSetupMerge true &&
652 test_config branch.main.remote one &&
653 test_config branch.main.merge refs/heads/main &&
657 test_expect_success
'push with dry-run' '
659 mk_test testrepo heads/main &&
660 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
661 git push --dry-run testrepo : &&
662 check_push_result testrepo $old_commit heads/main
665 test_expect_success
'push updates local refs' '
667 mk_test testrepo heads/main &&
668 mk_child testrepo child &&
673 test $(git rev-parse main) = \
674 $(git rev-parse remotes/origin/main)
679 test_expect_success
'push updates up-to-date local refs' '
681 mk_test testrepo heads/main &&
682 mk_child testrepo child1 &&
683 mk_child testrepo child2 &&
684 (cd child1 && git pull .. main && git push) &&
687 git pull ../child1 main &&
689 test $(git rev-parse main) = \
690 $(git rev-parse remotes/origin/main)
695 test_expect_success
'push preserves up-to-date packed refs' '
697 mk_test testrepo heads/main &&
698 mk_child testrepo child &&
702 ! test -f .git/refs/remotes/origin/main
707 test_expect_success
'push does not update local refs on failure' '
709 mk_test testrepo heads/main &&
710 mk_child testrepo child &&
711 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
712 chmod +x testrepo/.git/hooks/pre-receive &&
716 test_must_fail git push &&
717 test $(git rev-parse main) != \
718 $(git rev-parse remotes/origin/main)
723 test_expect_success
'allow deleting an invalid remote ref' '
725 mk_test testrepo heads/branch &&
726 rm -f testrepo/.git/objects/??/* &&
727 git push testrepo :refs/heads/branch &&
728 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
732 test_expect_success
'pushing valid refs triggers post-receive and post-update hooks' '
733 mk_test_with_hooks testrepo heads/main heads/next &&
734 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
735 newmain=$(git show-ref -s --verify refs/heads/main) &&
736 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
738 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
741 cat >pre-receive.expect <<-EOF &&
742 $orgmain $newmain refs/heads/main
743 $orgnext $newnext refs/heads/next
746 cat >update.expect <<-EOF &&
747 refs/heads/main $orgmain $newmain
748 refs/heads/next $orgnext $newnext
751 cat >post-receive.expect <<-EOF &&
752 $orgmain $newmain refs/heads/main
753 $orgnext $newnext refs/heads/next
756 cat >post-update.expect <<-EOF &&
761 test_cmp pre-receive.expect pre-receive.actual &&
762 test_cmp update.expect update.actual &&
763 test_cmp post-receive.expect post-receive.actual &&
764 test_cmp post-update.expect post-update.actual
768 test_expect_success
'deleting dangling ref triggers hooks with correct args' '
769 mk_test_with_hooks testrepo heads/branch &&
770 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
771 rm -f testrepo/.git/objects/??/* &&
772 git push testrepo :refs/heads/branch &&
775 cat >pre-receive.expect <<-EOF &&
776 $orig $ZERO_OID refs/heads/branch
779 cat >update.expect <<-EOF &&
780 refs/heads/branch $orig $ZERO_OID
783 cat >post-receive.expect <<-EOF &&
784 $orig $ZERO_OID refs/heads/branch
787 cat >post-update.expect <<-EOF &&
791 test_cmp pre-receive.expect pre-receive.actual &&
792 test_cmp update.expect update.actual &&
793 test_cmp post-receive.expect post-receive.actual &&
794 test_cmp post-update.expect post-update.actual
798 test_expect_success
'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
799 mk_test_with_hooks testrepo heads/main &&
800 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
801 newmain=$(git show-ref -s --verify refs/heads/main) &&
802 git push testrepo main :refs/heads/nonexistent &&
805 cat >pre-receive.expect <<-EOF &&
806 $orgmain $newmain refs/heads/main
807 $ZERO_OID $ZERO_OID refs/heads/nonexistent
810 cat >update.expect <<-EOF &&
811 refs/heads/main $orgmain $newmain
812 refs/heads/nonexistent $ZERO_OID $ZERO_OID
815 cat >post-receive.expect <<-EOF &&
816 $orgmain $newmain refs/heads/main
819 cat >post-update.expect <<-EOF &&
823 test_cmp pre-receive.expect pre-receive.actual &&
824 test_cmp update.expect update.actual &&
825 test_cmp post-receive.expect post-receive.actual &&
826 test_cmp post-update.expect post-update.actual
830 test_expect_success
'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
831 mk_test_with_hooks testrepo heads/main &&
832 git push testrepo :refs/heads/nonexistent &&
835 cat >pre-receive.expect <<-EOF &&
836 $ZERO_OID $ZERO_OID refs/heads/nonexistent
839 cat >update.expect <<-EOF &&
840 refs/heads/nonexistent $ZERO_OID $ZERO_OID
843 test_cmp pre-receive.expect pre-receive.actual &&
844 test_cmp update.expect update.actual &&
845 test_path_is_missing post-receive.actual &&
846 test_path_is_missing post-update.actual
850 test_expect_success
'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
851 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
852 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
853 newmain=$(git show-ref -s --verify refs/heads/main) &&
854 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
856 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
857 newseen=$(git show-ref -s --verify refs/heads/main) &&
858 git push testrepo refs/heads/main:refs/heads/main \
859 refs/heads/main:refs/heads/seen :refs/heads/next \
860 :refs/heads/nonexistent &&
863 cat >pre-receive.expect <<-EOF &&
864 $orgmain $newmain refs/heads/main
865 $orgnext $newnext refs/heads/next
866 $orgseen $newseen refs/heads/seen
867 $ZERO_OID $ZERO_OID refs/heads/nonexistent
870 cat >update.expect <<-EOF &&
871 refs/heads/main $orgmain $newmain
872 refs/heads/next $orgnext $newnext
873 refs/heads/seen $orgseen $newseen
874 refs/heads/nonexistent $ZERO_OID $ZERO_OID
877 cat >post-receive.expect <<-EOF &&
878 $orgmain $newmain refs/heads/main
879 $orgnext $newnext refs/heads/next
880 $orgseen $newseen refs/heads/seen
883 cat >post-update.expect <<-EOF &&
889 test_cmp pre-receive.expect pre-receive.actual &&
890 test_cmp update.expect update.actual &&
891 test_cmp post-receive.expect post-receive.actual &&
892 test_cmp post-update.expect post-update.actual
896 test_expect_success
'allow deleting a ref using --delete' '
897 mk_test testrepo heads/main &&
898 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
899 git push testrepo --delete main &&
900 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
903 test_expect_success
'allow deleting a tag using --delete' '
904 mk_test testrepo heads/main &&
905 git tag -a -m dummy_message deltag heads/main &&
906 git push testrepo --tags &&
907 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
908 git push testrepo --delete tag deltag &&
909 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
912 test_expect_success
'push --delete without args aborts' '
913 mk_test testrepo heads/main &&
914 test_must_fail git push testrepo --delete
917 test_expect_success
'push --delete refuses src:dest refspecs' '
918 mk_test testrepo heads/main &&
919 test_must_fail git push testrepo --delete main:foo
922 test_expect_success
'push --delete refuses empty string' '
923 mk_test testrepo heads/master &&
924 test_must_fail git push testrepo --delete ""
927 test_expect_success
'push --delete onelevel refspecs' '
928 mk_test testrepo heads/main &&
929 git -C testrepo update-ref refs/onelevel refs/heads/main &&
930 git push testrepo --delete refs/onelevel &&
931 test_must_fail git -C testrepo rev-parse --verify refs/onelevel
934 test_expect_success
'warn on push to HEAD of non-bare repository' '
935 mk_test testrepo heads/main &&
939 git config receive.denyCurrentBranch warn
941 git push testrepo main 2>stderr &&
942 grep "warning: updating the current branch" stderr
945 test_expect_success
'deny push to HEAD of non-bare repository' '
946 mk_test testrepo heads/main &&
950 git config receive.denyCurrentBranch true
952 test_must_fail git push testrepo main
955 test_expect_success
'allow push to HEAD of bare repository (bare)' '
956 mk_test testrepo heads/main &&
960 git config receive.denyCurrentBranch true &&
961 git config core.bare true
963 git push testrepo main 2>stderr &&
964 ! grep "warning: updating the current branch" stderr
967 test_expect_success
'allow push to HEAD of non-bare repository (config)' '
968 mk_test testrepo heads/main &&
972 git config receive.denyCurrentBranch false
974 git push testrepo main 2>stderr &&
975 ! grep "warning: updating the current branch" stderr
978 test_expect_success
'fetch with branches' '
980 git branch second $the_first_commit &&
981 git checkout second &&
982 mkdir testrepo/.git/branches &&
983 echo ".." > testrepo/.git/branches/branch1 &&
987 echo "$the_commit commit refs/heads/branch1" >expect &&
988 git for-each-ref refs/heads >actual &&
989 test_cmp expect actual
994 test_expect_success
'fetch with branches containing #' '
996 mkdir testrepo/.git/branches &&
997 echo "..#second" > testrepo/.git/branches/branch2 &&
1000 git fetch branch2 &&
1001 echo "$the_first_commit commit refs/heads/branch2" >expect &&
1002 git for-each-ref refs/heads >actual &&
1003 test_cmp expect actual
1008 test_expect_success
'push with branches' '
1009 mk_empty testrepo &&
1010 git checkout second &&
1012 test_when_finished "rm -rf .git/branches" &&
1013 mkdir .git/branches &&
1014 echo "testrepo" > .git/branches/branch1 &&
1019 echo "$the_first_commit commit refs/heads/main" >expect &&
1020 git for-each-ref refs/heads >actual &&
1021 test_cmp expect actual
1025 test_expect_success
'push with branches containing #' '
1026 mk_empty testrepo &&
1028 test_when_finished "rm -rf .git/branches" &&
1029 mkdir .git/branches &&
1030 echo "testrepo#branch3" > .git/branches/branch2 &&
1035 echo "$the_first_commit commit refs/heads/branch3" >expect &&
1036 git for-each-ref refs/heads >actual &&
1037 test_cmp expect actual
1042 test_expect_success
'push into aliased refs (consistent)' '
1043 mk_test testrepo heads/main &&
1044 mk_child testrepo child1 &&
1045 mk_child testrepo child2 &&
1049 git symbolic-ref refs/heads/bar refs/heads/foo &&
1050 git config receive.denyCurrentBranch false
1057 git commit -a -m child2 &&
1060 git push ../child1 foo bar
1064 test_expect_success
'push into aliased refs (inconsistent)' '
1065 mk_test testrepo heads/main &&
1066 mk_child testrepo child1 &&
1067 mk_child testrepo child2 &&
1071 git symbolic-ref refs/heads/bar refs/heads/foo &&
1072 git config receive.denyCurrentBranch false
1079 git commit -a -m child2 &&
1084 git commit -a -m child2 &&
1086 test_must_fail git push ../child1 foo bar 2>stderr &&
1087 grep "refusing inconsistent update" stderr
1091 test_force_push_tag
() {
1092 tag_type_description
=$1
1095 test_expect_success
"force pushing required to update $tag_type_description" "
1096 mk_test testrepo heads/main &&
1097 mk_child testrepo child1 &&
1098 mk_child testrepo child2 &&
1102 git push ../child2 testTag &&
1105 git commit -m 'file1' &&
1106 git tag $tag_args testTag &&
1107 test_must_fail git push ../child2 testTag &&
1108 git push --force ../child2 testTag &&
1109 git tag $tag_args testTag HEAD~ &&
1110 test_must_fail git push ../child2 testTag &&
1111 git push --force ../child2 testTag &&
1113 # Clobbering without + in refspec needs --force
1114 git tag -f testTag &&
1115 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1116 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1118 # Clobbering with + in refspec does not need --force
1119 git tag -f testTag HEAD~ &&
1120 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1122 # Clobbering with --no-force still obeys + in refspec
1123 git tag -f testTag &&
1124 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1126 # Clobbering with/without --force and 'tag <name>' format
1127 git tag -f testTag HEAD~ &&
1128 test_must_fail git push ../child2 tag testTag &&
1129 git push --force ../child2 tag testTag
1134 test_force_push_tag
"lightweight tag" "-f"
1135 test_force_push_tag
"annotated tag" "-f -a -m'tag message'"
1137 test_force_fetch_tag
() {
1138 tag_type_description
=$1
1141 test_expect_success
"fetch will not clobber an existing $tag_type_description without --force" "
1142 mk_test testrepo heads/main &&
1143 mk_child testrepo child1 &&
1144 mk_child testrepo child2 &&
1148 git -C ../child1 fetch origin tag testTag &&
1151 git commit -m 'file1' &&
1152 git tag $tag_args testTag &&
1153 test_must_fail git -C ../child1 fetch origin tag testTag &&
1154 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1159 test_force_fetch_tag
"lightweight tag" "-f"
1160 test_force_fetch_tag
"annotated tag" "-f -a -m'tag message'"
1162 test_expect_success
'push --porcelain' '
1163 mk_empty testrepo &&
1164 echo >.git/foo "To testrepo" &&
1165 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1166 echo >>.git/foo "Done" &&
1167 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1170 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1171 git for-each-ref refs/remotes/origin >actual &&
1172 test_cmp expect actual
1174 test_cmp .git/foo .git/bar
1177 test_expect_success
'push --porcelain bad url' '
1178 mk_empty testrepo &&
1179 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1180 ! grep -q Done .git/bar
1183 test_expect_success
'push --porcelain rejected' '
1184 mk_empty testrepo &&
1185 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1187 git reset --hard origin/main^ &&
1188 git config receive.denyCurrentBranch true) &&
1190 echo >.git/foo "To testrepo" &&
1191 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1192 echo >>.git/foo "Done" &&
1194 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1195 test_cmp .git/foo .git/bar
1198 test_expect_success
'push --porcelain --dry-run rejected' '
1199 mk_empty testrepo &&
1200 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1202 git reset --hard origin/main &&
1203 git config receive.denyCurrentBranch true) &&
1205 echo >.git/foo "To testrepo" &&
1206 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1207 echo >>.git/foo "Done" &&
1209 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1210 test_cmp .git/foo .git/bar
1213 test_expect_success
'push --prune' '
1214 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1215 git push --prune testrepo : &&
1216 check_push_result testrepo $the_commit heads/main &&
1217 check_push_result testrepo $the_first_commit heads/second &&
1218 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1221 test_expect_success
'push --prune refspec' '
1222 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1223 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1224 check_push_result testrepo $the_commit tmp/main &&
1225 check_push_result testrepo $the_first_commit tmp/second &&
1226 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1229 for configsection
in transfer receive
1231 test_expect_success
"push to update a ref hidden by $configsection.hiderefs" '
1232 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1235 git config $configsection.hiderefs refs/hidden
1238 # push to unhidden ref succeeds normally
1239 git push testrepo main:refs/heads/main &&
1240 check_push_result testrepo $the_commit heads/main &&
1242 # push to update a hidden ref should fail
1243 test_must_fail git push testrepo main:refs/hidden/one &&
1244 check_push_result testrepo $the_first_commit hidden/one &&
1246 # push to delete a hidden ref should fail
1247 test_must_fail git push testrepo :refs/hidden/two &&
1248 check_push_result testrepo $the_first_commit hidden/two &&
1250 # idempotent push to update a hidden ref should fail
1251 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1252 check_push_result testrepo $the_first_commit hidden/three
1256 test_expect_success
'fetch exact SHA1' '
1257 mk_test testrepo heads/main hidden/one &&
1258 git push testrepo main:refs/hidden/one &&
1261 git config transfer.hiderefs refs/hidden
1263 check_push_result testrepo $the_commit hidden/one &&
1265 mk_child testrepo child &&
1269 # make sure $the_commit does not exist here
1272 test_must_fail git cat-file -t $the_commit &&
1274 # Some protocol versions (e.g. 2) support fetching
1275 # unadvertised objects, so restrict this test to v0.
1277 # fetching the hidden object should fail by default
1278 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1279 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1280 test_grep "Server does not allow request for unadvertised object" err &&
1281 test_must_fail git rev-parse --verify refs/heads/copy &&
1283 # the server side can allow it to succeed
1286 git config uploadpack.allowtipsha1inwant true
1289 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1290 cat >expect <<-EOF &&
1295 git rev-parse --verify refs/heads/copy &&
1296 git rev-parse --verify refs/heads/extra
1298 test_cmp expect actual
1302 test_expect_success
'fetch exact SHA1 in protocol v2' '
1303 mk_test testrepo heads/main hidden/one &&
1304 git push testrepo main:refs/hidden/one &&
1305 git -C testrepo config transfer.hiderefs refs/hidden &&
1306 check_push_result testrepo $the_commit hidden/one &&
1308 mk_child testrepo child &&
1309 git -C child config protocol.version 2 &&
1311 # make sure $the_commit does not exist here
1312 git -C child repack -a -d &&
1313 git -C child prune &&
1314 test_must_fail git -C child cat-file -t $the_commit &&
1316 # fetching the hidden object succeeds by default
1317 # NEEDSWORK: should this match the v0 behavior instead?
1318 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1321 for configallowtipsha1inwant
in true false
1323 test_expect_success
"shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1324 mk_empty testrepo &&
1327 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1328 git commit --allow-empty -m foo &&
1329 git commit --allow-empty -m bar
1331 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1335 # Some protocol versions (e.g. 2) support fetching
1336 # unadvertised objects, so restrict this test to v0.
1337 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1338 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1339 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1340 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1341 git cat-file commit $SHA1
1345 test_expect_success
"deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1346 mk_empty testrepo &&
1349 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1350 git commit --allow-empty -m foo &&
1351 git commit --allow-empty -m bar &&
1352 git commit --allow-empty -m xyz
1354 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1355 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1356 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1359 git reset --hard $SHA1_2 &&
1360 git cat-file commit $SHA1_1 &&
1361 git cat-file commit $SHA1_3
1366 # Some protocol versions (e.g. 2) support fetching
1367 # unadvertised objects, so restrict this test to v0.
1368 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1369 git fetch ../testrepo/.git $SHA1_3 &&
1370 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1371 git fetch ../testrepo/.git $SHA1_1 &&
1372 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1373 git fetch ../testrepo/.git $SHA1_1 &&
1374 git cat-file commit $SHA1_1 &&
1375 test_must_fail git cat-file commit $SHA1_2 &&
1376 git fetch ../testrepo/.git $SHA1_2 &&
1377 git cat-file commit $SHA1_2 &&
1378 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1379 git fetch ../testrepo/.git $SHA1_3 2>err &&
1380 # ideally we would insist this be on a "remote error:"
1381 # line, but it is racy; see the commit message
1382 test_grep "not our ref.*$SHA1_3\$" err
1387 test_expect_success
'fetch follows tags by default' '
1388 mk_test testrepo heads/main &&
1389 test_when_finished "rm -rf src" &&
1393 git pull ../testrepo main &&
1394 git tag -m "annotated" tag &&
1395 git for-each-ref >tmp1 &&
1396 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1397 sort -k 3 >../expect
1399 test_when_finished "rm -rf dst" &&
1403 git remote add origin ../src &&
1404 git config branch.main.remote origin &&
1405 git config branch.main.merge refs/heads/main &&
1407 git for-each-ref >../actual
1409 test_cmp expect actual
1412 test_expect_success
'peeled advertisements are not considered ref tips' '
1413 mk_empty testrepo &&
1414 git -C testrepo commit --allow-empty -m one &&
1415 git -C testrepo commit --allow-empty -m two &&
1416 git -C testrepo tag -m foo mytag HEAD^ &&
1417 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1418 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1419 git fetch testrepo $oid 2>err &&
1420 test_grep "Server does not allow request for unadvertised object" err
1423 test_expect_success
'pushing a specific ref applies remote.$name.push as refmap' '
1424 mk_test testrepo heads/main &&
1425 test_when_finished "rm -rf src" &&
1427 test_when_finished "rm -rf dst" &&
1428 git init --bare dst &&
1431 git pull ../testrepo main &&
1433 git config remote.dst.url ../dst &&
1434 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1435 git push dst main &&
1436 git show-ref refs/heads/main |
1437 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1441 test_must_fail git show-ref refs/heads/next &&
1442 test_must_fail git show-ref refs/heads/main &&
1443 git show-ref refs/remotes/src/main >actual
1445 test_cmp dst/expect dst/actual
1448 test_expect_success
'with no remote.$name.push, it is not used as refmap' '
1449 mk_test testrepo heads/main &&
1450 test_when_finished "rm -rf src" &&
1452 test_when_finished "rm -rf dst" &&
1453 git init --bare dst &&
1456 git pull ../testrepo main &&
1458 git config remote.dst.url ../dst &&
1459 git config push.default matching &&
1460 git push dst main &&
1461 git show-ref refs/heads/main >../dst/expect
1465 test_must_fail git show-ref refs/heads/next &&
1466 git show-ref refs/heads/main >actual
1468 test_cmp dst/expect dst/actual
1471 test_expect_success
'with no remote.$name.push, upstream mapping is used' '
1472 mk_test testrepo heads/main &&
1473 test_when_finished "rm -rf src" &&
1475 test_when_finished "rm -rf dst" &&
1476 git init --bare dst &&
1479 git pull ../testrepo main &&
1481 git config remote.dst.url ../dst &&
1482 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1483 git config push.default upstream &&
1485 git config branch.main.merge refs/heads/trunk &&
1486 git config branch.main.remote dst &&
1488 git push dst main &&
1489 git show-ref refs/heads/main |
1490 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1494 test_must_fail git show-ref refs/heads/main &&
1495 test_must_fail git show-ref refs/heads/next &&
1496 git show-ref refs/heads/trunk >actual
1498 test_cmp dst/expect dst/actual
1501 test_expect_success
'push does not follow tags by default' '
1502 mk_test testrepo heads/main &&
1503 test_when_finished "rm -rf src" &&
1505 test_when_finished "rm -rf dst" &&
1506 git init --bare dst &&
1509 git pull ../testrepo main &&
1510 git tag -m "annotated" tag &&
1511 git checkout -b another &&
1512 git commit --allow-empty -m "future commit" &&
1513 git tag -m "future" future &&
1514 git checkout main &&
1515 git for-each-ref refs/heads/main >../expect &&
1516 git push ../dst main
1520 git for-each-ref >../actual
1522 test_cmp expect actual
1525 test_expect_success
'push --follow-tags only pushes relevant tags' '
1526 mk_test testrepo heads/main &&
1527 test_when_finished "rm -rf src" &&
1529 test_when_finished "rm -rf dst" &&
1530 git init --bare dst &&
1533 git pull ../testrepo main &&
1534 git tag -m "annotated" tag &&
1535 git checkout -b another &&
1536 git commit --allow-empty -m "future commit" &&
1537 git tag -m "future" future &&
1538 git checkout main &&
1539 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1540 git push --follow-tags ../dst main
1544 git for-each-ref >../actual
1546 test_cmp expect actual
1549 test_expect_success
'push --no-thin must produce non-thin pack' '
1550 cat >>path1 <<\EOF &&
1551 keep base version of path1 big enough, compared to the new changes
1552 later, in order to pass size heuristics in
1553 builtin/pack-objects.c:try_delta()
1555 git commit -am initial &&
1557 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1558 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1559 echo modified >> path1 &&
1560 git commit -am modified &&
1562 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1563 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1566 test_expect_success
'pushing a tag pushes the tagged object' '
1567 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1568 git tag -m foo tag-of-blob $blob &&
1569 test_when_finished "rm -rf dst.git" &&
1570 git init --bare dst.git &&
1571 git push dst.git tag-of-blob &&
1572 # the receiving index-pack should have noticed
1573 # any problems, but we double check
1574 echo unreferenced >expect &&
1575 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1576 test_cmp expect actual
1579 test_expect_success
'push into bare respects core.logallrefupdates' '
1580 test_when_finished "rm -rf dst.git" &&
1581 git init --bare dst.git &&
1582 git -C dst.git config core.logallrefupdates true &&
1584 # double push to test both with and without
1585 # the actual pack transfer
1586 git push dst.git main:one &&
1587 echo "one@{0} push" >expect &&
1588 git -C dst.git log -g --format="%gd %gs" one >actual &&
1589 test_cmp expect actual &&
1591 git push dst.git main:two &&
1592 echo "two@{0} push" >expect &&
1593 git -C dst.git log -g --format="%gd %gs" two >actual &&
1594 test_cmp expect actual
1597 test_expect_success
'fetch into bare respects core.logallrefupdates' '
1598 test_when_finished "rm -rf dst.git" &&
1599 git init --bare dst.git &&
1602 git config core.logallrefupdates true &&
1604 # as above, we double-fetch to test both
1605 # with and without pack transfer
1606 git fetch .. main:one &&
1607 echo "one@{0} fetch .. main:one: storing head" >expect &&
1608 git log -g --format="%gd %gs" one >actual &&
1609 test_cmp expect actual &&
1611 git fetch .. main:two &&
1612 echo "two@{0} fetch .. main:two: storing head" >expect &&
1613 git log -g --format="%gd %gs" two >actual &&
1614 test_cmp expect actual
1618 test_expect_success
'receive.denyCurrentBranch = updateInstead' '
1619 mk_empty testrepo &&
1620 git push testrepo main &&
1624 git config receive.denyCurrentBranch updateInstead
1626 test_commit third path2 &&
1628 # Try pushing into a repository with pristine working tree
1629 git push testrepo main &&
1632 git update-index -q --refresh &&
1633 git diff-files --quiet -- &&
1634 git diff-index --quiet --cached HEAD -- &&
1635 test third = "$(cat path2)" &&
1636 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1639 # Try pushing into a repository with working tree needing a refresh
1642 git reset --hard HEAD^ &&
1643 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1644 test-tool chmtime +100 path1
1646 git push testrepo main &&
1649 git update-index -q --refresh &&
1650 git diff-files --quiet -- &&
1651 git diff-index --quiet --cached HEAD -- &&
1652 test_cmp ../path1 path1 &&
1653 test third = "$(cat path2)" &&
1654 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1657 # Update what is to be pushed
1658 test_commit fourth path2 &&
1660 # Try pushing into a repository with a dirty working tree
1661 # (1) the working tree updated
1666 test_must_fail git push testrepo main &&
1669 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1670 git diff --quiet --cached &&
1671 test changed = "$(cat path1)"
1674 # (2) the index updated
1677 echo changed >path1 &&
1680 test_must_fail git push testrepo main &&
1683 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1685 test changed = "$(cat path1)"
1688 # Introduce a new file in the update
1689 test_commit fifth path3 &&
1691 # (3) the working tree has an untracked file that would interfere
1697 test_must_fail git push testrepo main &&
1700 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1702 git diff --quiet --cached &&
1703 test changed = "$(cat path3)"
1706 # (4) the target changes to what gets pushed but it still is a change
1710 echo fifth >path3 &&
1713 test_must_fail git push testrepo main &&
1716 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1718 test fifth = "$(cat path3)"
1721 # (5) push into void
1722 test_when_finished "rm -rf void" &&
1726 git config receive.denyCurrentBranch updateInstead
1728 git push void main &&
1731 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1733 git diff --cached --quiet
1736 # (6) updateInstead intervened by fast-forward check
1737 test_must_fail git push void main^:main &&
1738 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1739 git -C void diff --quiet &&
1740 git -C void diff --cached --quiet
1743 test_expect_success
'updateInstead with push-to-checkout hook' '
1744 test_when_finished "rm -rf testrepo" &&
1745 git init testrepo &&
1746 git -C testrepo pull .. main &&
1747 git -C testrepo reset --hard HEAD^^ &&
1748 git -C testrepo tag initial &&
1749 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1750 test_hook -C testrepo push-to-checkout <<-\EOF &&
1751 echo >&2 updating from $(git rev-parse HEAD)
1752 echo >&2 updating to "$1"
1754 git update-index -q --refresh &&
1755 git read-tree -u -m HEAD "$1" || {
1757 echo >&2 read-tree failed
1762 # Try pushing into a pristine
1763 git push testrepo main &&
1767 git diff HEAD --quiet &&
1768 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1771 # Try pushing into a repository with conflicting change
1774 git reset --hard initial &&
1775 echo conflicting >path2
1777 test_must_fail git push testrepo main &&
1780 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1781 test conflicting = "$(cat path2)" &&
1782 git diff-index --quiet --cached HEAD
1785 # Try pushing into a repository with unrelated change
1788 git reset --hard initial &&
1789 echo unrelated >path1 &&
1790 echo irrelevant >path5 &&
1793 git push testrepo main &&
1796 test "$(cat path1)" = unrelated &&
1797 test "$(cat path5)" = irrelevant &&
1798 test "$(git diff --name-only --cached HEAD)" = path5 &&
1799 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1803 test_when_finished "rm -rf void" &&
1805 git -C void config receive.denyCurrentBranch updateInstead &&
1806 test_hook -C void push-to-checkout <<-\EOF &&
1807 if git rev-parse --quiet --verify HEAD
1810 echo >&2 updating from $(git rev-parse HEAD)
1813 echo >&2 pushing into void
1815 echo >&2 updating to "$1"
1817 git update-index -q --refresh &&
1820 git read-tree -u -m HEAD "$1" ;;
1822 git read-tree -u -m "$1" ;;
1825 echo >&2 read-tree failed
1830 git push void main &&
1834 git diff --cached --quiet &&
1835 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1839 test_expect_success
'denyCurrentBranch and worktrees' '
1840 git worktree add new-wt &&
1841 git clone . cloned &&
1842 test_commit -C cloned first &&
1843 test_config receive.denyCurrentBranch refuse &&
1844 test_must_fail git -C cloned push origin HEAD:new-wt &&
1845 test_config receive.denyCurrentBranch updateInstead &&
1846 git -C cloned push origin HEAD:new-wt &&
1847 test_path_exists new-wt/first.t &&
1848 test_must_fail git -C cloned push --delete origin new-wt
1851 test_expect_success
'denyCurrentBranch and bare repository worktrees' '
1852 test_when_finished "rm -fr bare.git" &&
1853 git clone --bare . bare.git &&
1854 git -C bare.git worktree add wt &&
1855 test_commit grape &&
1856 git -C bare.git config receive.denyCurrentBranch refuse &&
1857 test_must_fail git push bare.git HEAD:wt &&
1858 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1859 git push bare.git HEAD:wt &&
1860 test_path_exists bare.git/wt/grape.t &&
1861 test_must_fail git push --delete bare.git wt
1864 test_expect_success
'refuse fetch to current branch of worktree' '
1865 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1866 git worktree add wt &&
1867 test_commit apple &&
1868 test_must_fail git fetch . HEAD:wt &&
1869 git fetch -u . HEAD:wt
1872 test_expect_success
'refuse fetch to current branch of bare repository worktree' '
1873 test_when_finished "rm -fr bare.git" &&
1874 git clone --bare . bare.git &&
1875 git -C bare.git worktree add wt &&
1876 test_commit banana &&
1877 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1878 git -C bare.git fetch -u .. HEAD:wt
1881 test_expect_success
'refuse to push a hidden ref, and make sure do not pollute the repository' '
1882 mk_empty testrepo &&
1883 git -C testrepo config receive.hiderefs refs/hidden &&
1884 git -C testrepo config receive.unpackLimit 1 &&
1885 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1886 test_dir_is_empty testrepo/.git/objects/pack
1889 test_expect_success
'push with config push.useBitmaps' '
1890 mk_test testrepo heads/main &&
1891 git checkout main &&
1892 test_unconfig push.useBitmaps &&
1893 GIT_TRACE2_EVENT="$PWD/default" \
1894 git push --quiet testrepo main:test &&
1895 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1896 --thin --delta-base-offset -q <default &&
1898 test_config push.useBitmaps true &&
1899 GIT_TRACE2_EVENT="$PWD/true" \
1900 git push --quiet testrepo main:test2 &&
1901 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1902 --thin --delta-base-offset -q <true &&
1904 test_config push.useBitmaps false &&
1905 GIT_TRACE2_EVENT="$PWD/false" \
1906 git push --quiet testrepo main:test3 &&
1907 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1908 --thin --delta-base-offset -q --no-use-bitmap-index <false