fetch-pack: add tracing for negotiation rounds
[git.git] / t / t5516-fetch-push.sh
blob3211002d466867fb2abb99eeff897029a7fdfa71
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 git submodule add ./submodule_upstream submodule &&
226 mk_empty testrepo &&
227 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
228 test_commit -C testrepo unrelated_commit &&
229 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
230 GIT_TRACE2_EVENT="$(pwd)/event" git -c submodule.recurse=true \
231 -c protocol.version=2 -c push.negotiate=1 \
232 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
233 grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
234 ! grep "Fetching submodule" err
237 test_expect_success 'push without wildcard' '
238 mk_empty testrepo &&
240 git push testrepo refs/heads/main:refs/remotes/origin/main &&
242 cd testrepo &&
243 echo "$the_commit commit refs/remotes/origin/main" >expect &&
244 git for-each-ref refs/remotes/origin >actual &&
245 test_cmp expect actual
249 test_expect_success 'push with wildcard' '
250 mk_empty testrepo &&
252 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
254 cd testrepo &&
255 echo "$the_commit commit refs/remotes/origin/main" >expect &&
256 git for-each-ref refs/remotes/origin >actual &&
257 test_cmp expect actual
261 test_expect_success 'push with insteadOf' '
262 mk_empty testrepo &&
263 TRASH="$(pwd)/" &&
264 test_config "url.$TRASH.insteadOf" trash/ &&
265 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
267 cd testrepo &&
268 echo "$the_commit commit refs/remotes/origin/main" >expect &&
269 git for-each-ref refs/remotes/origin >actual &&
270 test_cmp expect actual
274 test_expect_success 'push with pushInsteadOf' '
275 mk_empty testrepo &&
276 TRASH="$(pwd)/" &&
277 test_config "url.$TRASH.pushInsteadOf" trash/ &&
278 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
280 cd testrepo &&
281 echo "$the_commit commit refs/remotes/origin/main" >expect &&
282 git for-each-ref refs/remotes/origin >actual &&
283 test_cmp expect actual
287 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
288 mk_empty testrepo &&
289 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
290 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
291 test_config remote.r.url trash/wrong &&
292 test_config remote.r.pushurl "testrepo/" &&
293 git push r refs/heads/main:refs/remotes/origin/main &&
295 cd testrepo &&
296 echo "$the_commit commit refs/remotes/origin/main" >expect &&
297 git for-each-ref refs/remotes/origin >actual &&
298 test_cmp expect actual
302 test_expect_success 'push with matching heads' '
304 mk_test testrepo heads/main &&
305 git push testrepo : &&
306 check_push_result testrepo $the_commit heads/main
310 test_expect_success 'push with matching heads on the command line' '
312 mk_test testrepo heads/main &&
313 git push testrepo : &&
314 check_push_result testrepo $the_commit heads/main
318 test_expect_success 'failed (non-fast-forward) push with matching heads' '
320 mk_test testrepo heads/main &&
321 git push testrepo : &&
322 git commit --amend -massaged &&
323 test_must_fail git push testrepo &&
324 check_push_result testrepo $the_commit heads/main &&
325 git reset --hard $the_commit
329 test_expect_success 'push --force with matching heads' '
331 mk_test testrepo heads/main &&
332 git push testrepo : &&
333 git commit --amend -massaged &&
334 git push --force testrepo : &&
335 ! check_push_result testrepo $the_commit heads/main &&
336 git reset --hard $the_commit
340 test_expect_success 'push with matching heads and forced update' '
342 mk_test testrepo heads/main &&
343 git push testrepo : &&
344 git commit --amend -massaged &&
345 git push testrepo +: &&
346 ! check_push_result testrepo $the_commit heads/main &&
347 git reset --hard $the_commit
351 test_expect_success 'push with no ambiguity (1)' '
353 mk_test testrepo heads/main &&
354 git push testrepo main:main &&
355 check_push_result testrepo $the_commit heads/main
359 test_expect_success 'push with no ambiguity (2)' '
361 mk_test testrepo remotes/origin/main &&
362 git push testrepo main:origin/main &&
363 check_push_result testrepo $the_commit remotes/origin/main
367 test_expect_success 'push with colon-less refspec, no ambiguity' '
369 mk_test testrepo heads/main heads/t/main &&
370 git branch -f t/main main &&
371 git push testrepo main &&
372 check_push_result testrepo $the_commit heads/main &&
373 check_push_result testrepo $the_first_commit heads/t/main
377 test_expect_success 'push with weak ambiguity (1)' '
379 mk_test testrepo heads/main remotes/origin/main &&
380 git push testrepo main:main &&
381 check_push_result testrepo $the_commit heads/main &&
382 check_push_result testrepo $the_first_commit remotes/origin/main
386 test_expect_success 'push with weak ambiguity (2)' '
388 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
389 git push testrepo main:main &&
390 check_push_result testrepo $the_commit heads/main &&
391 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
395 test_expect_success 'push with ambiguity' '
397 mk_test testrepo heads/frotz tags/frotz &&
398 test_must_fail git push testrepo main:frotz &&
399 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
403 test_expect_success 'push with colon-less refspec (1)' '
405 mk_test testrepo heads/frotz tags/frotz &&
406 git branch -f frotz main &&
407 git push testrepo frotz &&
408 check_push_result testrepo $the_commit heads/frotz &&
409 check_push_result testrepo $the_first_commit tags/frotz
413 test_expect_success 'push with colon-less refspec (2)' '
415 mk_test testrepo heads/frotz tags/frotz &&
416 if git show-ref --verify -q refs/heads/frotz
417 then
418 git branch -D frotz
419 fi &&
420 git tag -f frotz &&
421 git push -f testrepo frotz &&
422 check_push_result testrepo $the_commit tags/frotz &&
423 check_push_result testrepo $the_first_commit heads/frotz
427 test_expect_success 'push with colon-less refspec (3)' '
429 mk_test testrepo &&
430 if git show-ref --verify -q refs/tags/frotz
431 then
432 git tag -d frotz
433 fi &&
434 git branch -f frotz main &&
435 git push testrepo frotz &&
436 check_push_result testrepo $the_commit heads/frotz &&
437 test 1 = $( cd testrepo && git show-ref | wc -l )
440 test_expect_success 'push with colon-less refspec (4)' '
442 mk_test testrepo &&
443 if git show-ref --verify -q refs/heads/frotz
444 then
445 git branch -D frotz
446 fi &&
447 git tag -f frotz &&
448 git push testrepo frotz &&
449 check_push_result testrepo $the_commit tags/frotz &&
450 test 1 = $( cd testrepo && git show-ref | wc -l )
454 test_expect_success 'push head with non-existent, incomplete dest' '
456 mk_test testrepo &&
457 git push testrepo main:branch &&
458 check_push_result testrepo $the_commit heads/branch
462 test_expect_success 'push tag with non-existent, incomplete dest' '
464 mk_test testrepo &&
465 git tag -f v1.0 &&
466 git push testrepo v1.0:tag &&
467 check_push_result testrepo $the_commit tags/tag
471 test_expect_success 'push sha1 with non-existent, incomplete dest' '
473 mk_test testrepo &&
474 test_must_fail git push testrepo $(git rev-parse main):foo
478 test_expect_success 'push ref expression with non-existent, incomplete dest' '
480 mk_test testrepo &&
481 test_must_fail git push testrepo main^:branch
485 for head in HEAD @
488 test_expect_success "push with $head" '
489 mk_test testrepo heads/main &&
490 git checkout main &&
491 git push testrepo $head &&
492 check_push_result testrepo $the_commit heads/main
495 test_expect_success "push with $head nonexisting at remote" '
496 mk_test testrepo heads/main &&
497 git checkout -b local main &&
498 test_when_finished "git checkout main; git branch -D local" &&
499 git push testrepo $head &&
500 check_push_result testrepo $the_commit heads/local
503 test_expect_success "push with +$head" '
504 mk_test testrepo heads/main &&
505 git checkout -b local main &&
506 test_when_finished "git checkout main; git branch -D local" &&
507 git push testrepo main local &&
508 check_push_result testrepo $the_commit heads/main &&
509 check_push_result testrepo $the_commit heads/local &&
511 # Without force rewinding should fail
512 git reset --hard $head^ &&
513 test_must_fail git push testrepo $head &&
514 check_push_result testrepo $the_commit heads/local &&
516 # With force rewinding should succeed
517 git push testrepo +$head &&
518 check_push_result testrepo $the_first_commit heads/local
521 test_expect_success "push $head with non-existent, incomplete dest" '
522 mk_test testrepo &&
523 git checkout main &&
524 git push testrepo $head:branch &&
525 check_push_result testrepo $the_commit heads/branch
529 test_expect_success "push with config remote.*.push = $head" '
530 mk_test testrepo heads/local &&
531 git checkout main &&
532 git branch -f local $the_commit &&
533 test_when_finished "git branch -D local" &&
535 cd testrepo &&
536 git checkout local &&
537 git reset --hard $the_first_commit
538 ) &&
539 test_config remote.there.url testrepo &&
540 test_config remote.there.push $head &&
541 test_config branch.main.remote there &&
542 git push &&
543 check_push_result testrepo $the_commit heads/main &&
544 check_push_result testrepo $the_first_commit heads/local
547 done
549 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
550 mk_test testrepo heads/main &&
551 git checkout $the_first_commit &&
552 test_config remote.there.url testrepo &&
553 test_config remote.there.push refs/heads/main:refs/heads/main &&
554 git push there &&
555 check_push_result testrepo $the_commit heads/main
558 test_expect_success 'push with remote.pushdefault' '
559 mk_test up_repo heads/main &&
560 mk_test down_repo heads/main &&
561 test_config remote.up.url up_repo &&
562 test_config remote.down.url down_repo &&
563 test_config branch.main.remote up &&
564 test_config remote.pushdefault down &&
565 test_config push.default matching &&
566 git push &&
567 check_push_result up_repo $the_first_commit heads/main &&
568 check_push_result down_repo $the_commit heads/main
571 test_expect_success 'push with config remote.*.pushurl' '
573 mk_test testrepo heads/main &&
574 git checkout main &&
575 test_config remote.there.url test2repo &&
576 test_config remote.there.pushurl testrepo &&
577 git push there : &&
578 check_push_result testrepo $the_commit heads/main
581 test_expect_success 'push with config branch.*.pushremote' '
582 mk_test up_repo heads/main &&
583 mk_test side_repo heads/main &&
584 mk_test down_repo heads/main &&
585 test_config remote.up.url up_repo &&
586 test_config remote.pushdefault side_repo &&
587 test_config remote.down.url down_repo &&
588 test_config branch.main.remote up &&
589 test_config branch.main.pushremote down &&
590 test_config push.default matching &&
591 git push &&
592 check_push_result up_repo $the_first_commit heads/main &&
593 check_push_result side_repo $the_first_commit heads/main &&
594 check_push_result down_repo $the_commit heads/main
597 test_expect_success 'branch.*.pushremote config order is irrelevant' '
598 mk_test one_repo heads/main &&
599 mk_test two_repo heads/main &&
600 test_config remote.one.url one_repo &&
601 test_config remote.two.url two_repo &&
602 test_config branch.main.pushremote two_repo &&
603 test_config remote.pushdefault one_repo &&
604 test_config push.default matching &&
605 git push &&
606 check_push_result one_repo $the_first_commit heads/main &&
607 check_push_result two_repo $the_commit heads/main
610 test_expect_success 'push rejects empty branch name entries' '
611 mk_test one_repo heads/main &&
612 test_config remote.one.url one_repo &&
613 test_config branch..remote one &&
614 test_config branch..merge refs/heads/ &&
615 test_config branch.main.remote one &&
616 test_config branch.main.merge refs/heads/main &&
617 test_must_fail git push 2>err &&
618 grep "bad config variable .branch\.\." err
621 test_expect_success 'push ignores "branch." config without subsection' '
622 mk_test one_repo heads/main &&
623 test_config remote.one.url one_repo &&
624 test_config branch.autoSetupMerge true &&
625 test_config branch.main.remote one &&
626 test_config branch.main.merge refs/heads/main &&
627 git push
630 test_expect_success 'push with dry-run' '
632 mk_test testrepo heads/main &&
633 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
634 git push --dry-run testrepo : &&
635 check_push_result testrepo $old_commit heads/main
638 test_expect_success 'push updates local refs' '
640 mk_test testrepo heads/main &&
641 mk_child testrepo child &&
643 cd child &&
644 git pull .. main &&
645 git push &&
646 test $(git rev-parse main) = \
647 $(git rev-parse remotes/origin/main)
652 test_expect_success 'push updates up-to-date local refs' '
654 mk_test testrepo heads/main &&
655 mk_child testrepo child1 &&
656 mk_child testrepo child2 &&
657 (cd child1 && git pull .. main && git push) &&
659 cd child2 &&
660 git pull ../child1 main &&
661 git push &&
662 test $(git rev-parse main) = \
663 $(git rev-parse remotes/origin/main)
668 test_expect_success 'push preserves up-to-date packed refs' '
670 mk_test testrepo heads/main &&
671 mk_child testrepo child &&
673 cd child &&
674 git push &&
675 ! test -f .git/refs/remotes/origin/main
680 test_expect_success 'push does not update local refs on failure' '
682 mk_test testrepo heads/main &&
683 mk_child testrepo child &&
684 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
685 chmod +x testrepo/.git/hooks/pre-receive &&
687 cd child &&
688 git pull .. main &&
689 test_must_fail git push &&
690 test $(git rev-parse main) != \
691 $(git rev-parse remotes/origin/main)
696 test_expect_success 'allow deleting an invalid remote ref' '
698 mk_test testrepo heads/branch &&
699 rm -f testrepo/.git/objects/??/* &&
700 git push testrepo :refs/heads/branch &&
701 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
705 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
706 mk_test_with_hooks testrepo heads/main heads/next &&
707 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
708 newmain=$(git show-ref -s --verify refs/heads/main) &&
709 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
710 newnext=$ZERO_OID &&
711 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
713 cd testrepo/.git &&
714 cat >pre-receive.expect <<-EOF &&
715 $orgmain $newmain refs/heads/main
716 $orgnext $newnext refs/heads/next
719 cat >update.expect <<-EOF &&
720 refs/heads/main $orgmain $newmain
721 refs/heads/next $orgnext $newnext
724 cat >post-receive.expect <<-EOF &&
725 $orgmain $newmain refs/heads/main
726 $orgnext $newnext refs/heads/next
729 cat >post-update.expect <<-EOF &&
730 refs/heads/main
731 refs/heads/next
734 test_cmp pre-receive.expect pre-receive.actual &&
735 test_cmp update.expect update.actual &&
736 test_cmp post-receive.expect post-receive.actual &&
737 test_cmp post-update.expect post-update.actual
741 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
742 mk_test_with_hooks testrepo heads/branch &&
743 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
744 rm -f testrepo/.git/objects/??/* &&
745 git push testrepo :refs/heads/branch &&
747 cd testrepo/.git &&
748 cat >pre-receive.expect <<-EOF &&
749 $orig $ZERO_OID refs/heads/branch
752 cat >update.expect <<-EOF &&
753 refs/heads/branch $orig $ZERO_OID
756 cat >post-receive.expect <<-EOF &&
757 $orig $ZERO_OID refs/heads/branch
760 cat >post-update.expect <<-EOF &&
761 refs/heads/branch
764 test_cmp pre-receive.expect pre-receive.actual &&
765 test_cmp update.expect update.actual &&
766 test_cmp post-receive.expect post-receive.actual &&
767 test_cmp post-update.expect post-update.actual
771 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
772 mk_test_with_hooks testrepo heads/main &&
773 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
774 newmain=$(git show-ref -s --verify refs/heads/main) &&
775 git push testrepo main :refs/heads/nonexistent &&
777 cd testrepo/.git &&
778 cat >pre-receive.expect <<-EOF &&
779 $orgmain $newmain refs/heads/main
780 $ZERO_OID $ZERO_OID refs/heads/nonexistent
783 cat >update.expect <<-EOF &&
784 refs/heads/main $orgmain $newmain
785 refs/heads/nonexistent $ZERO_OID $ZERO_OID
788 cat >post-receive.expect <<-EOF &&
789 $orgmain $newmain refs/heads/main
792 cat >post-update.expect <<-EOF &&
793 refs/heads/main
796 test_cmp pre-receive.expect pre-receive.actual &&
797 test_cmp update.expect update.actual &&
798 test_cmp post-receive.expect post-receive.actual &&
799 test_cmp post-update.expect post-update.actual
803 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
804 mk_test_with_hooks testrepo heads/main &&
805 git push testrepo :refs/heads/nonexistent &&
807 cd testrepo/.git &&
808 cat >pre-receive.expect <<-EOF &&
809 $ZERO_OID $ZERO_OID refs/heads/nonexistent
812 cat >update.expect <<-EOF &&
813 refs/heads/nonexistent $ZERO_OID $ZERO_OID
816 test_cmp pre-receive.expect pre-receive.actual &&
817 test_cmp update.expect update.actual &&
818 test_path_is_missing post-receive.actual &&
819 test_path_is_missing post-update.actual
823 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
824 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
825 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
826 newmain=$(git show-ref -s --verify refs/heads/main) &&
827 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
828 newnext=$ZERO_OID &&
829 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
830 newseen=$(git show-ref -s --verify refs/heads/main) &&
831 git push testrepo refs/heads/main:refs/heads/main \
832 refs/heads/main:refs/heads/seen :refs/heads/next \
833 :refs/heads/nonexistent &&
835 cd testrepo/.git &&
836 cat >pre-receive.expect <<-EOF &&
837 $orgmain $newmain refs/heads/main
838 $orgnext $newnext refs/heads/next
839 $orgseen $newseen refs/heads/seen
840 $ZERO_OID $ZERO_OID refs/heads/nonexistent
843 cat >update.expect <<-EOF &&
844 refs/heads/main $orgmain $newmain
845 refs/heads/next $orgnext $newnext
846 refs/heads/seen $orgseen $newseen
847 refs/heads/nonexistent $ZERO_OID $ZERO_OID
850 cat >post-receive.expect <<-EOF &&
851 $orgmain $newmain refs/heads/main
852 $orgnext $newnext refs/heads/next
853 $orgseen $newseen refs/heads/seen
856 cat >post-update.expect <<-EOF &&
857 refs/heads/main
858 refs/heads/next
859 refs/heads/seen
862 test_cmp pre-receive.expect pre-receive.actual &&
863 test_cmp update.expect update.actual &&
864 test_cmp post-receive.expect post-receive.actual &&
865 test_cmp post-update.expect post-update.actual
869 test_expect_success 'allow deleting a ref using --delete' '
870 mk_test testrepo heads/main &&
871 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
872 git push testrepo --delete main &&
873 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
876 test_expect_success 'allow deleting a tag using --delete' '
877 mk_test testrepo heads/main &&
878 git tag -a -m dummy_message deltag heads/main &&
879 git push testrepo --tags &&
880 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
881 git push testrepo --delete tag deltag &&
882 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
885 test_expect_success 'push --delete without args aborts' '
886 mk_test testrepo heads/main &&
887 test_must_fail git push testrepo --delete
890 test_expect_success 'push --delete refuses src:dest refspecs' '
891 mk_test testrepo heads/main &&
892 test_must_fail git push testrepo --delete main:foo
895 test_expect_success 'push --delete refuses empty string' '
896 mk_test testrepo heads/master &&
897 test_must_fail git push testrepo --delete ""
900 test_expect_success 'warn on push to HEAD of non-bare repository' '
901 mk_test testrepo heads/main &&
903 cd testrepo &&
904 git checkout main &&
905 git config receive.denyCurrentBranch warn
906 ) &&
907 git push testrepo main 2>stderr &&
908 grep "warning: updating the current branch" stderr
911 test_expect_success 'deny push to HEAD of non-bare repository' '
912 mk_test testrepo heads/main &&
914 cd testrepo &&
915 git checkout main &&
916 git config receive.denyCurrentBranch true
917 ) &&
918 test_must_fail git push testrepo main
921 test_expect_success 'allow push to HEAD of bare repository (bare)' '
922 mk_test testrepo heads/main &&
924 cd testrepo &&
925 git checkout main &&
926 git config receive.denyCurrentBranch true &&
927 git config core.bare true
928 ) &&
929 git push testrepo main 2>stderr &&
930 ! grep "warning: updating the current branch" stderr
933 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
934 mk_test testrepo heads/main &&
936 cd testrepo &&
937 git checkout main &&
938 git config receive.denyCurrentBranch false
939 ) &&
940 git push testrepo main 2>stderr &&
941 ! grep "warning: updating the current branch" stderr
944 test_expect_success 'fetch with branches' '
945 mk_empty testrepo &&
946 git branch second $the_first_commit &&
947 git checkout second &&
948 mkdir testrepo/.git/branches &&
949 echo ".." > testrepo/.git/branches/branch1 &&
951 cd testrepo &&
952 git fetch branch1 &&
953 echo "$the_commit commit refs/heads/branch1" >expect &&
954 git for-each-ref refs/heads >actual &&
955 test_cmp expect actual
956 ) &&
957 git checkout main
960 test_expect_success 'fetch with branches containing #' '
961 mk_empty testrepo &&
962 mkdir testrepo/.git/branches &&
963 echo "..#second" > testrepo/.git/branches/branch2 &&
965 cd testrepo &&
966 git fetch branch2 &&
967 echo "$the_first_commit commit refs/heads/branch2" >expect &&
968 git for-each-ref refs/heads >actual &&
969 test_cmp expect actual
970 ) &&
971 git checkout main
974 test_expect_success 'push with branches' '
975 mk_empty testrepo &&
976 git checkout second &&
978 test_when_finished "rm -rf .git/branches" &&
979 mkdir .git/branches &&
980 echo "testrepo" > .git/branches/branch1 &&
982 git push branch1 &&
984 cd testrepo &&
985 echo "$the_first_commit commit refs/heads/main" >expect &&
986 git for-each-ref refs/heads >actual &&
987 test_cmp expect actual
991 test_expect_success 'push with branches containing #' '
992 mk_empty testrepo &&
994 test_when_finished "rm -rf .git/branches" &&
995 mkdir .git/branches &&
996 echo "testrepo#branch3" > .git/branches/branch2 &&
998 git push branch2 &&
1000 cd testrepo &&
1001 echo "$the_first_commit commit refs/heads/branch3" >expect &&
1002 git for-each-ref refs/heads >actual &&
1003 test_cmp expect actual
1004 ) &&
1005 git checkout main
1008 test_expect_success 'push into aliased refs (consistent)' '
1009 mk_test testrepo heads/main &&
1010 mk_child testrepo child1 &&
1011 mk_child testrepo child2 &&
1013 cd child1 &&
1014 git branch foo &&
1015 git symbolic-ref refs/heads/bar refs/heads/foo &&
1016 git config receive.denyCurrentBranch false
1017 ) &&
1019 cd child2 &&
1020 >path2 &&
1021 git add path2 &&
1022 test_tick &&
1023 git commit -a -m child2 &&
1024 git branch foo &&
1025 git branch bar &&
1026 git push ../child1 foo bar
1030 test_expect_success 'push into aliased refs (inconsistent)' '
1031 mk_test testrepo heads/main &&
1032 mk_child testrepo child1 &&
1033 mk_child testrepo child2 &&
1035 cd child1 &&
1036 git branch foo &&
1037 git symbolic-ref refs/heads/bar refs/heads/foo &&
1038 git config receive.denyCurrentBranch false
1039 ) &&
1041 cd child2 &&
1042 >path2 &&
1043 git add path2 &&
1044 test_tick &&
1045 git commit -a -m child2 &&
1046 git branch foo &&
1047 >path3 &&
1048 git add path3 &&
1049 test_tick &&
1050 git commit -a -m child2 &&
1051 git branch bar &&
1052 test_must_fail git push ../child1 foo bar 2>stderr &&
1053 grep "refusing inconsistent update" stderr
1057 test_force_push_tag () {
1058 tag_type_description=$1
1059 tag_args=$2
1061 test_expect_success "force pushing required to update $tag_type_description" "
1062 mk_test testrepo heads/main &&
1063 mk_child testrepo child1 &&
1064 mk_child testrepo child2 &&
1066 cd child1 &&
1067 git tag testTag &&
1068 git push ../child2 testTag &&
1069 >file1 &&
1070 git add file1 &&
1071 git commit -m 'file1' &&
1072 git tag $tag_args testTag &&
1073 test_must_fail git push ../child2 testTag &&
1074 git push --force ../child2 testTag &&
1075 git tag $tag_args testTag HEAD~ &&
1076 test_must_fail git push ../child2 testTag &&
1077 git push --force ../child2 testTag &&
1079 # Clobbering without + in refspec needs --force
1080 git tag -f testTag &&
1081 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1082 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1084 # Clobbering with + in refspec does not need --force
1085 git tag -f testTag HEAD~ &&
1086 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1088 # Clobbering with --no-force still obeys + in refspec
1089 git tag -f testTag &&
1090 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1092 # Clobbering with/without --force and 'tag <name>' format
1093 git tag -f testTag HEAD~ &&
1094 test_must_fail git push ../child2 tag testTag &&
1095 git push --force ../child2 tag testTag
1100 test_force_push_tag "lightweight tag" "-f"
1101 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1103 test_force_fetch_tag () {
1104 tag_type_description=$1
1105 tag_args=$2
1107 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1108 mk_test testrepo heads/main &&
1109 mk_child testrepo child1 &&
1110 mk_child testrepo child2 &&
1112 cd testrepo &&
1113 git tag testTag &&
1114 git -C ../child1 fetch origin tag testTag &&
1115 >file1 &&
1116 git add file1 &&
1117 git commit -m 'file1' &&
1118 git tag $tag_args testTag &&
1119 test_must_fail git -C ../child1 fetch origin tag testTag &&
1120 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1125 test_force_fetch_tag "lightweight tag" "-f"
1126 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1128 test_expect_success 'push --porcelain' '
1129 mk_empty testrepo &&
1130 echo >.git/foo "To testrepo" &&
1131 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1132 echo >>.git/foo "Done" &&
1133 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1135 cd testrepo &&
1136 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1137 git for-each-ref refs/remotes/origin >actual &&
1138 test_cmp expect actual
1139 ) &&
1140 test_cmp .git/foo .git/bar
1143 test_expect_success 'push --porcelain bad url' '
1144 mk_empty testrepo &&
1145 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1146 ! grep -q Done .git/bar
1149 test_expect_success 'push --porcelain rejected' '
1150 mk_empty testrepo &&
1151 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1152 (cd testrepo &&
1153 git reset --hard origin/main^ &&
1154 git config receive.denyCurrentBranch true) &&
1156 echo >.git/foo "To testrepo" &&
1157 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1158 echo >>.git/foo "Done" &&
1160 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1161 test_cmp .git/foo .git/bar
1164 test_expect_success 'push --porcelain --dry-run rejected' '
1165 mk_empty testrepo &&
1166 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1167 (cd testrepo &&
1168 git reset --hard origin/main &&
1169 git config receive.denyCurrentBranch true) &&
1171 echo >.git/foo "To testrepo" &&
1172 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1173 echo >>.git/foo "Done" &&
1175 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1176 test_cmp .git/foo .git/bar
1179 test_expect_success 'push --prune' '
1180 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1181 git push --prune testrepo : &&
1182 check_push_result testrepo $the_commit heads/main &&
1183 check_push_result testrepo $the_first_commit heads/second &&
1184 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1187 test_expect_success 'push --prune refspec' '
1188 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1189 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1190 check_push_result testrepo $the_commit tmp/main &&
1191 check_push_result testrepo $the_first_commit tmp/second &&
1192 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1195 for configsection in transfer receive
1197 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1198 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1200 cd testrepo &&
1201 git config $configsection.hiderefs refs/hidden
1202 ) &&
1204 # push to unhidden ref succeeds normally
1205 git push testrepo main:refs/heads/main &&
1206 check_push_result testrepo $the_commit heads/main &&
1208 # push to update a hidden ref should fail
1209 test_must_fail git push testrepo main:refs/hidden/one &&
1210 check_push_result testrepo $the_first_commit hidden/one &&
1212 # push to delete a hidden ref should fail
1213 test_must_fail git push testrepo :refs/hidden/two &&
1214 check_push_result testrepo $the_first_commit hidden/two &&
1216 # idempotent push to update a hidden ref should fail
1217 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1218 check_push_result testrepo $the_first_commit hidden/three
1220 done
1222 test_expect_success 'fetch exact SHA1' '
1223 mk_test testrepo heads/main hidden/one &&
1224 git push testrepo main:refs/hidden/one &&
1226 cd testrepo &&
1227 git config transfer.hiderefs refs/hidden
1228 ) &&
1229 check_push_result testrepo $the_commit hidden/one &&
1231 mk_child testrepo child &&
1233 cd child &&
1235 # make sure $the_commit does not exist here
1236 git repack -a -d &&
1237 git prune &&
1238 test_must_fail git cat-file -t $the_commit &&
1240 # Some protocol versions (e.g. 2) support fetching
1241 # unadvertised objects, so restrict this test to v0.
1243 # fetching the hidden object should fail by default
1244 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1245 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1246 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1247 test_must_fail git rev-parse --verify refs/heads/copy &&
1249 # the server side can allow it to succeed
1251 cd ../testrepo &&
1252 git config uploadpack.allowtipsha1inwant true
1253 ) &&
1255 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1256 cat >expect <<-EOF &&
1257 $the_commit
1258 $the_first_commit
1261 git rev-parse --verify refs/heads/copy &&
1262 git rev-parse --verify refs/heads/extra
1263 } >actual &&
1264 test_cmp expect actual
1268 test_expect_success 'fetch exact SHA1 in protocol v2' '
1269 mk_test testrepo heads/main hidden/one &&
1270 git push testrepo main:refs/hidden/one &&
1271 git -C testrepo config transfer.hiderefs refs/hidden &&
1272 check_push_result testrepo $the_commit hidden/one &&
1274 mk_child testrepo child &&
1275 git -C child config protocol.version 2 &&
1277 # make sure $the_commit does not exist here
1278 git -C child repack -a -d &&
1279 git -C child prune &&
1280 test_must_fail git -C child cat-file -t $the_commit &&
1282 # fetching the hidden object succeeds by default
1283 # NEEDSWORK: should this match the v0 behavior instead?
1284 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1287 for configallowtipsha1inwant in true false
1289 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1290 mk_empty testrepo &&
1292 cd testrepo &&
1293 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1294 git commit --allow-empty -m foo &&
1295 git commit --allow-empty -m bar
1296 ) &&
1297 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1298 mk_empty shallow &&
1300 cd shallow &&
1301 # Some protocol versions (e.g. 2) support fetching
1302 # unadvertised objects, so restrict this test to v0.
1303 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1304 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1305 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1306 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1307 git cat-file commit $SHA1
1311 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1312 mk_empty testrepo &&
1314 cd testrepo &&
1315 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1316 git commit --allow-empty -m foo &&
1317 git commit --allow-empty -m bar &&
1318 git commit --allow-empty -m xyz
1319 ) &&
1320 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1321 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1322 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1324 cd testrepo &&
1325 git reset --hard $SHA1_2 &&
1326 git cat-file commit $SHA1_1 &&
1327 git cat-file commit $SHA1_3
1328 ) &&
1329 mk_empty shallow &&
1331 cd shallow &&
1332 # Some protocol versions (e.g. 2) support fetching
1333 # unadvertised objects, so restrict this test to v0.
1334 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1335 git fetch ../testrepo/.git $SHA1_3 &&
1336 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1337 git fetch ../testrepo/.git $SHA1_1 &&
1338 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1339 git fetch ../testrepo/.git $SHA1_1 &&
1340 git cat-file commit $SHA1_1 &&
1341 test_must_fail git cat-file commit $SHA1_2 &&
1342 git fetch ../testrepo/.git $SHA1_2 &&
1343 git cat-file commit $SHA1_2 &&
1344 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1345 git fetch ../testrepo/.git $SHA1_3 2>err &&
1346 # ideally we would insist this be on a "remote error:"
1347 # line, but it is racy; see the commit message
1348 test_i18ngrep "not our ref.*$SHA1_3\$" err
1351 done
1353 test_expect_success 'fetch follows tags by default' '
1354 mk_test testrepo heads/main &&
1355 test_when_finished "rm -rf src" &&
1356 git init src &&
1358 cd src &&
1359 git pull ../testrepo main &&
1360 git tag -m "annotated" tag &&
1361 git for-each-ref >tmp1 &&
1362 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1363 sort -k 3 >../expect
1364 ) &&
1365 test_when_finished "rm -rf dst" &&
1366 git init dst &&
1368 cd dst &&
1369 git remote add origin ../src &&
1370 git config branch.main.remote origin &&
1371 git config branch.main.merge refs/heads/main &&
1372 git pull &&
1373 git for-each-ref >../actual
1374 ) &&
1375 test_cmp expect actual
1378 test_expect_success 'peeled advertisements are not considered ref tips' '
1379 mk_empty testrepo &&
1380 git -C testrepo commit --allow-empty -m one &&
1381 git -C testrepo commit --allow-empty -m two &&
1382 git -C testrepo tag -m foo mytag HEAD^ &&
1383 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1384 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1385 git fetch testrepo $oid 2>err &&
1386 test_i18ngrep "Server does not allow request for unadvertised object" err
1389 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1390 mk_test testrepo heads/main &&
1391 test_when_finished "rm -rf src" &&
1392 git init src &&
1393 test_when_finished "rm -rf dst" &&
1394 git init --bare dst &&
1396 cd src &&
1397 git pull ../testrepo main &&
1398 git branch next &&
1399 git config remote.dst.url ../dst &&
1400 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1401 git push dst main &&
1402 git show-ref refs/heads/main |
1403 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1404 ) &&
1406 cd dst &&
1407 test_must_fail git show-ref refs/heads/next &&
1408 test_must_fail git show-ref refs/heads/main &&
1409 git show-ref refs/remotes/src/main >actual
1410 ) &&
1411 test_cmp dst/expect dst/actual
1414 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1415 mk_test testrepo heads/main &&
1416 test_when_finished "rm -rf src" &&
1417 git init src &&
1418 test_when_finished "rm -rf dst" &&
1419 git init --bare dst &&
1421 cd src &&
1422 git pull ../testrepo main &&
1423 git branch next &&
1424 git config remote.dst.url ../dst &&
1425 git config push.default matching &&
1426 git push dst main &&
1427 git show-ref refs/heads/main >../dst/expect
1428 ) &&
1430 cd dst &&
1431 test_must_fail git show-ref refs/heads/next &&
1432 git show-ref refs/heads/main >actual
1433 ) &&
1434 test_cmp dst/expect dst/actual
1437 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1438 mk_test testrepo heads/main &&
1439 test_when_finished "rm -rf src" &&
1440 git init src &&
1441 test_when_finished "rm -rf dst" &&
1442 git init --bare dst &&
1444 cd src &&
1445 git pull ../testrepo main &&
1446 git branch next &&
1447 git config remote.dst.url ../dst &&
1448 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1449 git config push.default upstream &&
1451 git config branch.main.merge refs/heads/trunk &&
1452 git config branch.main.remote dst &&
1454 git push dst main &&
1455 git show-ref refs/heads/main |
1456 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1457 ) &&
1459 cd dst &&
1460 test_must_fail git show-ref refs/heads/main &&
1461 test_must_fail git show-ref refs/heads/next &&
1462 git show-ref refs/heads/trunk >actual
1463 ) &&
1464 test_cmp dst/expect dst/actual
1467 test_expect_success 'push does not follow tags by default' '
1468 mk_test testrepo heads/main &&
1469 test_when_finished "rm -rf src" &&
1470 git init src &&
1471 test_when_finished "rm -rf dst" &&
1472 git init --bare dst &&
1474 cd src &&
1475 git pull ../testrepo main &&
1476 git tag -m "annotated" tag &&
1477 git checkout -b another &&
1478 git commit --allow-empty -m "future commit" &&
1479 git tag -m "future" future &&
1480 git checkout main &&
1481 git for-each-ref refs/heads/main >../expect &&
1482 git push ../dst main
1483 ) &&
1485 cd dst &&
1486 git for-each-ref >../actual
1487 ) &&
1488 test_cmp expect actual
1491 test_expect_success 'push --follow-tags only pushes relevant tags' '
1492 mk_test testrepo heads/main &&
1493 test_when_finished "rm -rf src" &&
1494 git init src &&
1495 test_when_finished "rm -rf dst" &&
1496 git init --bare dst &&
1498 cd src &&
1499 git pull ../testrepo main &&
1500 git tag -m "annotated" tag &&
1501 git checkout -b another &&
1502 git commit --allow-empty -m "future commit" &&
1503 git tag -m "future" future &&
1504 git checkout main &&
1505 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1506 git push --follow-tags ../dst main
1507 ) &&
1509 cd dst &&
1510 git for-each-ref >../actual
1511 ) &&
1512 test_cmp expect actual
1515 test_expect_success 'push --no-thin must produce non-thin pack' '
1516 cat >>path1 <<\EOF &&
1517 keep base version of path1 big enough, compared to the new changes
1518 later, in order to pass size heuristics in
1519 builtin/pack-objects.c:try_delta()
1521 git commit -am initial &&
1522 git init no-thin &&
1523 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1524 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1525 echo modified >> path1 &&
1526 git commit -am modified &&
1527 git repack -adf &&
1528 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1529 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1532 test_expect_success 'pushing a tag pushes the tagged object' '
1533 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1534 git tag -m foo tag-of-blob $blob &&
1535 test_when_finished "rm -rf dst.git" &&
1536 git init --bare dst.git &&
1537 git push dst.git tag-of-blob &&
1538 # the receiving index-pack should have noticed
1539 # any problems, but we double check
1540 echo unreferenced >expect &&
1541 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1542 test_cmp expect actual
1545 test_expect_success 'push into bare respects core.logallrefupdates' '
1546 test_when_finished "rm -rf dst.git" &&
1547 git init --bare dst.git &&
1548 git -C dst.git config core.logallrefupdates true &&
1550 # double push to test both with and without
1551 # the actual pack transfer
1552 git push dst.git main:one &&
1553 echo "one@{0} push" >expect &&
1554 git -C dst.git log -g --format="%gd %gs" one >actual &&
1555 test_cmp expect actual &&
1557 git push dst.git main:two &&
1558 echo "two@{0} push" >expect &&
1559 git -C dst.git log -g --format="%gd %gs" two >actual &&
1560 test_cmp expect actual
1563 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1564 test_when_finished "rm -rf dst.git" &&
1565 git init --bare dst.git &&
1567 cd dst.git &&
1568 git config core.logallrefupdates true &&
1570 # as above, we double-fetch to test both
1571 # with and without pack transfer
1572 git fetch .. main:one &&
1573 echo "one@{0} fetch .. main:one: storing head" >expect &&
1574 git log -g --format="%gd %gs" one >actual &&
1575 test_cmp expect actual &&
1577 git fetch .. main:two &&
1578 echo "two@{0} fetch .. main:two: storing head" >expect &&
1579 git log -g --format="%gd %gs" two >actual &&
1580 test_cmp expect actual
1584 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1585 mk_empty testrepo &&
1586 git push testrepo main &&
1588 cd testrepo &&
1589 git reset --hard &&
1590 git config receive.denyCurrentBranch updateInstead
1591 ) &&
1592 test_commit third path2 &&
1594 # Try pushing into a repository with pristine working tree
1595 git push testrepo main &&
1597 cd testrepo &&
1598 git update-index -q --refresh &&
1599 git diff-files --quiet -- &&
1600 git diff-index --quiet --cached HEAD -- &&
1601 test third = "$(cat path2)" &&
1602 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1603 ) &&
1605 # Try pushing into a repository with working tree needing a refresh
1607 cd testrepo &&
1608 git reset --hard HEAD^ &&
1609 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1610 test-tool chmtime +100 path1
1611 ) &&
1612 git push testrepo main &&
1614 cd testrepo &&
1615 git update-index -q --refresh &&
1616 git diff-files --quiet -- &&
1617 git diff-index --quiet --cached HEAD -- &&
1618 test_cmp ../path1 path1 &&
1619 test third = "$(cat path2)" &&
1620 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1621 ) &&
1623 # Update what is to be pushed
1624 test_commit fourth path2 &&
1626 # Try pushing into a repository with a dirty working tree
1627 # (1) the working tree updated
1629 cd testrepo &&
1630 echo changed >path1
1631 ) &&
1632 test_must_fail git push testrepo main &&
1634 cd testrepo &&
1635 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1636 git diff --quiet --cached &&
1637 test changed = "$(cat path1)"
1638 ) &&
1640 # (2) the index updated
1642 cd testrepo &&
1643 echo changed >path1 &&
1644 git add path1
1645 ) &&
1646 test_must_fail git push testrepo main &&
1648 cd testrepo &&
1649 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1650 git diff --quiet &&
1651 test changed = "$(cat path1)"
1652 ) &&
1654 # Introduce a new file in the update
1655 test_commit fifth path3 &&
1657 # (3) the working tree has an untracked file that would interfere
1659 cd testrepo &&
1660 git reset --hard &&
1661 echo changed >path3
1662 ) &&
1663 test_must_fail git push testrepo main &&
1665 cd testrepo &&
1666 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1667 git diff --quiet &&
1668 git diff --quiet --cached &&
1669 test changed = "$(cat path3)"
1670 ) &&
1672 # (4) the target changes to what gets pushed but it still is a change
1674 cd testrepo &&
1675 git reset --hard &&
1676 echo fifth >path3 &&
1677 git add path3
1678 ) &&
1679 test_must_fail git push testrepo main &&
1681 cd testrepo &&
1682 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1683 git diff --quiet &&
1684 test fifth = "$(cat path3)"
1685 ) &&
1687 # (5) push into void
1688 test_when_finished "rm -rf void" &&
1689 git init void &&
1691 cd void &&
1692 git config receive.denyCurrentBranch updateInstead
1693 ) &&
1694 git push void main &&
1696 cd void &&
1697 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1698 git diff --quiet &&
1699 git diff --cached --quiet
1700 ) &&
1702 # (6) updateInstead intervened by fast-forward check
1703 test_must_fail git push void main^:main &&
1704 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1705 git -C void diff --quiet &&
1706 git -C void diff --cached --quiet
1709 test_expect_success 'updateInstead with push-to-checkout hook' '
1710 test_when_finished "rm -rf testrepo" &&
1711 git init testrepo &&
1712 git -C testrepo pull .. main &&
1713 git -C testrepo reset --hard HEAD^^ &&
1714 git -C testrepo tag initial &&
1715 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1716 test_hook -C testrepo push-to-checkout <<-\EOF &&
1717 echo >&2 updating from $(git rev-parse HEAD)
1718 echo >&2 updating to "$1"
1720 git update-index -q --refresh &&
1721 git read-tree -u -m HEAD "$1" || {
1722 status=$?
1723 echo >&2 read-tree failed
1724 exit $status
1728 # Try pushing into a pristine
1729 git push testrepo main &&
1731 cd testrepo &&
1732 git diff --quiet &&
1733 git diff HEAD --quiet &&
1734 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1735 ) &&
1737 # Try pushing into a repository with conflicting change
1739 cd testrepo &&
1740 git reset --hard initial &&
1741 echo conflicting >path2
1742 ) &&
1743 test_must_fail git push testrepo main &&
1745 cd testrepo &&
1746 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1747 test conflicting = "$(cat path2)" &&
1748 git diff-index --quiet --cached HEAD
1749 ) &&
1751 # Try pushing into a repository with unrelated change
1753 cd testrepo &&
1754 git reset --hard initial &&
1755 echo unrelated >path1 &&
1756 echo irrelevant >path5 &&
1757 git add path5
1758 ) &&
1759 git push testrepo main &&
1761 cd testrepo &&
1762 test "$(cat path1)" = unrelated &&
1763 test "$(cat path5)" = irrelevant &&
1764 test "$(git diff --name-only --cached HEAD)" = path5 &&
1765 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1766 ) &&
1768 # push into void
1769 test_when_finished "rm -rf void" &&
1770 git init void &&
1771 git -C void config receive.denyCurrentBranch updateInstead &&
1772 test_hook -C void push-to-checkout <<-\EOF &&
1773 if git rev-parse --quiet --verify HEAD
1774 then
1775 has_head=yes
1776 echo >&2 updating from $(git rev-parse HEAD)
1777 else
1778 has_head=no
1779 echo >&2 pushing into void
1781 echo >&2 updating to "$1"
1783 git update-index -q --refresh &&
1784 case "$has_head" in
1785 yes)
1786 git read-tree -u -m HEAD "$1" ;;
1788 git read-tree -u -m "$1" ;;
1789 esac || {
1790 status=$?
1791 echo >&2 read-tree failed
1792 exit $status
1796 git push void main &&
1798 cd void &&
1799 git diff --quiet &&
1800 git diff --cached --quiet &&
1801 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1805 test_expect_success 'denyCurrentBranch and worktrees' '
1806 git worktree add new-wt &&
1807 git clone . cloned &&
1808 test_commit -C cloned first &&
1809 test_config receive.denyCurrentBranch refuse &&
1810 test_must_fail git -C cloned push origin HEAD:new-wt &&
1811 test_config receive.denyCurrentBranch updateInstead &&
1812 git -C cloned push origin HEAD:new-wt &&
1813 test_path_exists new-wt/first.t &&
1814 test_must_fail git -C cloned push --delete origin new-wt
1817 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1818 test_when_finished "rm -fr bare.git" &&
1819 git clone --bare . bare.git &&
1820 git -C bare.git worktree add wt &&
1821 test_commit grape &&
1822 git -C bare.git config receive.denyCurrentBranch refuse &&
1823 test_must_fail git push bare.git HEAD:wt &&
1824 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1825 git push bare.git HEAD:wt &&
1826 test_path_exists bare.git/wt/grape.t &&
1827 test_must_fail git push --delete bare.git wt
1830 test_expect_success 'refuse fetch to current branch of worktree' '
1831 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1832 git worktree add wt &&
1833 test_commit apple &&
1834 test_must_fail git fetch . HEAD:wt &&
1835 git fetch -u . HEAD:wt
1838 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1839 test_when_finished "rm -fr bare.git" &&
1840 git clone --bare . bare.git &&
1841 git -C bare.git worktree add wt &&
1842 test_commit banana &&
1843 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1844 git -C bare.git fetch -u .. HEAD:wt
1847 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1848 mk_empty testrepo &&
1849 git -C testrepo config receive.hiderefs refs/hidden &&
1850 git -C testrepo config receive.unpackLimit 1 &&
1851 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1852 test_dir_is_empty testrepo/.git/objects/pack
1855 test_expect_success LIBCURL 'fetch warns or fails when using username:password' '
1856 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1857 test_must_fail git -c transfer.credentialsInUrl=allow fetch https://username:password@localhost 2>err &&
1858 ! grep "$message" err &&
1860 test_must_fail git -c transfer.credentialsInUrl=warn fetch https://username:password@localhost 2>err &&
1861 grep "warning: $message" err >warnings &&
1862 test_line_count = 3 warnings &&
1864 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:password@localhost 2>err &&
1865 grep "fatal: $message" err >warnings &&
1866 test_line_count = 1 warnings &&
1868 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:@localhost 2>err &&
1869 grep "fatal: $message" err >warnings &&
1870 test_line_count = 1 warnings
1874 test_expect_success LIBCURL 'push warns or fails when using username:password' '
1875 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1876 test_must_fail git -c transfer.credentialsInUrl=allow push https://username:password@localhost 2>err &&
1877 ! grep "$message" err &&
1879 test_must_fail git -c transfer.credentialsInUrl=warn push https://username:password@localhost 2>err &&
1880 grep "warning: $message" err >warnings &&
1881 test_must_fail git -c transfer.credentialsInUrl=die push https://username:password@localhost 2>err &&
1882 grep "fatal: $message" err >warnings &&
1883 test_line_count = 1 warnings
1886 test_expect_success 'push with config push.useBitmaps' '
1887 mk_test testrepo heads/main &&
1888 git checkout main &&
1889 test_unconfig push.useBitmaps &&
1890 GIT_TRACE2_EVENT="$PWD/default" \
1891 git push testrepo main:test &&
1892 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1893 --thin --delta-base-offset -q <default &&
1895 test_config push.useBitmaps true &&
1896 GIT_TRACE2_EVENT="$PWD/true" \
1897 git push testrepo main:test2 &&
1898 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1899 --thin --delta-base-offset -q <true &&
1901 test_config push.useBitmaps false &&
1902 GIT_TRACE2_EVENT="$PWD/false" \
1903 git push testrepo main:test3 &&
1904 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1905 --thin --delta-base-offset -q --no-use-bitmap-index <false
1908 test_done