Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t5516-fetch-push.sh
blob541adbb31034f11820c77972719258ef49131353
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-lib.sh
23 D=$(pwd)
25 mk_empty () {
26 repo_name="$1"
27 test_when_finished "rm -rf \"$repo_name\"" &&
28 test_path_is_missing "$repo_name" &&
29 git init "$repo_name" &&
30 git -C "$repo_name" config receive.denyCurrentBranch warn
33 mk_test () {
34 repo_name="$1"
35 shift
37 mk_empty "$repo_name" &&
39 for ref in "$@"
41 git push "$repo_name" $the_first_commit:refs/$ref ||
42 exit
43 done &&
44 cd "$repo_name" &&
45 for ref in "$@"
47 echo "$the_first_commit" >expect &&
48 git show-ref -s --verify refs/$ref >actual &&
49 test_cmp expect actual ||
50 exit
51 done &&
52 git fsck --full
56 mk_test_with_hooks() {
57 repo_name=$1
58 mk_test "$@" &&
59 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
60 cat - >>pre-receive.actual
61 EOF
63 test_hook -C "$repo_name" update <<-'EOF' &&
64 printf "%s %s %s\n" "$@" >>update.actual
65 EOF
67 test_hook -C "$repo_name" post-receive <<-'EOF' &&
68 cat - >>post-receive.actual
69 EOF
71 test_hook -C "$repo_name" post-update <<-'EOF'
72 for ref in "$@"
74 printf "%s\n" "$ref" >>post-update.actual
75 done
76 EOF
79 mk_child() {
80 test_when_finished "rm -rf \"$2\"" &&
81 git clone "$1" "$2"
84 check_push_result () {
85 test $# -ge 3 ||
86 BUG "check_push_result requires at least 3 parameters"
88 repo_name="$1"
89 shift
92 cd "$repo_name" &&
93 echo "$1" >expect &&
94 shift &&
95 for ref in "$@"
97 git show-ref -s --verify refs/$ref >actual &&
98 test_cmp expect actual ||
99 exit
100 done &&
101 git fsck --full
105 test_expect_success setup '
107 >path1 &&
108 git add path1 &&
109 test_tick &&
110 git commit -a -m repo &&
111 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
113 >path2 &&
114 git add path2 &&
115 test_tick &&
116 git commit -a -m second &&
117 the_commit=$(git show-ref -s --verify refs/heads/main)
121 test_expect_success 'fetch without wildcard' '
122 mk_empty testrepo &&
124 cd testrepo &&
125 git fetch .. refs/heads/main:refs/remotes/origin/main &&
127 echo "$the_commit commit refs/remotes/origin/main" >expect &&
128 git for-each-ref refs/remotes/origin >actual &&
129 test_cmp expect actual
133 test_expect_success 'fetch with wildcard' '
134 mk_empty testrepo &&
136 cd testrepo &&
137 git config remote.up.url .. &&
138 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
139 git fetch up &&
141 echo "$the_commit commit refs/remotes/origin/main" >expect &&
142 git for-each-ref refs/remotes/origin >actual &&
143 test_cmp expect actual
147 test_expect_success 'fetch with insteadOf' '
148 mk_empty testrepo &&
150 TRASH=$(pwd)/ &&
151 cd testrepo &&
152 git config "url.$TRASH.insteadOf" trash/ &&
153 git config remote.up.url trash/. &&
154 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
155 git fetch up &&
157 echo "$the_commit commit refs/remotes/origin/main" >expect &&
158 git for-each-ref refs/remotes/origin >actual &&
159 test_cmp expect actual
163 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
164 mk_empty testrepo &&
166 TRASH=$(pwd)/ &&
167 cd testrepo &&
168 git config "url.trash/.pushInsteadOf" "$TRASH" &&
169 git config remote.up.url "$TRASH." &&
170 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
171 git fetch up &&
173 echo "$the_commit commit refs/remotes/origin/main" >expect &&
174 git for-each-ref refs/remotes/origin >actual &&
175 test_cmp expect actual
179 grep_wrote () {
180 object_count=$1
181 file_name=$2
182 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
185 test_expect_success 'push without negotiation' '
186 mk_empty testrepo &&
187 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
188 test_commit -C testrepo unrelated_commit &&
189 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
190 test_when_finished "rm event" &&
191 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
192 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
195 test_expect_success 'push with negotiation' '
196 mk_empty testrepo &&
197 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
198 test_commit -C testrepo unrelated_commit &&
199 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
200 test_when_finished "rm event" &&
201 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
202 grep_wrote 2 event # 1 commit, 1 tree
205 test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
206 mk_empty testrepo &&
207 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
208 test_commit -C testrepo unrelated_commit &&
209 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
210 test_when_finished "rm event" &&
211 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
212 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
213 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
214 test_i18ngrep "push negotiation failed" err
217 test_expect_success 'push with negotiation does not attempt to fetch submodules' '
218 mk_empty submodule_upstream &&
219 test_commit -C submodule_upstream submodule_commit &&
220 git submodule add ./submodule_upstream submodule &&
221 mk_empty testrepo &&
222 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
223 test_commit -C testrepo unrelated_commit &&
224 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
225 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
226 ! grep "Fetching submodule" err
229 test_expect_success 'push without wildcard' '
230 mk_empty testrepo &&
232 git push testrepo refs/heads/main:refs/remotes/origin/main &&
234 cd testrepo &&
235 echo "$the_commit commit refs/remotes/origin/main" >expect &&
236 git for-each-ref refs/remotes/origin >actual &&
237 test_cmp expect actual
241 test_expect_success 'push with wildcard' '
242 mk_empty testrepo &&
244 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
246 cd testrepo &&
247 echo "$the_commit commit refs/remotes/origin/main" >expect &&
248 git for-each-ref refs/remotes/origin >actual &&
249 test_cmp expect actual
253 test_expect_success 'push with insteadOf' '
254 mk_empty testrepo &&
255 TRASH="$(pwd)/" &&
256 test_config "url.$TRASH.insteadOf" trash/ &&
257 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
259 cd testrepo &&
260 echo "$the_commit commit refs/remotes/origin/main" >expect &&
261 git for-each-ref refs/remotes/origin >actual &&
262 test_cmp expect actual
266 test_expect_success 'push with pushInsteadOf' '
267 mk_empty testrepo &&
268 TRASH="$(pwd)/" &&
269 test_config "url.$TRASH.pushInsteadOf" trash/ &&
270 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
272 cd testrepo &&
273 echo "$the_commit commit refs/remotes/origin/main" >expect &&
274 git for-each-ref refs/remotes/origin >actual &&
275 test_cmp expect actual
279 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
280 mk_empty testrepo &&
281 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
282 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
283 test_config remote.r.url trash/wrong &&
284 test_config remote.r.pushurl "testrepo/" &&
285 git push r refs/heads/main:refs/remotes/origin/main &&
287 cd testrepo &&
288 echo "$the_commit commit refs/remotes/origin/main" >expect &&
289 git for-each-ref refs/remotes/origin >actual &&
290 test_cmp expect actual
294 test_expect_success 'push with matching heads' '
296 mk_test testrepo heads/main &&
297 git push testrepo : &&
298 check_push_result testrepo $the_commit heads/main
302 test_expect_success 'push with matching heads on the command line' '
304 mk_test testrepo heads/main &&
305 git push testrepo : &&
306 check_push_result testrepo $the_commit heads/main
310 test_expect_success 'failed (non-fast-forward) push with matching heads' '
312 mk_test testrepo heads/main &&
313 git push testrepo : &&
314 git commit --amend -massaged &&
315 test_must_fail git push testrepo &&
316 check_push_result testrepo $the_commit heads/main &&
317 git reset --hard $the_commit
321 test_expect_success 'push --force with matching heads' '
323 mk_test testrepo heads/main &&
324 git push testrepo : &&
325 git commit --amend -massaged &&
326 git push --force testrepo : &&
327 ! check_push_result testrepo $the_commit heads/main &&
328 git reset --hard $the_commit
332 test_expect_success 'push with matching heads and forced update' '
334 mk_test testrepo heads/main &&
335 git push testrepo : &&
336 git commit --amend -massaged &&
337 git push testrepo +: &&
338 ! check_push_result testrepo $the_commit heads/main &&
339 git reset --hard $the_commit
343 test_expect_success 'push with no ambiguity (1)' '
345 mk_test testrepo heads/main &&
346 git push testrepo main:main &&
347 check_push_result testrepo $the_commit heads/main
351 test_expect_success 'push with no ambiguity (2)' '
353 mk_test testrepo remotes/origin/main &&
354 git push testrepo main:origin/main &&
355 check_push_result testrepo $the_commit remotes/origin/main
359 test_expect_success 'push with colon-less refspec, no ambiguity' '
361 mk_test testrepo heads/main heads/t/main &&
362 git branch -f t/main main &&
363 git push testrepo main &&
364 check_push_result testrepo $the_commit heads/main &&
365 check_push_result testrepo $the_first_commit heads/t/main
369 test_expect_success 'push with weak ambiguity (1)' '
371 mk_test testrepo heads/main remotes/origin/main &&
372 git push testrepo main:main &&
373 check_push_result testrepo $the_commit heads/main &&
374 check_push_result testrepo $the_first_commit remotes/origin/main
378 test_expect_success 'push with weak ambiguity (2)' '
380 mk_test testrepo heads/main remotes/origin/main remotes/another/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 remotes/another/main
387 test_expect_success 'push with ambiguity' '
389 mk_test testrepo heads/frotz tags/frotz &&
390 test_must_fail git push testrepo main:frotz &&
391 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
395 test_expect_success 'push with colon-less refspec (1)' '
397 mk_test testrepo heads/frotz tags/frotz &&
398 git branch -f frotz main &&
399 git push testrepo frotz &&
400 check_push_result testrepo $the_commit heads/frotz &&
401 check_push_result testrepo $the_first_commit tags/frotz
405 test_expect_success 'push with colon-less refspec (2)' '
407 mk_test testrepo heads/frotz tags/frotz &&
408 if git show-ref --verify -q refs/heads/frotz
409 then
410 git branch -D frotz
411 fi &&
412 git tag -f frotz &&
413 git push -f testrepo frotz &&
414 check_push_result testrepo $the_commit tags/frotz &&
415 check_push_result testrepo $the_first_commit heads/frotz
419 test_expect_success 'push with colon-less refspec (3)' '
421 mk_test testrepo &&
422 if git show-ref --verify -q refs/tags/frotz
423 then
424 git tag -d frotz
425 fi &&
426 git branch -f frotz main &&
427 git push testrepo frotz &&
428 check_push_result testrepo $the_commit heads/frotz &&
429 test 1 = $( cd testrepo && git show-ref | wc -l )
432 test_expect_success 'push with colon-less refspec (4)' '
434 mk_test testrepo &&
435 if git show-ref --verify -q refs/heads/frotz
436 then
437 git branch -D frotz
438 fi &&
439 git tag -f frotz &&
440 git push testrepo frotz &&
441 check_push_result testrepo $the_commit tags/frotz &&
442 test 1 = $( cd testrepo && git show-ref | wc -l )
446 test_expect_success 'push head with non-existent, incomplete dest' '
448 mk_test testrepo &&
449 git push testrepo main:branch &&
450 check_push_result testrepo $the_commit heads/branch
454 test_expect_success 'push tag with non-existent, incomplete dest' '
456 mk_test testrepo &&
457 git tag -f v1.0 &&
458 git push testrepo v1.0:tag &&
459 check_push_result testrepo $the_commit tags/tag
463 test_expect_success 'push sha1 with non-existent, incomplete dest' '
465 mk_test testrepo &&
466 test_must_fail git push testrepo $(git rev-parse main):foo
470 test_expect_success 'push ref expression with non-existent, incomplete dest' '
472 mk_test testrepo &&
473 test_must_fail git push testrepo main^:branch
477 for head in HEAD @
480 test_expect_success "push with $head" '
481 mk_test testrepo heads/main &&
482 git checkout main &&
483 git push testrepo $head &&
484 check_push_result testrepo $the_commit heads/main
487 test_expect_success "push with $head nonexisting at remote" '
488 mk_test testrepo heads/main &&
489 git checkout -b local main &&
490 test_when_finished "git checkout main; git branch -D local" &&
491 git push testrepo $head &&
492 check_push_result testrepo $the_commit heads/local
495 test_expect_success "push with +$head" '
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 main local &&
500 check_push_result testrepo $the_commit heads/main &&
501 check_push_result testrepo $the_commit heads/local &&
503 # Without force rewinding should fail
504 git reset --hard $head^ &&
505 test_must_fail git push testrepo $head &&
506 check_push_result testrepo $the_commit heads/local &&
508 # With force rewinding should succeed
509 git push testrepo +$head &&
510 check_push_result testrepo $the_first_commit heads/local
513 test_expect_success "push $head with non-existent, incomplete dest" '
514 mk_test testrepo &&
515 git checkout main &&
516 git push testrepo $head:branch &&
517 check_push_result testrepo $the_commit heads/branch
521 test_expect_success "push with config remote.*.push = $head" '
522 mk_test testrepo heads/local &&
523 git checkout main &&
524 git branch -f local $the_commit &&
525 test_when_finished "git branch -D local" &&
527 cd testrepo &&
528 git checkout local &&
529 git reset --hard $the_first_commit
530 ) &&
531 test_config remote.there.url testrepo &&
532 test_config remote.there.push $head &&
533 test_config branch.main.remote there &&
534 git push &&
535 check_push_result testrepo $the_commit heads/main &&
536 check_push_result testrepo $the_first_commit heads/local
539 done
541 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
542 mk_test testrepo heads/main &&
543 git checkout $the_first_commit &&
544 test_config remote.there.url testrepo &&
545 test_config remote.there.push refs/heads/main:refs/heads/main &&
546 git push there &&
547 check_push_result testrepo $the_commit heads/main
550 test_expect_success 'push with remote.pushdefault' '
551 mk_test up_repo heads/main &&
552 mk_test down_repo heads/main &&
553 test_config remote.up.url up_repo &&
554 test_config remote.down.url down_repo &&
555 test_config branch.main.remote up &&
556 test_config remote.pushdefault down &&
557 test_config push.default matching &&
558 git push &&
559 check_push_result up_repo $the_first_commit heads/main &&
560 check_push_result down_repo $the_commit heads/main
563 test_expect_success 'push with config remote.*.pushurl' '
565 mk_test testrepo heads/main &&
566 git checkout main &&
567 test_config remote.there.url test2repo &&
568 test_config remote.there.pushurl testrepo &&
569 git push there : &&
570 check_push_result testrepo $the_commit heads/main
573 test_expect_success 'push with config branch.*.pushremote' '
574 mk_test up_repo heads/main &&
575 mk_test side_repo heads/main &&
576 mk_test down_repo heads/main &&
577 test_config remote.up.url up_repo &&
578 test_config remote.pushdefault side_repo &&
579 test_config remote.down.url down_repo &&
580 test_config branch.main.remote up &&
581 test_config branch.main.pushremote down &&
582 test_config push.default matching &&
583 git push &&
584 check_push_result up_repo $the_first_commit heads/main &&
585 check_push_result side_repo $the_first_commit heads/main &&
586 check_push_result down_repo $the_commit heads/main
589 test_expect_success 'branch.*.pushremote config order is irrelevant' '
590 mk_test one_repo heads/main &&
591 mk_test two_repo heads/main &&
592 test_config remote.one.url one_repo &&
593 test_config remote.two.url two_repo &&
594 test_config branch.main.pushremote two_repo &&
595 test_config remote.pushdefault one_repo &&
596 test_config push.default matching &&
597 git push &&
598 check_push_result one_repo $the_first_commit heads/main &&
599 check_push_result two_repo $the_commit heads/main
602 test_expect_success 'push rejects empty branch name entries' '
603 mk_test one_repo heads/main &&
604 test_config remote.one.url one_repo &&
605 test_config branch..remote one &&
606 test_config branch..merge refs/heads/ &&
607 test_config branch.main.remote one &&
608 test_config branch.main.merge refs/heads/main &&
609 test_must_fail git push 2>err &&
610 grep "bad config variable .branch\.\." err
613 test_expect_success 'push ignores "branch." config without subsection' '
614 mk_test one_repo heads/main &&
615 test_config remote.one.url one_repo &&
616 test_config branch.autoSetupMerge true &&
617 test_config branch.main.remote one &&
618 test_config branch.main.merge refs/heads/main &&
619 git push
622 test_expect_success 'push with dry-run' '
624 mk_test testrepo heads/main &&
625 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
626 git push --dry-run testrepo : &&
627 check_push_result testrepo $old_commit heads/main
630 test_expect_success 'push updates local refs' '
632 mk_test testrepo heads/main &&
633 mk_child testrepo child &&
635 cd child &&
636 git pull .. main &&
637 git push &&
638 test $(git rev-parse main) = \
639 $(git rev-parse remotes/origin/main)
644 test_expect_success 'push updates up-to-date local refs' '
646 mk_test testrepo heads/main &&
647 mk_child testrepo child1 &&
648 mk_child testrepo child2 &&
649 (cd child1 && git pull .. main && git push) &&
651 cd child2 &&
652 git pull ../child1 main &&
653 git push &&
654 test $(git rev-parse main) = \
655 $(git rev-parse remotes/origin/main)
660 test_expect_success 'push preserves up-to-date packed refs' '
662 mk_test testrepo heads/main &&
663 mk_child testrepo child &&
665 cd child &&
666 git push &&
667 ! test -f .git/refs/remotes/origin/main
672 test_expect_success 'push does not update local refs on failure' '
674 mk_test testrepo heads/main &&
675 mk_child testrepo child &&
676 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
677 chmod +x testrepo/.git/hooks/pre-receive &&
679 cd child &&
680 git pull .. main &&
681 test_must_fail git push &&
682 test $(git rev-parse main) != \
683 $(git rev-parse remotes/origin/main)
688 test_expect_success 'allow deleting an invalid remote ref' '
690 mk_test testrepo heads/branch &&
691 rm -f testrepo/.git/objects/??/* &&
692 git push testrepo :refs/heads/branch &&
693 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
697 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
698 mk_test_with_hooks testrepo heads/main heads/next &&
699 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
700 newmain=$(git show-ref -s --verify refs/heads/main) &&
701 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
702 newnext=$ZERO_OID &&
703 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
705 cd testrepo/.git &&
706 cat >pre-receive.expect <<-EOF &&
707 $orgmain $newmain refs/heads/main
708 $orgnext $newnext refs/heads/next
711 cat >update.expect <<-EOF &&
712 refs/heads/main $orgmain $newmain
713 refs/heads/next $orgnext $newnext
716 cat >post-receive.expect <<-EOF &&
717 $orgmain $newmain refs/heads/main
718 $orgnext $newnext refs/heads/next
721 cat >post-update.expect <<-EOF &&
722 refs/heads/main
723 refs/heads/next
726 test_cmp pre-receive.expect pre-receive.actual &&
727 test_cmp update.expect update.actual &&
728 test_cmp post-receive.expect post-receive.actual &&
729 test_cmp post-update.expect post-update.actual
733 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
734 mk_test_with_hooks testrepo heads/branch &&
735 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
736 rm -f testrepo/.git/objects/??/* &&
737 git push testrepo :refs/heads/branch &&
739 cd testrepo/.git &&
740 cat >pre-receive.expect <<-EOF &&
741 $orig $ZERO_OID refs/heads/branch
744 cat >update.expect <<-EOF &&
745 refs/heads/branch $orig $ZERO_OID
748 cat >post-receive.expect <<-EOF &&
749 $orig $ZERO_OID refs/heads/branch
752 cat >post-update.expect <<-EOF &&
753 refs/heads/branch
756 test_cmp pre-receive.expect pre-receive.actual &&
757 test_cmp update.expect update.actual &&
758 test_cmp post-receive.expect post-receive.actual &&
759 test_cmp post-update.expect post-update.actual
763 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
764 mk_test_with_hooks testrepo heads/main &&
765 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
766 newmain=$(git show-ref -s --verify refs/heads/main) &&
767 git push testrepo main :refs/heads/nonexistent &&
769 cd testrepo/.git &&
770 cat >pre-receive.expect <<-EOF &&
771 $orgmain $newmain refs/heads/main
772 $ZERO_OID $ZERO_OID refs/heads/nonexistent
775 cat >update.expect <<-EOF &&
776 refs/heads/main $orgmain $newmain
777 refs/heads/nonexistent $ZERO_OID $ZERO_OID
780 cat >post-receive.expect <<-EOF &&
781 $orgmain $newmain refs/heads/main
784 cat >post-update.expect <<-EOF &&
785 refs/heads/main
788 test_cmp pre-receive.expect pre-receive.actual &&
789 test_cmp update.expect update.actual &&
790 test_cmp post-receive.expect post-receive.actual &&
791 test_cmp post-update.expect post-update.actual
795 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
796 mk_test_with_hooks testrepo heads/main &&
797 git push testrepo :refs/heads/nonexistent &&
799 cd testrepo/.git &&
800 cat >pre-receive.expect <<-EOF &&
801 $ZERO_OID $ZERO_OID refs/heads/nonexistent
804 cat >update.expect <<-EOF &&
805 refs/heads/nonexistent $ZERO_OID $ZERO_OID
808 test_cmp pre-receive.expect pre-receive.actual &&
809 test_cmp update.expect update.actual &&
810 test_path_is_missing post-receive.actual &&
811 test_path_is_missing post-update.actual
815 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
816 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
817 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
818 newmain=$(git show-ref -s --verify refs/heads/main) &&
819 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
820 newnext=$ZERO_OID &&
821 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
822 newseen=$(git show-ref -s --verify refs/heads/main) &&
823 git push testrepo refs/heads/main:refs/heads/main \
824 refs/heads/main:refs/heads/seen :refs/heads/next \
825 :refs/heads/nonexistent &&
827 cd testrepo/.git &&
828 cat >pre-receive.expect <<-EOF &&
829 $orgmain $newmain refs/heads/main
830 $orgnext $newnext refs/heads/next
831 $orgseen $newseen refs/heads/seen
832 $ZERO_OID $ZERO_OID refs/heads/nonexistent
835 cat >update.expect <<-EOF &&
836 refs/heads/main $orgmain $newmain
837 refs/heads/next $orgnext $newnext
838 refs/heads/seen $orgseen $newseen
839 refs/heads/nonexistent $ZERO_OID $ZERO_OID
842 cat >post-receive.expect <<-EOF &&
843 $orgmain $newmain refs/heads/main
844 $orgnext $newnext refs/heads/next
845 $orgseen $newseen refs/heads/seen
848 cat >post-update.expect <<-EOF &&
849 refs/heads/main
850 refs/heads/next
851 refs/heads/seen
854 test_cmp pre-receive.expect pre-receive.actual &&
855 test_cmp update.expect update.actual &&
856 test_cmp post-receive.expect post-receive.actual &&
857 test_cmp post-update.expect post-update.actual
861 test_expect_success 'allow deleting a ref using --delete' '
862 mk_test testrepo heads/main &&
863 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
864 git push testrepo --delete main &&
865 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
868 test_expect_success 'allow deleting a tag using --delete' '
869 mk_test testrepo heads/main &&
870 git tag -a -m dummy_message deltag heads/main &&
871 git push testrepo --tags &&
872 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
873 git push testrepo --delete tag deltag &&
874 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
877 test_expect_success 'push --delete without args aborts' '
878 mk_test testrepo heads/main &&
879 test_must_fail git push testrepo --delete
882 test_expect_success 'push --delete refuses src:dest refspecs' '
883 mk_test testrepo heads/main &&
884 test_must_fail git push testrepo --delete main:foo
887 test_expect_success 'push --delete refuses empty string' '
888 mk_test testrepo heads/master &&
889 test_must_fail git push testrepo --delete ""
892 test_expect_success 'warn on push to HEAD of non-bare repository' '
893 mk_test testrepo heads/main &&
895 cd testrepo &&
896 git checkout main &&
897 git config receive.denyCurrentBranch warn
898 ) &&
899 git push testrepo main 2>stderr &&
900 grep "warning: updating the current branch" stderr
903 test_expect_success 'deny push to HEAD of non-bare repository' '
904 mk_test testrepo heads/main &&
906 cd testrepo &&
907 git checkout main &&
908 git config receive.denyCurrentBranch true
909 ) &&
910 test_must_fail git push testrepo main
913 test_expect_success 'allow push to HEAD of bare repository (bare)' '
914 mk_test testrepo heads/main &&
916 cd testrepo &&
917 git checkout main &&
918 git config receive.denyCurrentBranch true &&
919 git config core.bare true
920 ) &&
921 git push testrepo main 2>stderr &&
922 ! grep "warning: updating the current branch" stderr
925 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
926 mk_test testrepo heads/main &&
928 cd testrepo &&
929 git checkout main &&
930 git config receive.denyCurrentBranch false
931 ) &&
932 git push testrepo main 2>stderr &&
933 ! grep "warning: updating the current branch" stderr
936 test_expect_success 'fetch with branches' '
937 mk_empty testrepo &&
938 git branch second $the_first_commit &&
939 git checkout second &&
940 echo ".." > testrepo/.git/branches/branch1 &&
942 cd testrepo &&
943 git fetch branch1 &&
944 echo "$the_commit commit refs/heads/branch1" >expect &&
945 git for-each-ref refs/heads >actual &&
946 test_cmp expect actual
947 ) &&
948 git checkout main
951 test_expect_success 'fetch with branches containing #' '
952 mk_empty testrepo &&
953 echo "..#second" > testrepo/.git/branches/branch2 &&
955 cd testrepo &&
956 git fetch branch2 &&
957 echo "$the_first_commit commit refs/heads/branch2" >expect &&
958 git for-each-ref refs/heads >actual &&
959 test_cmp expect actual
960 ) &&
961 git checkout main
964 test_expect_success 'push with branches' '
965 mk_empty testrepo &&
966 git checkout second &&
967 echo "testrepo" > .git/branches/branch1 &&
968 git push branch1 &&
970 cd testrepo &&
971 echo "$the_first_commit commit refs/heads/main" >expect &&
972 git for-each-ref refs/heads >actual &&
973 test_cmp expect actual
977 test_expect_success 'push with branches containing #' '
978 mk_empty testrepo &&
979 echo "testrepo#branch3" > .git/branches/branch2 &&
980 git push branch2 &&
982 cd testrepo &&
983 echo "$the_first_commit commit refs/heads/branch3" >expect &&
984 git for-each-ref refs/heads >actual &&
985 test_cmp expect actual
986 ) &&
987 git checkout main
990 test_expect_success 'push into aliased refs (consistent)' '
991 mk_test testrepo heads/main &&
992 mk_child testrepo child1 &&
993 mk_child testrepo child2 &&
995 cd child1 &&
996 git branch foo &&
997 git symbolic-ref refs/heads/bar refs/heads/foo &&
998 git config receive.denyCurrentBranch false
999 ) &&
1001 cd child2 &&
1002 >path2 &&
1003 git add path2 &&
1004 test_tick &&
1005 git commit -a -m child2 &&
1006 git branch foo &&
1007 git branch bar &&
1008 git push ../child1 foo bar
1012 test_expect_success 'push into aliased refs (inconsistent)' '
1013 mk_test testrepo heads/main &&
1014 mk_child testrepo child1 &&
1015 mk_child testrepo child2 &&
1017 cd child1 &&
1018 git branch foo &&
1019 git symbolic-ref refs/heads/bar refs/heads/foo &&
1020 git config receive.denyCurrentBranch false
1021 ) &&
1023 cd child2 &&
1024 >path2 &&
1025 git add path2 &&
1026 test_tick &&
1027 git commit -a -m child2 &&
1028 git branch foo &&
1029 >path3 &&
1030 git add path3 &&
1031 test_tick &&
1032 git commit -a -m child2 &&
1033 git branch bar &&
1034 test_must_fail git push ../child1 foo bar 2>stderr &&
1035 grep "refusing inconsistent update" stderr
1039 test_force_push_tag () {
1040 tag_type_description=$1
1041 tag_args=$2
1043 test_expect_success "force pushing required to update $tag_type_description" "
1044 mk_test testrepo heads/main &&
1045 mk_child testrepo child1 &&
1046 mk_child testrepo child2 &&
1048 cd child1 &&
1049 git tag testTag &&
1050 git push ../child2 testTag &&
1051 >file1 &&
1052 git add file1 &&
1053 git commit -m 'file1' &&
1054 git tag $tag_args testTag &&
1055 test_must_fail git push ../child2 testTag &&
1056 git push --force ../child2 testTag &&
1057 git tag $tag_args testTag HEAD~ &&
1058 test_must_fail git push ../child2 testTag &&
1059 git push --force ../child2 testTag &&
1061 # Clobbering without + in refspec needs --force
1062 git tag -f testTag &&
1063 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1064 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1066 # Clobbering with + in refspec does not need --force
1067 git tag -f testTag HEAD~ &&
1068 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1070 # Clobbering with --no-force still obeys + in refspec
1071 git tag -f testTag &&
1072 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1074 # Clobbering with/without --force and 'tag <name>' format
1075 git tag -f testTag HEAD~ &&
1076 test_must_fail git push ../child2 tag testTag &&
1077 git push --force ../child2 tag testTag
1082 test_force_push_tag "lightweight tag" "-f"
1083 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1085 test_force_fetch_tag () {
1086 tag_type_description=$1
1087 tag_args=$2
1089 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1090 mk_test testrepo heads/main &&
1091 mk_child testrepo child1 &&
1092 mk_child testrepo child2 &&
1094 cd testrepo &&
1095 git tag testTag &&
1096 git -C ../child1 fetch origin tag testTag &&
1097 >file1 &&
1098 git add file1 &&
1099 git commit -m 'file1' &&
1100 git tag $tag_args testTag &&
1101 test_must_fail git -C ../child1 fetch origin tag testTag &&
1102 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1107 test_force_fetch_tag "lightweight tag" "-f"
1108 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1110 test_expect_success 'push --porcelain' '
1111 mk_empty testrepo &&
1112 echo >.git/foo "To testrepo" &&
1113 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1114 echo >>.git/foo "Done" &&
1115 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1117 cd testrepo &&
1118 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1119 git for-each-ref refs/remotes/origin >actual &&
1120 test_cmp expect actual
1121 ) &&
1122 test_cmp .git/foo .git/bar
1125 test_expect_success 'push --porcelain bad url' '
1126 mk_empty testrepo &&
1127 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1128 ! grep -q Done .git/bar
1131 test_expect_success 'push --porcelain rejected' '
1132 mk_empty testrepo &&
1133 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1134 (cd testrepo &&
1135 git reset --hard origin/main^ &&
1136 git config receive.denyCurrentBranch true) &&
1138 echo >.git/foo "To testrepo" &&
1139 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1140 echo >>.git/foo "Done" &&
1142 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1143 test_cmp .git/foo .git/bar
1146 test_expect_success 'push --porcelain --dry-run rejected' '
1147 mk_empty testrepo &&
1148 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1149 (cd testrepo &&
1150 git reset --hard origin/main &&
1151 git config receive.denyCurrentBranch true) &&
1153 echo >.git/foo "To testrepo" &&
1154 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1155 echo >>.git/foo "Done" &&
1157 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1158 test_cmp .git/foo .git/bar
1161 test_expect_success 'push --prune' '
1162 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1163 git push --prune testrepo : &&
1164 check_push_result testrepo $the_commit heads/main &&
1165 check_push_result testrepo $the_first_commit heads/second &&
1166 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1169 test_expect_success 'push --prune refspec' '
1170 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1171 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1172 check_push_result testrepo $the_commit tmp/main &&
1173 check_push_result testrepo $the_first_commit tmp/second &&
1174 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1177 for configsection in transfer receive
1179 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1180 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1182 cd testrepo &&
1183 git config $configsection.hiderefs refs/hidden
1184 ) &&
1186 # push to unhidden ref succeeds normally
1187 git push testrepo main:refs/heads/main &&
1188 check_push_result testrepo $the_commit heads/main &&
1190 # push to update a hidden ref should fail
1191 test_must_fail git push testrepo main:refs/hidden/one &&
1192 check_push_result testrepo $the_first_commit hidden/one &&
1194 # push to delete a hidden ref should fail
1195 test_must_fail git push testrepo :refs/hidden/two &&
1196 check_push_result testrepo $the_first_commit hidden/two &&
1198 # idempotent push to update a hidden ref should fail
1199 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1200 check_push_result testrepo $the_first_commit hidden/three
1202 done
1204 test_expect_success 'fetch exact SHA1' '
1205 mk_test testrepo heads/main hidden/one &&
1206 git push testrepo main:refs/hidden/one &&
1208 cd testrepo &&
1209 git config transfer.hiderefs refs/hidden
1210 ) &&
1211 check_push_result testrepo $the_commit hidden/one &&
1213 mk_child testrepo child &&
1215 cd child &&
1217 # make sure $the_commit does not exist here
1218 git repack -a -d &&
1219 git prune &&
1220 test_must_fail git cat-file -t $the_commit &&
1222 # Some protocol versions (e.g. 2) support fetching
1223 # unadvertised objects, so restrict this test to v0.
1225 # fetching the hidden object should fail by default
1226 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1227 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1228 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1229 test_must_fail git rev-parse --verify refs/heads/copy &&
1231 # the server side can allow it to succeed
1233 cd ../testrepo &&
1234 git config uploadpack.allowtipsha1inwant true
1235 ) &&
1237 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1238 cat >expect <<-EOF &&
1239 $the_commit
1240 $the_first_commit
1243 git rev-parse --verify refs/heads/copy &&
1244 git rev-parse --verify refs/heads/extra
1245 } >actual &&
1246 test_cmp expect actual
1250 test_expect_success 'fetch exact SHA1 in protocol v2' '
1251 mk_test testrepo heads/main hidden/one &&
1252 git push testrepo main:refs/hidden/one &&
1253 git -C testrepo config transfer.hiderefs refs/hidden &&
1254 check_push_result testrepo $the_commit hidden/one &&
1256 mk_child testrepo child &&
1257 git -C child config protocol.version 2 &&
1259 # make sure $the_commit does not exist here
1260 git -C child repack -a -d &&
1261 git -C child prune &&
1262 test_must_fail git -C child cat-file -t $the_commit &&
1264 # fetching the hidden object succeeds by default
1265 # NEEDSWORK: should this match the v0 behavior instead?
1266 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1269 for configallowtipsha1inwant in true false
1271 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1272 mk_empty testrepo &&
1274 cd testrepo &&
1275 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1276 git commit --allow-empty -m foo &&
1277 git commit --allow-empty -m bar
1278 ) &&
1279 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1280 mk_empty shallow &&
1282 cd shallow &&
1283 # Some protocol versions (e.g. 2) support fetching
1284 # unadvertised objects, so restrict this test to v0.
1285 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1286 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1287 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1288 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1289 git cat-file commit $SHA1
1293 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1294 mk_empty testrepo &&
1296 cd testrepo &&
1297 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1298 git commit --allow-empty -m foo &&
1299 git commit --allow-empty -m bar &&
1300 git commit --allow-empty -m xyz
1301 ) &&
1302 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1303 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1304 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1306 cd testrepo &&
1307 git reset --hard $SHA1_2 &&
1308 git cat-file commit $SHA1_1 &&
1309 git cat-file commit $SHA1_3
1310 ) &&
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 ../testrepo/.git $SHA1_3 &&
1318 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1319 git fetch ../testrepo/.git $SHA1_1 &&
1320 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1321 git fetch ../testrepo/.git $SHA1_1 &&
1322 git cat-file commit $SHA1_1 &&
1323 test_must_fail git cat-file commit $SHA1_2 &&
1324 git fetch ../testrepo/.git $SHA1_2 &&
1325 git cat-file commit $SHA1_2 &&
1326 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1327 git fetch ../testrepo/.git $SHA1_3 2>err &&
1328 # ideally we would insist this be on a "remote error:"
1329 # line, but it is racy; see the commit message
1330 test_i18ngrep "not our ref.*$SHA1_3\$" err
1333 done
1335 test_expect_success 'fetch follows tags by default' '
1336 mk_test testrepo heads/main &&
1337 test_when_finished "rm -rf src" &&
1338 git init src &&
1340 cd src &&
1341 git pull ../testrepo main &&
1342 git tag -m "annotated" tag &&
1343 git for-each-ref >tmp1 &&
1344 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1345 sort -k 3 >../expect
1346 ) &&
1347 test_when_finished "rm -rf dst" &&
1348 git init dst &&
1350 cd dst &&
1351 git remote add origin ../src &&
1352 git config branch.main.remote origin &&
1353 git config branch.main.merge refs/heads/main &&
1354 git pull &&
1355 git for-each-ref >../actual
1356 ) &&
1357 test_cmp expect actual
1360 test_expect_success 'peeled advertisements are not considered ref tips' '
1361 mk_empty testrepo &&
1362 git -C testrepo commit --allow-empty -m one &&
1363 git -C testrepo commit --allow-empty -m two &&
1364 git -C testrepo tag -m foo mytag HEAD^ &&
1365 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1366 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1367 git fetch testrepo $oid 2>err &&
1368 test_i18ngrep "Server does not allow request for unadvertised object" err
1371 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1372 mk_test testrepo heads/main &&
1373 test_when_finished "rm -rf src" &&
1374 git init src &&
1375 test_when_finished "rm -rf dst" &&
1376 git init --bare dst &&
1378 cd src &&
1379 git pull ../testrepo main &&
1380 git branch next &&
1381 git config remote.dst.url ../dst &&
1382 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1383 git push dst main &&
1384 git show-ref refs/heads/main |
1385 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1386 ) &&
1388 cd dst &&
1389 test_must_fail git show-ref refs/heads/next &&
1390 test_must_fail git show-ref refs/heads/main &&
1391 git show-ref refs/remotes/src/main >actual
1392 ) &&
1393 test_cmp dst/expect dst/actual
1396 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1397 mk_test testrepo heads/main &&
1398 test_when_finished "rm -rf src" &&
1399 git init src &&
1400 test_when_finished "rm -rf dst" &&
1401 git init --bare dst &&
1403 cd src &&
1404 git pull ../testrepo main &&
1405 git branch next &&
1406 git config remote.dst.url ../dst &&
1407 git config push.default matching &&
1408 git push dst main &&
1409 git show-ref refs/heads/main >../dst/expect
1410 ) &&
1412 cd dst &&
1413 test_must_fail git show-ref refs/heads/next &&
1414 git show-ref refs/heads/main >actual
1415 ) &&
1416 test_cmp dst/expect dst/actual
1419 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1420 mk_test testrepo heads/main &&
1421 test_when_finished "rm -rf src" &&
1422 git init src &&
1423 test_when_finished "rm -rf dst" &&
1424 git init --bare dst &&
1426 cd src &&
1427 git pull ../testrepo main &&
1428 git branch next &&
1429 git config remote.dst.url ../dst &&
1430 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1431 git config push.default upstream &&
1433 git config branch.main.merge refs/heads/trunk &&
1434 git config branch.main.remote dst &&
1436 git push dst main &&
1437 git show-ref refs/heads/main |
1438 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1439 ) &&
1441 cd dst &&
1442 test_must_fail git show-ref refs/heads/main &&
1443 test_must_fail git show-ref refs/heads/next &&
1444 git show-ref refs/heads/trunk >actual
1445 ) &&
1446 test_cmp dst/expect dst/actual
1449 test_expect_success 'push does not follow tags by default' '
1450 mk_test testrepo heads/main &&
1451 test_when_finished "rm -rf src" &&
1452 git init src &&
1453 test_when_finished "rm -rf dst" &&
1454 git init --bare dst &&
1456 cd src &&
1457 git pull ../testrepo main &&
1458 git tag -m "annotated" tag &&
1459 git checkout -b another &&
1460 git commit --allow-empty -m "future commit" &&
1461 git tag -m "future" future &&
1462 git checkout main &&
1463 git for-each-ref refs/heads/main >../expect &&
1464 git push ../dst main
1465 ) &&
1467 cd dst &&
1468 git for-each-ref >../actual
1469 ) &&
1470 test_cmp expect actual
1473 test_expect_success 'push --follow-tags only pushes relevant tags' '
1474 mk_test testrepo heads/main &&
1475 test_when_finished "rm -rf src" &&
1476 git init src &&
1477 test_when_finished "rm -rf dst" &&
1478 git init --bare dst &&
1480 cd src &&
1481 git pull ../testrepo main &&
1482 git tag -m "annotated" tag &&
1483 git checkout -b another &&
1484 git commit --allow-empty -m "future commit" &&
1485 git tag -m "future" future &&
1486 git checkout main &&
1487 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1488 git push --follow-tags ../dst main
1489 ) &&
1491 cd dst &&
1492 git for-each-ref >../actual
1493 ) &&
1494 test_cmp expect actual
1497 test_expect_success 'push --no-thin must produce non-thin pack' '
1498 cat >>path1 <<\EOF &&
1499 keep base version of path1 big enough, compared to the new changes
1500 later, in order to pass size heuristics in
1501 builtin/pack-objects.c:try_delta()
1503 git commit -am initial &&
1504 git init no-thin &&
1505 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1506 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1507 echo modified >> path1 &&
1508 git commit -am modified &&
1509 git repack -adf &&
1510 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1511 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1514 test_expect_success 'pushing a tag pushes the tagged object' '
1515 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1516 git tag -m foo tag-of-blob $blob &&
1517 test_when_finished "rm -rf dst.git" &&
1518 git init --bare dst.git &&
1519 git push dst.git tag-of-blob &&
1520 # the receiving index-pack should have noticed
1521 # any problems, but we double check
1522 echo unreferenced >expect &&
1523 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1524 test_cmp expect actual
1527 test_expect_success 'push into bare respects core.logallrefupdates' '
1528 test_when_finished "rm -rf dst.git" &&
1529 git init --bare dst.git &&
1530 git -C dst.git config core.logallrefupdates true &&
1532 # double push to test both with and without
1533 # the actual pack transfer
1534 git push dst.git main:one &&
1535 echo "one@{0} push" >expect &&
1536 git -C dst.git log -g --format="%gd %gs" one >actual &&
1537 test_cmp expect actual &&
1539 git push dst.git main:two &&
1540 echo "two@{0} push" >expect &&
1541 git -C dst.git log -g --format="%gd %gs" two >actual &&
1542 test_cmp expect actual
1545 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1546 test_when_finished "rm -rf dst.git" &&
1547 git init --bare dst.git &&
1549 cd dst.git &&
1550 git config core.logallrefupdates true &&
1552 # as above, we double-fetch to test both
1553 # with and without pack transfer
1554 git fetch .. main:one &&
1555 echo "one@{0} fetch .. main:one: storing head" >expect &&
1556 git log -g --format="%gd %gs" one >actual &&
1557 test_cmp expect actual &&
1559 git fetch .. main:two &&
1560 echo "two@{0} fetch .. main:two: storing head" >expect &&
1561 git log -g --format="%gd %gs" two >actual &&
1562 test_cmp expect actual
1566 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1567 mk_empty testrepo &&
1568 git push testrepo main &&
1570 cd testrepo &&
1571 git reset --hard &&
1572 git config receive.denyCurrentBranch updateInstead
1573 ) &&
1574 test_commit third path2 &&
1576 # Try pushing into a repository with pristine working tree
1577 git push testrepo main &&
1579 cd testrepo &&
1580 git update-index -q --refresh &&
1581 git diff-files --quiet -- &&
1582 git diff-index --quiet --cached HEAD -- &&
1583 test third = "$(cat path2)" &&
1584 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1585 ) &&
1587 # Try pushing into a repository with working tree needing a refresh
1589 cd testrepo &&
1590 git reset --hard HEAD^ &&
1591 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1592 test-tool chmtime +100 path1
1593 ) &&
1594 git push testrepo main &&
1596 cd testrepo &&
1597 git update-index -q --refresh &&
1598 git diff-files --quiet -- &&
1599 git diff-index --quiet --cached HEAD -- &&
1600 test_cmp ../path1 path1 &&
1601 test third = "$(cat path2)" &&
1602 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1603 ) &&
1605 # Update what is to be pushed
1606 test_commit fourth path2 &&
1608 # Try pushing into a repository with a dirty working tree
1609 # (1) the working tree updated
1611 cd testrepo &&
1612 echo changed >path1
1613 ) &&
1614 test_must_fail git push testrepo main &&
1616 cd testrepo &&
1617 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1618 git diff --quiet --cached &&
1619 test changed = "$(cat path1)"
1620 ) &&
1622 # (2) the index updated
1624 cd testrepo &&
1625 echo changed >path1 &&
1626 git add path1
1627 ) &&
1628 test_must_fail git push testrepo main &&
1630 cd testrepo &&
1631 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1632 git diff --quiet &&
1633 test changed = "$(cat path1)"
1634 ) &&
1636 # Introduce a new file in the update
1637 test_commit fifth path3 &&
1639 # (3) the working tree has an untracked file that would interfere
1641 cd testrepo &&
1642 git reset --hard &&
1643 echo changed >path3
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 &&
1650 git diff --quiet --cached &&
1651 test changed = "$(cat path3)"
1652 ) &&
1654 # (4) the target changes to what gets pushed but it still is a change
1656 cd testrepo &&
1657 git reset --hard &&
1658 echo fifth >path3 &&
1659 git add path3
1660 ) &&
1661 test_must_fail git push testrepo main &&
1663 cd testrepo &&
1664 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1665 git diff --quiet &&
1666 test fifth = "$(cat path3)"
1667 ) &&
1669 # (5) push into void
1670 test_when_finished "rm -rf void" &&
1671 git init void &&
1673 cd void &&
1674 git config receive.denyCurrentBranch updateInstead
1675 ) &&
1676 git push void main &&
1678 cd void &&
1679 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1680 git diff --quiet &&
1681 git diff --cached --quiet
1682 ) &&
1684 # (6) updateInstead intervened by fast-forward check
1685 test_must_fail git push void main^:main &&
1686 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1687 git -C void diff --quiet &&
1688 git -C void diff --cached --quiet
1691 test_expect_success 'updateInstead with push-to-checkout hook' '
1692 test_when_finished "rm -rf testrepo" &&
1693 git init testrepo &&
1694 git -C testrepo pull .. main &&
1695 git -C testrepo reset --hard HEAD^^ &&
1696 git -C testrepo tag initial &&
1697 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1698 test_hook -C testrepo push-to-checkout <<-\EOF &&
1699 echo >&2 updating from $(git rev-parse HEAD)
1700 echo >&2 updating to "$1"
1702 git update-index -q --refresh &&
1703 git read-tree -u -m HEAD "$1" || {
1704 status=$?
1705 echo >&2 read-tree failed
1706 exit $status
1710 # Try pushing into a pristine
1711 git push testrepo main &&
1713 cd testrepo &&
1714 git diff --quiet &&
1715 git diff HEAD --quiet &&
1716 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1717 ) &&
1719 # Try pushing into a repository with conflicting change
1721 cd testrepo &&
1722 git reset --hard initial &&
1723 echo conflicting >path2
1724 ) &&
1725 test_must_fail git push testrepo main &&
1727 cd testrepo &&
1728 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1729 test conflicting = "$(cat path2)" &&
1730 git diff-index --quiet --cached HEAD
1731 ) &&
1733 # Try pushing into a repository with unrelated change
1735 cd testrepo &&
1736 git reset --hard initial &&
1737 echo unrelated >path1 &&
1738 echo irrelevant >path5 &&
1739 git add path5
1740 ) &&
1741 git push testrepo main &&
1743 cd testrepo &&
1744 test "$(cat path1)" = unrelated &&
1745 test "$(cat path5)" = irrelevant &&
1746 test "$(git diff --name-only --cached HEAD)" = path5 &&
1747 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1748 ) &&
1750 # push into void
1751 test_when_finished "rm -rf void" &&
1752 git init void &&
1753 git -C void config receive.denyCurrentBranch updateInstead &&
1754 test_hook -C void push-to-checkout <<-\EOF &&
1755 if git rev-parse --quiet --verify HEAD
1756 then
1757 has_head=yes
1758 echo >&2 updating from $(git rev-parse HEAD)
1759 else
1760 has_head=no
1761 echo >&2 pushing into void
1763 echo >&2 updating to "$1"
1765 git update-index -q --refresh &&
1766 case "$has_head" in
1767 yes)
1768 git read-tree -u -m HEAD "$1" ;;
1770 git read-tree -u -m "$1" ;;
1771 esac || {
1772 status=$?
1773 echo >&2 read-tree failed
1774 exit $status
1778 git push void main &&
1780 cd void &&
1781 git diff --quiet &&
1782 git diff --cached --quiet &&
1783 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1787 test_expect_success 'denyCurrentBranch and worktrees' '
1788 git worktree add new-wt &&
1789 git clone . cloned &&
1790 test_commit -C cloned first &&
1791 test_config receive.denyCurrentBranch refuse &&
1792 test_must_fail git -C cloned push origin HEAD:new-wt &&
1793 test_config receive.denyCurrentBranch updateInstead &&
1794 git -C cloned push origin HEAD:new-wt &&
1795 test_path_exists new-wt/first.t &&
1796 test_must_fail git -C cloned push --delete origin new-wt
1799 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1800 test_when_finished "rm -fr bare.git" &&
1801 git clone --bare . bare.git &&
1802 git -C bare.git worktree add wt &&
1803 test_commit grape &&
1804 git -C bare.git config receive.denyCurrentBranch refuse &&
1805 test_must_fail git push bare.git HEAD:wt &&
1806 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1807 git push bare.git HEAD:wt &&
1808 test_path_exists bare.git/wt/grape.t &&
1809 test_must_fail git push --delete bare.git wt
1812 test_expect_success 'refuse fetch to current branch of worktree' '
1813 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1814 git worktree add wt &&
1815 test_commit apple &&
1816 test_must_fail git fetch . HEAD:wt &&
1817 git fetch -u . HEAD:wt
1820 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1821 test_when_finished "rm -fr bare.git" &&
1822 git clone --bare . bare.git &&
1823 git -C bare.git worktree add wt &&
1824 test_commit banana &&
1825 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1826 git -C bare.git fetch -u .. HEAD:wt
1829 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1830 mk_empty testrepo &&
1831 git -C testrepo config receive.hiderefs refs/hidden &&
1832 git -C testrepo config receive.unpackLimit 1 &&
1833 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1834 test_dir_is_empty testrepo/.git/objects/pack
1837 test_expect_success LIBCURL 'fetch warns or fails when using username:password' '
1838 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1839 test_must_fail git -c transfer.credentialsInUrl=allow fetch https://username:password@localhost 2>err &&
1840 ! grep "$message" err &&
1842 test_must_fail git -c transfer.credentialsInUrl=warn fetch https://username:password@localhost 2>err &&
1843 grep "warning: $message" err >warnings &&
1844 test_line_count = 3 warnings &&
1846 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:password@localhost 2>err &&
1847 grep "fatal: $message" err >warnings &&
1848 test_line_count = 1 warnings &&
1850 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:@localhost 2>err &&
1851 grep "fatal: $message" err >warnings &&
1852 test_line_count = 1 warnings
1856 test_expect_success LIBCURL 'push warns or fails when using username:password' '
1857 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1858 test_must_fail git -c transfer.credentialsInUrl=allow push https://username:password@localhost 2>err &&
1859 ! grep "$message" err &&
1861 test_must_fail git -c transfer.credentialsInUrl=warn push https://username:password@localhost 2>err &&
1862 grep "warning: $message" err >warnings &&
1863 test_must_fail git -c transfer.credentialsInUrl=die push https://username:password@localhost 2>err &&
1864 grep "fatal: $message" err >warnings &&
1865 test_line_count = 1 warnings
1868 test_done