Merge branch 'js/short-help-outside-repo-fix'
[git/debian.git] / t / t5516-fetch-push.sh
blob3137eb8d4d2613005586ad0e8d1eea606f4b81bf
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 rm -fr "$repo_name" &&
27 mkdir "$repo_name" &&
29 cd "$repo_name" &&
30 git init &&
31 git config receive.denyCurrentBranch warn &&
32 mv .git/hooks .git/hooks-disabled
36 mk_test () {
37 repo_name="$1"
38 shift
40 mk_empty "$repo_name" &&
42 for ref in "$@"
44 git push "$repo_name" $the_first_commit:refs/$ref ||
45 exit
46 done &&
47 cd "$repo_name" &&
48 for ref in "$@"
50 echo "$the_first_commit" >expect &&
51 git show-ref -s --verify refs/$ref >actual &&
52 test_cmp expect actual ||
53 exit
54 done &&
55 git fsck --full
59 mk_test_with_hooks() {
60 repo_name=$1
61 mk_test "$@" &&
63 cd "$repo_name" &&
64 mkdir .git/hooks &&
65 cd .git/hooks &&
67 cat >pre-receive <<-'EOF' &&
68 #!/bin/sh
69 cat - >>pre-receive.actual
70 EOF
72 cat >update <<-'EOF' &&
73 #!/bin/sh
74 printf "%s %s %s\n" "$@" >>update.actual
75 EOF
77 cat >post-receive <<-'EOF' &&
78 #!/bin/sh
79 cat - >>post-receive.actual
80 EOF
82 cat >post-update <<-'EOF' &&
83 #!/bin/sh
84 for ref in "$@"
86 printf "%s\n" "$ref" >>post-update.actual
87 done
88 EOF
90 chmod +x pre-receive update post-receive post-update
94 mk_child() {
95 rm -rf "$2" &&
96 git clone "$1" "$2"
99 check_push_result () {
100 test $# -ge 3 ||
101 BUG "check_push_result requires at least 3 parameters"
103 repo_name="$1"
104 shift
107 cd "$repo_name" &&
108 echo "$1" >expect &&
109 shift &&
110 for ref in "$@"
112 git show-ref -s --verify refs/$ref >actual &&
113 test_cmp expect actual ||
114 exit
115 done &&
116 git fsck --full
120 test_expect_success setup '
122 >path1 &&
123 git add path1 &&
124 test_tick &&
125 git commit -a -m repo &&
126 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
128 >path2 &&
129 git add path2 &&
130 test_tick &&
131 git commit -a -m second &&
132 the_commit=$(git show-ref -s --verify refs/heads/main)
136 test_expect_success 'fetch without wildcard' '
137 mk_empty testrepo &&
139 cd testrepo &&
140 git fetch .. refs/heads/main:refs/remotes/origin/main &&
142 echo "$the_commit commit refs/remotes/origin/main" >expect &&
143 git for-each-ref refs/remotes/origin >actual &&
144 test_cmp expect actual
148 test_expect_success 'fetch with wildcard' '
149 mk_empty testrepo &&
151 cd testrepo &&
152 git config remote.up.url .. &&
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 insteadOf' '
163 mk_empty testrepo &&
165 TRASH=$(pwd)/ &&
166 cd testrepo &&
167 git config "url.$TRASH.insteadOf" 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 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
179 mk_empty testrepo &&
181 TRASH=$(pwd)/ &&
182 cd testrepo &&
183 git config "url.trash/.pushInsteadOf" "$TRASH" &&
184 git config remote.up.url "$TRASH." &&
185 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
186 git fetch up &&
188 echo "$the_commit commit refs/remotes/origin/main" >expect &&
189 git for-each-ref refs/remotes/origin >actual &&
190 test_cmp expect actual
194 grep_wrote () {
195 object_count=$1
196 file_name=$2
197 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
200 test_expect_success 'push with negotiation' '
201 # Without negotiation
202 mk_empty testrepo &&
203 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
204 test_commit -C testrepo unrelated_commit &&
205 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
206 echo now pushing without negotiation &&
207 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
208 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
210 # Same commands, but with negotiation
211 rm event &&
212 mk_empty testrepo &&
213 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
214 test_commit -C testrepo unrelated_commit &&
215 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
216 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
217 grep_wrote 2 event # 1 commit, 1 tree
220 test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
221 rm event &&
222 mk_empty testrepo &&
223 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
224 test_commit -C testrepo unrelated_commit &&
225 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
226 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
227 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
228 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
229 test_i18ngrep "push negotiation failed" err
232 test_expect_success 'push with negotiation does not attempt to fetch submodules' '
233 mk_empty submodule_upstream &&
234 test_commit -C submodule_upstream submodule_commit &&
235 git submodule add ./submodule_upstream submodule &&
236 mk_empty testrepo &&
237 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
238 test_commit -C testrepo unrelated_commit &&
239 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
240 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
241 ! grep "Fetching submodule" err
244 test_expect_success 'push without wildcard' '
245 mk_empty testrepo &&
247 git push testrepo refs/heads/main:refs/remotes/origin/main &&
249 cd testrepo &&
250 echo "$the_commit commit refs/remotes/origin/main" >expect &&
251 git for-each-ref refs/remotes/origin >actual &&
252 test_cmp expect actual
256 test_expect_success 'push with wildcard' '
257 mk_empty testrepo &&
259 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
261 cd testrepo &&
262 echo "$the_commit commit refs/remotes/origin/main" >expect &&
263 git for-each-ref refs/remotes/origin >actual &&
264 test_cmp expect actual
268 test_expect_success 'push with insteadOf' '
269 mk_empty testrepo &&
270 TRASH="$(pwd)/" &&
271 test_config "url.$TRASH.insteadOf" trash/ &&
272 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
274 cd testrepo &&
275 echo "$the_commit commit refs/remotes/origin/main" >expect &&
276 git for-each-ref refs/remotes/origin >actual &&
277 test_cmp expect actual
281 test_expect_success 'push with pushInsteadOf' '
282 mk_empty testrepo &&
283 TRASH="$(pwd)/" &&
284 test_config "url.$TRASH.pushInsteadOf" trash/ &&
285 git push trash/testrepo 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 pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
295 mk_empty testrepo &&
296 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
297 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
298 test_config remote.r.url trash/wrong &&
299 test_config remote.r.pushurl "testrepo/" &&
300 git push r refs/heads/main:refs/remotes/origin/main &&
302 cd testrepo &&
303 echo "$the_commit commit refs/remotes/origin/main" >expect &&
304 git for-each-ref refs/remotes/origin >actual &&
305 test_cmp expect actual
309 test_expect_success 'push with matching heads' '
311 mk_test testrepo heads/main &&
312 git push testrepo : &&
313 check_push_result testrepo $the_commit heads/main
317 test_expect_success 'push with matching heads on the command line' '
319 mk_test testrepo heads/main &&
320 git push testrepo : &&
321 check_push_result testrepo $the_commit heads/main
325 test_expect_success 'failed (non-fast-forward) push with matching heads' '
327 mk_test testrepo heads/main &&
328 git push testrepo : &&
329 git commit --amend -massaged &&
330 test_must_fail git push testrepo &&
331 check_push_result testrepo $the_commit heads/main &&
332 git reset --hard $the_commit
336 test_expect_success 'push --force with matching heads' '
338 mk_test testrepo heads/main &&
339 git push testrepo : &&
340 git commit --amend -massaged &&
341 git push --force testrepo : &&
342 ! check_push_result testrepo $the_commit heads/main &&
343 git reset --hard $the_commit
347 test_expect_success 'push with matching heads and forced update' '
349 mk_test testrepo heads/main &&
350 git push testrepo : &&
351 git commit --amend -massaged &&
352 git push testrepo +: &&
353 ! check_push_result testrepo $the_commit heads/main &&
354 git reset --hard $the_commit
358 test_expect_success 'push with no ambiguity (1)' '
360 mk_test testrepo heads/main &&
361 git push testrepo main:main &&
362 check_push_result testrepo $the_commit heads/main
366 test_expect_success 'push with no ambiguity (2)' '
368 mk_test testrepo remotes/origin/main &&
369 git push testrepo main:origin/main &&
370 check_push_result testrepo $the_commit remotes/origin/main
374 test_expect_success 'push with colon-less refspec, no ambiguity' '
376 mk_test testrepo heads/main heads/t/main &&
377 git branch -f t/main main &&
378 git push testrepo main &&
379 check_push_result testrepo $the_commit heads/main &&
380 check_push_result testrepo $the_first_commit heads/t/main
384 test_expect_success 'push with weak ambiguity (1)' '
386 mk_test testrepo heads/main remotes/origin/main &&
387 git push testrepo main:main &&
388 check_push_result testrepo $the_commit heads/main &&
389 check_push_result testrepo $the_first_commit remotes/origin/main
393 test_expect_success 'push with weak ambiguity (2)' '
395 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
396 git push testrepo main:main &&
397 check_push_result testrepo $the_commit heads/main &&
398 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
402 test_expect_success 'push with ambiguity' '
404 mk_test testrepo heads/frotz tags/frotz &&
405 test_must_fail git push testrepo main:frotz &&
406 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
410 test_expect_success 'push with colon-less refspec (1)' '
412 mk_test testrepo heads/frotz tags/frotz &&
413 git branch -f frotz main &&
414 git push testrepo frotz &&
415 check_push_result testrepo $the_commit heads/frotz &&
416 check_push_result testrepo $the_first_commit tags/frotz
420 test_expect_success 'push with colon-less refspec (2)' '
422 mk_test testrepo heads/frotz tags/frotz &&
423 if git show-ref --verify -q refs/heads/frotz
424 then
425 git branch -D frotz
426 fi &&
427 git tag -f frotz &&
428 git push -f testrepo frotz &&
429 check_push_result testrepo $the_commit tags/frotz &&
430 check_push_result testrepo $the_first_commit heads/frotz
434 test_expect_success 'push with colon-less refspec (3)' '
436 mk_test testrepo &&
437 if git show-ref --verify -q refs/tags/frotz
438 then
439 git tag -d frotz
440 fi &&
441 git branch -f frotz main &&
442 git push testrepo frotz &&
443 check_push_result testrepo $the_commit heads/frotz &&
444 test 1 = $( cd testrepo && git show-ref | wc -l )
447 test_expect_success 'push with colon-less refspec (4)' '
449 mk_test testrepo &&
450 if git show-ref --verify -q refs/heads/frotz
451 then
452 git branch -D frotz
453 fi &&
454 git tag -f frotz &&
455 git push testrepo frotz &&
456 check_push_result testrepo $the_commit tags/frotz &&
457 test 1 = $( cd testrepo && git show-ref | wc -l )
461 test_expect_success 'push head with non-existent, incomplete dest' '
463 mk_test testrepo &&
464 git push testrepo main:branch &&
465 check_push_result testrepo $the_commit heads/branch
469 test_expect_success 'push tag with non-existent, incomplete dest' '
471 mk_test testrepo &&
472 git tag -f v1.0 &&
473 git push testrepo v1.0:tag &&
474 check_push_result testrepo $the_commit tags/tag
478 test_expect_success 'push sha1 with non-existent, incomplete dest' '
480 mk_test testrepo &&
481 test_must_fail git push testrepo $(git rev-parse main):foo
485 test_expect_success 'push ref expression with non-existent, incomplete dest' '
487 mk_test testrepo &&
488 test_must_fail git push testrepo main^:branch
492 for head in HEAD @
495 test_expect_success "push with $head" '
496 mk_test testrepo heads/main &&
497 git checkout main &&
498 git push testrepo $head &&
499 check_push_result testrepo $the_commit heads/main
502 test_expect_success "push with $head nonexisting at remote" '
503 mk_test testrepo heads/main &&
504 git checkout -b local main &&
505 test_when_finished "git checkout main; git branch -D local" &&
506 git push testrepo $head &&
507 check_push_result testrepo $the_commit heads/local
510 test_expect_success "push with +$head" '
511 mk_test testrepo heads/main &&
512 git checkout -b local main &&
513 test_when_finished "git checkout main; git branch -D local" &&
514 git push testrepo main local &&
515 check_push_result testrepo $the_commit heads/main &&
516 check_push_result testrepo $the_commit heads/local &&
518 # Without force rewinding should fail
519 git reset --hard $head^ &&
520 test_must_fail git push testrepo $head &&
521 check_push_result testrepo $the_commit heads/local &&
523 # With force rewinding should succeed
524 git push testrepo +$head &&
525 check_push_result testrepo $the_first_commit heads/local
528 test_expect_success "push $head with non-existent, incomplete dest" '
529 mk_test testrepo &&
530 git checkout main &&
531 git push testrepo $head:branch &&
532 check_push_result testrepo $the_commit heads/branch
536 test_expect_success "push with config remote.*.push = $head" '
537 mk_test testrepo heads/local &&
538 git checkout main &&
539 git branch -f local $the_commit &&
540 test_when_finished "git branch -D local" &&
542 cd testrepo &&
543 git checkout local &&
544 git reset --hard $the_first_commit
545 ) &&
546 test_config remote.there.url testrepo &&
547 test_config remote.there.push $head &&
548 test_config branch.main.remote there &&
549 git push &&
550 check_push_result testrepo $the_commit heads/main &&
551 check_push_result testrepo $the_first_commit heads/local
554 done
556 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
557 mk_test testrepo heads/main &&
558 git checkout $the_first_commit &&
559 test_config remote.there.url testrepo &&
560 test_config remote.there.push refs/heads/main:refs/heads/main &&
561 git push there &&
562 check_push_result testrepo $the_commit heads/main
565 test_expect_success 'push with remote.pushdefault' '
566 mk_test up_repo heads/main &&
567 mk_test down_repo heads/main &&
568 test_config remote.up.url up_repo &&
569 test_config remote.down.url down_repo &&
570 test_config branch.main.remote up &&
571 test_config remote.pushdefault down &&
572 test_config push.default matching &&
573 git push &&
574 check_push_result up_repo $the_first_commit heads/main &&
575 check_push_result down_repo $the_commit heads/main
578 test_expect_success 'push with config remote.*.pushurl' '
580 mk_test testrepo heads/main &&
581 git checkout main &&
582 test_config remote.there.url test2repo &&
583 test_config remote.there.pushurl testrepo &&
584 git push there : &&
585 check_push_result testrepo $the_commit heads/main
588 test_expect_success 'push with config branch.*.pushremote' '
589 mk_test up_repo heads/main &&
590 mk_test side_repo heads/main &&
591 mk_test down_repo heads/main &&
592 test_config remote.up.url up_repo &&
593 test_config remote.pushdefault side_repo &&
594 test_config remote.down.url down_repo &&
595 test_config branch.main.remote up &&
596 test_config branch.main.pushremote down &&
597 test_config push.default matching &&
598 git push &&
599 check_push_result up_repo $the_first_commit heads/main &&
600 check_push_result side_repo $the_first_commit heads/main &&
601 check_push_result down_repo $the_commit heads/main
604 test_expect_success 'branch.*.pushremote config order is irrelevant' '
605 mk_test one_repo heads/main &&
606 mk_test two_repo heads/main &&
607 test_config remote.one.url one_repo &&
608 test_config remote.two.url two_repo &&
609 test_config branch.main.pushremote two_repo &&
610 test_config remote.pushdefault one_repo &&
611 test_config push.default matching &&
612 git push &&
613 check_push_result one_repo $the_first_commit heads/main &&
614 check_push_result two_repo $the_commit heads/main
617 test_expect_success 'push with dry-run' '
619 mk_test testrepo heads/main &&
620 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
621 git push --dry-run testrepo : &&
622 check_push_result testrepo $old_commit heads/main
625 test_expect_success 'push updates local refs' '
627 mk_test testrepo heads/main &&
628 mk_child testrepo child &&
630 cd child &&
631 git pull .. main &&
632 git push &&
633 test $(git rev-parse main) = \
634 $(git rev-parse remotes/origin/main)
639 test_expect_success 'push updates up-to-date local refs' '
641 mk_test testrepo heads/main &&
642 mk_child testrepo child1 &&
643 mk_child testrepo child2 &&
644 (cd child1 && git pull .. main && git push) &&
646 cd child2 &&
647 git pull ../child1 main &&
648 git push &&
649 test $(git rev-parse main) = \
650 $(git rev-parse remotes/origin/main)
655 test_expect_success 'push preserves up-to-date packed refs' '
657 mk_test testrepo heads/main &&
658 mk_child testrepo child &&
660 cd child &&
661 git push &&
662 ! test -f .git/refs/remotes/origin/main
667 test_expect_success 'push does not update local refs on failure' '
669 mk_test testrepo heads/main &&
670 mk_child testrepo child &&
671 mkdir testrepo/.git/hooks &&
672 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
673 chmod +x testrepo/.git/hooks/pre-receive &&
675 cd child &&
676 git pull .. main &&
677 test_must_fail git push &&
678 test $(git rev-parse main) != \
679 $(git rev-parse remotes/origin/main)
684 test_expect_success 'allow deleting an invalid remote ref' '
686 mk_test testrepo heads/branch &&
687 rm -f testrepo/.git/objects/??/* &&
688 git push testrepo :refs/heads/branch &&
689 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
693 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
694 mk_test_with_hooks testrepo heads/main heads/next &&
695 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
696 newmain=$(git show-ref -s --verify refs/heads/main) &&
697 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
698 newnext=$ZERO_OID &&
699 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
701 cd testrepo/.git &&
702 cat >pre-receive.expect <<-EOF &&
703 $orgmain $newmain refs/heads/main
704 $orgnext $newnext refs/heads/next
707 cat >update.expect <<-EOF &&
708 refs/heads/main $orgmain $newmain
709 refs/heads/next $orgnext $newnext
712 cat >post-receive.expect <<-EOF &&
713 $orgmain $newmain refs/heads/main
714 $orgnext $newnext refs/heads/next
717 cat >post-update.expect <<-EOF &&
718 refs/heads/main
719 refs/heads/next
722 test_cmp pre-receive.expect pre-receive.actual &&
723 test_cmp update.expect update.actual &&
724 test_cmp post-receive.expect post-receive.actual &&
725 test_cmp post-update.expect post-update.actual
729 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
730 mk_test_with_hooks testrepo heads/branch &&
731 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
732 rm -f testrepo/.git/objects/??/* &&
733 git push testrepo :refs/heads/branch &&
735 cd testrepo/.git &&
736 cat >pre-receive.expect <<-EOF &&
737 $orig $ZERO_OID refs/heads/branch
740 cat >update.expect <<-EOF &&
741 refs/heads/branch $orig $ZERO_OID
744 cat >post-receive.expect <<-EOF &&
745 $orig $ZERO_OID refs/heads/branch
748 cat >post-update.expect <<-EOF &&
749 refs/heads/branch
752 test_cmp pre-receive.expect pre-receive.actual &&
753 test_cmp update.expect update.actual &&
754 test_cmp post-receive.expect post-receive.actual &&
755 test_cmp post-update.expect post-update.actual
759 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
760 mk_test_with_hooks testrepo heads/main &&
761 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
762 newmain=$(git show-ref -s --verify refs/heads/main) &&
763 git push testrepo main :refs/heads/nonexistent &&
765 cd testrepo/.git &&
766 cat >pre-receive.expect <<-EOF &&
767 $orgmain $newmain refs/heads/main
768 $ZERO_OID $ZERO_OID refs/heads/nonexistent
771 cat >update.expect <<-EOF &&
772 refs/heads/main $orgmain $newmain
773 refs/heads/nonexistent $ZERO_OID $ZERO_OID
776 cat >post-receive.expect <<-EOF &&
777 $orgmain $newmain refs/heads/main
780 cat >post-update.expect <<-EOF &&
781 refs/heads/main
784 test_cmp pre-receive.expect pre-receive.actual &&
785 test_cmp update.expect update.actual &&
786 test_cmp post-receive.expect post-receive.actual &&
787 test_cmp post-update.expect post-update.actual
791 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
792 mk_test_with_hooks testrepo heads/main &&
793 git push testrepo :refs/heads/nonexistent &&
795 cd testrepo/.git &&
796 cat >pre-receive.expect <<-EOF &&
797 $ZERO_OID $ZERO_OID refs/heads/nonexistent
800 cat >update.expect <<-EOF &&
801 refs/heads/nonexistent $ZERO_OID $ZERO_OID
804 test_cmp pre-receive.expect pre-receive.actual &&
805 test_cmp update.expect update.actual &&
806 test_path_is_missing post-receive.actual &&
807 test_path_is_missing post-update.actual
811 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
812 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
813 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
814 newmain=$(git show-ref -s --verify refs/heads/main) &&
815 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
816 newnext=$ZERO_OID &&
817 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
818 newseen=$(git show-ref -s --verify refs/heads/main) &&
819 git push testrepo refs/heads/main:refs/heads/main \
820 refs/heads/main:refs/heads/seen :refs/heads/next \
821 :refs/heads/nonexistent &&
823 cd testrepo/.git &&
824 cat >pre-receive.expect <<-EOF &&
825 $orgmain $newmain refs/heads/main
826 $orgnext $newnext refs/heads/next
827 $orgseen $newseen refs/heads/seen
828 $ZERO_OID $ZERO_OID refs/heads/nonexistent
831 cat >update.expect <<-EOF &&
832 refs/heads/main $orgmain $newmain
833 refs/heads/next $orgnext $newnext
834 refs/heads/seen $orgseen $newseen
835 refs/heads/nonexistent $ZERO_OID $ZERO_OID
838 cat >post-receive.expect <<-EOF &&
839 $orgmain $newmain refs/heads/main
840 $orgnext $newnext refs/heads/next
841 $orgseen $newseen refs/heads/seen
844 cat >post-update.expect <<-EOF &&
845 refs/heads/main
846 refs/heads/next
847 refs/heads/seen
850 test_cmp pre-receive.expect pre-receive.actual &&
851 test_cmp update.expect update.actual &&
852 test_cmp post-receive.expect post-receive.actual &&
853 test_cmp post-update.expect post-update.actual
857 test_expect_success 'allow deleting a ref using --delete' '
858 mk_test testrepo heads/main &&
859 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
860 git push testrepo --delete main &&
861 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
864 test_expect_success 'allow deleting a tag using --delete' '
865 mk_test testrepo heads/main &&
866 git tag -a -m dummy_message deltag heads/main &&
867 git push testrepo --tags &&
868 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
869 git push testrepo --delete tag deltag &&
870 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
873 test_expect_success 'push --delete without args aborts' '
874 mk_test testrepo heads/main &&
875 test_must_fail git push testrepo --delete
878 test_expect_success 'push --delete refuses src:dest refspecs' '
879 mk_test testrepo heads/main &&
880 test_must_fail git push testrepo --delete main:foo
883 test_expect_success 'push --delete refuses empty string' '
884 mk_test testrepo heads/master &&
885 test_must_fail git push testrepo --delete ""
888 test_expect_success 'warn on push to HEAD of non-bare repository' '
889 mk_test testrepo heads/main &&
891 cd testrepo &&
892 git checkout main &&
893 git config receive.denyCurrentBranch warn
894 ) &&
895 git push testrepo main 2>stderr &&
896 grep "warning: updating the current branch" stderr
899 test_expect_success 'deny push to HEAD of non-bare repository' '
900 mk_test testrepo heads/main &&
902 cd testrepo &&
903 git checkout main &&
904 git config receive.denyCurrentBranch true
905 ) &&
906 test_must_fail git push testrepo main
909 test_expect_success 'allow push to HEAD of bare repository (bare)' '
910 mk_test testrepo heads/main &&
912 cd testrepo &&
913 git checkout main &&
914 git config receive.denyCurrentBranch true &&
915 git config core.bare true
916 ) &&
917 git push testrepo main 2>stderr &&
918 ! grep "warning: updating the current branch" stderr
921 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
922 mk_test testrepo heads/main &&
924 cd testrepo &&
925 git checkout main &&
926 git config receive.denyCurrentBranch false
927 ) &&
928 git push testrepo main 2>stderr &&
929 ! grep "warning: updating the current branch" stderr
932 test_expect_success 'fetch with branches' '
933 mk_empty testrepo &&
934 git branch second $the_first_commit &&
935 git checkout second &&
936 echo ".." > testrepo/.git/branches/branch1 &&
938 cd testrepo &&
939 git fetch branch1 &&
940 echo "$the_commit commit refs/heads/branch1" >expect &&
941 git for-each-ref refs/heads >actual &&
942 test_cmp expect actual
943 ) &&
944 git checkout main
947 test_expect_success 'fetch with branches containing #' '
948 mk_empty testrepo &&
949 echo "..#second" > testrepo/.git/branches/branch2 &&
951 cd testrepo &&
952 git fetch branch2 &&
953 echo "$the_first_commit commit refs/heads/branch2" >expect &&
954 git for-each-ref refs/heads >actual &&
955 test_cmp expect actual
956 ) &&
957 git checkout main
960 test_expect_success 'push with branches' '
961 mk_empty testrepo &&
962 git checkout second &&
963 echo "testrepo" > .git/branches/branch1 &&
964 git push branch1 &&
966 cd testrepo &&
967 echo "$the_first_commit commit refs/heads/main" >expect &&
968 git for-each-ref refs/heads >actual &&
969 test_cmp expect actual
973 test_expect_success 'push with branches containing #' '
974 mk_empty testrepo &&
975 echo "testrepo#branch3" > .git/branches/branch2 &&
976 git push branch2 &&
978 cd testrepo &&
979 echo "$the_first_commit commit refs/heads/branch3" >expect &&
980 git for-each-ref refs/heads >actual &&
981 test_cmp expect actual
982 ) &&
983 git checkout main
986 test_expect_success 'push into aliased refs (consistent)' '
987 mk_test testrepo heads/main &&
988 mk_child testrepo child1 &&
989 mk_child testrepo child2 &&
991 cd child1 &&
992 git branch foo &&
993 git symbolic-ref refs/heads/bar refs/heads/foo &&
994 git config receive.denyCurrentBranch false
995 ) &&
997 cd child2 &&
998 >path2 &&
999 git add path2 &&
1000 test_tick &&
1001 git commit -a -m child2 &&
1002 git branch foo &&
1003 git branch bar &&
1004 git push ../child1 foo bar
1008 test_expect_success 'push into aliased refs (inconsistent)' '
1009 mk_test testrepo heads/main &&
1010 mk_child testrepo child1 &&
1011 mk_child testrepo child2 &&
1013 cd child1 &&
1014 git branch foo &&
1015 git symbolic-ref refs/heads/bar refs/heads/foo &&
1016 git config receive.denyCurrentBranch false
1017 ) &&
1019 cd child2 &&
1020 >path2 &&
1021 git add path2 &&
1022 test_tick &&
1023 git commit -a -m child2 &&
1024 git branch foo &&
1025 >path3 &&
1026 git add path3 &&
1027 test_tick &&
1028 git commit -a -m child2 &&
1029 git branch bar &&
1030 test_must_fail git push ../child1 foo bar 2>stderr &&
1031 grep "refusing inconsistent update" stderr
1035 test_force_push_tag () {
1036 tag_type_description=$1
1037 tag_args=$2
1039 test_expect_success "force pushing required to update $tag_type_description" "
1040 mk_test testrepo heads/main &&
1041 mk_child testrepo child1 &&
1042 mk_child testrepo child2 &&
1044 cd child1 &&
1045 git tag testTag &&
1046 git push ../child2 testTag &&
1047 >file1 &&
1048 git add file1 &&
1049 git commit -m 'file1' &&
1050 git tag $tag_args testTag &&
1051 test_must_fail git push ../child2 testTag &&
1052 git push --force ../child2 testTag &&
1053 git tag $tag_args testTag HEAD~ &&
1054 test_must_fail git push ../child2 testTag &&
1055 git push --force ../child2 testTag &&
1057 # Clobbering without + in refspec needs --force
1058 git tag -f testTag &&
1059 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1060 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1062 # Clobbering with + in refspec does not need --force
1063 git tag -f testTag HEAD~ &&
1064 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1066 # Clobbering with --no-force still obeys + in refspec
1067 git tag -f testTag &&
1068 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1070 # Clobbering with/without --force and 'tag <name>' format
1071 git tag -f testTag HEAD~ &&
1072 test_must_fail git push ../child2 tag testTag &&
1073 git push --force ../child2 tag testTag
1078 test_force_push_tag "lightweight tag" "-f"
1079 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1081 test_force_fetch_tag () {
1082 tag_type_description=$1
1083 tag_args=$2
1085 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1086 mk_test testrepo heads/main &&
1087 mk_child testrepo child1 &&
1088 mk_child testrepo child2 &&
1090 cd testrepo &&
1091 git tag testTag &&
1092 git -C ../child1 fetch origin tag testTag &&
1093 >file1 &&
1094 git add file1 &&
1095 git commit -m 'file1' &&
1096 git tag $tag_args testTag &&
1097 test_must_fail git -C ../child1 fetch origin tag testTag &&
1098 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1103 test_force_fetch_tag "lightweight tag" "-f"
1104 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1106 test_expect_success 'push --porcelain' '
1107 mk_empty testrepo &&
1108 echo >.git/foo "To testrepo" &&
1109 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1110 echo >>.git/foo "Done" &&
1111 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1113 cd testrepo &&
1114 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1115 git for-each-ref refs/remotes/origin >actual &&
1116 test_cmp expect actual
1117 ) &&
1118 test_cmp .git/foo .git/bar
1121 test_expect_success 'push --porcelain bad url' '
1122 mk_empty testrepo &&
1123 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1124 ! grep -q Done .git/bar
1127 test_expect_success 'push --porcelain rejected' '
1128 mk_empty testrepo &&
1129 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1130 (cd testrepo &&
1131 git reset --hard origin/main^ &&
1132 git config receive.denyCurrentBranch true) &&
1134 echo >.git/foo "To testrepo" &&
1135 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1136 echo >>.git/foo "Done" &&
1138 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1139 test_cmp .git/foo .git/bar
1142 test_expect_success 'push --porcelain --dry-run rejected' '
1143 mk_empty testrepo &&
1144 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1145 (cd testrepo &&
1146 git reset --hard origin/main &&
1147 git config receive.denyCurrentBranch true) &&
1149 echo >.git/foo "To testrepo" &&
1150 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1151 echo >>.git/foo "Done" &&
1153 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1154 test_cmp .git/foo .git/bar
1157 test_expect_success 'push --prune' '
1158 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1159 git push --prune testrepo : &&
1160 check_push_result testrepo $the_commit heads/main &&
1161 check_push_result testrepo $the_first_commit heads/second &&
1162 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1165 test_expect_success 'push --prune refspec' '
1166 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1167 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1168 check_push_result testrepo $the_commit tmp/main &&
1169 check_push_result testrepo $the_first_commit tmp/second &&
1170 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1173 for configsection in transfer receive
1175 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1176 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1178 cd testrepo &&
1179 git config $configsection.hiderefs refs/hidden
1180 ) &&
1182 # push to unhidden ref succeeds normally
1183 git push testrepo main:refs/heads/main &&
1184 check_push_result testrepo $the_commit heads/main &&
1186 # push to update a hidden ref should fail
1187 test_must_fail git push testrepo main:refs/hidden/one &&
1188 check_push_result testrepo $the_first_commit hidden/one &&
1190 # push to delete a hidden ref should fail
1191 test_must_fail git push testrepo :refs/hidden/two &&
1192 check_push_result testrepo $the_first_commit hidden/two &&
1194 # idempotent push to update a hidden ref should fail
1195 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1196 check_push_result testrepo $the_first_commit hidden/three
1198 done
1200 test_expect_success 'fetch exact SHA1' '
1201 mk_test testrepo heads/main hidden/one &&
1202 git push testrepo main:refs/hidden/one &&
1204 cd testrepo &&
1205 git config transfer.hiderefs refs/hidden
1206 ) &&
1207 check_push_result testrepo $the_commit hidden/one &&
1209 mk_child testrepo child &&
1211 cd child &&
1213 # make sure $the_commit does not exist here
1214 git repack -a -d &&
1215 git prune &&
1216 test_must_fail git cat-file -t $the_commit &&
1218 # Some protocol versions (e.g. 2) support fetching
1219 # unadvertised objects, so restrict this test to v0.
1221 # fetching the hidden object should fail by default
1222 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1223 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1224 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1225 test_must_fail git rev-parse --verify refs/heads/copy &&
1227 # the server side can allow it to succeed
1229 cd ../testrepo &&
1230 git config uploadpack.allowtipsha1inwant true
1231 ) &&
1233 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1234 cat >expect <<-EOF &&
1235 $the_commit
1236 $the_first_commit
1239 git rev-parse --verify refs/heads/copy &&
1240 git rev-parse --verify refs/heads/extra
1241 } >actual &&
1242 test_cmp expect actual
1246 test_expect_success 'fetch exact SHA1 in protocol v2' '
1247 mk_test testrepo heads/main hidden/one &&
1248 git push testrepo main:refs/hidden/one &&
1249 git -C testrepo config transfer.hiderefs refs/hidden &&
1250 check_push_result testrepo $the_commit hidden/one &&
1252 mk_child testrepo child &&
1253 git -C child config protocol.version 2 &&
1255 # make sure $the_commit does not exist here
1256 git -C child repack -a -d &&
1257 git -C child prune &&
1258 test_must_fail git -C child cat-file -t $the_commit &&
1260 # fetching the hidden object succeeds by default
1261 # NEEDSWORK: should this match the v0 behavior instead?
1262 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1265 for configallowtipsha1inwant in true false
1267 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1268 mk_empty testrepo &&
1270 cd testrepo &&
1271 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1272 git commit --allow-empty -m foo &&
1273 git commit --allow-empty -m bar
1274 ) &&
1275 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1276 mk_empty shallow &&
1278 cd shallow &&
1279 # Some protocol versions (e.g. 2) support fetching
1280 # unadvertised objects, so restrict this test to v0.
1281 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1282 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1283 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1284 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1285 git cat-file commit $SHA1
1289 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1290 mk_empty testrepo &&
1292 cd testrepo &&
1293 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1294 git commit --allow-empty -m foo &&
1295 git commit --allow-empty -m bar &&
1296 git commit --allow-empty -m xyz
1297 ) &&
1298 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1299 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1300 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1302 cd testrepo &&
1303 git reset --hard $SHA1_2 &&
1304 git cat-file commit $SHA1_1 &&
1305 git cat-file commit $SHA1_3
1306 ) &&
1307 mk_empty shallow &&
1309 cd shallow &&
1310 # Some protocol versions (e.g. 2) support fetching
1311 # unadvertised objects, so restrict this test to v0.
1312 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1313 git fetch ../testrepo/.git $SHA1_3 &&
1314 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1315 git fetch ../testrepo/.git $SHA1_1 &&
1316 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1317 git fetch ../testrepo/.git $SHA1_1 &&
1318 git cat-file commit $SHA1_1 &&
1319 test_must_fail git cat-file commit $SHA1_2 &&
1320 git fetch ../testrepo/.git $SHA1_2 &&
1321 git cat-file commit $SHA1_2 &&
1322 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1323 git fetch ../testrepo/.git $SHA1_3 2>err &&
1324 # ideally we would insist this be on a "remote error:"
1325 # line, but it is racy; see the commit message
1326 test_i18ngrep "not our ref.*$SHA1_3\$" err
1329 done
1331 test_expect_success 'fetch follows tags by default' '
1332 mk_test testrepo heads/main &&
1333 rm -fr src dst &&
1334 git init src &&
1336 cd src &&
1337 git pull ../testrepo main &&
1338 git tag -m "annotated" tag &&
1339 git for-each-ref >tmp1 &&
1340 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1341 sort -k 3 >../expect
1342 ) &&
1343 git init dst &&
1345 cd dst &&
1346 git remote add origin ../src &&
1347 git config branch.main.remote origin &&
1348 git config branch.main.merge refs/heads/main &&
1349 git pull &&
1350 git for-each-ref >../actual
1351 ) &&
1352 test_cmp expect actual
1355 test_expect_success 'peeled advertisements are not considered ref tips' '
1356 mk_empty testrepo &&
1357 git -C testrepo commit --allow-empty -m one &&
1358 git -C testrepo commit --allow-empty -m two &&
1359 git -C testrepo tag -m foo mytag HEAD^ &&
1360 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1361 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1362 git fetch testrepo $oid 2>err &&
1363 test_i18ngrep "Server does not allow request for unadvertised object" err
1366 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1367 mk_test testrepo heads/main &&
1368 rm -fr src dst &&
1369 git init src &&
1370 git init --bare dst &&
1372 cd src &&
1373 git pull ../testrepo main &&
1374 git branch next &&
1375 git config remote.dst.url ../dst &&
1376 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1377 git push dst main &&
1378 git show-ref refs/heads/main |
1379 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1380 ) &&
1382 cd dst &&
1383 test_must_fail git show-ref refs/heads/next &&
1384 test_must_fail git show-ref refs/heads/main &&
1385 git show-ref refs/remotes/src/main >actual
1386 ) &&
1387 test_cmp dst/expect dst/actual
1390 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1391 mk_test testrepo heads/main &&
1392 rm -fr src dst &&
1393 git init src &&
1394 git init --bare dst &&
1396 cd src &&
1397 git pull ../testrepo main &&
1398 git branch next &&
1399 git config remote.dst.url ../dst &&
1400 git config push.default matching &&
1401 git push dst main &&
1402 git show-ref refs/heads/main >../dst/expect
1403 ) &&
1405 cd dst &&
1406 test_must_fail git show-ref refs/heads/next &&
1407 git show-ref refs/heads/main >actual
1408 ) &&
1409 test_cmp dst/expect dst/actual
1412 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1413 mk_test testrepo heads/main &&
1414 rm -fr src dst &&
1415 git init src &&
1416 git init --bare dst &&
1418 cd src &&
1419 git pull ../testrepo main &&
1420 git branch next &&
1421 git config remote.dst.url ../dst &&
1422 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1423 git config push.default upstream &&
1425 git config branch.main.merge refs/heads/trunk &&
1426 git config branch.main.remote dst &&
1428 git push dst main &&
1429 git show-ref refs/heads/main |
1430 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1431 ) &&
1433 cd dst &&
1434 test_must_fail git show-ref refs/heads/main &&
1435 test_must_fail git show-ref refs/heads/next &&
1436 git show-ref refs/heads/trunk >actual
1437 ) &&
1438 test_cmp dst/expect dst/actual
1441 test_expect_success 'push does not follow tags by default' '
1442 mk_test testrepo heads/main &&
1443 rm -fr src dst &&
1444 git init src &&
1445 git init --bare dst &&
1447 cd src &&
1448 git pull ../testrepo main &&
1449 git tag -m "annotated" tag &&
1450 git checkout -b another &&
1451 git commit --allow-empty -m "future commit" &&
1452 git tag -m "future" future &&
1453 git checkout main &&
1454 git for-each-ref refs/heads/main >../expect &&
1455 git push ../dst main
1456 ) &&
1458 cd dst &&
1459 git for-each-ref >../actual
1460 ) &&
1461 test_cmp expect actual
1464 test_expect_success 'push --follow-tags only pushes relevant tags' '
1465 mk_test testrepo heads/main &&
1466 rm -fr src dst &&
1467 git init src &&
1468 git init --bare dst &&
1470 cd src &&
1471 git pull ../testrepo main &&
1472 git tag -m "annotated" tag &&
1473 git checkout -b another &&
1474 git commit --allow-empty -m "future commit" &&
1475 git tag -m "future" future &&
1476 git checkout main &&
1477 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1478 git push --follow-tags ../dst main
1479 ) &&
1481 cd dst &&
1482 git for-each-ref >../actual
1483 ) &&
1484 test_cmp expect actual
1487 test_expect_success 'push --no-thin must produce non-thin pack' '
1488 cat >>path1 <<\EOF &&
1489 keep base version of path1 big enough, compared to the new changes
1490 later, in order to pass size heuristics in
1491 builtin/pack-objects.c:try_delta()
1493 git commit -am initial &&
1494 git init no-thin &&
1495 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1496 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1497 echo modified >> path1 &&
1498 git commit -am modified &&
1499 git repack -adf &&
1500 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1501 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1504 test_expect_success 'pushing a tag pushes the tagged object' '
1505 rm -rf dst.git &&
1506 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1507 git tag -m foo tag-of-blob $blob &&
1508 git init --bare dst.git &&
1509 git push dst.git tag-of-blob &&
1510 # the receiving index-pack should have noticed
1511 # any problems, but we double check
1512 echo unreferenced >expect &&
1513 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1514 test_cmp expect actual
1517 test_expect_success 'push into bare respects core.logallrefupdates' '
1518 rm -rf dst.git &&
1519 git init --bare dst.git &&
1520 git -C dst.git config core.logallrefupdates true &&
1522 # double push to test both with and without
1523 # the actual pack transfer
1524 git push dst.git main:one &&
1525 echo "one@{0} push" >expect &&
1526 git -C dst.git log -g --format="%gd %gs" one >actual &&
1527 test_cmp expect actual &&
1529 git push dst.git main:two &&
1530 echo "two@{0} push" >expect &&
1531 git -C dst.git log -g --format="%gd %gs" two >actual &&
1532 test_cmp expect actual
1535 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1536 rm -rf dst.git &&
1537 git init --bare dst.git &&
1539 cd dst.git &&
1540 git config core.logallrefupdates true &&
1542 # as above, we double-fetch to test both
1543 # with and without pack transfer
1544 git fetch .. main:one &&
1545 echo "one@{0} fetch .. main:one: storing head" >expect &&
1546 git log -g --format="%gd %gs" one >actual &&
1547 test_cmp expect actual &&
1549 git fetch .. main:two &&
1550 echo "two@{0} fetch .. main:two: storing head" >expect &&
1551 git log -g --format="%gd %gs" two >actual &&
1552 test_cmp expect actual
1556 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1557 git push testrepo main &&
1559 cd testrepo &&
1560 git reset --hard &&
1561 git config receive.denyCurrentBranch updateInstead
1562 ) &&
1563 test_commit third path2 &&
1565 # Try pushing into a repository with pristine working tree
1566 git push testrepo main &&
1568 cd testrepo &&
1569 git update-index -q --refresh &&
1570 git diff-files --quiet -- &&
1571 git diff-index --quiet --cached HEAD -- &&
1572 test third = "$(cat path2)" &&
1573 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1574 ) &&
1576 # Try pushing into a repository with working tree needing a refresh
1578 cd testrepo &&
1579 git reset --hard HEAD^ &&
1580 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1581 test-tool chmtime +100 path1
1582 ) &&
1583 git push testrepo main &&
1585 cd testrepo &&
1586 git update-index -q --refresh &&
1587 git diff-files --quiet -- &&
1588 git diff-index --quiet --cached HEAD -- &&
1589 test_cmp ../path1 path1 &&
1590 test third = "$(cat path2)" &&
1591 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1592 ) &&
1594 # Update what is to be pushed
1595 test_commit fourth path2 &&
1597 # Try pushing into a repository with a dirty working tree
1598 # (1) the working tree updated
1600 cd testrepo &&
1601 echo changed >path1
1602 ) &&
1603 test_must_fail git push testrepo main &&
1605 cd testrepo &&
1606 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1607 git diff --quiet --cached &&
1608 test changed = "$(cat path1)"
1609 ) &&
1611 # (2) the index updated
1613 cd testrepo &&
1614 echo changed >path1 &&
1615 git add path1
1616 ) &&
1617 test_must_fail git push testrepo main &&
1619 cd testrepo &&
1620 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1621 git diff --quiet &&
1622 test changed = "$(cat path1)"
1623 ) &&
1625 # Introduce a new file in the update
1626 test_commit fifth path3 &&
1628 # (3) the working tree has an untracked file that would interfere
1630 cd testrepo &&
1631 git reset --hard &&
1632 echo changed >path3
1633 ) &&
1634 test_must_fail git push testrepo main &&
1636 cd testrepo &&
1637 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1638 git diff --quiet &&
1639 git diff --quiet --cached &&
1640 test changed = "$(cat path3)"
1641 ) &&
1643 # (4) the target changes to what gets pushed but it still is a change
1645 cd testrepo &&
1646 git reset --hard &&
1647 echo fifth >path3 &&
1648 git add path3
1649 ) &&
1650 test_must_fail git push testrepo main &&
1652 cd testrepo &&
1653 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1654 git diff --quiet &&
1655 test fifth = "$(cat path3)"
1656 ) &&
1658 # (5) push into void
1659 rm -fr void &&
1660 git init void &&
1662 cd void &&
1663 git config receive.denyCurrentBranch updateInstead
1664 ) &&
1665 git push void main &&
1667 cd void &&
1668 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1669 git diff --quiet &&
1670 git diff --cached --quiet
1671 ) &&
1673 # (6) updateInstead intervened by fast-forward check
1674 test_must_fail git push void main^:main &&
1675 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1676 git -C void diff --quiet &&
1677 git -C void diff --cached --quiet
1680 test_expect_success 'updateInstead with push-to-checkout hook' '
1681 rm -fr testrepo &&
1682 git init testrepo &&
1684 cd testrepo &&
1685 git pull .. main &&
1686 git reset --hard HEAD^^ &&
1687 git tag initial &&
1688 git config receive.denyCurrentBranch updateInstead &&
1689 write_script .git/hooks/push-to-checkout <<-\EOF
1690 echo >&2 updating from $(git rev-parse HEAD)
1691 echo >&2 updating to "$1"
1693 git update-index -q --refresh &&
1694 git read-tree -u -m HEAD "$1" || {
1695 status=$?
1696 echo >&2 read-tree failed
1697 exit $status
1700 ) &&
1702 # Try pushing into a pristine
1703 git push testrepo main &&
1705 cd testrepo &&
1706 git diff --quiet &&
1707 git diff HEAD --quiet &&
1708 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1709 ) &&
1711 # Try pushing into a repository with conflicting change
1713 cd testrepo &&
1714 git reset --hard initial &&
1715 echo conflicting >path2
1716 ) &&
1717 test_must_fail git push testrepo main &&
1719 cd testrepo &&
1720 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1721 test conflicting = "$(cat path2)" &&
1722 git diff-index --quiet --cached HEAD
1723 ) &&
1725 # Try pushing into a repository with unrelated change
1727 cd testrepo &&
1728 git reset --hard initial &&
1729 echo unrelated >path1 &&
1730 echo irrelevant >path5 &&
1731 git add path5
1732 ) &&
1733 git push testrepo main &&
1735 cd testrepo &&
1736 test "$(cat path1)" = unrelated &&
1737 test "$(cat path5)" = irrelevant &&
1738 test "$(git diff --name-only --cached HEAD)" = path5 &&
1739 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1740 ) &&
1742 # push into void
1743 rm -fr void &&
1744 git init void &&
1746 cd void &&
1747 git config receive.denyCurrentBranch updateInstead &&
1748 write_script .git/hooks/push-to-checkout <<-\EOF
1749 if git rev-parse --quiet --verify HEAD
1750 then
1751 has_head=yes
1752 echo >&2 updating from $(git rev-parse HEAD)
1753 else
1754 has_head=no
1755 echo >&2 pushing into void
1757 echo >&2 updating to "$1"
1759 git update-index -q --refresh &&
1760 case "$has_head" in
1761 yes)
1762 git read-tree -u -m HEAD "$1" ;;
1764 git read-tree -u -m "$1" ;;
1765 esac || {
1766 status=$?
1767 echo >&2 read-tree failed
1768 exit $status
1771 ) &&
1773 git push void main &&
1775 cd void &&
1776 git diff --quiet &&
1777 git diff --cached --quiet &&
1778 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1782 test_expect_success 'denyCurrentBranch and worktrees' '
1783 git worktree add new-wt &&
1784 git clone . cloned &&
1785 test_commit -C cloned first &&
1786 test_config receive.denyCurrentBranch refuse &&
1787 test_must_fail git -C cloned push origin HEAD:new-wt &&
1788 test_config receive.denyCurrentBranch updateInstead &&
1789 git -C cloned push origin HEAD:new-wt &&
1790 test_path_exists new-wt/first.t &&
1791 test_must_fail git -C cloned push --delete origin new-wt
1794 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1795 test_when_finished "rm -fr bare.git" &&
1796 git clone --bare . bare.git &&
1797 git -C bare.git worktree add wt &&
1798 test_commit grape &&
1799 git -C bare.git config receive.denyCurrentBranch refuse &&
1800 test_must_fail git push bare.git HEAD:wt &&
1801 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1802 git push bare.git HEAD:wt &&
1803 test_path_exists bare.git/wt/grape.t &&
1804 test_must_fail git push --delete bare.git wt
1807 test_expect_success 'refuse fetch to current branch of worktree' '
1808 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1809 git worktree add wt &&
1810 test_commit apple &&
1811 test_must_fail git fetch . HEAD:wt &&
1812 git fetch -u . HEAD:wt
1815 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1816 test_when_finished "rm -fr bare.git" &&
1817 git clone --bare . bare.git &&
1818 git -C bare.git worktree add wt &&
1819 test_commit banana &&
1820 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1821 git -C bare.git fetch -u .. HEAD:wt
1824 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1825 mk_empty testrepo &&
1826 git -C testrepo config receive.hiderefs refs/hidden &&
1827 git -C testrepo config receive.unpackLimit 1 &&
1828 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1829 test_dir_is_empty testrepo/.git/objects/pack
1832 test_done