Merge branch 'js/range-diff-wo-dotdot'
[git/debian.git] / t / t5516-fetch-push.sh
blob15262b41929d81fe51ea03d1aba298860ca346b3
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 test_expect_success 'push without wildcard' '
195 mk_empty testrepo &&
197 git push testrepo refs/heads/main:refs/remotes/origin/main &&
199 cd testrepo &&
200 echo "$the_commit commit refs/remotes/origin/main" >expect &&
201 git for-each-ref refs/remotes/origin >actual &&
202 test_cmp expect actual
206 test_expect_success 'push with wildcard' '
207 mk_empty testrepo &&
209 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
211 cd testrepo &&
212 echo "$the_commit commit refs/remotes/origin/main" >expect &&
213 git for-each-ref refs/remotes/origin >actual &&
214 test_cmp expect actual
218 test_expect_success 'push with insteadOf' '
219 mk_empty testrepo &&
220 TRASH="$(pwd)/" &&
221 test_config "url.$TRASH.insteadOf" trash/ &&
222 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
224 cd testrepo &&
225 echo "$the_commit commit refs/remotes/origin/main" >expect &&
226 git for-each-ref refs/remotes/origin >actual &&
227 test_cmp expect actual
231 test_expect_success 'push with pushInsteadOf' '
232 mk_empty testrepo &&
233 TRASH="$(pwd)/" &&
234 test_config "url.$TRASH.pushInsteadOf" trash/ &&
235 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
237 cd testrepo &&
238 echo "$the_commit commit refs/remotes/origin/main" >expect &&
239 git for-each-ref refs/remotes/origin >actual &&
240 test_cmp expect actual
244 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
245 mk_empty testrepo &&
246 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
247 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
248 test_config remote.r.url trash/wrong &&
249 test_config remote.r.pushurl "testrepo/" &&
250 git push r refs/heads/main:refs/remotes/origin/main &&
252 cd testrepo &&
253 echo "$the_commit commit refs/remotes/origin/main" >expect &&
254 git for-each-ref refs/remotes/origin >actual &&
255 test_cmp expect actual
259 test_expect_success 'push with matching heads' '
261 mk_test testrepo heads/main &&
262 git push testrepo : &&
263 check_push_result testrepo $the_commit heads/main
267 test_expect_success 'push with matching heads on the command line' '
269 mk_test testrepo heads/main &&
270 git push testrepo : &&
271 check_push_result testrepo $the_commit heads/main
275 test_expect_success 'failed (non-fast-forward) push with matching heads' '
277 mk_test testrepo heads/main &&
278 git push testrepo : &&
279 git commit --amend -massaged &&
280 test_must_fail git push testrepo &&
281 check_push_result testrepo $the_commit heads/main &&
282 git reset --hard $the_commit
286 test_expect_success 'push --force with matching heads' '
288 mk_test testrepo heads/main &&
289 git push testrepo : &&
290 git commit --amend -massaged &&
291 git push --force testrepo : &&
292 ! check_push_result testrepo $the_commit heads/main &&
293 git reset --hard $the_commit
297 test_expect_success 'push with matching heads and forced update' '
299 mk_test testrepo heads/main &&
300 git push testrepo : &&
301 git commit --amend -massaged &&
302 git push testrepo +: &&
303 ! check_push_result testrepo $the_commit heads/main &&
304 git reset --hard $the_commit
308 test_expect_success 'push with no ambiguity (1)' '
310 mk_test testrepo heads/main &&
311 git push testrepo main:main &&
312 check_push_result testrepo $the_commit heads/main
316 test_expect_success 'push with no ambiguity (2)' '
318 mk_test testrepo remotes/origin/main &&
319 git push testrepo main:origin/main &&
320 check_push_result testrepo $the_commit remotes/origin/main
324 test_expect_success 'push with colon-less refspec, no ambiguity' '
326 mk_test testrepo heads/main heads/t/main &&
327 git branch -f t/main main &&
328 git push testrepo main &&
329 check_push_result testrepo $the_commit heads/main &&
330 check_push_result testrepo $the_first_commit heads/t/main
334 test_expect_success 'push with weak ambiguity (1)' '
336 mk_test testrepo heads/main remotes/origin/main &&
337 git push testrepo main:main &&
338 check_push_result testrepo $the_commit heads/main &&
339 check_push_result testrepo $the_first_commit remotes/origin/main
343 test_expect_success 'push with weak ambiguity (2)' '
345 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
346 git push testrepo main:main &&
347 check_push_result testrepo $the_commit heads/main &&
348 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
352 test_expect_success 'push with ambiguity' '
354 mk_test testrepo heads/frotz tags/frotz &&
355 test_must_fail git push testrepo main:frotz &&
356 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
360 test_expect_success 'push with colon-less refspec (1)' '
362 mk_test testrepo heads/frotz tags/frotz &&
363 git branch -f frotz main &&
364 git push testrepo frotz &&
365 check_push_result testrepo $the_commit heads/frotz &&
366 check_push_result testrepo $the_first_commit tags/frotz
370 test_expect_success 'push with colon-less refspec (2)' '
372 mk_test testrepo heads/frotz tags/frotz &&
373 if git show-ref --verify -q refs/heads/frotz
374 then
375 git branch -D frotz
376 fi &&
377 git tag -f frotz &&
378 git push -f testrepo frotz &&
379 check_push_result testrepo $the_commit tags/frotz &&
380 check_push_result testrepo $the_first_commit heads/frotz
384 test_expect_success 'push with colon-less refspec (3)' '
386 mk_test testrepo &&
387 if git show-ref --verify -q refs/tags/frotz
388 then
389 git tag -d frotz
390 fi &&
391 git branch -f frotz main &&
392 git push testrepo frotz &&
393 check_push_result testrepo $the_commit heads/frotz &&
394 test 1 = $( cd testrepo && git show-ref | wc -l )
397 test_expect_success 'push with colon-less refspec (4)' '
399 mk_test testrepo &&
400 if git show-ref --verify -q refs/heads/frotz
401 then
402 git branch -D frotz
403 fi &&
404 git tag -f frotz &&
405 git push testrepo frotz &&
406 check_push_result testrepo $the_commit tags/frotz &&
407 test 1 = $( cd testrepo && git show-ref | wc -l )
411 test_expect_success 'push head with non-existent, incomplete dest' '
413 mk_test testrepo &&
414 git push testrepo main:branch &&
415 check_push_result testrepo $the_commit heads/branch
419 test_expect_success 'push tag with non-existent, incomplete dest' '
421 mk_test testrepo &&
422 git tag -f v1.0 &&
423 git push testrepo v1.0:tag &&
424 check_push_result testrepo $the_commit tags/tag
428 test_expect_success 'push sha1 with non-existent, incomplete dest' '
430 mk_test testrepo &&
431 test_must_fail git push testrepo $(git rev-parse main):foo
435 test_expect_success 'push ref expression with non-existent, incomplete dest' '
437 mk_test testrepo &&
438 test_must_fail git push testrepo main^:branch
442 for head in HEAD @
445 test_expect_success "push with $head" '
446 mk_test testrepo heads/main &&
447 git checkout main &&
448 git push testrepo $head &&
449 check_push_result testrepo $the_commit heads/main
452 test_expect_success "push with $head nonexisting at remote" '
453 mk_test testrepo heads/main &&
454 git checkout -b local main &&
455 test_when_finished "git checkout main; git branch -D local" &&
456 git push testrepo $head &&
457 check_push_result testrepo $the_commit heads/local
460 test_expect_success "push with +$head" '
461 mk_test testrepo heads/main &&
462 git checkout -b local main &&
463 test_when_finished "git checkout main; git branch -D local" &&
464 git push testrepo main local &&
465 check_push_result testrepo $the_commit heads/main &&
466 check_push_result testrepo $the_commit heads/local &&
468 # Without force rewinding should fail
469 git reset --hard $head^ &&
470 test_must_fail git push testrepo $head &&
471 check_push_result testrepo $the_commit heads/local &&
473 # With force rewinding should succeed
474 git push testrepo +$head &&
475 check_push_result testrepo $the_first_commit heads/local
478 test_expect_success "push $head with non-existent, incomplete dest" '
479 mk_test testrepo &&
480 git checkout main &&
481 git push testrepo $head:branch &&
482 check_push_result testrepo $the_commit heads/branch
486 test_expect_success "push with config remote.*.push = $head" '
487 mk_test testrepo heads/local &&
488 git checkout main &&
489 git branch -f local $the_commit &&
490 test_when_finished "git branch -D local" &&
492 cd testrepo &&
493 git checkout local &&
494 git reset --hard $the_first_commit
495 ) &&
496 test_config remote.there.url testrepo &&
497 test_config remote.there.push $head &&
498 test_config branch.main.remote there &&
499 git push &&
500 check_push_result testrepo $the_commit heads/main &&
501 check_push_result testrepo $the_first_commit heads/local
504 done
506 test_expect_success 'push with remote.pushdefault' '
507 mk_test up_repo heads/main &&
508 mk_test down_repo heads/main &&
509 test_config remote.up.url up_repo &&
510 test_config remote.down.url down_repo &&
511 test_config branch.main.remote up &&
512 test_config remote.pushdefault down &&
513 test_config push.default matching &&
514 git push &&
515 check_push_result up_repo $the_first_commit heads/main &&
516 check_push_result down_repo $the_commit heads/main
519 test_expect_success 'push with config remote.*.pushurl' '
521 mk_test testrepo heads/main &&
522 git checkout main &&
523 test_config remote.there.url test2repo &&
524 test_config remote.there.pushurl testrepo &&
525 git push there : &&
526 check_push_result testrepo $the_commit heads/main
529 test_expect_success 'push with config branch.*.pushremote' '
530 mk_test up_repo heads/main &&
531 mk_test side_repo heads/main &&
532 mk_test down_repo heads/main &&
533 test_config remote.up.url up_repo &&
534 test_config remote.pushdefault side_repo &&
535 test_config remote.down.url down_repo &&
536 test_config branch.main.remote up &&
537 test_config branch.main.pushremote down &&
538 test_config push.default matching &&
539 git push &&
540 check_push_result up_repo $the_first_commit heads/main &&
541 check_push_result side_repo $the_first_commit heads/main &&
542 check_push_result down_repo $the_commit heads/main
545 test_expect_success 'branch.*.pushremote config order is irrelevant' '
546 mk_test one_repo heads/main &&
547 mk_test two_repo heads/main &&
548 test_config remote.one.url one_repo &&
549 test_config remote.two.url two_repo &&
550 test_config branch.main.pushremote two_repo &&
551 test_config remote.pushdefault one_repo &&
552 test_config push.default matching &&
553 git push &&
554 check_push_result one_repo $the_first_commit heads/main &&
555 check_push_result two_repo $the_commit heads/main
558 test_expect_success 'push with dry-run' '
560 mk_test testrepo heads/main &&
561 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
562 git push --dry-run testrepo : &&
563 check_push_result testrepo $old_commit heads/main
566 test_expect_success 'push updates local refs' '
568 mk_test testrepo heads/main &&
569 mk_child testrepo child &&
571 cd child &&
572 git pull .. main &&
573 git push &&
574 test $(git rev-parse main) = \
575 $(git rev-parse remotes/origin/main)
580 test_expect_success 'push updates up-to-date local refs' '
582 mk_test testrepo heads/main &&
583 mk_child testrepo child1 &&
584 mk_child testrepo child2 &&
585 (cd child1 && git pull .. main && git push) &&
587 cd child2 &&
588 git pull ../child1 main &&
589 git push &&
590 test $(git rev-parse main) = \
591 $(git rev-parse remotes/origin/main)
596 test_expect_success 'push preserves up-to-date packed refs' '
598 mk_test testrepo heads/main &&
599 mk_child testrepo child &&
601 cd child &&
602 git push &&
603 ! test -f .git/refs/remotes/origin/main
608 test_expect_success 'push does not update local refs on failure' '
610 mk_test testrepo heads/main &&
611 mk_child testrepo child &&
612 mkdir testrepo/.git/hooks &&
613 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
614 chmod +x testrepo/.git/hooks/pre-receive &&
616 cd child &&
617 git pull .. main &&
618 test_must_fail git push &&
619 test $(git rev-parse main) != \
620 $(git rev-parse remotes/origin/main)
625 test_expect_success 'allow deleting an invalid remote ref' '
627 mk_test testrepo heads/main &&
628 rm -f testrepo/.git/objects/??/* &&
629 git push testrepo :refs/heads/main &&
630 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
634 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
635 mk_test_with_hooks testrepo heads/main heads/next &&
636 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
637 newmain=$(git show-ref -s --verify refs/heads/main) &&
638 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
639 newnext=$ZERO_OID &&
640 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
642 cd testrepo/.git &&
643 cat >pre-receive.expect <<-EOF &&
644 $orgmain $newmain refs/heads/main
645 $orgnext $newnext refs/heads/next
648 cat >update.expect <<-EOF &&
649 refs/heads/main $orgmain $newmain
650 refs/heads/next $orgnext $newnext
653 cat >post-receive.expect <<-EOF &&
654 $orgmain $newmain refs/heads/main
655 $orgnext $newnext refs/heads/next
658 cat >post-update.expect <<-EOF &&
659 refs/heads/main
660 refs/heads/next
663 test_cmp pre-receive.expect pre-receive.actual &&
664 test_cmp update.expect update.actual &&
665 test_cmp post-receive.expect post-receive.actual &&
666 test_cmp post-update.expect post-update.actual
670 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
671 mk_test_with_hooks testrepo heads/main &&
672 rm -f testrepo/.git/objects/??/* &&
673 git push testrepo :refs/heads/main &&
675 cd testrepo/.git &&
676 cat >pre-receive.expect <<-EOF &&
677 $ZERO_OID $ZERO_OID refs/heads/main
680 cat >update.expect <<-EOF &&
681 refs/heads/main $ZERO_OID $ZERO_OID
684 cat >post-receive.expect <<-EOF &&
685 $ZERO_OID $ZERO_OID refs/heads/main
688 cat >post-update.expect <<-EOF &&
689 refs/heads/main
692 test_cmp pre-receive.expect pre-receive.actual &&
693 test_cmp update.expect update.actual &&
694 test_cmp post-receive.expect post-receive.actual &&
695 test_cmp post-update.expect post-update.actual
699 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
700 mk_test_with_hooks testrepo heads/main &&
701 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
702 newmain=$(git show-ref -s --verify refs/heads/main) &&
703 git push testrepo main :refs/heads/nonexistent &&
705 cd testrepo/.git &&
706 cat >pre-receive.expect <<-EOF &&
707 $orgmain $newmain refs/heads/main
708 $ZERO_OID $ZERO_OID refs/heads/nonexistent
711 cat >update.expect <<-EOF &&
712 refs/heads/main $orgmain $newmain
713 refs/heads/nonexistent $ZERO_OID $ZERO_OID
716 cat >post-receive.expect <<-EOF &&
717 $orgmain $newmain refs/heads/main
720 cat >post-update.expect <<-EOF &&
721 refs/heads/main
724 test_cmp pre-receive.expect pre-receive.actual &&
725 test_cmp update.expect update.actual &&
726 test_cmp post-receive.expect post-receive.actual &&
727 test_cmp post-update.expect post-update.actual
731 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
732 mk_test_with_hooks testrepo heads/main &&
733 git push testrepo :refs/heads/nonexistent &&
735 cd testrepo/.git &&
736 cat >pre-receive.expect <<-EOF &&
737 $ZERO_OID $ZERO_OID refs/heads/nonexistent
740 cat >update.expect <<-EOF &&
741 refs/heads/nonexistent $ZERO_OID $ZERO_OID
744 test_cmp pre-receive.expect pre-receive.actual &&
745 test_cmp update.expect update.actual &&
746 test_path_is_missing post-receive.actual &&
747 test_path_is_missing post-update.actual
751 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
752 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
753 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
754 newmain=$(git show-ref -s --verify refs/heads/main) &&
755 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
756 newnext=$ZERO_OID &&
757 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
758 newseen=$(git show-ref -s --verify refs/heads/main) &&
759 git push testrepo refs/heads/main:refs/heads/main \
760 refs/heads/main:refs/heads/seen :refs/heads/next \
761 :refs/heads/nonexistent &&
763 cd testrepo/.git &&
764 cat >pre-receive.expect <<-EOF &&
765 $orgmain $newmain refs/heads/main
766 $orgnext $newnext refs/heads/next
767 $orgseen $newseen refs/heads/seen
768 $ZERO_OID $ZERO_OID refs/heads/nonexistent
771 cat >update.expect <<-EOF &&
772 refs/heads/main $orgmain $newmain
773 refs/heads/next $orgnext $newnext
774 refs/heads/seen $orgseen $newseen
775 refs/heads/nonexistent $ZERO_OID $ZERO_OID
778 cat >post-receive.expect <<-EOF &&
779 $orgmain $newmain refs/heads/main
780 $orgnext $newnext refs/heads/next
781 $orgseen $newseen refs/heads/seen
784 cat >post-update.expect <<-EOF &&
785 refs/heads/main
786 refs/heads/next
787 refs/heads/seen
790 test_cmp pre-receive.expect pre-receive.actual &&
791 test_cmp update.expect update.actual &&
792 test_cmp post-receive.expect post-receive.actual &&
793 test_cmp post-update.expect post-update.actual
797 test_expect_success 'allow deleting a ref using --delete' '
798 mk_test testrepo heads/main &&
799 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
800 git push testrepo --delete main &&
801 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
804 test_expect_success 'allow deleting a tag using --delete' '
805 mk_test testrepo heads/main &&
806 git tag -a -m dummy_message deltag heads/main &&
807 git push testrepo --tags &&
808 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
809 git push testrepo --delete tag deltag &&
810 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
813 test_expect_success 'push --delete without args aborts' '
814 mk_test testrepo heads/main &&
815 test_must_fail git push testrepo --delete
818 test_expect_success 'push --delete refuses src:dest refspecs' '
819 mk_test testrepo heads/main &&
820 test_must_fail git push testrepo --delete main:foo
823 test_expect_success 'warn on push to HEAD of non-bare repository' '
824 mk_test testrepo heads/main &&
826 cd testrepo &&
827 git checkout main &&
828 git config receive.denyCurrentBranch warn
829 ) &&
830 git push testrepo main 2>stderr &&
831 grep "warning: updating the current branch" stderr
834 test_expect_success 'deny push to HEAD of non-bare repository' '
835 mk_test testrepo heads/main &&
837 cd testrepo &&
838 git checkout main &&
839 git config receive.denyCurrentBranch true
840 ) &&
841 test_must_fail git push testrepo main
844 test_expect_success 'allow push to HEAD of bare repository (bare)' '
845 mk_test testrepo heads/main &&
847 cd testrepo &&
848 git checkout main &&
849 git config receive.denyCurrentBranch true &&
850 git config core.bare true
851 ) &&
852 git push testrepo main 2>stderr &&
853 ! grep "warning: updating the current branch" stderr
856 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
857 mk_test testrepo heads/main &&
859 cd testrepo &&
860 git checkout main &&
861 git config receive.denyCurrentBranch false
862 ) &&
863 git push testrepo main 2>stderr &&
864 ! grep "warning: updating the current branch" stderr
867 test_expect_success 'fetch with branches' '
868 mk_empty testrepo &&
869 git branch second $the_first_commit &&
870 git checkout second &&
871 echo ".." > testrepo/.git/branches/branch1 &&
873 cd testrepo &&
874 git fetch branch1 &&
875 echo "$the_commit commit refs/heads/branch1" >expect &&
876 git for-each-ref refs/heads >actual &&
877 test_cmp expect actual
878 ) &&
879 git checkout main
882 test_expect_success 'fetch with branches containing #' '
883 mk_empty testrepo &&
884 echo "..#second" > testrepo/.git/branches/branch2 &&
886 cd testrepo &&
887 git fetch branch2 &&
888 echo "$the_first_commit commit refs/heads/branch2" >expect &&
889 git for-each-ref refs/heads >actual &&
890 test_cmp expect actual
891 ) &&
892 git checkout main
895 test_expect_success 'push with branches' '
896 mk_empty testrepo &&
897 git checkout second &&
898 echo "testrepo" > .git/branches/branch1 &&
899 git push branch1 &&
901 cd testrepo &&
902 echo "$the_first_commit commit refs/heads/main" >expect &&
903 git for-each-ref refs/heads >actual &&
904 test_cmp expect actual
908 test_expect_success 'push with branches containing #' '
909 mk_empty testrepo &&
910 echo "testrepo#branch3" > .git/branches/branch2 &&
911 git push branch2 &&
913 cd testrepo &&
914 echo "$the_first_commit commit refs/heads/branch3" >expect &&
915 git for-each-ref refs/heads >actual &&
916 test_cmp expect actual
917 ) &&
918 git checkout main
921 test_expect_success 'push into aliased refs (consistent)' '
922 mk_test testrepo heads/main &&
923 mk_child testrepo child1 &&
924 mk_child testrepo child2 &&
926 cd child1 &&
927 git branch foo &&
928 git symbolic-ref refs/heads/bar refs/heads/foo &&
929 git config receive.denyCurrentBranch false
930 ) &&
932 cd child2 &&
933 >path2 &&
934 git add path2 &&
935 test_tick &&
936 git commit -a -m child2 &&
937 git branch foo &&
938 git branch bar &&
939 git push ../child1 foo bar
943 test_expect_success 'push into aliased refs (inconsistent)' '
944 mk_test testrepo heads/main &&
945 mk_child testrepo child1 &&
946 mk_child testrepo child2 &&
948 cd child1 &&
949 git branch foo &&
950 git symbolic-ref refs/heads/bar refs/heads/foo &&
951 git config receive.denyCurrentBranch false
952 ) &&
954 cd child2 &&
955 >path2 &&
956 git add path2 &&
957 test_tick &&
958 git commit -a -m child2 &&
959 git branch foo &&
960 >path3 &&
961 git add path3 &&
962 test_tick &&
963 git commit -a -m child2 &&
964 git branch bar &&
965 test_must_fail git push ../child1 foo bar 2>stderr &&
966 grep "refusing inconsistent update" stderr
970 test_force_push_tag () {
971 tag_type_description=$1
972 tag_args=$2
974 test_expect_success "force pushing required to update $tag_type_description" "
975 mk_test testrepo heads/main &&
976 mk_child testrepo child1 &&
977 mk_child testrepo child2 &&
979 cd child1 &&
980 git tag testTag &&
981 git push ../child2 testTag &&
982 >file1 &&
983 git add file1 &&
984 git commit -m 'file1' &&
985 git tag $tag_args testTag &&
986 test_must_fail git push ../child2 testTag &&
987 git push --force ../child2 testTag &&
988 git tag $tag_args testTag HEAD~ &&
989 test_must_fail git push ../child2 testTag &&
990 git push --force ../child2 testTag &&
992 # Clobbering without + in refspec needs --force
993 git tag -f testTag &&
994 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
995 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
997 # Clobbering with + in refspec does not need --force
998 git tag -f testTag HEAD~ &&
999 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1001 # Clobbering with --no-force still obeys + in refspec
1002 git tag -f testTag &&
1003 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1005 # Clobbering with/without --force and 'tag <name>' format
1006 git tag -f testTag HEAD~ &&
1007 test_must_fail git push ../child2 tag testTag &&
1008 git push --force ../child2 tag testTag
1013 test_force_push_tag "lightweight tag" "-f"
1014 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1016 test_force_fetch_tag () {
1017 tag_type_description=$1
1018 tag_args=$2
1020 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1021 mk_test testrepo heads/main &&
1022 mk_child testrepo child1 &&
1023 mk_child testrepo child2 &&
1025 cd testrepo &&
1026 git tag testTag &&
1027 git -C ../child1 fetch origin tag testTag &&
1028 >file1 &&
1029 git add file1 &&
1030 git commit -m 'file1' &&
1031 git tag $tag_args testTag &&
1032 test_must_fail git -C ../child1 fetch origin tag testTag &&
1033 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1038 test_force_fetch_tag "lightweight tag" "-f"
1039 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1041 test_expect_success 'push --porcelain' '
1042 mk_empty testrepo &&
1043 echo >.git/foo "To testrepo" &&
1044 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1045 echo >>.git/foo "Done" &&
1046 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1048 cd testrepo &&
1049 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1050 git for-each-ref refs/remotes/origin >actual &&
1051 test_cmp expect actual
1052 ) &&
1053 test_cmp .git/foo .git/bar
1056 test_expect_success 'push --porcelain bad url' '
1057 mk_empty testrepo &&
1058 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1059 ! grep -q Done .git/bar
1062 test_expect_success 'push --porcelain rejected' '
1063 mk_empty testrepo &&
1064 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1065 (cd testrepo &&
1066 git reset --hard origin/main^ &&
1067 git config receive.denyCurrentBranch true) &&
1069 echo >.git/foo "To testrepo" &&
1070 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1071 echo >>.git/foo "Done" &&
1073 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1074 test_cmp .git/foo .git/bar
1077 test_expect_success 'push --porcelain --dry-run rejected' '
1078 mk_empty testrepo &&
1079 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1080 (cd testrepo &&
1081 git reset --hard origin/main &&
1082 git config receive.denyCurrentBranch true) &&
1084 echo >.git/foo "To testrepo" &&
1085 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1086 echo >>.git/foo "Done" &&
1088 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1089 test_cmp .git/foo .git/bar
1092 test_expect_success 'push --prune' '
1093 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1094 git push --prune testrepo : &&
1095 check_push_result testrepo $the_commit heads/main &&
1096 check_push_result testrepo $the_first_commit heads/second &&
1097 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1100 test_expect_success 'push --prune refspec' '
1101 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1102 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1103 check_push_result testrepo $the_commit tmp/main &&
1104 check_push_result testrepo $the_first_commit tmp/second &&
1105 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1108 for configsection in transfer receive
1110 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1111 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1113 cd testrepo &&
1114 git config $configsection.hiderefs refs/hidden
1115 ) &&
1117 # push to unhidden ref succeeds normally
1118 git push testrepo main:refs/heads/main &&
1119 check_push_result testrepo $the_commit heads/main &&
1121 # push to update a hidden ref should fail
1122 test_must_fail git push testrepo main:refs/hidden/one &&
1123 check_push_result testrepo $the_first_commit hidden/one &&
1125 # push to delete a hidden ref should fail
1126 test_must_fail git push testrepo :refs/hidden/two &&
1127 check_push_result testrepo $the_first_commit hidden/two &&
1129 # idempotent push to update a hidden ref should fail
1130 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1131 check_push_result testrepo $the_first_commit hidden/three
1133 done
1135 test_expect_success 'fetch exact SHA1' '
1136 mk_test testrepo heads/main hidden/one &&
1137 git push testrepo main:refs/hidden/one &&
1139 cd testrepo &&
1140 git config transfer.hiderefs refs/hidden
1141 ) &&
1142 check_push_result testrepo $the_commit hidden/one &&
1144 mk_child testrepo child &&
1146 cd child &&
1148 # make sure $the_commit does not exist here
1149 git repack -a -d &&
1150 git prune &&
1151 test_must_fail git cat-file -t $the_commit &&
1153 # Some protocol versions (e.g. 2) support fetching
1154 # unadvertised objects, so restrict this test to v0.
1156 # fetching the hidden object should fail by default
1157 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1158 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1159 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1160 test_must_fail git rev-parse --verify refs/heads/copy &&
1162 # the server side can allow it to succeed
1164 cd ../testrepo &&
1165 git config uploadpack.allowtipsha1inwant true
1166 ) &&
1168 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1169 cat >expect <<-EOF &&
1170 $the_commit
1171 $the_first_commit
1174 git rev-parse --verify refs/heads/copy &&
1175 git rev-parse --verify refs/heads/extra
1176 } >actual &&
1177 test_cmp expect actual
1181 test_expect_success 'fetch exact SHA1 in protocol v2' '
1182 mk_test testrepo heads/main hidden/one &&
1183 git push testrepo main:refs/hidden/one &&
1184 git -C testrepo config transfer.hiderefs refs/hidden &&
1185 check_push_result testrepo $the_commit hidden/one &&
1187 mk_child testrepo child &&
1188 git -C child config protocol.version 2 &&
1190 # make sure $the_commit does not exist here
1191 git -C child repack -a -d &&
1192 git -C child prune &&
1193 test_must_fail git -C child cat-file -t $the_commit &&
1195 # fetching the hidden object succeeds by default
1196 # NEEDSWORK: should this match the v0 behavior instead?
1197 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1200 for configallowtipsha1inwant in true false
1202 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1203 mk_empty testrepo &&
1205 cd testrepo &&
1206 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1207 git commit --allow-empty -m foo &&
1208 git commit --allow-empty -m bar
1209 ) &&
1210 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1211 mk_empty shallow &&
1213 cd shallow &&
1214 # Some protocol versions (e.g. 2) support fetching
1215 # unadvertised objects, so restrict this test to v0.
1216 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1217 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1218 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1219 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1220 git cat-file commit $SHA1
1224 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1225 mk_empty testrepo &&
1227 cd testrepo &&
1228 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1229 git commit --allow-empty -m foo &&
1230 git commit --allow-empty -m bar &&
1231 git commit --allow-empty -m xyz
1232 ) &&
1233 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1234 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1235 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1237 cd testrepo &&
1238 git reset --hard $SHA1_2 &&
1239 git cat-file commit $SHA1_1 &&
1240 git cat-file commit $SHA1_3
1241 ) &&
1242 mk_empty shallow &&
1244 cd shallow &&
1245 # Some protocol versions (e.g. 2) support fetching
1246 # unadvertised objects, so restrict this test to v0.
1247 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1248 git fetch ../testrepo/.git $SHA1_3 &&
1249 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1250 git fetch ../testrepo/.git $SHA1_1 &&
1251 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1252 git fetch ../testrepo/.git $SHA1_1 &&
1253 git cat-file commit $SHA1_1 &&
1254 test_must_fail git cat-file commit $SHA1_2 &&
1255 git fetch ../testrepo/.git $SHA1_2 &&
1256 git cat-file commit $SHA1_2 &&
1257 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1258 git fetch ../testrepo/.git $SHA1_3 2>err &&
1259 # ideally we would insist this be on a "remote error:"
1260 # line, but it is racy; see the commit message
1261 test_i18ngrep "not our ref.*$SHA1_3\$" err
1264 done
1266 test_expect_success 'fetch follows tags by default' '
1267 mk_test testrepo heads/main &&
1268 rm -fr src dst &&
1269 git init src &&
1271 cd src &&
1272 git pull ../testrepo main &&
1273 git tag -m "annotated" tag &&
1274 git for-each-ref >tmp1 &&
1276 cat tmp1
1277 sed -n "s|refs/heads/main$|refs/remotes/origin/main|p" tmp1
1279 sort -k 3 >../expect
1280 ) &&
1281 git init dst &&
1283 cd dst &&
1284 git remote add origin ../src &&
1285 git config branch.main.remote origin &&
1286 git config branch.main.merge refs/heads/main &&
1287 git pull &&
1288 git for-each-ref >../actual
1289 ) &&
1290 test_cmp expect actual
1293 test_expect_success 'peeled advertisements are not considered ref tips' '
1294 mk_empty testrepo &&
1295 git -C testrepo commit --allow-empty -m one &&
1296 git -C testrepo commit --allow-empty -m two &&
1297 git -C testrepo tag -m foo mytag HEAD^ &&
1298 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1299 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1300 git fetch testrepo $oid 2>err &&
1301 test_i18ngrep "Server does not allow request for unadvertised object" err
1304 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1305 mk_test testrepo heads/main &&
1306 rm -fr src dst &&
1307 git init src &&
1308 git init --bare dst &&
1310 cd src &&
1311 git pull ../testrepo main &&
1312 git branch next &&
1313 git config remote.dst.url ../dst &&
1314 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1315 git push dst main &&
1316 git show-ref refs/heads/main |
1317 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1318 ) &&
1320 cd dst &&
1321 test_must_fail git show-ref refs/heads/next &&
1322 test_must_fail git show-ref refs/heads/main &&
1323 git show-ref refs/remotes/src/main >actual
1324 ) &&
1325 test_cmp dst/expect dst/actual
1328 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1329 mk_test testrepo heads/main &&
1330 rm -fr src dst &&
1331 git init src &&
1332 git init --bare dst &&
1334 cd src &&
1335 git pull ../testrepo main &&
1336 git branch next &&
1337 git config remote.dst.url ../dst &&
1338 git config push.default matching &&
1339 git push dst main &&
1340 git show-ref refs/heads/main >../dst/expect
1341 ) &&
1343 cd dst &&
1344 test_must_fail git show-ref refs/heads/next &&
1345 git show-ref refs/heads/main >actual
1346 ) &&
1347 test_cmp dst/expect dst/actual
1350 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1351 mk_test testrepo heads/main &&
1352 rm -fr src dst &&
1353 git init src &&
1354 git init --bare dst &&
1356 cd src &&
1357 git pull ../testrepo main &&
1358 git branch next &&
1359 git config remote.dst.url ../dst &&
1360 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1361 git config push.default upstream &&
1363 git config branch.main.merge refs/heads/trunk &&
1364 git config branch.main.remote dst &&
1366 git push dst main &&
1367 git show-ref refs/heads/main |
1368 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1369 ) &&
1371 cd dst &&
1372 test_must_fail git show-ref refs/heads/main &&
1373 test_must_fail git show-ref refs/heads/next &&
1374 git show-ref refs/heads/trunk >actual
1375 ) &&
1376 test_cmp dst/expect dst/actual
1379 test_expect_success 'push does not follow tags by default' '
1380 mk_test testrepo heads/main &&
1381 rm -fr src dst &&
1382 git init src &&
1383 git init --bare dst &&
1385 cd src &&
1386 git pull ../testrepo main &&
1387 git tag -m "annotated" tag &&
1388 git checkout -b another &&
1389 git commit --allow-empty -m "future commit" &&
1390 git tag -m "future" future &&
1391 git checkout main &&
1392 git for-each-ref refs/heads/main >../expect &&
1393 git push ../dst main
1394 ) &&
1396 cd dst &&
1397 git for-each-ref >../actual
1398 ) &&
1399 test_cmp expect actual
1402 test_expect_success 'push --follow-tags only pushes relevant tags' '
1403 mk_test testrepo heads/main &&
1404 rm -fr src dst &&
1405 git init src &&
1406 git init --bare dst &&
1408 cd src &&
1409 git pull ../testrepo main &&
1410 git tag -m "annotated" tag &&
1411 git checkout -b another &&
1412 git commit --allow-empty -m "future commit" &&
1413 git tag -m "future" future &&
1414 git checkout main &&
1415 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1416 git push --follow-tags ../dst main
1417 ) &&
1419 cd dst &&
1420 git for-each-ref >../actual
1421 ) &&
1422 test_cmp expect actual
1425 test_expect_success 'push --no-thin must produce non-thin pack' '
1426 cat >>path1 <<\EOF &&
1427 keep base version of path1 big enough, compared to the new changes
1428 later, in order to pass size heuristics in
1429 builtin/pack-objects.c:try_delta()
1431 git commit -am initial &&
1432 git init no-thin &&
1433 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1434 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1435 echo modified >> path1 &&
1436 git commit -am modified &&
1437 git repack -adf &&
1438 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1439 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1442 test_expect_success 'pushing a tag pushes the tagged object' '
1443 rm -rf dst.git &&
1444 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1445 git tag -m foo tag-of-blob $blob &&
1446 git init --bare dst.git &&
1447 git push dst.git tag-of-blob &&
1448 # the receiving index-pack should have noticed
1449 # any problems, but we double check
1450 echo unreferenced >expect &&
1451 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1452 test_cmp expect actual
1455 test_expect_success 'push into bare respects core.logallrefupdates' '
1456 rm -rf dst.git &&
1457 git init --bare dst.git &&
1458 git -C dst.git config core.logallrefupdates true &&
1460 # double push to test both with and without
1461 # the actual pack transfer
1462 git push dst.git main:one &&
1463 echo "one@{0} push" >expect &&
1464 git -C dst.git log -g --format="%gd %gs" one >actual &&
1465 test_cmp expect actual &&
1467 git push dst.git main:two &&
1468 echo "two@{0} push" >expect &&
1469 git -C dst.git log -g --format="%gd %gs" two >actual &&
1470 test_cmp expect actual
1473 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1474 rm -rf dst.git &&
1475 git init --bare dst.git &&
1477 cd dst.git &&
1478 git config core.logallrefupdates true &&
1480 # as above, we double-fetch to test both
1481 # with and without pack transfer
1482 git fetch .. main:one &&
1483 echo "one@{0} fetch .. main:one: storing head" >expect &&
1484 git log -g --format="%gd %gs" one >actual &&
1485 test_cmp expect actual &&
1487 git fetch .. main:two &&
1488 echo "two@{0} fetch .. main:two: storing head" >expect &&
1489 git log -g --format="%gd %gs" two >actual &&
1490 test_cmp expect actual
1494 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1495 git push testrepo main &&
1497 cd testrepo &&
1498 git reset --hard &&
1499 git config receive.denyCurrentBranch updateInstead
1500 ) &&
1501 test_commit third path2 &&
1503 # Try pushing into a repository with pristine working tree
1504 git push testrepo main &&
1506 cd testrepo &&
1507 git update-index -q --refresh &&
1508 git diff-files --quiet -- &&
1509 git diff-index --quiet --cached HEAD -- &&
1510 test third = "$(cat path2)" &&
1511 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1512 ) &&
1514 # Try pushing into a repository with working tree needing a refresh
1516 cd testrepo &&
1517 git reset --hard HEAD^ &&
1518 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1519 test-tool chmtime +100 path1
1520 ) &&
1521 git push testrepo main &&
1523 cd testrepo &&
1524 git update-index -q --refresh &&
1525 git diff-files --quiet -- &&
1526 git diff-index --quiet --cached HEAD -- &&
1527 test_cmp ../path1 path1 &&
1528 test third = "$(cat path2)" &&
1529 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1530 ) &&
1532 # Update what is to be pushed
1533 test_commit fourth path2 &&
1535 # Try pushing into a repository with a dirty working tree
1536 # (1) the working tree updated
1538 cd testrepo &&
1539 echo changed >path1
1540 ) &&
1541 test_must_fail git push testrepo main &&
1543 cd testrepo &&
1544 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1545 git diff --quiet --cached &&
1546 test changed = "$(cat path1)"
1547 ) &&
1549 # (2) the index updated
1551 cd testrepo &&
1552 echo changed >path1 &&
1553 git add path1
1554 ) &&
1555 test_must_fail git push testrepo main &&
1557 cd testrepo &&
1558 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1559 git diff --quiet &&
1560 test changed = "$(cat path1)"
1561 ) &&
1563 # Introduce a new file in the update
1564 test_commit fifth path3 &&
1566 # (3) the working tree has an untracked file that would interfere
1568 cd testrepo &&
1569 git reset --hard &&
1570 echo changed >path3
1571 ) &&
1572 test_must_fail git push testrepo main &&
1574 cd testrepo &&
1575 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1576 git diff --quiet &&
1577 git diff --quiet --cached &&
1578 test changed = "$(cat path3)"
1579 ) &&
1581 # (4) the target changes to what gets pushed but it still is a change
1583 cd testrepo &&
1584 git reset --hard &&
1585 echo fifth >path3 &&
1586 git add path3
1587 ) &&
1588 test_must_fail git push testrepo main &&
1590 cd testrepo &&
1591 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1592 git diff --quiet &&
1593 test fifth = "$(cat path3)"
1594 ) &&
1596 # (5) push into void
1597 rm -fr void &&
1598 git init void &&
1600 cd void &&
1601 git config receive.denyCurrentBranch updateInstead
1602 ) &&
1603 git push void main &&
1605 cd void &&
1606 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1607 git diff --quiet &&
1608 git diff --cached --quiet
1609 ) &&
1611 # (6) updateInstead intervened by fast-forward check
1612 test_must_fail git push void main^:main &&
1613 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1614 git -C void diff --quiet &&
1615 git -C void diff --cached --quiet
1618 test_expect_success 'updateInstead with push-to-checkout hook' '
1619 rm -fr testrepo &&
1620 git init testrepo &&
1622 cd testrepo &&
1623 git pull .. main &&
1624 git reset --hard HEAD^^ &&
1625 git tag initial &&
1626 git config receive.denyCurrentBranch updateInstead &&
1627 write_script .git/hooks/push-to-checkout <<-\EOF
1628 echo >&2 updating from $(git rev-parse HEAD)
1629 echo >&2 updating to "$1"
1631 git update-index -q --refresh &&
1632 git read-tree -u -m HEAD "$1" || {
1633 status=$?
1634 echo >&2 read-tree failed
1635 exit $status
1638 ) &&
1640 # Try pushing into a pristine
1641 git push testrepo main &&
1643 cd testrepo &&
1644 git diff --quiet &&
1645 git diff HEAD --quiet &&
1646 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1647 ) &&
1649 # Try pushing into a repository with conflicting change
1651 cd testrepo &&
1652 git reset --hard initial &&
1653 echo conflicting >path2
1654 ) &&
1655 test_must_fail git push testrepo main &&
1657 cd testrepo &&
1658 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1659 test conflicting = "$(cat path2)" &&
1660 git diff-index --quiet --cached HEAD
1661 ) &&
1663 # Try pushing into a repository with unrelated change
1665 cd testrepo &&
1666 git reset --hard initial &&
1667 echo unrelated >path1 &&
1668 echo irrelevant >path5 &&
1669 git add path5
1670 ) &&
1671 git push testrepo main &&
1673 cd testrepo &&
1674 test "$(cat path1)" = unrelated &&
1675 test "$(cat path5)" = irrelevant &&
1676 test "$(git diff --name-only --cached HEAD)" = path5 &&
1677 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1678 ) &&
1680 # push into void
1681 rm -fr void &&
1682 git init void &&
1684 cd void &&
1685 git config receive.denyCurrentBranch updateInstead &&
1686 write_script .git/hooks/push-to-checkout <<-\EOF
1687 if git rev-parse --quiet --verify HEAD
1688 then
1689 has_head=yes
1690 echo >&2 updating from $(git rev-parse HEAD)
1691 else
1692 has_head=no
1693 echo >&2 pushing into void
1695 echo >&2 updating to "$1"
1697 git update-index -q --refresh &&
1698 case "$has_head" in
1699 yes)
1700 git read-tree -u -m HEAD "$1" ;;
1702 git read-tree -u -m "$1" ;;
1703 esac || {
1704 status=$?
1705 echo >&2 read-tree failed
1706 exit $status
1709 ) &&
1711 git push void main &&
1713 cd void &&
1714 git diff --quiet &&
1715 git diff --cached --quiet &&
1716 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1720 test_expect_success 'denyCurrentBranch and worktrees' '
1721 git worktree add new-wt &&
1722 git clone . cloned &&
1723 test_commit -C cloned first &&
1724 test_config receive.denyCurrentBranch refuse &&
1725 test_must_fail git -C cloned push origin HEAD:new-wt &&
1726 test_config receive.denyCurrentBranch updateInstead &&
1727 git -C cloned push origin HEAD:new-wt &&
1728 test_must_fail git -C cloned push --delete origin new-wt
1731 test_done