Merge branch 'ab/sequencer-unleak'
[git.git] / t / t5516-fetch-push.sh
blob98a27a2948b5f6c1b1c318b43c5fab3505f1426e
1 #!/bin/sh
3 test_description='Basic fetch/push functionality.
5 This test checks the following functionality:
7 * command-line syntax
8 * refspecs
9 * fast-forward detection, and overriding it
10 * configuration
11 * hooks
12 * --porcelain output format
13 * hiderefs
14 * reflogs
15 * URL validation
18 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
19 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
21 TEST_CREATE_REPO_NO_TEMPLATE=1
22 . ./test-lib.sh
24 D=$(pwd)
26 mk_empty () {
27 repo_name="$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
35 mk_test () {
36 repo_name="$1"
37 shift
39 mk_empty "$repo_name" &&
41 for ref in "$@"
43 git push "$repo_name" $the_first_commit:refs/$ref ||
44 exit
45 done &&
46 cd "$repo_name" &&
47 for ref in "$@"
49 echo "$the_first_commit" >expect &&
50 git show-ref -s --verify refs/$ref >actual &&
51 test_cmp expect actual ||
52 exit
53 done &&
54 git fsck --full
58 mk_test_with_hooks() {
59 repo_name=$1
60 mk_test "$@" &&
61 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
62 cat - >>pre-receive.actual
63 EOF
65 test_hook -C "$repo_name" update <<-'EOF' &&
66 printf "%s %s %s\n" "$@" >>update.actual
67 EOF
69 test_hook -C "$repo_name" post-receive <<-'EOF' &&
70 cat - >>post-receive.actual
71 EOF
73 test_hook -C "$repo_name" post-update <<-'EOF'
74 for ref in "$@"
76 printf "%s\n" "$ref" >>post-update.actual
77 done
78 EOF
81 mk_child() {
82 test_when_finished "rm -rf \"$2\"" &&
83 git clone --template= "$1" "$2"
86 check_push_result () {
87 test $# -ge 3 ||
88 BUG "check_push_result requires at least 3 parameters"
90 repo_name="$1"
91 shift
94 cd "$repo_name" &&
95 echo "$1" >expect &&
96 shift &&
97 for ref in "$@"
99 git show-ref -s --verify refs/$ref >actual &&
100 test_cmp expect actual ||
101 exit
102 done &&
103 git fsck --full
107 test_expect_success setup '
109 >path1 &&
110 git add path1 &&
111 test_tick &&
112 git commit -a -m repo &&
113 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
115 >path2 &&
116 git add path2 &&
117 test_tick &&
118 git commit -a -m second &&
119 the_commit=$(git show-ref -s --verify refs/heads/main)
123 test_expect_success 'fetch without wildcard' '
124 mk_empty testrepo &&
126 cd testrepo &&
127 git fetch .. refs/heads/main:refs/remotes/origin/main &&
129 echo "$the_commit commit refs/remotes/origin/main" >expect &&
130 git for-each-ref refs/remotes/origin >actual &&
131 test_cmp expect actual
135 test_expect_success 'fetch with wildcard' '
136 mk_empty testrepo &&
138 cd testrepo &&
139 git config remote.up.url .. &&
140 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
141 git fetch up &&
143 echo "$the_commit commit refs/remotes/origin/main" >expect &&
144 git for-each-ref refs/remotes/origin >actual &&
145 test_cmp expect actual
149 test_expect_success 'fetch with insteadOf' '
150 mk_empty testrepo &&
152 TRASH=$(pwd)/ &&
153 cd testrepo &&
154 git config "url.$TRASH.insteadOf" trash/ &&
155 git config remote.up.url trash/. &&
156 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
157 git fetch up &&
159 echo "$the_commit commit refs/remotes/origin/main" >expect &&
160 git for-each-ref refs/remotes/origin >actual &&
161 test_cmp expect actual
165 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
166 mk_empty testrepo &&
168 TRASH=$(pwd)/ &&
169 cd testrepo &&
170 git config "url.trash/.pushInsteadOf" "$TRASH" &&
171 git config remote.up.url "$TRASH." &&
172 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
173 git fetch up &&
175 echo "$the_commit commit refs/remotes/origin/main" >expect &&
176 git for-each-ref refs/remotes/origin >actual &&
177 test_cmp expect actual
181 grep_wrote () {
182 object_count=$1
183 file_name=$2
184 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
187 test_expect_success 'push without negotiation' '
188 mk_empty testrepo &&
189 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
190 test_commit -C testrepo unrelated_commit &&
191 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
192 test_when_finished "rm event" &&
193 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
194 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
197 test_expect_success 'push with negotiation' '
198 mk_empty testrepo &&
199 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
200 test_commit -C testrepo unrelated_commit &&
201 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
202 test_when_finished "rm event" &&
203 GIT_TRACE2_EVENT="$(pwd)/event" \
204 git -c protocol.version=2 -c push.negotiate=1 \
205 push testrepo refs/heads/main:refs/remotes/origin/main &&
206 grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
207 grep_wrote 2 event # 1 commit, 1 tree
210 test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
211 mk_empty testrepo &&
212 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
213 test_commit -C testrepo unrelated_commit &&
214 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
215 test_when_finished "rm event" &&
216 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
217 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
218 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
219 test_i18ngrep "push negotiation failed" err
222 test_expect_success 'push with negotiation does not attempt to fetch submodules' '
223 mk_empty submodule_upstream &&
224 test_commit -C submodule_upstream submodule_commit &&
225 test_config_global protocol.file.allow always &&
226 git submodule add ./submodule_upstream submodule &&
227 mk_empty testrepo &&
228 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
229 test_commit -C testrepo unrelated_commit &&
230 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
231 GIT_TRACE2_EVENT="$(pwd)/event" git -c submodule.recurse=true \
232 -c protocol.version=2 -c push.negotiate=1 \
233 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
234 grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
235 ! grep "Fetching submodule" err
238 test_expect_success 'push without wildcard' '
239 mk_empty testrepo &&
241 git push testrepo refs/heads/main:refs/remotes/origin/main &&
243 cd testrepo &&
244 echo "$the_commit commit refs/remotes/origin/main" >expect &&
245 git for-each-ref refs/remotes/origin >actual &&
246 test_cmp expect actual
250 test_expect_success 'push with wildcard' '
251 mk_empty testrepo &&
253 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
255 cd testrepo &&
256 echo "$the_commit commit refs/remotes/origin/main" >expect &&
257 git for-each-ref refs/remotes/origin >actual &&
258 test_cmp expect actual
262 test_expect_success 'push with insteadOf' '
263 mk_empty testrepo &&
264 TRASH="$(pwd)/" &&
265 test_config "url.$TRASH.insteadOf" trash/ &&
266 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
268 cd testrepo &&
269 echo "$the_commit commit refs/remotes/origin/main" >expect &&
270 git for-each-ref refs/remotes/origin >actual &&
271 test_cmp expect actual
275 test_expect_success 'push with pushInsteadOf' '
276 mk_empty testrepo &&
277 TRASH="$(pwd)/" &&
278 test_config "url.$TRASH.pushInsteadOf" trash/ &&
279 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
281 cd testrepo &&
282 echo "$the_commit commit refs/remotes/origin/main" >expect &&
283 git for-each-ref refs/remotes/origin >actual &&
284 test_cmp expect actual
288 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
289 mk_empty testrepo &&
290 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
291 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
292 test_config remote.r.url trash/wrong &&
293 test_config remote.r.pushurl "testrepo/" &&
294 git push r refs/heads/main:refs/remotes/origin/main &&
296 cd testrepo &&
297 echo "$the_commit commit refs/remotes/origin/main" >expect &&
298 git for-each-ref refs/remotes/origin >actual &&
299 test_cmp expect actual
303 test_expect_success 'push with matching heads' '
305 mk_test testrepo heads/main &&
306 git push testrepo : &&
307 check_push_result testrepo $the_commit heads/main
311 test_expect_success 'push with matching heads on the command line' '
313 mk_test testrepo heads/main &&
314 git push testrepo : &&
315 check_push_result testrepo $the_commit heads/main
319 test_expect_success 'failed (non-fast-forward) push with matching heads' '
321 mk_test testrepo heads/main &&
322 git push testrepo : &&
323 git commit --amend -massaged &&
324 test_must_fail git push testrepo &&
325 check_push_result testrepo $the_commit heads/main &&
326 git reset --hard $the_commit
330 test_expect_success 'push --force with matching heads' '
332 mk_test testrepo heads/main &&
333 git push testrepo : &&
334 git commit --amend -massaged &&
335 git push --force testrepo : &&
336 ! check_push_result testrepo $the_commit heads/main &&
337 git reset --hard $the_commit
341 test_expect_success 'push with matching heads and forced update' '
343 mk_test testrepo heads/main &&
344 git push testrepo : &&
345 git commit --amend -massaged &&
346 git push testrepo +: &&
347 ! check_push_result testrepo $the_commit heads/main &&
348 git reset --hard $the_commit
352 test_expect_success 'push with no ambiguity (1)' '
354 mk_test testrepo heads/main &&
355 git push testrepo main:main &&
356 check_push_result testrepo $the_commit heads/main
360 test_expect_success 'push with no ambiguity (2)' '
362 mk_test testrepo remotes/origin/main &&
363 git push testrepo main:origin/main &&
364 check_push_result testrepo $the_commit remotes/origin/main
368 test_expect_success 'push with colon-less refspec, no ambiguity' '
370 mk_test testrepo heads/main heads/t/main &&
371 git branch -f t/main main &&
372 git push testrepo main &&
373 check_push_result testrepo $the_commit heads/main &&
374 check_push_result testrepo $the_first_commit heads/t/main
378 test_expect_success 'push with weak ambiguity (1)' '
380 mk_test testrepo heads/main remotes/origin/main &&
381 git push testrepo main:main &&
382 check_push_result testrepo $the_commit heads/main &&
383 check_push_result testrepo $the_first_commit remotes/origin/main
387 test_expect_success 'push with weak ambiguity (2)' '
389 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
390 git push testrepo main:main &&
391 check_push_result testrepo $the_commit heads/main &&
392 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
396 test_expect_success 'push with ambiguity' '
398 mk_test testrepo heads/frotz tags/frotz &&
399 test_must_fail git push testrepo main:frotz &&
400 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
404 test_expect_success 'push with colon-less refspec (1)' '
406 mk_test testrepo heads/frotz tags/frotz &&
407 git branch -f frotz main &&
408 git push testrepo frotz &&
409 check_push_result testrepo $the_commit heads/frotz &&
410 check_push_result testrepo $the_first_commit tags/frotz
414 test_expect_success 'push with colon-less refspec (2)' '
416 mk_test testrepo heads/frotz tags/frotz &&
417 if git show-ref --verify -q refs/heads/frotz
418 then
419 git branch -D frotz
420 fi &&
421 git tag -f frotz &&
422 git push -f testrepo frotz &&
423 check_push_result testrepo $the_commit tags/frotz &&
424 check_push_result testrepo $the_first_commit heads/frotz
428 test_expect_success 'push with colon-less refspec (3)' '
430 mk_test testrepo &&
431 if git show-ref --verify -q refs/tags/frotz
432 then
433 git tag -d frotz
434 fi &&
435 git branch -f frotz main &&
436 git push testrepo frotz &&
437 check_push_result testrepo $the_commit heads/frotz &&
438 test 1 = $( cd testrepo && git show-ref | wc -l )
441 test_expect_success 'push with colon-less refspec (4)' '
443 mk_test testrepo &&
444 if git show-ref --verify -q refs/heads/frotz
445 then
446 git branch -D frotz
447 fi &&
448 git tag -f frotz &&
449 git push testrepo frotz &&
450 check_push_result testrepo $the_commit tags/frotz &&
451 test 1 = $( cd testrepo && git show-ref | wc -l )
455 test_expect_success 'push head with non-existent, incomplete dest' '
457 mk_test testrepo &&
458 git push testrepo main:branch &&
459 check_push_result testrepo $the_commit heads/branch
463 test_expect_success 'push tag with non-existent, incomplete dest' '
465 mk_test testrepo &&
466 git tag -f v1.0 &&
467 git push testrepo v1.0:tag &&
468 check_push_result testrepo $the_commit tags/tag
472 test_expect_success 'push sha1 with non-existent, incomplete dest' '
474 mk_test testrepo &&
475 test_must_fail git push testrepo $(git rev-parse main):foo
479 test_expect_success 'push ref expression with non-existent, incomplete dest' '
481 mk_test testrepo &&
482 test_must_fail git push testrepo main^:branch
486 for head in HEAD @
489 test_expect_success "push with $head" '
490 mk_test testrepo heads/main &&
491 git checkout main &&
492 git push testrepo $head &&
493 check_push_result testrepo $the_commit heads/main
496 test_expect_success "push with $head nonexisting at remote" '
497 mk_test testrepo heads/main &&
498 git checkout -b local main &&
499 test_when_finished "git checkout main; git branch -D local" &&
500 git push testrepo $head &&
501 check_push_result testrepo $the_commit heads/local
504 test_expect_success "push with +$head" '
505 mk_test testrepo heads/main &&
506 git checkout -b local main &&
507 test_when_finished "git checkout main; git branch -D local" &&
508 git push testrepo main local &&
509 check_push_result testrepo $the_commit heads/main &&
510 check_push_result testrepo $the_commit heads/local &&
512 # Without force rewinding should fail
513 git reset --hard $head^ &&
514 test_must_fail git push testrepo $head &&
515 check_push_result testrepo $the_commit heads/local &&
517 # With force rewinding should succeed
518 git push testrepo +$head &&
519 check_push_result testrepo $the_first_commit heads/local
522 test_expect_success "push $head with non-existent, incomplete dest" '
523 mk_test testrepo &&
524 git checkout main &&
525 git push testrepo $head:branch &&
526 check_push_result testrepo $the_commit heads/branch
530 test_expect_success "push with config remote.*.push = $head" '
531 mk_test testrepo heads/local &&
532 git checkout main &&
533 git branch -f local $the_commit &&
534 test_when_finished "git branch -D local" &&
536 cd testrepo &&
537 git checkout local &&
538 git reset --hard $the_first_commit
539 ) &&
540 test_config remote.there.url testrepo &&
541 test_config remote.there.push $head &&
542 test_config branch.main.remote there &&
543 git push &&
544 check_push_result testrepo $the_commit heads/main &&
545 check_push_result testrepo $the_first_commit heads/local
548 done
550 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
551 mk_test testrepo heads/main &&
552 git checkout $the_first_commit &&
553 test_config remote.there.url testrepo &&
554 test_config remote.there.push refs/heads/main:refs/heads/main &&
555 git push there &&
556 check_push_result testrepo $the_commit heads/main
559 test_expect_success 'push with remote.pushdefault' '
560 mk_test up_repo heads/main &&
561 mk_test down_repo heads/main &&
562 test_config remote.up.url up_repo &&
563 test_config remote.down.url down_repo &&
564 test_config branch.main.remote up &&
565 test_config remote.pushdefault down &&
566 test_config push.default matching &&
567 git push &&
568 check_push_result up_repo $the_first_commit heads/main &&
569 check_push_result down_repo $the_commit heads/main
572 test_expect_success 'push with config remote.*.pushurl' '
574 mk_test testrepo heads/main &&
575 git checkout main &&
576 test_config remote.there.url test2repo &&
577 test_config remote.there.pushurl testrepo &&
578 git push there : &&
579 check_push_result testrepo $the_commit heads/main
582 test_expect_success 'push with config branch.*.pushremote' '
583 mk_test up_repo heads/main &&
584 mk_test side_repo heads/main &&
585 mk_test down_repo heads/main &&
586 test_config remote.up.url up_repo &&
587 test_config remote.pushdefault side_repo &&
588 test_config remote.down.url down_repo &&
589 test_config branch.main.remote up &&
590 test_config branch.main.pushremote down &&
591 test_config push.default matching &&
592 git push &&
593 check_push_result up_repo $the_first_commit heads/main &&
594 check_push_result side_repo $the_first_commit heads/main &&
595 check_push_result down_repo $the_commit heads/main
598 test_expect_success 'branch.*.pushremote config order is irrelevant' '
599 mk_test one_repo heads/main &&
600 mk_test two_repo heads/main &&
601 test_config remote.one.url one_repo &&
602 test_config remote.two.url two_repo &&
603 test_config branch.main.pushremote two_repo &&
604 test_config remote.pushdefault one_repo &&
605 test_config push.default matching &&
606 git push &&
607 check_push_result one_repo $the_first_commit heads/main &&
608 check_push_result two_repo $the_commit heads/main
611 test_expect_success 'push rejects empty branch name entries' '
612 mk_test one_repo heads/main &&
613 test_config remote.one.url one_repo &&
614 test_config branch..remote one &&
615 test_config branch..merge refs/heads/ &&
616 test_config branch.main.remote one &&
617 test_config branch.main.merge refs/heads/main &&
618 test_must_fail git push 2>err &&
619 grep "bad config variable .branch\.\." err
622 test_expect_success 'push ignores "branch." config without subsection' '
623 mk_test one_repo heads/main &&
624 test_config remote.one.url one_repo &&
625 test_config branch.autoSetupMerge true &&
626 test_config branch.main.remote one &&
627 test_config branch.main.merge refs/heads/main &&
628 git push
631 test_expect_success 'push with dry-run' '
633 mk_test testrepo heads/main &&
634 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
635 git push --dry-run testrepo : &&
636 check_push_result testrepo $old_commit heads/main
639 test_expect_success 'push updates local refs' '
641 mk_test testrepo heads/main &&
642 mk_child testrepo child &&
644 cd child &&
645 git pull .. main &&
646 git push &&
647 test $(git rev-parse main) = \
648 $(git rev-parse remotes/origin/main)
653 test_expect_success 'push updates up-to-date local refs' '
655 mk_test testrepo heads/main &&
656 mk_child testrepo child1 &&
657 mk_child testrepo child2 &&
658 (cd child1 && git pull .. main && git push) &&
660 cd child2 &&
661 git pull ../child1 main &&
662 git push &&
663 test $(git rev-parse main) = \
664 $(git rev-parse remotes/origin/main)
669 test_expect_success 'push preserves up-to-date packed refs' '
671 mk_test testrepo heads/main &&
672 mk_child testrepo child &&
674 cd child &&
675 git push &&
676 ! test -f .git/refs/remotes/origin/main
681 test_expect_success 'push does not update local refs on failure' '
683 mk_test testrepo heads/main &&
684 mk_child testrepo child &&
685 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
686 chmod +x testrepo/.git/hooks/pre-receive &&
688 cd child &&
689 git pull .. main &&
690 test_must_fail git push &&
691 test $(git rev-parse main) != \
692 $(git rev-parse remotes/origin/main)
697 test_expect_success 'allow deleting an invalid remote ref' '
699 mk_test testrepo heads/branch &&
700 rm -f testrepo/.git/objects/??/* &&
701 git push testrepo :refs/heads/branch &&
702 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
706 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
707 mk_test_with_hooks testrepo heads/main heads/next &&
708 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
709 newmain=$(git show-ref -s --verify refs/heads/main) &&
710 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
711 newnext=$ZERO_OID &&
712 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
714 cd testrepo/.git &&
715 cat >pre-receive.expect <<-EOF &&
716 $orgmain $newmain refs/heads/main
717 $orgnext $newnext refs/heads/next
720 cat >update.expect <<-EOF &&
721 refs/heads/main $orgmain $newmain
722 refs/heads/next $orgnext $newnext
725 cat >post-receive.expect <<-EOF &&
726 $orgmain $newmain refs/heads/main
727 $orgnext $newnext refs/heads/next
730 cat >post-update.expect <<-EOF &&
731 refs/heads/main
732 refs/heads/next
735 test_cmp pre-receive.expect pre-receive.actual &&
736 test_cmp update.expect update.actual &&
737 test_cmp post-receive.expect post-receive.actual &&
738 test_cmp post-update.expect post-update.actual
742 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
743 mk_test_with_hooks testrepo heads/branch &&
744 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
745 rm -f testrepo/.git/objects/??/* &&
746 git push testrepo :refs/heads/branch &&
748 cd testrepo/.git &&
749 cat >pre-receive.expect <<-EOF &&
750 $orig $ZERO_OID refs/heads/branch
753 cat >update.expect <<-EOF &&
754 refs/heads/branch $orig $ZERO_OID
757 cat >post-receive.expect <<-EOF &&
758 $orig $ZERO_OID refs/heads/branch
761 cat >post-update.expect <<-EOF &&
762 refs/heads/branch
765 test_cmp pre-receive.expect pre-receive.actual &&
766 test_cmp update.expect update.actual &&
767 test_cmp post-receive.expect post-receive.actual &&
768 test_cmp post-update.expect post-update.actual
772 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
773 mk_test_with_hooks testrepo heads/main &&
774 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
775 newmain=$(git show-ref -s --verify refs/heads/main) &&
776 git push testrepo main :refs/heads/nonexistent &&
778 cd testrepo/.git &&
779 cat >pre-receive.expect <<-EOF &&
780 $orgmain $newmain refs/heads/main
781 $ZERO_OID $ZERO_OID refs/heads/nonexistent
784 cat >update.expect <<-EOF &&
785 refs/heads/main $orgmain $newmain
786 refs/heads/nonexistent $ZERO_OID $ZERO_OID
789 cat >post-receive.expect <<-EOF &&
790 $orgmain $newmain refs/heads/main
793 cat >post-update.expect <<-EOF &&
794 refs/heads/main
797 test_cmp pre-receive.expect pre-receive.actual &&
798 test_cmp update.expect update.actual &&
799 test_cmp post-receive.expect post-receive.actual &&
800 test_cmp post-update.expect post-update.actual
804 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
805 mk_test_with_hooks testrepo heads/main &&
806 git push testrepo :refs/heads/nonexistent &&
808 cd testrepo/.git &&
809 cat >pre-receive.expect <<-EOF &&
810 $ZERO_OID $ZERO_OID refs/heads/nonexistent
813 cat >update.expect <<-EOF &&
814 refs/heads/nonexistent $ZERO_OID $ZERO_OID
817 test_cmp pre-receive.expect pre-receive.actual &&
818 test_cmp update.expect update.actual &&
819 test_path_is_missing post-receive.actual &&
820 test_path_is_missing post-update.actual
824 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
825 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
826 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
827 newmain=$(git show-ref -s --verify refs/heads/main) &&
828 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
829 newnext=$ZERO_OID &&
830 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
831 newseen=$(git show-ref -s --verify refs/heads/main) &&
832 git push testrepo refs/heads/main:refs/heads/main \
833 refs/heads/main:refs/heads/seen :refs/heads/next \
834 :refs/heads/nonexistent &&
836 cd testrepo/.git &&
837 cat >pre-receive.expect <<-EOF &&
838 $orgmain $newmain refs/heads/main
839 $orgnext $newnext refs/heads/next
840 $orgseen $newseen refs/heads/seen
841 $ZERO_OID $ZERO_OID refs/heads/nonexistent
844 cat >update.expect <<-EOF &&
845 refs/heads/main $orgmain $newmain
846 refs/heads/next $orgnext $newnext
847 refs/heads/seen $orgseen $newseen
848 refs/heads/nonexistent $ZERO_OID $ZERO_OID
851 cat >post-receive.expect <<-EOF &&
852 $orgmain $newmain refs/heads/main
853 $orgnext $newnext refs/heads/next
854 $orgseen $newseen refs/heads/seen
857 cat >post-update.expect <<-EOF &&
858 refs/heads/main
859 refs/heads/next
860 refs/heads/seen
863 test_cmp pre-receive.expect pre-receive.actual &&
864 test_cmp update.expect update.actual &&
865 test_cmp post-receive.expect post-receive.actual &&
866 test_cmp post-update.expect post-update.actual
870 test_expect_success 'allow deleting a ref using --delete' '
871 mk_test testrepo heads/main &&
872 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
873 git push testrepo --delete main &&
874 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
877 test_expect_success 'allow deleting a tag using --delete' '
878 mk_test testrepo heads/main &&
879 git tag -a -m dummy_message deltag heads/main &&
880 git push testrepo --tags &&
881 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
882 git push testrepo --delete tag deltag &&
883 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
886 test_expect_success 'push --delete without args aborts' '
887 mk_test testrepo heads/main &&
888 test_must_fail git push testrepo --delete
891 test_expect_success 'push --delete refuses src:dest refspecs' '
892 mk_test testrepo heads/main &&
893 test_must_fail git push testrepo --delete main:foo
896 test_expect_success 'push --delete refuses empty string' '
897 mk_test testrepo heads/master &&
898 test_must_fail git push testrepo --delete ""
901 test_expect_success 'warn on push to HEAD of non-bare repository' '
902 mk_test testrepo heads/main &&
904 cd testrepo &&
905 git checkout main &&
906 git config receive.denyCurrentBranch warn
907 ) &&
908 git push testrepo main 2>stderr &&
909 grep "warning: updating the current branch" stderr
912 test_expect_success 'deny push to HEAD of non-bare repository' '
913 mk_test testrepo heads/main &&
915 cd testrepo &&
916 git checkout main &&
917 git config receive.denyCurrentBranch true
918 ) &&
919 test_must_fail git push testrepo main
922 test_expect_success 'allow push to HEAD of bare repository (bare)' '
923 mk_test testrepo heads/main &&
925 cd testrepo &&
926 git checkout main &&
927 git config receive.denyCurrentBranch true &&
928 git config core.bare true
929 ) &&
930 git push testrepo main 2>stderr &&
931 ! grep "warning: updating the current branch" stderr
934 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
935 mk_test testrepo heads/main &&
937 cd testrepo &&
938 git checkout main &&
939 git config receive.denyCurrentBranch false
940 ) &&
941 git push testrepo main 2>stderr &&
942 ! grep "warning: updating the current branch" stderr
945 test_expect_success 'fetch with branches' '
946 mk_empty testrepo &&
947 git branch second $the_first_commit &&
948 git checkout second &&
949 mkdir testrepo/.git/branches &&
950 echo ".." > testrepo/.git/branches/branch1 &&
952 cd testrepo &&
953 git fetch branch1 &&
954 echo "$the_commit commit refs/heads/branch1" >expect &&
955 git for-each-ref refs/heads >actual &&
956 test_cmp expect actual
957 ) &&
958 git checkout main
961 test_expect_success 'fetch with branches containing #' '
962 mk_empty testrepo &&
963 mkdir testrepo/.git/branches &&
964 echo "..#second" > testrepo/.git/branches/branch2 &&
966 cd testrepo &&
967 git fetch branch2 &&
968 echo "$the_first_commit commit refs/heads/branch2" >expect &&
969 git for-each-ref refs/heads >actual &&
970 test_cmp expect actual
971 ) &&
972 git checkout main
975 test_expect_success 'push with branches' '
976 mk_empty testrepo &&
977 git checkout second &&
979 test_when_finished "rm -rf .git/branches" &&
980 mkdir .git/branches &&
981 echo "testrepo" > .git/branches/branch1 &&
983 git push branch1 &&
985 cd testrepo &&
986 echo "$the_first_commit commit refs/heads/main" >expect &&
987 git for-each-ref refs/heads >actual &&
988 test_cmp expect actual
992 test_expect_success 'push with branches containing #' '
993 mk_empty testrepo &&
995 test_when_finished "rm -rf .git/branches" &&
996 mkdir .git/branches &&
997 echo "testrepo#branch3" > .git/branches/branch2 &&
999 git push branch2 &&
1001 cd testrepo &&
1002 echo "$the_first_commit commit refs/heads/branch3" >expect &&
1003 git for-each-ref refs/heads >actual &&
1004 test_cmp expect actual
1005 ) &&
1006 git checkout main
1009 test_expect_success 'push into aliased refs (consistent)' '
1010 mk_test testrepo heads/main &&
1011 mk_child testrepo child1 &&
1012 mk_child testrepo child2 &&
1014 cd child1 &&
1015 git branch foo &&
1016 git symbolic-ref refs/heads/bar refs/heads/foo &&
1017 git config receive.denyCurrentBranch false
1018 ) &&
1020 cd child2 &&
1021 >path2 &&
1022 git add path2 &&
1023 test_tick &&
1024 git commit -a -m child2 &&
1025 git branch foo &&
1026 git branch bar &&
1027 git push ../child1 foo bar
1031 test_expect_success 'push into aliased refs (inconsistent)' '
1032 mk_test testrepo heads/main &&
1033 mk_child testrepo child1 &&
1034 mk_child testrepo child2 &&
1036 cd child1 &&
1037 git branch foo &&
1038 git symbolic-ref refs/heads/bar refs/heads/foo &&
1039 git config receive.denyCurrentBranch false
1040 ) &&
1042 cd child2 &&
1043 >path2 &&
1044 git add path2 &&
1045 test_tick &&
1046 git commit -a -m child2 &&
1047 git branch foo &&
1048 >path3 &&
1049 git add path3 &&
1050 test_tick &&
1051 git commit -a -m child2 &&
1052 git branch bar &&
1053 test_must_fail git push ../child1 foo bar 2>stderr &&
1054 grep "refusing inconsistent update" stderr
1058 test_force_push_tag () {
1059 tag_type_description=$1
1060 tag_args=$2
1062 test_expect_success "force pushing required to update $tag_type_description" "
1063 mk_test testrepo heads/main &&
1064 mk_child testrepo child1 &&
1065 mk_child testrepo child2 &&
1067 cd child1 &&
1068 git tag testTag &&
1069 git push ../child2 testTag &&
1070 >file1 &&
1071 git add file1 &&
1072 git commit -m 'file1' &&
1073 git tag $tag_args testTag &&
1074 test_must_fail git push ../child2 testTag &&
1075 git push --force ../child2 testTag &&
1076 git tag $tag_args testTag HEAD~ &&
1077 test_must_fail git push ../child2 testTag &&
1078 git push --force ../child2 testTag &&
1080 # Clobbering without + in refspec needs --force
1081 git tag -f testTag &&
1082 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1083 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1085 # Clobbering with + in refspec does not need --force
1086 git tag -f testTag HEAD~ &&
1087 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1089 # Clobbering with --no-force still obeys + in refspec
1090 git tag -f testTag &&
1091 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1093 # Clobbering with/without --force and 'tag <name>' format
1094 git tag -f testTag HEAD~ &&
1095 test_must_fail git push ../child2 tag testTag &&
1096 git push --force ../child2 tag testTag
1101 test_force_push_tag "lightweight tag" "-f"
1102 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1104 test_force_fetch_tag () {
1105 tag_type_description=$1
1106 tag_args=$2
1108 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1109 mk_test testrepo heads/main &&
1110 mk_child testrepo child1 &&
1111 mk_child testrepo child2 &&
1113 cd testrepo &&
1114 git tag testTag &&
1115 git -C ../child1 fetch origin tag testTag &&
1116 >file1 &&
1117 git add file1 &&
1118 git commit -m 'file1' &&
1119 git tag $tag_args testTag &&
1120 test_must_fail git -C ../child1 fetch origin tag testTag &&
1121 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1126 test_force_fetch_tag "lightweight tag" "-f"
1127 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1129 test_expect_success 'push --porcelain' '
1130 mk_empty testrepo &&
1131 echo >.git/foo "To testrepo" &&
1132 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1133 echo >>.git/foo "Done" &&
1134 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1136 cd testrepo &&
1137 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1138 git for-each-ref refs/remotes/origin >actual &&
1139 test_cmp expect actual
1140 ) &&
1141 test_cmp .git/foo .git/bar
1144 test_expect_success 'push --porcelain bad url' '
1145 mk_empty testrepo &&
1146 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1147 ! grep -q Done .git/bar
1150 test_expect_success 'push --porcelain rejected' '
1151 mk_empty testrepo &&
1152 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1153 (cd testrepo &&
1154 git reset --hard origin/main^ &&
1155 git config receive.denyCurrentBranch true) &&
1157 echo >.git/foo "To testrepo" &&
1158 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1159 echo >>.git/foo "Done" &&
1161 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1162 test_cmp .git/foo .git/bar
1165 test_expect_success 'push --porcelain --dry-run rejected' '
1166 mk_empty testrepo &&
1167 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1168 (cd testrepo &&
1169 git reset --hard origin/main &&
1170 git config receive.denyCurrentBranch true) &&
1172 echo >.git/foo "To testrepo" &&
1173 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1174 echo >>.git/foo "Done" &&
1176 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1177 test_cmp .git/foo .git/bar
1180 test_expect_success 'push --prune' '
1181 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1182 git push --prune testrepo : &&
1183 check_push_result testrepo $the_commit heads/main &&
1184 check_push_result testrepo $the_first_commit heads/second &&
1185 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1188 test_expect_success 'push --prune refspec' '
1189 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1190 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1191 check_push_result testrepo $the_commit tmp/main &&
1192 check_push_result testrepo $the_first_commit tmp/second &&
1193 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1196 for configsection in transfer receive
1198 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1199 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1201 cd testrepo &&
1202 git config $configsection.hiderefs refs/hidden
1203 ) &&
1205 # push to unhidden ref succeeds normally
1206 git push testrepo main:refs/heads/main &&
1207 check_push_result testrepo $the_commit heads/main &&
1209 # push to update a hidden ref should fail
1210 test_must_fail git push testrepo main:refs/hidden/one &&
1211 check_push_result testrepo $the_first_commit hidden/one &&
1213 # push to delete a hidden ref should fail
1214 test_must_fail git push testrepo :refs/hidden/two &&
1215 check_push_result testrepo $the_first_commit hidden/two &&
1217 # idempotent push to update a hidden ref should fail
1218 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1219 check_push_result testrepo $the_first_commit hidden/three
1221 done
1223 test_expect_success 'fetch exact SHA1' '
1224 mk_test testrepo heads/main hidden/one &&
1225 git push testrepo main:refs/hidden/one &&
1227 cd testrepo &&
1228 git config transfer.hiderefs refs/hidden
1229 ) &&
1230 check_push_result testrepo $the_commit hidden/one &&
1232 mk_child testrepo child &&
1234 cd child &&
1236 # make sure $the_commit does not exist here
1237 git repack -a -d &&
1238 git prune &&
1239 test_must_fail git cat-file -t $the_commit &&
1241 # Some protocol versions (e.g. 2) support fetching
1242 # unadvertised objects, so restrict this test to v0.
1244 # fetching the hidden object should fail by default
1245 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1246 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1247 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1248 test_must_fail git rev-parse --verify refs/heads/copy &&
1250 # the server side can allow it to succeed
1252 cd ../testrepo &&
1253 git config uploadpack.allowtipsha1inwant true
1254 ) &&
1256 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1257 cat >expect <<-EOF &&
1258 $the_commit
1259 $the_first_commit
1262 git rev-parse --verify refs/heads/copy &&
1263 git rev-parse --verify refs/heads/extra
1264 } >actual &&
1265 test_cmp expect actual
1269 test_expect_success 'fetch exact SHA1 in protocol v2' '
1270 mk_test testrepo heads/main hidden/one &&
1271 git push testrepo main:refs/hidden/one &&
1272 git -C testrepo config transfer.hiderefs refs/hidden &&
1273 check_push_result testrepo $the_commit hidden/one &&
1275 mk_child testrepo child &&
1276 git -C child config protocol.version 2 &&
1278 # make sure $the_commit does not exist here
1279 git -C child repack -a -d &&
1280 git -C child prune &&
1281 test_must_fail git -C child cat-file -t $the_commit &&
1283 # fetching the hidden object succeeds by default
1284 # NEEDSWORK: should this match the v0 behavior instead?
1285 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1288 for configallowtipsha1inwant in true false
1290 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1291 mk_empty testrepo &&
1293 cd testrepo &&
1294 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1295 git commit --allow-empty -m foo &&
1296 git commit --allow-empty -m bar
1297 ) &&
1298 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1299 mk_empty shallow &&
1301 cd shallow &&
1302 # Some protocol versions (e.g. 2) support fetching
1303 # unadvertised objects, so restrict this test to v0.
1304 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1305 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1306 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1307 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1308 git cat-file commit $SHA1
1312 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1313 mk_empty testrepo &&
1315 cd testrepo &&
1316 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1317 git commit --allow-empty -m foo &&
1318 git commit --allow-empty -m bar &&
1319 git commit --allow-empty -m xyz
1320 ) &&
1321 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1322 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1323 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1325 cd testrepo &&
1326 git reset --hard $SHA1_2 &&
1327 git cat-file commit $SHA1_1 &&
1328 git cat-file commit $SHA1_3
1329 ) &&
1330 mk_empty shallow &&
1332 cd shallow &&
1333 # Some protocol versions (e.g. 2) support fetching
1334 # unadvertised objects, so restrict this test to v0.
1335 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1336 git fetch ../testrepo/.git $SHA1_3 &&
1337 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1338 git fetch ../testrepo/.git $SHA1_1 &&
1339 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1340 git fetch ../testrepo/.git $SHA1_1 &&
1341 git cat-file commit $SHA1_1 &&
1342 test_must_fail git cat-file commit $SHA1_2 &&
1343 git fetch ../testrepo/.git $SHA1_2 &&
1344 git cat-file commit $SHA1_2 &&
1345 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1346 git fetch ../testrepo/.git $SHA1_3 2>err &&
1347 # ideally we would insist this be on a "remote error:"
1348 # line, but it is racy; see the commit message
1349 test_i18ngrep "not our ref.*$SHA1_3\$" err
1352 done
1354 test_expect_success 'fetch follows tags by default' '
1355 mk_test testrepo heads/main &&
1356 test_when_finished "rm -rf src" &&
1357 git init src &&
1359 cd src &&
1360 git pull ../testrepo main &&
1361 git tag -m "annotated" tag &&
1362 git for-each-ref >tmp1 &&
1363 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1364 sort -k 3 >../expect
1365 ) &&
1366 test_when_finished "rm -rf dst" &&
1367 git init dst &&
1369 cd dst &&
1370 git remote add origin ../src &&
1371 git config branch.main.remote origin &&
1372 git config branch.main.merge refs/heads/main &&
1373 git pull &&
1374 git for-each-ref >../actual
1375 ) &&
1376 test_cmp expect actual
1379 test_expect_success 'peeled advertisements are not considered ref tips' '
1380 mk_empty testrepo &&
1381 git -C testrepo commit --allow-empty -m one &&
1382 git -C testrepo commit --allow-empty -m two &&
1383 git -C testrepo tag -m foo mytag HEAD^ &&
1384 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1385 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1386 git fetch testrepo $oid 2>err &&
1387 test_i18ngrep "Server does not allow request for unadvertised object" err
1390 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1391 mk_test testrepo heads/main &&
1392 test_when_finished "rm -rf src" &&
1393 git init src &&
1394 test_when_finished "rm -rf dst" &&
1395 git init --bare dst &&
1397 cd src &&
1398 git pull ../testrepo main &&
1399 git branch next &&
1400 git config remote.dst.url ../dst &&
1401 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1402 git push dst main &&
1403 git show-ref refs/heads/main |
1404 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1405 ) &&
1407 cd dst &&
1408 test_must_fail git show-ref refs/heads/next &&
1409 test_must_fail git show-ref refs/heads/main &&
1410 git show-ref refs/remotes/src/main >actual
1411 ) &&
1412 test_cmp dst/expect dst/actual
1415 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1416 mk_test testrepo heads/main &&
1417 test_when_finished "rm -rf src" &&
1418 git init src &&
1419 test_when_finished "rm -rf dst" &&
1420 git init --bare dst &&
1422 cd src &&
1423 git pull ../testrepo main &&
1424 git branch next &&
1425 git config remote.dst.url ../dst &&
1426 git config push.default matching &&
1427 git push dst main &&
1428 git show-ref refs/heads/main >../dst/expect
1429 ) &&
1431 cd dst &&
1432 test_must_fail git show-ref refs/heads/next &&
1433 git show-ref refs/heads/main >actual
1434 ) &&
1435 test_cmp dst/expect dst/actual
1438 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1439 mk_test testrepo heads/main &&
1440 test_when_finished "rm -rf src" &&
1441 git init src &&
1442 test_when_finished "rm -rf dst" &&
1443 git init --bare dst &&
1445 cd src &&
1446 git pull ../testrepo main &&
1447 git branch next &&
1448 git config remote.dst.url ../dst &&
1449 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1450 git config push.default upstream &&
1452 git config branch.main.merge refs/heads/trunk &&
1453 git config branch.main.remote dst &&
1455 git push dst main &&
1456 git show-ref refs/heads/main |
1457 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1458 ) &&
1460 cd dst &&
1461 test_must_fail git show-ref refs/heads/main &&
1462 test_must_fail git show-ref refs/heads/next &&
1463 git show-ref refs/heads/trunk >actual
1464 ) &&
1465 test_cmp dst/expect dst/actual
1468 test_expect_success 'push does not follow tags by default' '
1469 mk_test testrepo heads/main &&
1470 test_when_finished "rm -rf src" &&
1471 git init src &&
1472 test_when_finished "rm -rf dst" &&
1473 git init --bare dst &&
1475 cd src &&
1476 git pull ../testrepo main &&
1477 git tag -m "annotated" tag &&
1478 git checkout -b another &&
1479 git commit --allow-empty -m "future commit" &&
1480 git tag -m "future" future &&
1481 git checkout main &&
1482 git for-each-ref refs/heads/main >../expect &&
1483 git push ../dst main
1484 ) &&
1486 cd dst &&
1487 git for-each-ref >../actual
1488 ) &&
1489 test_cmp expect actual
1492 test_expect_success 'push --follow-tags only pushes relevant tags' '
1493 mk_test testrepo heads/main &&
1494 test_when_finished "rm -rf src" &&
1495 git init src &&
1496 test_when_finished "rm -rf dst" &&
1497 git init --bare dst &&
1499 cd src &&
1500 git pull ../testrepo main &&
1501 git tag -m "annotated" tag &&
1502 git checkout -b another &&
1503 git commit --allow-empty -m "future commit" &&
1504 git tag -m "future" future &&
1505 git checkout main &&
1506 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1507 git push --follow-tags ../dst main
1508 ) &&
1510 cd dst &&
1511 git for-each-ref >../actual
1512 ) &&
1513 test_cmp expect actual
1516 test_expect_success 'push --no-thin must produce non-thin pack' '
1517 cat >>path1 <<\EOF &&
1518 keep base version of path1 big enough, compared to the new changes
1519 later, in order to pass size heuristics in
1520 builtin/pack-objects.c:try_delta()
1522 git commit -am initial &&
1523 git init no-thin &&
1524 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1525 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1526 echo modified >> path1 &&
1527 git commit -am modified &&
1528 git repack -adf &&
1529 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1530 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1533 test_expect_success 'pushing a tag pushes the tagged object' '
1534 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1535 git tag -m foo tag-of-blob $blob &&
1536 test_when_finished "rm -rf dst.git" &&
1537 git init --bare dst.git &&
1538 git push dst.git tag-of-blob &&
1539 # the receiving index-pack should have noticed
1540 # any problems, but we double check
1541 echo unreferenced >expect &&
1542 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1543 test_cmp expect actual
1546 test_expect_success 'push into bare respects core.logallrefupdates' '
1547 test_when_finished "rm -rf dst.git" &&
1548 git init --bare dst.git &&
1549 git -C dst.git config core.logallrefupdates true &&
1551 # double push to test both with and without
1552 # the actual pack transfer
1553 git push dst.git main:one &&
1554 echo "one@{0} push" >expect &&
1555 git -C dst.git log -g --format="%gd %gs" one >actual &&
1556 test_cmp expect actual &&
1558 git push dst.git main:two &&
1559 echo "two@{0} push" >expect &&
1560 git -C dst.git log -g --format="%gd %gs" two >actual &&
1561 test_cmp expect actual
1564 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1565 test_when_finished "rm -rf dst.git" &&
1566 git init --bare dst.git &&
1568 cd dst.git &&
1569 git config core.logallrefupdates true &&
1571 # as above, we double-fetch to test both
1572 # with and without pack transfer
1573 git fetch .. main:one &&
1574 echo "one@{0} fetch .. main:one: storing head" >expect &&
1575 git log -g --format="%gd %gs" one >actual &&
1576 test_cmp expect actual &&
1578 git fetch .. main:two &&
1579 echo "two@{0} fetch .. main:two: storing head" >expect &&
1580 git log -g --format="%gd %gs" two >actual &&
1581 test_cmp expect actual
1585 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1586 mk_empty testrepo &&
1587 git push testrepo main &&
1589 cd testrepo &&
1590 git reset --hard &&
1591 git config receive.denyCurrentBranch updateInstead
1592 ) &&
1593 test_commit third path2 &&
1595 # Try pushing into a repository with pristine working tree
1596 git push testrepo main &&
1598 cd testrepo &&
1599 git update-index -q --refresh &&
1600 git diff-files --quiet -- &&
1601 git diff-index --quiet --cached HEAD -- &&
1602 test third = "$(cat path2)" &&
1603 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1604 ) &&
1606 # Try pushing into a repository with working tree needing a refresh
1608 cd testrepo &&
1609 git reset --hard HEAD^ &&
1610 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1611 test-tool chmtime +100 path1
1612 ) &&
1613 git push testrepo main &&
1615 cd testrepo &&
1616 git update-index -q --refresh &&
1617 git diff-files --quiet -- &&
1618 git diff-index --quiet --cached HEAD -- &&
1619 test_cmp ../path1 path1 &&
1620 test third = "$(cat path2)" &&
1621 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1622 ) &&
1624 # Update what is to be pushed
1625 test_commit fourth path2 &&
1627 # Try pushing into a repository with a dirty working tree
1628 # (1) the working tree updated
1630 cd testrepo &&
1631 echo changed >path1
1632 ) &&
1633 test_must_fail git push testrepo main &&
1635 cd testrepo &&
1636 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1637 git diff --quiet --cached &&
1638 test changed = "$(cat path1)"
1639 ) &&
1641 # (2) the index updated
1643 cd testrepo &&
1644 echo changed >path1 &&
1645 git add path1
1646 ) &&
1647 test_must_fail git push testrepo main &&
1649 cd testrepo &&
1650 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1651 git diff --quiet &&
1652 test changed = "$(cat path1)"
1653 ) &&
1655 # Introduce a new file in the update
1656 test_commit fifth path3 &&
1658 # (3) the working tree has an untracked file that would interfere
1660 cd testrepo &&
1661 git reset --hard &&
1662 echo changed >path3
1663 ) &&
1664 test_must_fail git push testrepo main &&
1666 cd testrepo &&
1667 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1668 git diff --quiet &&
1669 git diff --quiet --cached &&
1670 test changed = "$(cat path3)"
1671 ) &&
1673 # (4) the target changes to what gets pushed but it still is a change
1675 cd testrepo &&
1676 git reset --hard &&
1677 echo fifth >path3 &&
1678 git add path3
1679 ) &&
1680 test_must_fail git push testrepo main &&
1682 cd testrepo &&
1683 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1684 git diff --quiet &&
1685 test fifth = "$(cat path3)"
1686 ) &&
1688 # (5) push into void
1689 test_when_finished "rm -rf void" &&
1690 git init void &&
1692 cd void &&
1693 git config receive.denyCurrentBranch updateInstead
1694 ) &&
1695 git push void main &&
1697 cd void &&
1698 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1699 git diff --quiet &&
1700 git diff --cached --quiet
1701 ) &&
1703 # (6) updateInstead intervened by fast-forward check
1704 test_must_fail git push void main^:main &&
1705 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1706 git -C void diff --quiet &&
1707 git -C void diff --cached --quiet
1710 test_expect_success 'updateInstead with push-to-checkout hook' '
1711 test_when_finished "rm -rf testrepo" &&
1712 git init testrepo &&
1713 git -C testrepo pull .. main &&
1714 git -C testrepo reset --hard HEAD^^ &&
1715 git -C testrepo tag initial &&
1716 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1717 test_hook -C testrepo push-to-checkout <<-\EOF &&
1718 echo >&2 updating from $(git rev-parse HEAD)
1719 echo >&2 updating to "$1"
1721 git update-index -q --refresh &&
1722 git read-tree -u -m HEAD "$1" || {
1723 status=$?
1724 echo >&2 read-tree failed
1725 exit $status
1729 # Try pushing into a pristine
1730 git push testrepo main &&
1732 cd testrepo &&
1733 git diff --quiet &&
1734 git diff HEAD --quiet &&
1735 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1736 ) &&
1738 # Try pushing into a repository with conflicting change
1740 cd testrepo &&
1741 git reset --hard initial &&
1742 echo conflicting >path2
1743 ) &&
1744 test_must_fail git push testrepo main &&
1746 cd testrepo &&
1747 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1748 test conflicting = "$(cat path2)" &&
1749 git diff-index --quiet --cached HEAD
1750 ) &&
1752 # Try pushing into a repository with unrelated change
1754 cd testrepo &&
1755 git reset --hard initial &&
1756 echo unrelated >path1 &&
1757 echo irrelevant >path5 &&
1758 git add path5
1759 ) &&
1760 git push testrepo main &&
1762 cd testrepo &&
1763 test "$(cat path1)" = unrelated &&
1764 test "$(cat path5)" = irrelevant &&
1765 test "$(git diff --name-only --cached HEAD)" = path5 &&
1766 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1767 ) &&
1769 # push into void
1770 test_when_finished "rm -rf void" &&
1771 git init void &&
1772 git -C void config receive.denyCurrentBranch updateInstead &&
1773 test_hook -C void push-to-checkout <<-\EOF &&
1774 if git rev-parse --quiet --verify HEAD
1775 then
1776 has_head=yes
1777 echo >&2 updating from $(git rev-parse HEAD)
1778 else
1779 has_head=no
1780 echo >&2 pushing into void
1782 echo >&2 updating to "$1"
1784 git update-index -q --refresh &&
1785 case "$has_head" in
1786 yes)
1787 git read-tree -u -m HEAD "$1" ;;
1789 git read-tree -u -m "$1" ;;
1790 esac || {
1791 status=$?
1792 echo >&2 read-tree failed
1793 exit $status
1797 git push void main &&
1799 cd void &&
1800 git diff --quiet &&
1801 git diff --cached --quiet &&
1802 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1806 test_expect_success 'denyCurrentBranch and worktrees' '
1807 git worktree add new-wt &&
1808 git clone . cloned &&
1809 test_commit -C cloned first &&
1810 test_config receive.denyCurrentBranch refuse &&
1811 test_must_fail git -C cloned push origin HEAD:new-wt &&
1812 test_config receive.denyCurrentBranch updateInstead &&
1813 git -C cloned push origin HEAD:new-wt &&
1814 test_path_exists new-wt/first.t &&
1815 test_must_fail git -C cloned push --delete origin new-wt
1818 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1819 test_when_finished "rm -fr bare.git" &&
1820 git clone --bare . bare.git &&
1821 git -C bare.git worktree add wt &&
1822 test_commit grape &&
1823 git -C bare.git config receive.denyCurrentBranch refuse &&
1824 test_must_fail git push bare.git HEAD:wt &&
1825 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1826 git push bare.git HEAD:wt &&
1827 test_path_exists bare.git/wt/grape.t &&
1828 test_must_fail git push --delete bare.git wt
1831 test_expect_success 'refuse fetch to current branch of worktree' '
1832 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1833 git worktree add wt &&
1834 test_commit apple &&
1835 test_must_fail git fetch . HEAD:wt &&
1836 git fetch -u . HEAD:wt
1839 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1840 test_when_finished "rm -fr bare.git" &&
1841 git clone --bare . bare.git &&
1842 git -C bare.git worktree add wt &&
1843 test_commit banana &&
1844 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1845 git -C bare.git fetch -u .. HEAD:wt
1848 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1849 mk_empty testrepo &&
1850 git -C testrepo config receive.hiderefs refs/hidden &&
1851 git -C testrepo config receive.unpackLimit 1 &&
1852 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1853 test_dir_is_empty testrepo/.git/objects/pack
1856 test_expect_success 'push with config push.useBitmaps' '
1857 mk_test testrepo heads/main &&
1858 git checkout main &&
1859 test_unconfig push.useBitmaps &&
1860 GIT_TRACE2_EVENT="$PWD/default" \
1861 git push --quiet testrepo main:test &&
1862 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1863 --thin --delta-base-offset -q <default &&
1865 test_config push.useBitmaps true &&
1866 GIT_TRACE2_EVENT="$PWD/true" \
1867 git push --quiet testrepo main:test2 &&
1868 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1869 --thin --delta-base-offset -q <true &&
1871 test_config push.useBitmaps false &&
1872 GIT_TRACE2_EVENT="$PWD/false" \
1873 git push --quiet testrepo main:test3 &&
1874 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1875 --thin --delta-base-offset -q --no-use-bitmap-index <false
1878 test_done