rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t5516-fetch-push.sh
blob4dfb080433e0a0bb95a4547c83618a6b3708ee08
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
17 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
18 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20 . ./test-lib.sh
22 D=$(pwd)
24 mk_empty () {
25 repo_name="$1"
26 test_when_finished "rm -rf \"$repo_name\"" &&
27 test_path_is_missing "$repo_name" &&
28 git init "$repo_name" &&
29 git -C "$repo_name" config receive.denyCurrentBranch warn
32 mk_test () {
33 repo_name="$1"
34 shift
36 mk_empty "$repo_name" &&
38 for ref in "$@"
40 git push "$repo_name" $the_first_commit:refs/$ref ||
41 exit
42 done &&
43 cd "$repo_name" &&
44 for ref in "$@"
46 echo "$the_first_commit" >expect &&
47 git show-ref -s --verify refs/$ref >actual &&
48 test_cmp expect actual ||
49 exit
50 done &&
51 git fsck --full
55 mk_test_with_hooks() {
56 repo_name=$1
57 mk_test "$@" &&
58 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
59 cat - >>pre-receive.actual
60 EOF
62 test_hook -C "$repo_name" update <<-'EOF' &&
63 printf "%s %s %s\n" "$@" >>update.actual
64 EOF
66 test_hook -C "$repo_name" post-receive <<-'EOF' &&
67 cat - >>post-receive.actual
68 EOF
70 test_hook -C "$repo_name" post-update <<-'EOF'
71 for ref in "$@"
73 printf "%s\n" "$ref" >>post-update.actual
74 done
75 EOF
78 mk_child() {
79 test_when_finished "rm -rf \"$2\"" &&
80 git clone "$1" "$2"
83 check_push_result () {
84 test $# -ge 3 ||
85 BUG "check_push_result requires at least 3 parameters"
87 repo_name="$1"
88 shift
91 cd "$repo_name" &&
92 echo "$1" >expect &&
93 shift &&
94 for ref in "$@"
96 git show-ref -s --verify refs/$ref >actual &&
97 test_cmp expect actual ||
98 exit
99 done &&
100 git fsck --full
104 test_expect_success setup '
106 >path1 &&
107 git add path1 &&
108 test_tick &&
109 git commit -a -m repo &&
110 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
112 >path2 &&
113 git add path2 &&
114 test_tick &&
115 git commit -a -m second &&
116 the_commit=$(git show-ref -s --verify refs/heads/main)
120 test_expect_success 'fetch without wildcard' '
121 mk_empty testrepo &&
123 cd testrepo &&
124 git fetch .. refs/heads/main:refs/remotes/origin/main &&
126 echo "$the_commit commit refs/remotes/origin/main" >expect &&
127 git for-each-ref refs/remotes/origin >actual &&
128 test_cmp expect actual
132 test_expect_success 'fetch with wildcard' '
133 mk_empty testrepo &&
135 cd testrepo &&
136 git config remote.up.url .. &&
137 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
138 git fetch up &&
140 echo "$the_commit commit refs/remotes/origin/main" >expect &&
141 git for-each-ref refs/remotes/origin >actual &&
142 test_cmp expect actual
146 test_expect_success 'fetch with insteadOf' '
147 mk_empty testrepo &&
149 TRASH=$(pwd)/ &&
150 cd testrepo &&
151 git config "url.$TRASH.insteadOf" trash/ &&
152 git config remote.up.url trash/. &&
153 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
154 git fetch up &&
156 echo "$the_commit commit refs/remotes/origin/main" >expect &&
157 git for-each-ref refs/remotes/origin >actual &&
158 test_cmp expect actual
162 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
163 mk_empty testrepo &&
165 TRASH=$(pwd)/ &&
166 cd testrepo &&
167 git config "url.trash/.pushInsteadOf" "$TRASH" &&
168 git config remote.up.url "$TRASH." &&
169 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
170 git fetch up &&
172 echo "$the_commit commit refs/remotes/origin/main" >expect &&
173 git for-each-ref refs/remotes/origin >actual &&
174 test_cmp expect actual
178 grep_wrote () {
179 object_count=$1
180 file_name=$2
181 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
184 test_expect_success 'push without negotiation' '
185 mk_empty testrepo &&
186 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
187 test_commit -C testrepo unrelated_commit &&
188 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
189 test_when_finished "rm event" &&
190 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
191 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
194 test_expect_success 'push with negotiation' '
195 mk_empty testrepo &&
196 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
197 test_commit -C testrepo unrelated_commit &&
198 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
199 test_when_finished "rm event" &&
200 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
201 grep_wrote 2 event # 1 commit, 1 tree
204 test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
205 mk_empty testrepo &&
206 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
207 test_commit -C testrepo unrelated_commit &&
208 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
209 test_when_finished "rm event" &&
210 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
211 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
212 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
213 test_i18ngrep "push negotiation failed" err
216 test_expect_success 'push with negotiation does not attempt to fetch submodules' '
217 mk_empty submodule_upstream &&
218 test_commit -C submodule_upstream submodule_commit &&
219 git submodule add ./submodule_upstream submodule &&
220 mk_empty testrepo &&
221 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
222 test_commit -C testrepo unrelated_commit &&
223 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
224 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
225 ! grep "Fetching submodule" err
228 test_expect_success 'push without wildcard' '
229 mk_empty testrepo &&
231 git push testrepo refs/heads/main:refs/remotes/origin/main &&
233 cd testrepo &&
234 echo "$the_commit commit refs/remotes/origin/main" >expect &&
235 git for-each-ref refs/remotes/origin >actual &&
236 test_cmp expect actual
240 test_expect_success 'push with wildcard' '
241 mk_empty testrepo &&
243 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
245 cd testrepo &&
246 echo "$the_commit commit refs/remotes/origin/main" >expect &&
247 git for-each-ref refs/remotes/origin >actual &&
248 test_cmp expect actual
252 test_expect_success 'push with insteadOf' '
253 mk_empty testrepo &&
254 TRASH="$(pwd)/" &&
255 test_config "url.$TRASH.insteadOf" trash/ &&
256 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
258 cd testrepo &&
259 echo "$the_commit commit refs/remotes/origin/main" >expect &&
260 git for-each-ref refs/remotes/origin >actual &&
261 test_cmp expect actual
265 test_expect_success 'push with pushInsteadOf' '
266 mk_empty testrepo &&
267 TRASH="$(pwd)/" &&
268 test_config "url.$TRASH.pushInsteadOf" trash/ &&
269 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
271 cd testrepo &&
272 echo "$the_commit commit refs/remotes/origin/main" >expect &&
273 git for-each-ref refs/remotes/origin >actual &&
274 test_cmp expect actual
278 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
279 mk_empty testrepo &&
280 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
281 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
282 test_config remote.r.url trash/wrong &&
283 test_config remote.r.pushurl "testrepo/" &&
284 git push r refs/heads/main:refs/remotes/origin/main &&
286 cd testrepo &&
287 echo "$the_commit commit refs/remotes/origin/main" >expect &&
288 git for-each-ref refs/remotes/origin >actual &&
289 test_cmp expect actual
293 test_expect_success 'push with matching heads' '
295 mk_test testrepo heads/main &&
296 git push testrepo : &&
297 check_push_result testrepo $the_commit heads/main
301 test_expect_success 'push with matching heads on the command line' '
303 mk_test testrepo heads/main &&
304 git push testrepo : &&
305 check_push_result testrepo $the_commit heads/main
309 test_expect_success 'failed (non-fast-forward) push with matching heads' '
311 mk_test testrepo heads/main &&
312 git push testrepo : &&
313 git commit --amend -massaged &&
314 test_must_fail git push testrepo &&
315 check_push_result testrepo $the_commit heads/main &&
316 git reset --hard $the_commit
320 test_expect_success 'push --force with matching heads' '
322 mk_test testrepo heads/main &&
323 git push testrepo : &&
324 git commit --amend -massaged &&
325 git push --force testrepo : &&
326 ! check_push_result testrepo $the_commit heads/main &&
327 git reset --hard $the_commit
331 test_expect_success 'push with matching heads and forced update' '
333 mk_test testrepo heads/main &&
334 git push testrepo : &&
335 git commit --amend -massaged &&
336 git push testrepo +: &&
337 ! check_push_result testrepo $the_commit heads/main &&
338 git reset --hard $the_commit
342 test_expect_success 'push with no ambiguity (1)' '
344 mk_test testrepo heads/main &&
345 git push testrepo main:main &&
346 check_push_result testrepo $the_commit heads/main
350 test_expect_success 'push with no ambiguity (2)' '
352 mk_test testrepo remotes/origin/main &&
353 git push testrepo main:origin/main &&
354 check_push_result testrepo $the_commit remotes/origin/main
358 test_expect_success 'push with colon-less refspec, no ambiguity' '
360 mk_test testrepo heads/main heads/t/main &&
361 git branch -f t/main main &&
362 git push testrepo main &&
363 check_push_result testrepo $the_commit heads/main &&
364 check_push_result testrepo $the_first_commit heads/t/main
368 test_expect_success 'push with weak ambiguity (1)' '
370 mk_test testrepo heads/main remotes/origin/main &&
371 git push testrepo main:main &&
372 check_push_result testrepo $the_commit heads/main &&
373 check_push_result testrepo $the_first_commit remotes/origin/main
377 test_expect_success 'push with weak ambiguity (2)' '
379 mk_test testrepo heads/main remotes/origin/main remotes/another/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 remotes/another/main
386 test_expect_success 'push with ambiguity' '
388 mk_test testrepo heads/frotz tags/frotz &&
389 test_must_fail git push testrepo main:frotz &&
390 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
394 test_expect_success 'push with colon-less refspec (1)' '
396 mk_test testrepo heads/frotz tags/frotz &&
397 git branch -f frotz main &&
398 git push testrepo frotz &&
399 check_push_result testrepo $the_commit heads/frotz &&
400 check_push_result testrepo $the_first_commit tags/frotz
404 test_expect_success 'push with colon-less refspec (2)' '
406 mk_test testrepo heads/frotz tags/frotz &&
407 if git show-ref --verify -q refs/heads/frotz
408 then
409 git branch -D frotz
410 fi &&
411 git tag -f frotz &&
412 git push -f testrepo frotz &&
413 check_push_result testrepo $the_commit tags/frotz &&
414 check_push_result testrepo $the_first_commit heads/frotz
418 test_expect_success 'push with colon-less refspec (3)' '
420 mk_test testrepo &&
421 if git show-ref --verify -q refs/tags/frotz
422 then
423 git tag -d frotz
424 fi &&
425 git branch -f frotz main &&
426 git push testrepo frotz &&
427 check_push_result testrepo $the_commit heads/frotz &&
428 test 1 = $( cd testrepo && git show-ref | wc -l )
431 test_expect_success 'push with colon-less refspec (4)' '
433 mk_test testrepo &&
434 if git show-ref --verify -q refs/heads/frotz
435 then
436 git branch -D frotz
437 fi &&
438 git tag -f frotz &&
439 git push testrepo frotz &&
440 check_push_result testrepo $the_commit tags/frotz &&
441 test 1 = $( cd testrepo && git show-ref | wc -l )
445 test_expect_success 'push head with non-existent, incomplete dest' '
447 mk_test testrepo &&
448 git push testrepo main:branch &&
449 check_push_result testrepo $the_commit heads/branch
453 test_expect_success 'push tag with non-existent, incomplete dest' '
455 mk_test testrepo &&
456 git tag -f v1.0 &&
457 git push testrepo v1.0:tag &&
458 check_push_result testrepo $the_commit tags/tag
462 test_expect_success 'push sha1 with non-existent, incomplete dest' '
464 mk_test testrepo &&
465 test_must_fail git push testrepo $(git rev-parse main):foo
469 test_expect_success 'push ref expression with non-existent, incomplete dest' '
471 mk_test testrepo &&
472 test_must_fail git push testrepo main^:branch
476 for head in HEAD @
479 test_expect_success "push with $head" '
480 mk_test testrepo heads/main &&
481 git checkout main &&
482 git push testrepo $head &&
483 check_push_result testrepo $the_commit heads/main
486 test_expect_success "push with $head nonexisting at remote" '
487 mk_test testrepo heads/main &&
488 git checkout -b local main &&
489 test_when_finished "git checkout main; git branch -D local" &&
490 git push testrepo $head &&
491 check_push_result testrepo $the_commit heads/local
494 test_expect_success "push with +$head" '
495 mk_test testrepo heads/main &&
496 git checkout -b local main &&
497 test_when_finished "git checkout main; git branch -D local" &&
498 git push testrepo main local &&
499 check_push_result testrepo $the_commit heads/main &&
500 check_push_result testrepo $the_commit heads/local &&
502 # Without force rewinding should fail
503 git reset --hard $head^ &&
504 test_must_fail git push testrepo $head &&
505 check_push_result testrepo $the_commit heads/local &&
507 # With force rewinding should succeed
508 git push testrepo +$head &&
509 check_push_result testrepo $the_first_commit heads/local
512 test_expect_success "push $head with non-existent, incomplete dest" '
513 mk_test testrepo &&
514 git checkout main &&
515 git push testrepo $head:branch &&
516 check_push_result testrepo $the_commit heads/branch
520 test_expect_success "push with config remote.*.push = $head" '
521 mk_test testrepo heads/local &&
522 git checkout main &&
523 git branch -f local $the_commit &&
524 test_when_finished "git branch -D local" &&
526 cd testrepo &&
527 git checkout local &&
528 git reset --hard $the_first_commit
529 ) &&
530 test_config remote.there.url testrepo &&
531 test_config remote.there.push $head &&
532 test_config branch.main.remote there &&
533 git push &&
534 check_push_result testrepo $the_commit heads/main &&
535 check_push_result testrepo $the_first_commit heads/local
538 done
540 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
541 mk_test testrepo heads/main &&
542 git checkout $the_first_commit &&
543 test_config remote.there.url testrepo &&
544 test_config remote.there.push refs/heads/main:refs/heads/main &&
545 git push there &&
546 check_push_result testrepo $the_commit heads/main
549 test_expect_success 'push with remote.pushdefault' '
550 mk_test up_repo heads/main &&
551 mk_test down_repo heads/main &&
552 test_config remote.up.url up_repo &&
553 test_config remote.down.url down_repo &&
554 test_config branch.main.remote up &&
555 test_config remote.pushdefault down &&
556 test_config push.default matching &&
557 git push &&
558 check_push_result up_repo $the_first_commit heads/main &&
559 check_push_result down_repo $the_commit heads/main
562 test_expect_success 'push with config remote.*.pushurl' '
564 mk_test testrepo heads/main &&
565 git checkout main &&
566 test_config remote.there.url test2repo &&
567 test_config remote.there.pushurl testrepo &&
568 git push there : &&
569 check_push_result testrepo $the_commit heads/main
572 test_expect_success 'push with config branch.*.pushremote' '
573 mk_test up_repo heads/main &&
574 mk_test side_repo heads/main &&
575 mk_test down_repo heads/main &&
576 test_config remote.up.url up_repo &&
577 test_config remote.pushdefault side_repo &&
578 test_config remote.down.url down_repo &&
579 test_config branch.main.remote up &&
580 test_config branch.main.pushremote down &&
581 test_config push.default matching &&
582 git push &&
583 check_push_result up_repo $the_first_commit heads/main &&
584 check_push_result side_repo $the_first_commit heads/main &&
585 check_push_result down_repo $the_commit heads/main
588 test_expect_success 'branch.*.pushremote config order is irrelevant' '
589 mk_test one_repo heads/main &&
590 mk_test two_repo heads/main &&
591 test_config remote.one.url one_repo &&
592 test_config remote.two.url two_repo &&
593 test_config branch.main.pushremote two_repo &&
594 test_config remote.pushdefault one_repo &&
595 test_config push.default matching &&
596 git push &&
597 check_push_result one_repo $the_first_commit heads/main &&
598 check_push_result two_repo $the_commit heads/main
601 test_expect_success 'push with dry-run' '
603 mk_test testrepo heads/main &&
604 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
605 git push --dry-run testrepo : &&
606 check_push_result testrepo $old_commit heads/main
609 test_expect_success 'push updates local refs' '
611 mk_test testrepo heads/main &&
612 mk_child testrepo child &&
614 cd child &&
615 git pull .. main &&
616 git push &&
617 test $(git rev-parse main) = \
618 $(git rev-parse remotes/origin/main)
623 test_expect_success 'push updates up-to-date local refs' '
625 mk_test testrepo heads/main &&
626 mk_child testrepo child1 &&
627 mk_child testrepo child2 &&
628 (cd child1 && git pull .. main && git push) &&
630 cd child2 &&
631 git pull ../child1 main &&
632 git push &&
633 test $(git rev-parse main) = \
634 $(git rev-parse remotes/origin/main)
639 test_expect_success 'push preserves up-to-date packed refs' '
641 mk_test testrepo heads/main &&
642 mk_child testrepo child &&
644 cd child &&
645 git push &&
646 ! test -f .git/refs/remotes/origin/main
651 test_expect_success 'push does not update local refs on failure' '
653 mk_test testrepo heads/main &&
654 mk_child testrepo child &&
655 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
656 chmod +x testrepo/.git/hooks/pre-receive &&
658 cd child &&
659 git pull .. main &&
660 test_must_fail git push &&
661 test $(git rev-parse main) != \
662 $(git rev-parse remotes/origin/main)
667 test_expect_success 'allow deleting an invalid remote ref' '
669 mk_test testrepo heads/branch &&
670 rm -f testrepo/.git/objects/??/* &&
671 git push testrepo :refs/heads/branch &&
672 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
676 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
677 mk_test_with_hooks testrepo heads/main heads/next &&
678 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
679 newmain=$(git show-ref -s --verify refs/heads/main) &&
680 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
681 newnext=$ZERO_OID &&
682 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
684 cd testrepo/.git &&
685 cat >pre-receive.expect <<-EOF &&
686 $orgmain $newmain refs/heads/main
687 $orgnext $newnext refs/heads/next
690 cat >update.expect <<-EOF &&
691 refs/heads/main $orgmain $newmain
692 refs/heads/next $orgnext $newnext
695 cat >post-receive.expect <<-EOF &&
696 $orgmain $newmain refs/heads/main
697 $orgnext $newnext refs/heads/next
700 cat >post-update.expect <<-EOF &&
701 refs/heads/main
702 refs/heads/next
705 test_cmp pre-receive.expect pre-receive.actual &&
706 test_cmp update.expect update.actual &&
707 test_cmp post-receive.expect post-receive.actual &&
708 test_cmp post-update.expect post-update.actual
712 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
713 mk_test_with_hooks testrepo heads/branch &&
714 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
715 rm -f testrepo/.git/objects/??/* &&
716 git push testrepo :refs/heads/branch &&
718 cd testrepo/.git &&
719 cat >pre-receive.expect <<-EOF &&
720 $orig $ZERO_OID refs/heads/branch
723 cat >update.expect <<-EOF &&
724 refs/heads/branch $orig $ZERO_OID
727 cat >post-receive.expect <<-EOF &&
728 $orig $ZERO_OID refs/heads/branch
731 cat >post-update.expect <<-EOF &&
732 refs/heads/branch
735 test_cmp pre-receive.expect pre-receive.actual &&
736 test_cmp update.expect update.actual &&
737 test_cmp post-receive.expect post-receive.actual &&
738 test_cmp post-update.expect post-update.actual
742 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
743 mk_test_with_hooks testrepo heads/main &&
744 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
745 newmain=$(git show-ref -s --verify refs/heads/main) &&
746 git push testrepo main :refs/heads/nonexistent &&
748 cd testrepo/.git &&
749 cat >pre-receive.expect <<-EOF &&
750 $orgmain $newmain refs/heads/main
751 $ZERO_OID $ZERO_OID refs/heads/nonexistent
754 cat >update.expect <<-EOF &&
755 refs/heads/main $orgmain $newmain
756 refs/heads/nonexistent $ZERO_OID $ZERO_OID
759 cat >post-receive.expect <<-EOF &&
760 $orgmain $newmain refs/heads/main
763 cat >post-update.expect <<-EOF &&
764 refs/heads/main
767 test_cmp pre-receive.expect pre-receive.actual &&
768 test_cmp update.expect update.actual &&
769 test_cmp post-receive.expect post-receive.actual &&
770 test_cmp post-update.expect post-update.actual
774 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
775 mk_test_with_hooks testrepo heads/main &&
776 git push testrepo :refs/heads/nonexistent &&
778 cd testrepo/.git &&
779 cat >pre-receive.expect <<-EOF &&
780 $ZERO_OID $ZERO_OID refs/heads/nonexistent
783 cat >update.expect <<-EOF &&
784 refs/heads/nonexistent $ZERO_OID $ZERO_OID
787 test_cmp pre-receive.expect pre-receive.actual &&
788 test_cmp update.expect update.actual &&
789 test_path_is_missing post-receive.actual &&
790 test_path_is_missing post-update.actual
794 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
795 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
796 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
797 newmain=$(git show-ref -s --verify refs/heads/main) &&
798 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
799 newnext=$ZERO_OID &&
800 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
801 newseen=$(git show-ref -s --verify refs/heads/main) &&
802 git push testrepo refs/heads/main:refs/heads/main \
803 refs/heads/main:refs/heads/seen :refs/heads/next \
804 :refs/heads/nonexistent &&
806 cd testrepo/.git &&
807 cat >pre-receive.expect <<-EOF &&
808 $orgmain $newmain refs/heads/main
809 $orgnext $newnext refs/heads/next
810 $orgseen $newseen refs/heads/seen
811 $ZERO_OID $ZERO_OID refs/heads/nonexistent
814 cat >update.expect <<-EOF &&
815 refs/heads/main $orgmain $newmain
816 refs/heads/next $orgnext $newnext
817 refs/heads/seen $orgseen $newseen
818 refs/heads/nonexistent $ZERO_OID $ZERO_OID
821 cat >post-receive.expect <<-EOF &&
822 $orgmain $newmain refs/heads/main
823 $orgnext $newnext refs/heads/next
824 $orgseen $newseen refs/heads/seen
827 cat >post-update.expect <<-EOF &&
828 refs/heads/main
829 refs/heads/next
830 refs/heads/seen
833 test_cmp pre-receive.expect pre-receive.actual &&
834 test_cmp update.expect update.actual &&
835 test_cmp post-receive.expect post-receive.actual &&
836 test_cmp post-update.expect post-update.actual
840 test_expect_success 'allow deleting a ref using --delete' '
841 mk_test testrepo heads/main &&
842 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
843 git push testrepo --delete main &&
844 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
847 test_expect_success 'allow deleting a tag using --delete' '
848 mk_test testrepo heads/main &&
849 git tag -a -m dummy_message deltag heads/main &&
850 git push testrepo --tags &&
851 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
852 git push testrepo --delete tag deltag &&
853 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
856 test_expect_success 'push --delete without args aborts' '
857 mk_test testrepo heads/main &&
858 test_must_fail git push testrepo --delete
861 test_expect_success 'push --delete refuses src:dest refspecs' '
862 mk_test testrepo heads/main &&
863 test_must_fail git push testrepo --delete main:foo
866 test_expect_success 'push --delete refuses empty string' '
867 mk_test testrepo heads/master &&
868 test_must_fail git push testrepo --delete ""
871 test_expect_success 'warn on push to HEAD of non-bare repository' '
872 mk_test testrepo heads/main &&
874 cd testrepo &&
875 git checkout main &&
876 git config receive.denyCurrentBranch warn
877 ) &&
878 git push testrepo main 2>stderr &&
879 grep "warning: updating the current branch" stderr
882 test_expect_success 'deny push to HEAD of non-bare repository' '
883 mk_test testrepo heads/main &&
885 cd testrepo &&
886 git checkout main &&
887 git config receive.denyCurrentBranch true
888 ) &&
889 test_must_fail git push testrepo main
892 test_expect_success 'allow push to HEAD of bare repository (bare)' '
893 mk_test testrepo heads/main &&
895 cd testrepo &&
896 git checkout main &&
897 git config receive.denyCurrentBranch true &&
898 git config core.bare true
899 ) &&
900 git push testrepo main 2>stderr &&
901 ! grep "warning: updating the current branch" stderr
904 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
905 mk_test testrepo heads/main &&
907 cd testrepo &&
908 git checkout main &&
909 git config receive.denyCurrentBranch false
910 ) &&
911 git push testrepo main 2>stderr &&
912 ! grep "warning: updating the current branch" stderr
915 test_expect_success 'fetch with branches' '
916 mk_empty testrepo &&
917 git branch second $the_first_commit &&
918 git checkout second &&
919 echo ".." > testrepo/.git/branches/branch1 &&
921 cd testrepo &&
922 git fetch branch1 &&
923 echo "$the_commit commit refs/heads/branch1" >expect &&
924 git for-each-ref refs/heads >actual &&
925 test_cmp expect actual
926 ) &&
927 git checkout main
930 test_expect_success 'fetch with branches containing #' '
931 mk_empty testrepo &&
932 echo "..#second" > testrepo/.git/branches/branch2 &&
934 cd testrepo &&
935 git fetch branch2 &&
936 echo "$the_first_commit commit refs/heads/branch2" >expect &&
937 git for-each-ref refs/heads >actual &&
938 test_cmp expect actual
939 ) &&
940 git checkout main
943 test_expect_success 'push with branches' '
944 mk_empty testrepo &&
945 git checkout second &&
946 echo "testrepo" > .git/branches/branch1 &&
947 git push branch1 &&
949 cd testrepo &&
950 echo "$the_first_commit commit refs/heads/main" >expect &&
951 git for-each-ref refs/heads >actual &&
952 test_cmp expect actual
956 test_expect_success 'push with branches containing #' '
957 mk_empty testrepo &&
958 echo "testrepo#branch3" > .git/branches/branch2 &&
959 git push branch2 &&
961 cd testrepo &&
962 echo "$the_first_commit commit refs/heads/branch3" >expect &&
963 git for-each-ref refs/heads >actual &&
964 test_cmp expect actual
965 ) &&
966 git checkout main
969 test_expect_success 'push into aliased refs (consistent)' '
970 mk_test testrepo heads/main &&
971 mk_child testrepo child1 &&
972 mk_child testrepo child2 &&
974 cd child1 &&
975 git branch foo &&
976 git symbolic-ref refs/heads/bar refs/heads/foo &&
977 git config receive.denyCurrentBranch false
978 ) &&
980 cd child2 &&
981 >path2 &&
982 git add path2 &&
983 test_tick &&
984 git commit -a -m child2 &&
985 git branch foo &&
986 git branch bar &&
987 git push ../child1 foo bar
991 test_expect_success 'push into aliased refs (inconsistent)' '
992 mk_test testrepo heads/main &&
993 mk_child testrepo child1 &&
994 mk_child testrepo child2 &&
996 cd child1 &&
997 git branch foo &&
998 git symbolic-ref refs/heads/bar refs/heads/foo &&
999 git config receive.denyCurrentBranch false
1000 ) &&
1002 cd child2 &&
1003 >path2 &&
1004 git add path2 &&
1005 test_tick &&
1006 git commit -a -m child2 &&
1007 git branch foo &&
1008 >path3 &&
1009 git add path3 &&
1010 test_tick &&
1011 git commit -a -m child2 &&
1012 git branch bar &&
1013 test_must_fail git push ../child1 foo bar 2>stderr &&
1014 grep "refusing inconsistent update" stderr
1018 test_force_push_tag () {
1019 tag_type_description=$1
1020 tag_args=$2
1022 test_expect_success "force pushing required to update $tag_type_description" "
1023 mk_test testrepo heads/main &&
1024 mk_child testrepo child1 &&
1025 mk_child testrepo child2 &&
1027 cd child1 &&
1028 git tag testTag &&
1029 git push ../child2 testTag &&
1030 >file1 &&
1031 git add file1 &&
1032 git commit -m 'file1' &&
1033 git tag $tag_args testTag &&
1034 test_must_fail git push ../child2 testTag &&
1035 git push --force ../child2 testTag &&
1036 git tag $tag_args testTag HEAD~ &&
1037 test_must_fail git push ../child2 testTag &&
1038 git push --force ../child2 testTag &&
1040 # Clobbering without + in refspec needs --force
1041 git tag -f testTag &&
1042 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1043 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1045 # Clobbering with + in refspec does not need --force
1046 git tag -f testTag HEAD~ &&
1047 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1049 # Clobbering with --no-force still obeys + in refspec
1050 git tag -f testTag &&
1051 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1053 # Clobbering with/without --force and 'tag <name>' format
1054 git tag -f testTag HEAD~ &&
1055 test_must_fail git push ../child2 tag testTag &&
1056 git push --force ../child2 tag testTag
1061 test_force_push_tag "lightweight tag" "-f"
1062 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1064 test_force_fetch_tag () {
1065 tag_type_description=$1
1066 tag_args=$2
1068 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1069 mk_test testrepo heads/main &&
1070 mk_child testrepo child1 &&
1071 mk_child testrepo child2 &&
1073 cd testrepo &&
1074 git tag testTag &&
1075 git -C ../child1 fetch origin tag testTag &&
1076 >file1 &&
1077 git add file1 &&
1078 git commit -m 'file1' &&
1079 git tag $tag_args testTag &&
1080 test_must_fail git -C ../child1 fetch origin tag testTag &&
1081 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1086 test_force_fetch_tag "lightweight tag" "-f"
1087 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1089 test_expect_success 'push --porcelain' '
1090 mk_empty testrepo &&
1091 echo >.git/foo "To testrepo" &&
1092 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1093 echo >>.git/foo "Done" &&
1094 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1096 cd testrepo &&
1097 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1098 git for-each-ref refs/remotes/origin >actual &&
1099 test_cmp expect actual
1100 ) &&
1101 test_cmp .git/foo .git/bar
1104 test_expect_success 'push --porcelain bad url' '
1105 mk_empty testrepo &&
1106 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1107 ! grep -q Done .git/bar
1110 test_expect_success 'push --porcelain rejected' '
1111 mk_empty testrepo &&
1112 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1113 (cd testrepo &&
1114 git reset --hard origin/main^ &&
1115 git config receive.denyCurrentBranch true) &&
1117 echo >.git/foo "To testrepo" &&
1118 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1119 echo >>.git/foo "Done" &&
1121 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1122 test_cmp .git/foo .git/bar
1125 test_expect_success 'push --porcelain --dry-run rejected' '
1126 mk_empty testrepo &&
1127 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1128 (cd testrepo &&
1129 git reset --hard origin/main &&
1130 git config receive.denyCurrentBranch true) &&
1132 echo >.git/foo "To testrepo" &&
1133 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1134 echo >>.git/foo "Done" &&
1136 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1137 test_cmp .git/foo .git/bar
1140 test_expect_success 'push --prune' '
1141 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1142 git push --prune testrepo : &&
1143 check_push_result testrepo $the_commit heads/main &&
1144 check_push_result testrepo $the_first_commit heads/second &&
1145 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1148 test_expect_success 'push --prune refspec' '
1149 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1150 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1151 check_push_result testrepo $the_commit tmp/main &&
1152 check_push_result testrepo $the_first_commit tmp/second &&
1153 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1156 for configsection in transfer receive
1158 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1159 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1161 cd testrepo &&
1162 git config $configsection.hiderefs refs/hidden
1163 ) &&
1165 # push to unhidden ref succeeds normally
1166 git push testrepo main:refs/heads/main &&
1167 check_push_result testrepo $the_commit heads/main &&
1169 # push to update a hidden ref should fail
1170 test_must_fail git push testrepo main:refs/hidden/one &&
1171 check_push_result testrepo $the_first_commit hidden/one &&
1173 # push to delete a hidden ref should fail
1174 test_must_fail git push testrepo :refs/hidden/two &&
1175 check_push_result testrepo $the_first_commit hidden/two &&
1177 # idempotent push to update a hidden ref should fail
1178 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1179 check_push_result testrepo $the_first_commit hidden/three
1181 done
1183 test_expect_success 'fetch exact SHA1' '
1184 mk_test testrepo heads/main hidden/one &&
1185 git push testrepo main:refs/hidden/one &&
1187 cd testrepo &&
1188 git config transfer.hiderefs refs/hidden
1189 ) &&
1190 check_push_result testrepo $the_commit hidden/one &&
1192 mk_child testrepo child &&
1194 cd child &&
1196 # make sure $the_commit does not exist here
1197 git repack -a -d &&
1198 git prune &&
1199 test_must_fail git cat-file -t $the_commit &&
1201 # Some protocol versions (e.g. 2) support fetching
1202 # unadvertised objects, so restrict this test to v0.
1204 # fetching the hidden object should fail by default
1205 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1206 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1207 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1208 test_must_fail git rev-parse --verify refs/heads/copy &&
1210 # the server side can allow it to succeed
1212 cd ../testrepo &&
1213 git config uploadpack.allowtipsha1inwant true
1214 ) &&
1216 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1217 cat >expect <<-EOF &&
1218 $the_commit
1219 $the_first_commit
1222 git rev-parse --verify refs/heads/copy &&
1223 git rev-parse --verify refs/heads/extra
1224 } >actual &&
1225 test_cmp expect actual
1229 test_expect_success 'fetch exact SHA1 in protocol v2' '
1230 mk_test testrepo heads/main hidden/one &&
1231 git push testrepo main:refs/hidden/one &&
1232 git -C testrepo config transfer.hiderefs refs/hidden &&
1233 check_push_result testrepo $the_commit hidden/one &&
1235 mk_child testrepo child &&
1236 git -C child config protocol.version 2 &&
1238 # make sure $the_commit does not exist here
1239 git -C child repack -a -d &&
1240 git -C child prune &&
1241 test_must_fail git -C child cat-file -t $the_commit &&
1243 # fetching the hidden object succeeds by default
1244 # NEEDSWORK: should this match the v0 behavior instead?
1245 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1248 for configallowtipsha1inwant in true false
1250 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1251 mk_empty testrepo &&
1253 cd testrepo &&
1254 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1255 git commit --allow-empty -m foo &&
1256 git commit --allow-empty -m bar
1257 ) &&
1258 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1259 mk_empty shallow &&
1261 cd shallow &&
1262 # Some protocol versions (e.g. 2) support fetching
1263 # unadvertised objects, so restrict this test to v0.
1264 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1265 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1266 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1267 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1268 git cat-file commit $SHA1
1272 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1273 mk_empty testrepo &&
1275 cd testrepo &&
1276 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1277 git commit --allow-empty -m foo &&
1278 git commit --allow-empty -m bar &&
1279 git commit --allow-empty -m xyz
1280 ) &&
1281 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1282 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1283 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1285 cd testrepo &&
1286 git reset --hard $SHA1_2 &&
1287 git cat-file commit $SHA1_1 &&
1288 git cat-file commit $SHA1_3
1289 ) &&
1290 mk_empty shallow &&
1292 cd shallow &&
1293 # Some protocol versions (e.g. 2) support fetching
1294 # unadvertised objects, so restrict this test to v0.
1295 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1296 git fetch ../testrepo/.git $SHA1_3 &&
1297 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1298 git fetch ../testrepo/.git $SHA1_1 &&
1299 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1300 git fetch ../testrepo/.git $SHA1_1 &&
1301 git cat-file commit $SHA1_1 &&
1302 test_must_fail git cat-file commit $SHA1_2 &&
1303 git fetch ../testrepo/.git $SHA1_2 &&
1304 git cat-file commit $SHA1_2 &&
1305 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1306 git fetch ../testrepo/.git $SHA1_3 2>err &&
1307 # ideally we would insist this be on a "remote error:"
1308 # line, but it is racy; see the commit message
1309 test_i18ngrep "not our ref.*$SHA1_3\$" err
1312 done
1314 test_expect_success 'fetch follows tags by default' '
1315 mk_test testrepo heads/main &&
1316 test_when_finished "rm -rf src" &&
1317 git init src &&
1319 cd src &&
1320 git pull ../testrepo main &&
1321 git tag -m "annotated" tag &&
1322 git for-each-ref >tmp1 &&
1323 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1324 sort -k 3 >../expect
1325 ) &&
1326 test_when_finished "rm -rf dst" &&
1327 git init dst &&
1329 cd dst &&
1330 git remote add origin ../src &&
1331 git config branch.main.remote origin &&
1332 git config branch.main.merge refs/heads/main &&
1333 git pull &&
1334 git for-each-ref >../actual
1335 ) &&
1336 test_cmp expect actual
1339 test_expect_success 'peeled advertisements are not considered ref tips' '
1340 mk_empty testrepo &&
1341 git -C testrepo commit --allow-empty -m one &&
1342 git -C testrepo commit --allow-empty -m two &&
1343 git -C testrepo tag -m foo mytag HEAD^ &&
1344 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1345 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1346 git fetch testrepo $oid 2>err &&
1347 test_i18ngrep "Server does not allow request for unadvertised object" err
1350 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1351 mk_test testrepo heads/main &&
1352 test_when_finished "rm -rf src" &&
1353 git init src &&
1354 test_when_finished "rm -rf dst" &&
1355 git init --bare dst &&
1357 cd src &&
1358 git pull ../testrepo main &&
1359 git branch next &&
1360 git config remote.dst.url ../dst &&
1361 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1362 git push dst main &&
1363 git show-ref refs/heads/main |
1364 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1365 ) &&
1367 cd dst &&
1368 test_must_fail git show-ref refs/heads/next &&
1369 test_must_fail git show-ref refs/heads/main &&
1370 git show-ref refs/remotes/src/main >actual
1371 ) &&
1372 test_cmp dst/expect dst/actual
1375 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1376 mk_test testrepo heads/main &&
1377 test_when_finished "rm -rf src" &&
1378 git init src &&
1379 test_when_finished "rm -rf dst" &&
1380 git init --bare dst &&
1382 cd src &&
1383 git pull ../testrepo main &&
1384 git branch next &&
1385 git config remote.dst.url ../dst &&
1386 git config push.default matching &&
1387 git push dst main &&
1388 git show-ref refs/heads/main >../dst/expect
1389 ) &&
1391 cd dst &&
1392 test_must_fail git show-ref refs/heads/next &&
1393 git show-ref refs/heads/main >actual
1394 ) &&
1395 test_cmp dst/expect dst/actual
1398 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1399 mk_test testrepo heads/main &&
1400 test_when_finished "rm -rf src" &&
1401 git init src &&
1402 test_when_finished "rm -rf dst" &&
1403 git init --bare dst &&
1405 cd src &&
1406 git pull ../testrepo main &&
1407 git branch next &&
1408 git config remote.dst.url ../dst &&
1409 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1410 git config push.default upstream &&
1412 git config branch.main.merge refs/heads/trunk &&
1413 git config branch.main.remote dst &&
1415 git push dst main &&
1416 git show-ref refs/heads/main |
1417 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1418 ) &&
1420 cd dst &&
1421 test_must_fail git show-ref refs/heads/main &&
1422 test_must_fail git show-ref refs/heads/next &&
1423 git show-ref refs/heads/trunk >actual
1424 ) &&
1425 test_cmp dst/expect dst/actual
1428 test_expect_success 'push does not follow tags by default' '
1429 mk_test testrepo heads/main &&
1430 test_when_finished "rm -rf src" &&
1431 git init src &&
1432 test_when_finished "rm -rf dst" &&
1433 git init --bare dst &&
1435 cd src &&
1436 git pull ../testrepo main &&
1437 git tag -m "annotated" tag &&
1438 git checkout -b another &&
1439 git commit --allow-empty -m "future commit" &&
1440 git tag -m "future" future &&
1441 git checkout main &&
1442 git for-each-ref refs/heads/main >../expect &&
1443 git push ../dst main
1444 ) &&
1446 cd dst &&
1447 git for-each-ref >../actual
1448 ) &&
1449 test_cmp expect actual
1452 test_expect_success 'push --follow-tags only pushes relevant tags' '
1453 mk_test testrepo heads/main &&
1454 test_when_finished "rm -rf src" &&
1455 git init src &&
1456 test_when_finished "rm -rf dst" &&
1457 git init --bare dst &&
1459 cd src &&
1460 git pull ../testrepo main &&
1461 git tag -m "annotated" tag &&
1462 git checkout -b another &&
1463 git commit --allow-empty -m "future commit" &&
1464 git tag -m "future" future &&
1465 git checkout main &&
1466 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1467 git push --follow-tags ../dst main
1468 ) &&
1470 cd dst &&
1471 git for-each-ref >../actual
1472 ) &&
1473 test_cmp expect actual
1476 test_expect_success 'push --no-thin must produce non-thin pack' '
1477 cat >>path1 <<\EOF &&
1478 keep base version of path1 big enough, compared to the new changes
1479 later, in order to pass size heuristics in
1480 builtin/pack-objects.c:try_delta()
1482 git commit -am initial &&
1483 git init no-thin &&
1484 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1485 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1486 echo modified >> path1 &&
1487 git commit -am modified &&
1488 git repack -adf &&
1489 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1490 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1493 test_expect_success 'pushing a tag pushes the tagged object' '
1494 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1495 git tag -m foo tag-of-blob $blob &&
1496 test_when_finished "rm -rf dst.git" &&
1497 git init --bare dst.git &&
1498 git push dst.git tag-of-blob &&
1499 # the receiving index-pack should have noticed
1500 # any problems, but we double check
1501 echo unreferenced >expect &&
1502 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1503 test_cmp expect actual
1506 test_expect_success 'push into bare respects core.logallrefupdates' '
1507 test_when_finished "rm -rf dst.git" &&
1508 git init --bare dst.git &&
1509 git -C dst.git config core.logallrefupdates true &&
1511 # double push to test both with and without
1512 # the actual pack transfer
1513 git push dst.git main:one &&
1514 echo "one@{0} push" >expect &&
1515 git -C dst.git log -g --format="%gd %gs" one >actual &&
1516 test_cmp expect actual &&
1518 git push dst.git main:two &&
1519 echo "two@{0} push" >expect &&
1520 git -C dst.git log -g --format="%gd %gs" two >actual &&
1521 test_cmp expect actual
1524 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1525 test_when_finished "rm -rf dst.git" &&
1526 git init --bare dst.git &&
1528 cd dst.git &&
1529 git config core.logallrefupdates true &&
1531 # as above, we double-fetch to test both
1532 # with and without pack transfer
1533 git fetch .. main:one &&
1534 echo "one@{0} fetch .. main:one: storing head" >expect &&
1535 git log -g --format="%gd %gs" one >actual &&
1536 test_cmp expect actual &&
1538 git fetch .. main:two &&
1539 echo "two@{0} fetch .. main:two: storing head" >expect &&
1540 git log -g --format="%gd %gs" two >actual &&
1541 test_cmp expect actual
1545 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1546 mk_empty testrepo &&
1547 git push testrepo main &&
1549 cd testrepo &&
1550 git reset --hard &&
1551 git config receive.denyCurrentBranch updateInstead
1552 ) &&
1553 test_commit third path2 &&
1555 # Try pushing into a repository with pristine working tree
1556 git push testrepo main &&
1558 cd testrepo &&
1559 git update-index -q --refresh &&
1560 git diff-files --quiet -- &&
1561 git diff-index --quiet --cached HEAD -- &&
1562 test third = "$(cat path2)" &&
1563 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1564 ) &&
1566 # Try pushing into a repository with working tree needing a refresh
1568 cd testrepo &&
1569 git reset --hard HEAD^ &&
1570 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1571 test-tool chmtime +100 path1
1572 ) &&
1573 git push testrepo main &&
1575 cd testrepo &&
1576 git update-index -q --refresh &&
1577 git diff-files --quiet -- &&
1578 git diff-index --quiet --cached HEAD -- &&
1579 test_cmp ../path1 path1 &&
1580 test third = "$(cat path2)" &&
1581 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1582 ) &&
1584 # Update what is to be pushed
1585 test_commit fourth path2 &&
1587 # Try pushing into a repository with a dirty working tree
1588 # (1) the working tree updated
1590 cd testrepo &&
1591 echo changed >path1
1592 ) &&
1593 test_must_fail git push testrepo main &&
1595 cd testrepo &&
1596 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1597 git diff --quiet --cached &&
1598 test changed = "$(cat path1)"
1599 ) &&
1601 # (2) the index updated
1603 cd testrepo &&
1604 echo changed >path1 &&
1605 git add path1
1606 ) &&
1607 test_must_fail git push testrepo main &&
1609 cd testrepo &&
1610 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1611 git diff --quiet &&
1612 test changed = "$(cat path1)"
1613 ) &&
1615 # Introduce a new file in the update
1616 test_commit fifth path3 &&
1618 # (3) the working tree has an untracked file that would interfere
1620 cd testrepo &&
1621 git reset --hard &&
1622 echo changed >path3
1623 ) &&
1624 test_must_fail git push testrepo main &&
1626 cd testrepo &&
1627 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1628 git diff --quiet &&
1629 git diff --quiet --cached &&
1630 test changed = "$(cat path3)"
1631 ) &&
1633 # (4) the target changes to what gets pushed but it still is a change
1635 cd testrepo &&
1636 git reset --hard &&
1637 echo fifth >path3 &&
1638 git add path3
1639 ) &&
1640 test_must_fail git push testrepo main &&
1642 cd testrepo &&
1643 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1644 git diff --quiet &&
1645 test fifth = "$(cat path3)"
1646 ) &&
1648 # (5) push into void
1649 test_when_finished "rm -rf void" &&
1650 git init void &&
1652 cd void &&
1653 git config receive.denyCurrentBranch updateInstead
1654 ) &&
1655 git push void main &&
1657 cd void &&
1658 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1659 git diff --quiet &&
1660 git diff --cached --quiet
1661 ) &&
1663 # (6) updateInstead intervened by fast-forward check
1664 test_must_fail git push void main^:main &&
1665 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1666 git -C void diff --quiet &&
1667 git -C void diff --cached --quiet
1670 test_expect_success 'updateInstead with push-to-checkout hook' '
1671 test_when_finished "rm -rf testrepo" &&
1672 git init testrepo &&
1673 git -C testrepo pull .. main &&
1674 git -C testrepo reset --hard HEAD^^ &&
1675 git -C testrepo tag initial &&
1676 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1677 test_hook -C testrepo push-to-checkout <<-\EOF &&
1678 echo >&2 updating from $(git rev-parse HEAD)
1679 echo >&2 updating to "$1"
1681 git update-index -q --refresh &&
1682 git read-tree -u -m HEAD "$1" || {
1683 status=$?
1684 echo >&2 read-tree failed
1685 exit $status
1689 # Try pushing into a pristine
1690 git push testrepo main &&
1692 cd testrepo &&
1693 git diff --quiet &&
1694 git diff HEAD --quiet &&
1695 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1696 ) &&
1698 # Try pushing into a repository with conflicting change
1700 cd testrepo &&
1701 git reset --hard initial &&
1702 echo conflicting >path2
1703 ) &&
1704 test_must_fail git push testrepo main &&
1706 cd testrepo &&
1707 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1708 test conflicting = "$(cat path2)" &&
1709 git diff-index --quiet --cached HEAD
1710 ) &&
1712 # Try pushing into a repository with unrelated change
1714 cd testrepo &&
1715 git reset --hard initial &&
1716 echo unrelated >path1 &&
1717 echo irrelevant >path5 &&
1718 git add path5
1719 ) &&
1720 git push testrepo main &&
1722 cd testrepo &&
1723 test "$(cat path1)" = unrelated &&
1724 test "$(cat path5)" = irrelevant &&
1725 test "$(git diff --name-only --cached HEAD)" = path5 &&
1726 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1727 ) &&
1729 # push into void
1730 test_when_finished "rm -rf void" &&
1731 git init void &&
1732 git -C void config receive.denyCurrentBranch updateInstead &&
1733 test_hook -C void push-to-checkout <<-\EOF &&
1734 if git rev-parse --quiet --verify HEAD
1735 then
1736 has_head=yes
1737 echo >&2 updating from $(git rev-parse HEAD)
1738 else
1739 has_head=no
1740 echo >&2 pushing into void
1742 echo >&2 updating to "$1"
1744 git update-index -q --refresh &&
1745 case "$has_head" in
1746 yes)
1747 git read-tree -u -m HEAD "$1" ;;
1749 git read-tree -u -m "$1" ;;
1750 esac || {
1751 status=$?
1752 echo >&2 read-tree failed
1753 exit $status
1757 git push void main &&
1759 cd void &&
1760 git diff --quiet &&
1761 git diff --cached --quiet &&
1762 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1766 test_expect_success 'denyCurrentBranch and worktrees' '
1767 git worktree add new-wt &&
1768 git clone . cloned &&
1769 test_commit -C cloned first &&
1770 test_config receive.denyCurrentBranch refuse &&
1771 test_must_fail git -C cloned push origin HEAD:new-wt &&
1772 test_config receive.denyCurrentBranch updateInstead &&
1773 git -C cloned push origin HEAD:new-wt &&
1774 test_path_exists new-wt/first.t &&
1775 test_must_fail git -C cloned push --delete origin new-wt
1778 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1779 test_when_finished "rm -fr bare.git" &&
1780 git clone --bare . bare.git &&
1781 git -C bare.git worktree add wt &&
1782 test_commit grape &&
1783 git -C bare.git config receive.denyCurrentBranch refuse &&
1784 test_must_fail git push bare.git HEAD:wt &&
1785 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1786 git push bare.git HEAD:wt &&
1787 test_path_exists bare.git/wt/grape.t &&
1788 test_must_fail git push --delete bare.git wt
1791 test_expect_success 'refuse fetch to current branch of worktree' '
1792 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1793 git worktree add wt &&
1794 test_commit apple &&
1795 test_must_fail git fetch . HEAD:wt &&
1796 git fetch -u . HEAD:wt
1799 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
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 banana &&
1804 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1805 git -C bare.git fetch -u .. HEAD:wt
1808 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1809 mk_empty testrepo &&
1810 git -C testrepo config receive.hiderefs refs/hidden &&
1811 git -C testrepo config receive.unpackLimit 1 &&
1812 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1813 test_dir_is_empty testrepo/.git/objects/pack
1816 test_done