Merge branch 'fc/advice-diverged-history'
[git/debian.git] / t / t5516-fetch-push.sh
blob19ebefa5aceb1afd3916f903971013e985836329
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 onelevel ref' '
405 mk_test testrepo heads/main &&
406 test_must_fail git push testrepo HEAD:refs/onelevel
409 test_expect_success 'push with colon-less refspec (1)' '
411 mk_test testrepo heads/frotz tags/frotz &&
412 git branch -f frotz main &&
413 git push testrepo frotz &&
414 check_push_result testrepo $the_commit heads/frotz &&
415 check_push_result testrepo $the_first_commit tags/frotz
419 test_expect_success 'push with colon-less refspec (2)' '
421 mk_test testrepo heads/frotz tags/frotz &&
422 if git show-ref --verify -q refs/heads/frotz
423 then
424 git branch -D frotz
425 fi &&
426 git tag -f frotz &&
427 git push -f testrepo frotz &&
428 check_push_result testrepo $the_commit tags/frotz &&
429 check_push_result testrepo $the_first_commit heads/frotz
433 test_expect_success 'push with colon-less refspec (3)' '
435 mk_test testrepo &&
436 if git show-ref --verify -q refs/tags/frotz
437 then
438 git tag -d frotz
439 fi &&
440 git branch -f frotz main &&
441 git push testrepo frotz &&
442 check_push_result testrepo $the_commit heads/frotz &&
443 test 1 = $( cd testrepo && git show-ref | wc -l )
446 test_expect_success 'push with colon-less refspec (4)' '
448 mk_test testrepo &&
449 if git show-ref --verify -q refs/heads/frotz
450 then
451 git branch -D frotz
452 fi &&
453 git tag -f frotz &&
454 git push testrepo frotz &&
455 check_push_result testrepo $the_commit tags/frotz &&
456 test 1 = $( cd testrepo && git show-ref | wc -l )
460 test_expect_success 'push head with non-existent, incomplete dest' '
462 mk_test testrepo &&
463 git push testrepo main:branch &&
464 check_push_result testrepo $the_commit heads/branch
468 test_expect_success 'push tag with non-existent, incomplete dest' '
470 mk_test testrepo &&
471 git tag -f v1.0 &&
472 git push testrepo v1.0:tag &&
473 check_push_result testrepo $the_commit tags/tag
477 test_expect_success 'push sha1 with non-existent, incomplete dest' '
479 mk_test testrepo &&
480 test_must_fail git push testrepo $(git rev-parse main):foo
484 test_expect_success 'push ref expression with non-existent, incomplete dest' '
486 mk_test testrepo &&
487 test_must_fail git push testrepo main^:branch
491 for head in HEAD @
494 test_expect_success "push with $head" '
495 mk_test testrepo heads/main &&
496 git checkout main &&
497 git push testrepo $head &&
498 check_push_result testrepo $the_commit heads/main
501 test_expect_success "push with $head nonexisting at remote" '
502 mk_test testrepo heads/main &&
503 git checkout -b local main &&
504 test_when_finished "git checkout main; git branch -D local" &&
505 git push testrepo $head &&
506 check_push_result testrepo $the_commit heads/local
509 test_expect_success "push with +$head" '
510 mk_test testrepo heads/main &&
511 git checkout -b local main &&
512 test_when_finished "git checkout main; git branch -D local" &&
513 git push testrepo main local &&
514 check_push_result testrepo $the_commit heads/main &&
515 check_push_result testrepo $the_commit heads/local &&
517 # Without force rewinding should fail
518 git reset --hard $head^ &&
519 test_must_fail git push testrepo $head &&
520 check_push_result testrepo $the_commit heads/local &&
522 # With force rewinding should succeed
523 git push testrepo +$head &&
524 check_push_result testrepo $the_first_commit heads/local
527 test_expect_success "push $head with non-existent, incomplete dest" '
528 mk_test testrepo &&
529 git checkout main &&
530 git push testrepo $head:branch &&
531 check_push_result testrepo $the_commit heads/branch
535 test_expect_success "push with config remote.*.push = $head" '
536 mk_test testrepo heads/local &&
537 git checkout main &&
538 git branch -f local $the_commit &&
539 test_when_finished "git branch -D local" &&
541 cd testrepo &&
542 git checkout local &&
543 git reset --hard $the_first_commit
544 ) &&
545 test_config remote.there.url testrepo &&
546 test_config remote.there.push $head &&
547 test_config branch.main.remote there &&
548 git push &&
549 check_push_result testrepo $the_commit heads/main &&
550 check_push_result testrepo $the_first_commit heads/local
553 done
555 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
556 mk_test testrepo heads/main &&
557 git checkout $the_first_commit &&
558 test_config remote.there.url testrepo &&
559 test_config remote.there.push refs/heads/main:refs/heads/main &&
560 git push there &&
561 check_push_result testrepo $the_commit heads/main
564 test_expect_success 'push with remote.pushdefault' '
565 mk_test up_repo heads/main &&
566 mk_test down_repo heads/main &&
567 test_config remote.up.url up_repo &&
568 test_config remote.down.url down_repo &&
569 test_config branch.main.remote up &&
570 test_config remote.pushdefault down &&
571 test_config push.default matching &&
572 git push &&
573 check_push_result up_repo $the_first_commit heads/main &&
574 check_push_result down_repo $the_commit heads/main
577 test_expect_success 'push with config remote.*.pushurl' '
579 mk_test testrepo heads/main &&
580 git checkout main &&
581 test_config remote.there.url test2repo &&
582 test_config remote.there.pushurl testrepo &&
583 git push there : &&
584 check_push_result testrepo $the_commit heads/main
587 test_expect_success 'push with config branch.*.pushremote' '
588 mk_test up_repo heads/main &&
589 mk_test side_repo heads/main &&
590 mk_test down_repo heads/main &&
591 test_config remote.up.url up_repo &&
592 test_config remote.pushdefault side_repo &&
593 test_config remote.down.url down_repo &&
594 test_config branch.main.remote up &&
595 test_config branch.main.pushremote down &&
596 test_config push.default matching &&
597 git push &&
598 check_push_result up_repo $the_first_commit heads/main &&
599 check_push_result side_repo $the_first_commit heads/main &&
600 check_push_result down_repo $the_commit heads/main
603 test_expect_success 'branch.*.pushremote config order is irrelevant' '
604 mk_test one_repo heads/main &&
605 mk_test two_repo heads/main &&
606 test_config remote.one.url one_repo &&
607 test_config remote.two.url two_repo &&
608 test_config branch.main.pushremote two_repo &&
609 test_config remote.pushdefault one_repo &&
610 test_config push.default matching &&
611 git push &&
612 check_push_result one_repo $the_first_commit heads/main &&
613 check_push_result two_repo $the_commit heads/main
616 test_expect_success 'push rejects empty branch name entries' '
617 mk_test one_repo heads/main &&
618 test_config remote.one.url one_repo &&
619 test_config branch..remote one &&
620 test_config branch..merge refs/heads/ &&
621 test_config branch.main.remote one &&
622 test_config branch.main.merge refs/heads/main &&
623 test_must_fail git push 2>err &&
624 grep "bad config variable .branch\.\." err
627 test_expect_success 'push ignores "branch." config without subsection' '
628 mk_test one_repo heads/main &&
629 test_config remote.one.url one_repo &&
630 test_config branch.autoSetupMerge true &&
631 test_config branch.main.remote one &&
632 test_config branch.main.merge refs/heads/main &&
633 git push
636 test_expect_success 'push with dry-run' '
638 mk_test testrepo heads/main &&
639 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
640 git push --dry-run testrepo : &&
641 check_push_result testrepo $old_commit heads/main
644 test_expect_success 'push updates local refs' '
646 mk_test testrepo heads/main &&
647 mk_child testrepo child &&
649 cd child &&
650 git pull .. main &&
651 git push &&
652 test $(git rev-parse main) = \
653 $(git rev-parse remotes/origin/main)
658 test_expect_success 'push updates up-to-date local refs' '
660 mk_test testrepo heads/main &&
661 mk_child testrepo child1 &&
662 mk_child testrepo child2 &&
663 (cd child1 && git pull .. main && git push) &&
665 cd child2 &&
666 git pull ../child1 main &&
667 git push &&
668 test $(git rev-parse main) = \
669 $(git rev-parse remotes/origin/main)
674 test_expect_success 'push preserves up-to-date packed refs' '
676 mk_test testrepo heads/main &&
677 mk_child testrepo child &&
679 cd child &&
680 git push &&
681 ! test -f .git/refs/remotes/origin/main
686 test_expect_success 'push does not update local refs on failure' '
688 mk_test testrepo heads/main &&
689 mk_child testrepo child &&
690 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
691 chmod +x testrepo/.git/hooks/pre-receive &&
693 cd child &&
694 git pull .. main &&
695 test_must_fail git push &&
696 test $(git rev-parse main) != \
697 $(git rev-parse remotes/origin/main)
702 test_expect_success 'allow deleting an invalid remote ref' '
704 mk_test testrepo heads/branch &&
705 rm -f testrepo/.git/objects/??/* &&
706 git push testrepo :refs/heads/branch &&
707 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
711 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
712 mk_test_with_hooks testrepo heads/main heads/next &&
713 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
714 newmain=$(git show-ref -s --verify refs/heads/main) &&
715 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
716 newnext=$ZERO_OID &&
717 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
719 cd testrepo/.git &&
720 cat >pre-receive.expect <<-EOF &&
721 $orgmain $newmain refs/heads/main
722 $orgnext $newnext refs/heads/next
725 cat >update.expect <<-EOF &&
726 refs/heads/main $orgmain $newmain
727 refs/heads/next $orgnext $newnext
730 cat >post-receive.expect <<-EOF &&
731 $orgmain $newmain refs/heads/main
732 $orgnext $newnext refs/heads/next
735 cat >post-update.expect <<-EOF &&
736 refs/heads/main
737 refs/heads/next
740 test_cmp pre-receive.expect pre-receive.actual &&
741 test_cmp update.expect update.actual &&
742 test_cmp post-receive.expect post-receive.actual &&
743 test_cmp post-update.expect post-update.actual
747 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
748 mk_test_with_hooks testrepo heads/branch &&
749 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
750 rm -f testrepo/.git/objects/??/* &&
751 git push testrepo :refs/heads/branch &&
753 cd testrepo/.git &&
754 cat >pre-receive.expect <<-EOF &&
755 $orig $ZERO_OID refs/heads/branch
758 cat >update.expect <<-EOF &&
759 refs/heads/branch $orig $ZERO_OID
762 cat >post-receive.expect <<-EOF &&
763 $orig $ZERO_OID refs/heads/branch
766 cat >post-update.expect <<-EOF &&
767 refs/heads/branch
770 test_cmp pre-receive.expect pre-receive.actual &&
771 test_cmp update.expect update.actual &&
772 test_cmp post-receive.expect post-receive.actual &&
773 test_cmp post-update.expect post-update.actual
777 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
778 mk_test_with_hooks testrepo heads/main &&
779 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
780 newmain=$(git show-ref -s --verify refs/heads/main) &&
781 git push testrepo main :refs/heads/nonexistent &&
783 cd testrepo/.git &&
784 cat >pre-receive.expect <<-EOF &&
785 $orgmain $newmain refs/heads/main
786 $ZERO_OID $ZERO_OID refs/heads/nonexistent
789 cat >update.expect <<-EOF &&
790 refs/heads/main $orgmain $newmain
791 refs/heads/nonexistent $ZERO_OID $ZERO_OID
794 cat >post-receive.expect <<-EOF &&
795 $orgmain $newmain refs/heads/main
798 cat >post-update.expect <<-EOF &&
799 refs/heads/main
802 test_cmp pre-receive.expect pre-receive.actual &&
803 test_cmp update.expect update.actual &&
804 test_cmp post-receive.expect post-receive.actual &&
805 test_cmp post-update.expect post-update.actual
809 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
810 mk_test_with_hooks testrepo heads/main &&
811 git push testrepo :refs/heads/nonexistent &&
813 cd testrepo/.git &&
814 cat >pre-receive.expect <<-EOF &&
815 $ZERO_OID $ZERO_OID refs/heads/nonexistent
818 cat >update.expect <<-EOF &&
819 refs/heads/nonexistent $ZERO_OID $ZERO_OID
822 test_cmp pre-receive.expect pre-receive.actual &&
823 test_cmp update.expect update.actual &&
824 test_path_is_missing post-receive.actual &&
825 test_path_is_missing post-update.actual
829 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
830 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
831 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
832 newmain=$(git show-ref -s --verify refs/heads/main) &&
833 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
834 newnext=$ZERO_OID &&
835 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
836 newseen=$(git show-ref -s --verify refs/heads/main) &&
837 git push testrepo refs/heads/main:refs/heads/main \
838 refs/heads/main:refs/heads/seen :refs/heads/next \
839 :refs/heads/nonexistent &&
841 cd testrepo/.git &&
842 cat >pre-receive.expect <<-EOF &&
843 $orgmain $newmain refs/heads/main
844 $orgnext $newnext refs/heads/next
845 $orgseen $newseen refs/heads/seen
846 $ZERO_OID $ZERO_OID refs/heads/nonexistent
849 cat >update.expect <<-EOF &&
850 refs/heads/main $orgmain $newmain
851 refs/heads/next $orgnext $newnext
852 refs/heads/seen $orgseen $newseen
853 refs/heads/nonexistent $ZERO_OID $ZERO_OID
856 cat >post-receive.expect <<-EOF &&
857 $orgmain $newmain refs/heads/main
858 $orgnext $newnext refs/heads/next
859 $orgseen $newseen refs/heads/seen
862 cat >post-update.expect <<-EOF &&
863 refs/heads/main
864 refs/heads/next
865 refs/heads/seen
868 test_cmp pre-receive.expect pre-receive.actual &&
869 test_cmp update.expect update.actual &&
870 test_cmp post-receive.expect post-receive.actual &&
871 test_cmp post-update.expect post-update.actual
875 test_expect_success 'allow deleting a ref using --delete' '
876 mk_test testrepo heads/main &&
877 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
878 git push testrepo --delete main &&
879 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
882 test_expect_success 'allow deleting a tag using --delete' '
883 mk_test testrepo heads/main &&
884 git tag -a -m dummy_message deltag heads/main &&
885 git push testrepo --tags &&
886 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
887 git push testrepo --delete tag deltag &&
888 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
891 test_expect_success 'push --delete without args aborts' '
892 mk_test testrepo heads/main &&
893 test_must_fail git push testrepo --delete
896 test_expect_success 'push --delete refuses src:dest refspecs' '
897 mk_test testrepo heads/main &&
898 test_must_fail git push testrepo --delete main:foo
901 test_expect_success 'push --delete refuses empty string' '
902 mk_test testrepo heads/master &&
903 test_must_fail git push testrepo --delete ""
906 test_expect_success 'push --delete onelevel refspecs' '
907 mk_test testrepo heads/main &&
908 git -C testrepo update-ref refs/onelevel refs/heads/main &&
909 git push testrepo --delete refs/onelevel &&
910 test_must_fail git -C testrepo rev-parse --verify refs/onelevel
913 test_expect_success 'warn on push to HEAD of non-bare repository' '
914 mk_test testrepo heads/main &&
916 cd testrepo &&
917 git checkout main &&
918 git config receive.denyCurrentBranch warn
919 ) &&
920 git push testrepo main 2>stderr &&
921 grep "warning: updating the current branch" stderr
924 test_expect_success 'deny push to HEAD of non-bare repository' '
925 mk_test testrepo heads/main &&
927 cd testrepo &&
928 git checkout main &&
929 git config receive.denyCurrentBranch true
930 ) &&
931 test_must_fail git push testrepo main
934 test_expect_success 'allow push to HEAD of bare repository (bare)' '
935 mk_test testrepo heads/main &&
937 cd testrepo &&
938 git checkout main &&
939 git config receive.denyCurrentBranch true &&
940 git config core.bare true
941 ) &&
942 git push testrepo main 2>stderr &&
943 ! grep "warning: updating the current branch" stderr
946 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
947 mk_test testrepo heads/main &&
949 cd testrepo &&
950 git checkout main &&
951 git config receive.denyCurrentBranch false
952 ) &&
953 git push testrepo main 2>stderr &&
954 ! grep "warning: updating the current branch" stderr
957 test_expect_success 'fetch with branches' '
958 mk_empty testrepo &&
959 git branch second $the_first_commit &&
960 git checkout second &&
961 mkdir testrepo/.git/branches &&
962 echo ".." > testrepo/.git/branches/branch1 &&
964 cd testrepo &&
965 git fetch branch1 &&
966 echo "$the_commit commit refs/heads/branch1" >expect &&
967 git for-each-ref refs/heads >actual &&
968 test_cmp expect actual
969 ) &&
970 git checkout main
973 test_expect_success 'fetch with branches containing #' '
974 mk_empty testrepo &&
975 mkdir testrepo/.git/branches &&
976 echo "..#second" > testrepo/.git/branches/branch2 &&
978 cd testrepo &&
979 git fetch branch2 &&
980 echo "$the_first_commit commit refs/heads/branch2" >expect &&
981 git for-each-ref refs/heads >actual &&
982 test_cmp expect actual
983 ) &&
984 git checkout main
987 test_expect_success 'push with branches' '
988 mk_empty testrepo &&
989 git checkout second &&
991 test_when_finished "rm -rf .git/branches" &&
992 mkdir .git/branches &&
993 echo "testrepo" > .git/branches/branch1 &&
995 git push branch1 &&
997 cd testrepo &&
998 echo "$the_first_commit commit refs/heads/main" >expect &&
999 git for-each-ref refs/heads >actual &&
1000 test_cmp expect actual
1004 test_expect_success 'push with branches containing #' '
1005 mk_empty testrepo &&
1007 test_when_finished "rm -rf .git/branches" &&
1008 mkdir .git/branches &&
1009 echo "testrepo#branch3" > .git/branches/branch2 &&
1011 git push branch2 &&
1013 cd testrepo &&
1014 echo "$the_first_commit commit refs/heads/branch3" >expect &&
1015 git for-each-ref refs/heads >actual &&
1016 test_cmp expect actual
1017 ) &&
1018 git checkout main
1021 test_expect_success 'push into aliased refs (consistent)' '
1022 mk_test testrepo heads/main &&
1023 mk_child testrepo child1 &&
1024 mk_child testrepo child2 &&
1026 cd child1 &&
1027 git branch foo &&
1028 git symbolic-ref refs/heads/bar refs/heads/foo &&
1029 git config receive.denyCurrentBranch false
1030 ) &&
1032 cd child2 &&
1033 >path2 &&
1034 git add path2 &&
1035 test_tick &&
1036 git commit -a -m child2 &&
1037 git branch foo &&
1038 git branch bar &&
1039 git push ../child1 foo bar
1043 test_expect_success 'push into aliased refs (inconsistent)' '
1044 mk_test testrepo heads/main &&
1045 mk_child testrepo child1 &&
1046 mk_child testrepo child2 &&
1048 cd child1 &&
1049 git branch foo &&
1050 git symbolic-ref refs/heads/bar refs/heads/foo &&
1051 git config receive.denyCurrentBranch false
1052 ) &&
1054 cd child2 &&
1055 >path2 &&
1056 git add path2 &&
1057 test_tick &&
1058 git commit -a -m child2 &&
1059 git branch foo &&
1060 >path3 &&
1061 git add path3 &&
1062 test_tick &&
1063 git commit -a -m child2 &&
1064 git branch bar &&
1065 test_must_fail git push ../child1 foo bar 2>stderr &&
1066 grep "refusing inconsistent update" stderr
1070 test_force_push_tag () {
1071 tag_type_description=$1
1072 tag_args=$2
1074 test_expect_success "force pushing required to update $tag_type_description" "
1075 mk_test testrepo heads/main &&
1076 mk_child testrepo child1 &&
1077 mk_child testrepo child2 &&
1079 cd child1 &&
1080 git tag testTag &&
1081 git push ../child2 testTag &&
1082 >file1 &&
1083 git add file1 &&
1084 git commit -m 'file1' &&
1085 git tag $tag_args testTag &&
1086 test_must_fail git push ../child2 testTag &&
1087 git push --force ../child2 testTag &&
1088 git tag $tag_args testTag HEAD~ &&
1089 test_must_fail git push ../child2 testTag &&
1090 git push --force ../child2 testTag &&
1092 # Clobbering without + in refspec needs --force
1093 git tag -f testTag &&
1094 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1095 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1097 # Clobbering with + in refspec does not need --force
1098 git tag -f testTag HEAD~ &&
1099 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1101 # Clobbering with --no-force still obeys + in refspec
1102 git tag -f testTag &&
1103 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1105 # Clobbering with/without --force and 'tag <name>' format
1106 git tag -f testTag HEAD~ &&
1107 test_must_fail git push ../child2 tag testTag &&
1108 git push --force ../child2 tag testTag
1113 test_force_push_tag "lightweight tag" "-f"
1114 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1116 test_force_fetch_tag () {
1117 tag_type_description=$1
1118 tag_args=$2
1120 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1121 mk_test testrepo heads/main &&
1122 mk_child testrepo child1 &&
1123 mk_child testrepo child2 &&
1125 cd testrepo &&
1126 git tag testTag &&
1127 git -C ../child1 fetch origin tag testTag &&
1128 >file1 &&
1129 git add file1 &&
1130 git commit -m 'file1' &&
1131 git tag $tag_args testTag &&
1132 test_must_fail git -C ../child1 fetch origin tag testTag &&
1133 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1138 test_force_fetch_tag "lightweight tag" "-f"
1139 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1141 test_expect_success 'push --porcelain' '
1142 mk_empty testrepo &&
1143 echo >.git/foo "To testrepo" &&
1144 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1145 echo >>.git/foo "Done" &&
1146 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1148 cd testrepo &&
1149 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1150 git for-each-ref refs/remotes/origin >actual &&
1151 test_cmp expect actual
1152 ) &&
1153 test_cmp .git/foo .git/bar
1156 test_expect_success 'push --porcelain bad url' '
1157 mk_empty testrepo &&
1158 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1159 ! grep -q Done .git/bar
1162 test_expect_success 'push --porcelain rejected' '
1163 mk_empty testrepo &&
1164 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1165 (cd testrepo &&
1166 git reset --hard origin/main^ &&
1167 git config receive.denyCurrentBranch true) &&
1169 echo >.git/foo "To testrepo" &&
1170 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1171 echo >>.git/foo "Done" &&
1173 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1174 test_cmp .git/foo .git/bar
1177 test_expect_success 'push --porcelain --dry-run rejected' '
1178 mk_empty testrepo &&
1179 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1180 (cd testrepo &&
1181 git reset --hard origin/main &&
1182 git config receive.denyCurrentBranch true) &&
1184 echo >.git/foo "To testrepo" &&
1185 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1186 echo >>.git/foo "Done" &&
1188 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1189 test_cmp .git/foo .git/bar
1192 test_expect_success 'push --prune' '
1193 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1194 git push --prune testrepo : &&
1195 check_push_result testrepo $the_commit heads/main &&
1196 check_push_result testrepo $the_first_commit heads/second &&
1197 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1200 test_expect_success 'push --prune refspec' '
1201 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1202 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1203 check_push_result testrepo $the_commit tmp/main &&
1204 check_push_result testrepo $the_first_commit tmp/second &&
1205 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1208 for configsection in transfer receive
1210 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1211 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1213 cd testrepo &&
1214 git config $configsection.hiderefs refs/hidden
1215 ) &&
1217 # push to unhidden ref succeeds normally
1218 git push testrepo main:refs/heads/main &&
1219 check_push_result testrepo $the_commit heads/main &&
1221 # push to update a hidden ref should fail
1222 test_must_fail git push testrepo main:refs/hidden/one &&
1223 check_push_result testrepo $the_first_commit hidden/one &&
1225 # push to delete a hidden ref should fail
1226 test_must_fail git push testrepo :refs/hidden/two &&
1227 check_push_result testrepo $the_first_commit hidden/two &&
1229 # idempotent push to update a hidden ref should fail
1230 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1231 check_push_result testrepo $the_first_commit hidden/three
1233 done
1235 test_expect_success 'fetch exact SHA1' '
1236 mk_test testrepo heads/main hidden/one &&
1237 git push testrepo main:refs/hidden/one &&
1239 cd testrepo &&
1240 git config transfer.hiderefs refs/hidden
1241 ) &&
1242 check_push_result testrepo $the_commit hidden/one &&
1244 mk_child testrepo child &&
1246 cd child &&
1248 # make sure $the_commit does not exist here
1249 git repack -a -d &&
1250 git prune &&
1251 test_must_fail git cat-file -t $the_commit &&
1253 # Some protocol versions (e.g. 2) support fetching
1254 # unadvertised objects, so restrict this test to v0.
1256 # fetching the hidden object should fail by default
1257 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1258 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1259 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1260 test_must_fail git rev-parse --verify refs/heads/copy &&
1262 # the server side can allow it to succeed
1264 cd ../testrepo &&
1265 git config uploadpack.allowtipsha1inwant true
1266 ) &&
1268 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1269 cat >expect <<-EOF &&
1270 $the_commit
1271 $the_first_commit
1274 git rev-parse --verify refs/heads/copy &&
1275 git rev-parse --verify refs/heads/extra
1276 } >actual &&
1277 test_cmp expect actual
1281 test_expect_success 'fetch exact SHA1 in protocol v2' '
1282 mk_test testrepo heads/main hidden/one &&
1283 git push testrepo main:refs/hidden/one &&
1284 git -C testrepo config transfer.hiderefs refs/hidden &&
1285 check_push_result testrepo $the_commit hidden/one &&
1287 mk_child testrepo child &&
1288 git -C child config protocol.version 2 &&
1290 # make sure $the_commit does not exist here
1291 git -C child repack -a -d &&
1292 git -C child prune &&
1293 test_must_fail git -C child cat-file -t $the_commit &&
1295 # fetching the hidden object succeeds by default
1296 # NEEDSWORK: should this match the v0 behavior instead?
1297 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1300 for configallowtipsha1inwant in true false
1302 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1303 mk_empty testrepo &&
1305 cd testrepo &&
1306 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1307 git commit --allow-empty -m foo &&
1308 git commit --allow-empty -m bar
1309 ) &&
1310 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1311 mk_empty shallow &&
1313 cd shallow &&
1314 # Some protocol versions (e.g. 2) support fetching
1315 # unadvertised objects, so restrict this test to v0.
1316 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1317 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1318 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1319 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1320 git cat-file commit $SHA1
1324 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1325 mk_empty testrepo &&
1327 cd testrepo &&
1328 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1329 git commit --allow-empty -m foo &&
1330 git commit --allow-empty -m bar &&
1331 git commit --allow-empty -m xyz
1332 ) &&
1333 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1334 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1335 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1337 cd testrepo &&
1338 git reset --hard $SHA1_2 &&
1339 git cat-file commit $SHA1_1 &&
1340 git cat-file commit $SHA1_3
1341 ) &&
1342 mk_empty shallow &&
1344 cd shallow &&
1345 # Some protocol versions (e.g. 2) support fetching
1346 # unadvertised objects, so restrict this test to v0.
1347 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1348 git fetch ../testrepo/.git $SHA1_3 &&
1349 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1350 git fetch ../testrepo/.git $SHA1_1 &&
1351 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1352 git fetch ../testrepo/.git $SHA1_1 &&
1353 git cat-file commit $SHA1_1 &&
1354 test_must_fail git cat-file commit $SHA1_2 &&
1355 git fetch ../testrepo/.git $SHA1_2 &&
1356 git cat-file commit $SHA1_2 &&
1357 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1358 git fetch ../testrepo/.git $SHA1_3 2>err &&
1359 # ideally we would insist this be on a "remote error:"
1360 # line, but it is racy; see the commit message
1361 test_i18ngrep "not our ref.*$SHA1_3\$" err
1364 done
1366 test_expect_success 'fetch follows tags by default' '
1367 mk_test testrepo heads/main &&
1368 test_when_finished "rm -rf src" &&
1369 git init src &&
1371 cd src &&
1372 git pull ../testrepo main &&
1373 git tag -m "annotated" tag &&
1374 git for-each-ref >tmp1 &&
1375 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1376 sort -k 3 >../expect
1377 ) &&
1378 test_when_finished "rm -rf dst" &&
1379 git init dst &&
1381 cd dst &&
1382 git remote add origin ../src &&
1383 git config branch.main.remote origin &&
1384 git config branch.main.merge refs/heads/main &&
1385 git pull &&
1386 git for-each-ref >../actual
1387 ) &&
1388 test_cmp expect actual
1391 test_expect_success 'peeled advertisements are not considered ref tips' '
1392 mk_empty testrepo &&
1393 git -C testrepo commit --allow-empty -m one &&
1394 git -C testrepo commit --allow-empty -m two &&
1395 git -C testrepo tag -m foo mytag HEAD^ &&
1396 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1397 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1398 git fetch testrepo $oid 2>err &&
1399 test_i18ngrep "Server does not allow request for unadvertised object" err
1402 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1403 mk_test testrepo heads/main &&
1404 test_when_finished "rm -rf src" &&
1405 git init src &&
1406 test_when_finished "rm -rf dst" &&
1407 git init --bare dst &&
1409 cd src &&
1410 git pull ../testrepo main &&
1411 git branch next &&
1412 git config remote.dst.url ../dst &&
1413 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1414 git push dst main &&
1415 git show-ref refs/heads/main |
1416 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1417 ) &&
1419 cd dst &&
1420 test_must_fail git show-ref refs/heads/next &&
1421 test_must_fail git show-ref refs/heads/main &&
1422 git show-ref refs/remotes/src/main >actual
1423 ) &&
1424 test_cmp dst/expect dst/actual
1427 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1428 mk_test testrepo heads/main &&
1429 test_when_finished "rm -rf src" &&
1430 git init src &&
1431 test_when_finished "rm -rf dst" &&
1432 git init --bare dst &&
1434 cd src &&
1435 git pull ../testrepo main &&
1436 git branch next &&
1437 git config remote.dst.url ../dst &&
1438 git config push.default matching &&
1439 git push dst main &&
1440 git show-ref refs/heads/main >../dst/expect
1441 ) &&
1443 cd dst &&
1444 test_must_fail git show-ref refs/heads/next &&
1445 git show-ref refs/heads/main >actual
1446 ) &&
1447 test_cmp dst/expect dst/actual
1450 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1451 mk_test testrepo heads/main &&
1452 test_when_finished "rm -rf src" &&
1453 git init src &&
1454 test_when_finished "rm -rf dst" &&
1455 git init --bare dst &&
1457 cd src &&
1458 git pull ../testrepo main &&
1459 git branch next &&
1460 git config remote.dst.url ../dst &&
1461 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1462 git config push.default upstream &&
1464 git config branch.main.merge refs/heads/trunk &&
1465 git config branch.main.remote dst &&
1467 git push dst main &&
1468 git show-ref refs/heads/main |
1469 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1470 ) &&
1472 cd dst &&
1473 test_must_fail git show-ref refs/heads/main &&
1474 test_must_fail git show-ref refs/heads/next &&
1475 git show-ref refs/heads/trunk >actual
1476 ) &&
1477 test_cmp dst/expect dst/actual
1480 test_expect_success 'push does not follow tags by default' '
1481 mk_test testrepo heads/main &&
1482 test_when_finished "rm -rf src" &&
1483 git init src &&
1484 test_when_finished "rm -rf dst" &&
1485 git init --bare dst &&
1487 cd src &&
1488 git pull ../testrepo main &&
1489 git tag -m "annotated" tag &&
1490 git checkout -b another &&
1491 git commit --allow-empty -m "future commit" &&
1492 git tag -m "future" future &&
1493 git checkout main &&
1494 git for-each-ref refs/heads/main >../expect &&
1495 git push ../dst main
1496 ) &&
1498 cd dst &&
1499 git for-each-ref >../actual
1500 ) &&
1501 test_cmp expect actual
1504 test_expect_success 'push --follow-tags only pushes relevant tags' '
1505 mk_test testrepo heads/main &&
1506 test_when_finished "rm -rf src" &&
1507 git init src &&
1508 test_when_finished "rm -rf dst" &&
1509 git init --bare dst &&
1511 cd src &&
1512 git pull ../testrepo main &&
1513 git tag -m "annotated" tag &&
1514 git checkout -b another &&
1515 git commit --allow-empty -m "future commit" &&
1516 git tag -m "future" future &&
1517 git checkout main &&
1518 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1519 git push --follow-tags ../dst main
1520 ) &&
1522 cd dst &&
1523 git for-each-ref >../actual
1524 ) &&
1525 test_cmp expect actual
1528 test_expect_success 'push --no-thin must produce non-thin pack' '
1529 cat >>path1 <<\EOF &&
1530 keep base version of path1 big enough, compared to the new changes
1531 later, in order to pass size heuristics in
1532 builtin/pack-objects.c:try_delta()
1534 git commit -am initial &&
1535 git init no-thin &&
1536 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1537 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1538 echo modified >> path1 &&
1539 git commit -am modified &&
1540 git repack -adf &&
1541 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1542 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1545 test_expect_success 'pushing a tag pushes the tagged object' '
1546 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1547 git tag -m foo tag-of-blob $blob &&
1548 test_when_finished "rm -rf dst.git" &&
1549 git init --bare dst.git &&
1550 git push dst.git tag-of-blob &&
1551 # the receiving index-pack should have noticed
1552 # any problems, but we double check
1553 echo unreferenced >expect &&
1554 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1555 test_cmp expect actual
1558 test_expect_success 'push into bare respects core.logallrefupdates' '
1559 test_when_finished "rm -rf dst.git" &&
1560 git init --bare dst.git &&
1561 git -C dst.git config core.logallrefupdates true &&
1563 # double push to test both with and without
1564 # the actual pack transfer
1565 git push dst.git main:one &&
1566 echo "one@{0} push" >expect &&
1567 git -C dst.git log -g --format="%gd %gs" one >actual &&
1568 test_cmp expect actual &&
1570 git push dst.git main:two &&
1571 echo "two@{0} push" >expect &&
1572 git -C dst.git log -g --format="%gd %gs" two >actual &&
1573 test_cmp expect actual
1576 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1577 test_when_finished "rm -rf dst.git" &&
1578 git init --bare dst.git &&
1580 cd dst.git &&
1581 git config core.logallrefupdates true &&
1583 # as above, we double-fetch to test both
1584 # with and without pack transfer
1585 git fetch .. main:one &&
1586 echo "one@{0} fetch .. main:one: storing head" >expect &&
1587 git log -g --format="%gd %gs" one >actual &&
1588 test_cmp expect actual &&
1590 git fetch .. main:two &&
1591 echo "two@{0} fetch .. main:two: storing head" >expect &&
1592 git log -g --format="%gd %gs" two >actual &&
1593 test_cmp expect actual
1597 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1598 mk_empty testrepo &&
1599 git push testrepo main &&
1601 cd testrepo &&
1602 git reset --hard &&
1603 git config receive.denyCurrentBranch updateInstead
1604 ) &&
1605 test_commit third path2 &&
1607 # Try pushing into a repository with pristine working tree
1608 git push testrepo main &&
1610 cd testrepo &&
1611 git update-index -q --refresh &&
1612 git diff-files --quiet -- &&
1613 git diff-index --quiet --cached HEAD -- &&
1614 test third = "$(cat path2)" &&
1615 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1616 ) &&
1618 # Try pushing into a repository with working tree needing a refresh
1620 cd testrepo &&
1621 git reset --hard HEAD^ &&
1622 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1623 test-tool chmtime +100 path1
1624 ) &&
1625 git push testrepo main &&
1627 cd testrepo &&
1628 git update-index -q --refresh &&
1629 git diff-files --quiet -- &&
1630 git diff-index --quiet --cached HEAD -- &&
1631 test_cmp ../path1 path1 &&
1632 test third = "$(cat path2)" &&
1633 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1634 ) &&
1636 # Update what is to be pushed
1637 test_commit fourth path2 &&
1639 # Try pushing into a repository with a dirty working tree
1640 # (1) the working tree updated
1642 cd testrepo &&
1643 echo changed >path1
1644 ) &&
1645 test_must_fail git push testrepo main &&
1647 cd testrepo &&
1648 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1649 git diff --quiet --cached &&
1650 test changed = "$(cat path1)"
1651 ) &&
1653 # (2) the index updated
1655 cd testrepo &&
1656 echo changed >path1 &&
1657 git add path1
1658 ) &&
1659 test_must_fail git push testrepo main &&
1661 cd testrepo &&
1662 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1663 git diff --quiet &&
1664 test changed = "$(cat path1)"
1665 ) &&
1667 # Introduce a new file in the update
1668 test_commit fifth path3 &&
1670 # (3) the working tree has an untracked file that would interfere
1672 cd testrepo &&
1673 git reset --hard &&
1674 echo changed >path3
1675 ) &&
1676 test_must_fail git push testrepo main &&
1678 cd testrepo &&
1679 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1680 git diff --quiet &&
1681 git diff --quiet --cached &&
1682 test changed = "$(cat path3)"
1683 ) &&
1685 # (4) the target changes to what gets pushed but it still is a change
1687 cd testrepo &&
1688 git reset --hard &&
1689 echo fifth >path3 &&
1690 git add path3
1691 ) &&
1692 test_must_fail git push testrepo main &&
1694 cd testrepo &&
1695 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1696 git diff --quiet &&
1697 test fifth = "$(cat path3)"
1698 ) &&
1700 # (5) push into void
1701 test_when_finished "rm -rf void" &&
1702 git init void &&
1704 cd void &&
1705 git config receive.denyCurrentBranch updateInstead
1706 ) &&
1707 git push void main &&
1709 cd void &&
1710 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1711 git diff --quiet &&
1712 git diff --cached --quiet
1713 ) &&
1715 # (6) updateInstead intervened by fast-forward check
1716 test_must_fail git push void main^:main &&
1717 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1718 git -C void diff --quiet &&
1719 git -C void diff --cached --quiet
1722 test_expect_success 'updateInstead with push-to-checkout hook' '
1723 test_when_finished "rm -rf testrepo" &&
1724 git init testrepo &&
1725 git -C testrepo pull .. main &&
1726 git -C testrepo reset --hard HEAD^^ &&
1727 git -C testrepo tag initial &&
1728 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1729 test_hook -C testrepo push-to-checkout <<-\EOF &&
1730 echo >&2 updating from $(git rev-parse HEAD)
1731 echo >&2 updating to "$1"
1733 git update-index -q --refresh &&
1734 git read-tree -u -m HEAD "$1" || {
1735 status=$?
1736 echo >&2 read-tree failed
1737 exit $status
1741 # Try pushing into a pristine
1742 git push testrepo main &&
1744 cd testrepo &&
1745 git diff --quiet &&
1746 git diff HEAD --quiet &&
1747 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1748 ) &&
1750 # Try pushing into a repository with conflicting change
1752 cd testrepo &&
1753 git reset --hard initial &&
1754 echo conflicting >path2
1755 ) &&
1756 test_must_fail git push testrepo main &&
1758 cd testrepo &&
1759 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1760 test conflicting = "$(cat path2)" &&
1761 git diff-index --quiet --cached HEAD
1762 ) &&
1764 # Try pushing into a repository with unrelated change
1766 cd testrepo &&
1767 git reset --hard initial &&
1768 echo unrelated >path1 &&
1769 echo irrelevant >path5 &&
1770 git add path5
1771 ) &&
1772 git push testrepo main &&
1774 cd testrepo &&
1775 test "$(cat path1)" = unrelated &&
1776 test "$(cat path5)" = irrelevant &&
1777 test "$(git diff --name-only --cached HEAD)" = path5 &&
1778 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1779 ) &&
1781 # push into void
1782 test_when_finished "rm -rf void" &&
1783 git init void &&
1784 git -C void config receive.denyCurrentBranch updateInstead &&
1785 test_hook -C void push-to-checkout <<-\EOF &&
1786 if git rev-parse --quiet --verify HEAD
1787 then
1788 has_head=yes
1789 echo >&2 updating from $(git rev-parse HEAD)
1790 else
1791 has_head=no
1792 echo >&2 pushing into void
1794 echo >&2 updating to "$1"
1796 git update-index -q --refresh &&
1797 case "$has_head" in
1798 yes)
1799 git read-tree -u -m HEAD "$1" ;;
1801 git read-tree -u -m "$1" ;;
1802 esac || {
1803 status=$?
1804 echo >&2 read-tree failed
1805 exit $status
1809 git push void main &&
1811 cd void &&
1812 git diff --quiet &&
1813 git diff --cached --quiet &&
1814 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1818 test_expect_success 'denyCurrentBranch and worktrees' '
1819 git worktree add new-wt &&
1820 git clone . cloned &&
1821 test_commit -C cloned first &&
1822 test_config receive.denyCurrentBranch refuse &&
1823 test_must_fail git -C cloned push origin HEAD:new-wt &&
1824 test_config receive.denyCurrentBranch updateInstead &&
1825 git -C cloned push origin HEAD:new-wt &&
1826 test_path_exists new-wt/first.t &&
1827 test_must_fail git -C cloned push --delete origin new-wt
1830 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1831 test_when_finished "rm -fr bare.git" &&
1832 git clone --bare . bare.git &&
1833 git -C bare.git worktree add wt &&
1834 test_commit grape &&
1835 git -C bare.git config receive.denyCurrentBranch refuse &&
1836 test_must_fail git push bare.git HEAD:wt &&
1837 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1838 git push bare.git HEAD:wt &&
1839 test_path_exists bare.git/wt/grape.t &&
1840 test_must_fail git push --delete bare.git wt
1843 test_expect_success 'refuse fetch to current branch of worktree' '
1844 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1845 git worktree add wt &&
1846 test_commit apple &&
1847 test_must_fail git fetch . HEAD:wt &&
1848 git fetch -u . HEAD:wt
1851 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
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 banana &&
1856 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1857 git -C bare.git fetch -u .. HEAD:wt
1860 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1861 mk_empty testrepo &&
1862 git -C testrepo config receive.hiderefs refs/hidden &&
1863 git -C testrepo config receive.unpackLimit 1 &&
1864 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1865 test_dir_is_empty testrepo/.git/objects/pack
1868 test_expect_success 'push with config push.useBitmaps' '
1869 mk_test testrepo heads/main &&
1870 git checkout main &&
1871 test_unconfig push.useBitmaps &&
1872 GIT_TRACE2_EVENT="$PWD/default" \
1873 git push --quiet testrepo main:test &&
1874 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1875 --thin --delta-base-offset -q <default &&
1877 test_config push.useBitmaps true &&
1878 GIT_TRACE2_EVENT="$PWD/true" \
1879 git push --quiet testrepo main:test2 &&
1880 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1881 --thin --delta-base-offset -q <true &&
1883 test_config push.useBitmaps false &&
1884 GIT_TRACE2_EVENT="$PWD/false" \
1885 git push --quiet testrepo main:test3 &&
1886 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1887 --thin --delta-base-offset -q --no-use-bitmap-index <false
1890 test_done