pkt-line.[ch]: remove unused packet_buf_write_len()
[alt-git.git] / t / t5516-fetch-push.sh
blob4db8edd9c85ab1c4b6606f3548e82df3953ca04b
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 without wildcard' '
233 mk_empty testrepo &&
235 git push 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 wildcard' '
245 mk_empty testrepo &&
247 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
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 insteadOf' '
257 mk_empty testrepo &&
258 TRASH="$(pwd)/" &&
259 test_config "url.$TRASH.insteadOf" trash/ &&
260 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
262 cd testrepo &&
263 echo "$the_commit commit refs/remotes/origin/main" >expect &&
264 git for-each-ref refs/remotes/origin >actual &&
265 test_cmp expect actual
269 test_expect_success 'push with pushInsteadOf' '
270 mk_empty testrepo &&
271 TRASH="$(pwd)/" &&
272 test_config "url.$TRASH.pushInsteadOf" trash/ &&
273 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
275 cd testrepo &&
276 echo "$the_commit commit refs/remotes/origin/main" >expect &&
277 git for-each-ref refs/remotes/origin >actual &&
278 test_cmp expect actual
282 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
283 mk_empty testrepo &&
284 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
285 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
286 test_config remote.r.url trash/wrong &&
287 test_config remote.r.pushurl "testrepo/" &&
288 git push r refs/heads/main:refs/remotes/origin/main &&
290 cd testrepo &&
291 echo "$the_commit commit refs/remotes/origin/main" >expect &&
292 git for-each-ref refs/remotes/origin >actual &&
293 test_cmp expect actual
297 test_expect_success 'push with matching heads' '
299 mk_test testrepo heads/main &&
300 git push testrepo : &&
301 check_push_result testrepo $the_commit heads/main
305 test_expect_success 'push with matching heads on the command line' '
307 mk_test testrepo heads/main &&
308 git push testrepo : &&
309 check_push_result testrepo $the_commit heads/main
313 test_expect_success 'failed (non-fast-forward) push with matching heads' '
315 mk_test testrepo heads/main &&
316 git push testrepo : &&
317 git commit --amend -massaged &&
318 test_must_fail git push testrepo &&
319 check_push_result testrepo $the_commit heads/main &&
320 git reset --hard $the_commit
324 test_expect_success 'push --force with matching heads' '
326 mk_test testrepo heads/main &&
327 git push testrepo : &&
328 git commit --amend -massaged &&
329 git push --force testrepo : &&
330 ! check_push_result testrepo $the_commit heads/main &&
331 git reset --hard $the_commit
335 test_expect_success 'push with matching heads and forced update' '
337 mk_test testrepo heads/main &&
338 git push testrepo : &&
339 git commit --amend -massaged &&
340 git push testrepo +: &&
341 ! check_push_result testrepo $the_commit heads/main &&
342 git reset --hard $the_commit
346 test_expect_success 'push with no ambiguity (1)' '
348 mk_test testrepo heads/main &&
349 git push testrepo main:main &&
350 check_push_result testrepo $the_commit heads/main
354 test_expect_success 'push with no ambiguity (2)' '
356 mk_test testrepo remotes/origin/main &&
357 git push testrepo main:origin/main &&
358 check_push_result testrepo $the_commit remotes/origin/main
362 test_expect_success 'push with colon-less refspec, no ambiguity' '
364 mk_test testrepo heads/main heads/t/main &&
365 git branch -f t/main main &&
366 git push testrepo main &&
367 check_push_result testrepo $the_commit heads/main &&
368 check_push_result testrepo $the_first_commit heads/t/main
372 test_expect_success 'push with weak ambiguity (1)' '
374 mk_test testrepo heads/main remotes/origin/main &&
375 git push testrepo main:main &&
376 check_push_result testrepo $the_commit heads/main &&
377 check_push_result testrepo $the_first_commit remotes/origin/main
381 test_expect_success 'push with weak ambiguity (2)' '
383 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
384 git push testrepo main:main &&
385 check_push_result testrepo $the_commit heads/main &&
386 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
390 test_expect_success 'push with ambiguity' '
392 mk_test testrepo heads/frotz tags/frotz &&
393 test_must_fail git push testrepo main:frotz &&
394 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
398 test_expect_success 'push with colon-less refspec (1)' '
400 mk_test testrepo heads/frotz tags/frotz &&
401 git branch -f frotz main &&
402 git push testrepo frotz &&
403 check_push_result testrepo $the_commit heads/frotz &&
404 check_push_result testrepo $the_first_commit tags/frotz
408 test_expect_success 'push with colon-less refspec (2)' '
410 mk_test testrepo heads/frotz tags/frotz &&
411 if git show-ref --verify -q refs/heads/frotz
412 then
413 git branch -D frotz
414 fi &&
415 git tag -f frotz &&
416 git push -f testrepo frotz &&
417 check_push_result testrepo $the_commit tags/frotz &&
418 check_push_result testrepo $the_first_commit heads/frotz
422 test_expect_success 'push with colon-less refspec (3)' '
424 mk_test testrepo &&
425 if git show-ref --verify -q refs/tags/frotz
426 then
427 git tag -d frotz
428 fi &&
429 git branch -f frotz main &&
430 git push testrepo frotz &&
431 check_push_result testrepo $the_commit heads/frotz &&
432 test 1 = $( cd testrepo && git show-ref | wc -l )
435 test_expect_success 'push with colon-less refspec (4)' '
437 mk_test testrepo &&
438 if git show-ref --verify -q refs/heads/frotz
439 then
440 git branch -D frotz
441 fi &&
442 git tag -f frotz &&
443 git push testrepo frotz &&
444 check_push_result testrepo $the_commit tags/frotz &&
445 test 1 = $( cd testrepo && git show-ref | wc -l )
449 test_expect_success 'push head with non-existent, incomplete dest' '
451 mk_test testrepo &&
452 git push testrepo main:branch &&
453 check_push_result testrepo $the_commit heads/branch
457 test_expect_success 'push tag with non-existent, incomplete dest' '
459 mk_test testrepo &&
460 git tag -f v1.0 &&
461 git push testrepo v1.0:tag &&
462 check_push_result testrepo $the_commit tags/tag
466 test_expect_success 'push sha1 with non-existent, incomplete dest' '
468 mk_test testrepo &&
469 test_must_fail git push testrepo $(git rev-parse main):foo
473 test_expect_success 'push ref expression with non-existent, incomplete dest' '
475 mk_test testrepo &&
476 test_must_fail git push testrepo main^:branch
480 for head in HEAD @
483 test_expect_success "push with $head" '
484 mk_test testrepo heads/main &&
485 git checkout main &&
486 git push testrepo $head &&
487 check_push_result testrepo $the_commit heads/main
490 test_expect_success "push with $head nonexisting at remote" '
491 mk_test testrepo heads/main &&
492 git checkout -b local main &&
493 test_when_finished "git checkout main; git branch -D local" &&
494 git push testrepo $head &&
495 check_push_result testrepo $the_commit heads/local
498 test_expect_success "push with +$head" '
499 mk_test testrepo heads/main &&
500 git checkout -b local main &&
501 test_when_finished "git checkout main; git branch -D local" &&
502 git push testrepo main local &&
503 check_push_result testrepo $the_commit heads/main &&
504 check_push_result testrepo $the_commit heads/local &&
506 # Without force rewinding should fail
507 git reset --hard $head^ &&
508 test_must_fail git push testrepo $head &&
509 check_push_result testrepo $the_commit heads/local &&
511 # With force rewinding should succeed
512 git push testrepo +$head &&
513 check_push_result testrepo $the_first_commit heads/local
516 test_expect_success "push $head with non-existent, incomplete dest" '
517 mk_test testrepo &&
518 git checkout main &&
519 git push testrepo $head:branch &&
520 check_push_result testrepo $the_commit heads/branch
524 test_expect_success "push with config remote.*.push = $head" '
525 mk_test testrepo heads/local &&
526 git checkout main &&
527 git branch -f local $the_commit &&
528 test_when_finished "git branch -D local" &&
530 cd testrepo &&
531 git checkout local &&
532 git reset --hard $the_first_commit
533 ) &&
534 test_config remote.there.url testrepo &&
535 test_config remote.there.push $head &&
536 test_config branch.main.remote there &&
537 git push &&
538 check_push_result testrepo $the_commit heads/main &&
539 check_push_result testrepo $the_first_commit heads/local
542 done
544 test_expect_success 'push with remote.pushdefault' '
545 mk_test up_repo heads/main &&
546 mk_test down_repo heads/main &&
547 test_config remote.up.url up_repo &&
548 test_config remote.down.url down_repo &&
549 test_config branch.main.remote up &&
550 test_config remote.pushdefault down &&
551 test_config push.default matching &&
552 git push &&
553 check_push_result up_repo $the_first_commit heads/main &&
554 check_push_result down_repo $the_commit heads/main
557 test_expect_success 'push with config remote.*.pushurl' '
559 mk_test testrepo heads/main &&
560 git checkout main &&
561 test_config remote.there.url test2repo &&
562 test_config remote.there.pushurl testrepo &&
563 git push there : &&
564 check_push_result testrepo $the_commit heads/main
567 test_expect_success 'push with config branch.*.pushremote' '
568 mk_test up_repo heads/main &&
569 mk_test side_repo heads/main &&
570 mk_test down_repo heads/main &&
571 test_config remote.up.url up_repo &&
572 test_config remote.pushdefault side_repo &&
573 test_config remote.down.url down_repo &&
574 test_config branch.main.remote up &&
575 test_config branch.main.pushremote down &&
576 test_config push.default matching &&
577 git push &&
578 check_push_result up_repo $the_first_commit heads/main &&
579 check_push_result side_repo $the_first_commit heads/main &&
580 check_push_result down_repo $the_commit heads/main
583 test_expect_success 'branch.*.pushremote config order is irrelevant' '
584 mk_test one_repo heads/main &&
585 mk_test two_repo heads/main &&
586 test_config remote.one.url one_repo &&
587 test_config remote.two.url two_repo &&
588 test_config branch.main.pushremote two_repo &&
589 test_config remote.pushdefault one_repo &&
590 test_config push.default matching &&
591 git push &&
592 check_push_result one_repo $the_first_commit heads/main &&
593 check_push_result two_repo $the_commit heads/main
596 test_expect_success 'push with dry-run' '
598 mk_test testrepo heads/main &&
599 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
600 git push --dry-run testrepo : &&
601 check_push_result testrepo $old_commit heads/main
604 test_expect_success 'push updates local refs' '
606 mk_test testrepo heads/main &&
607 mk_child testrepo child &&
609 cd child &&
610 git pull .. main &&
611 git push &&
612 test $(git rev-parse main) = \
613 $(git rev-parse remotes/origin/main)
618 test_expect_success 'push updates up-to-date local refs' '
620 mk_test testrepo heads/main &&
621 mk_child testrepo child1 &&
622 mk_child testrepo child2 &&
623 (cd child1 && git pull .. main && git push) &&
625 cd child2 &&
626 git pull ../child1 main &&
627 git push &&
628 test $(git rev-parse main) = \
629 $(git rev-parse remotes/origin/main)
634 test_expect_success 'push preserves up-to-date packed refs' '
636 mk_test testrepo heads/main &&
637 mk_child testrepo child &&
639 cd child &&
640 git push &&
641 ! test -f .git/refs/remotes/origin/main
646 test_expect_success 'push does not update local refs on failure' '
648 mk_test testrepo heads/main &&
649 mk_child testrepo child &&
650 mkdir testrepo/.git/hooks &&
651 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
652 chmod +x testrepo/.git/hooks/pre-receive &&
654 cd child &&
655 git pull .. main &&
656 test_must_fail git push &&
657 test $(git rev-parse main) != \
658 $(git rev-parse remotes/origin/main)
663 test_expect_success 'allow deleting an invalid remote ref' '
665 mk_test testrepo heads/main &&
666 rm -f testrepo/.git/objects/??/* &&
667 git push testrepo :refs/heads/main &&
668 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
672 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
673 mk_test_with_hooks testrepo heads/main heads/next &&
674 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
675 newmain=$(git show-ref -s --verify refs/heads/main) &&
676 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
677 newnext=$ZERO_OID &&
678 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
680 cd testrepo/.git &&
681 cat >pre-receive.expect <<-EOF &&
682 $orgmain $newmain refs/heads/main
683 $orgnext $newnext refs/heads/next
686 cat >update.expect <<-EOF &&
687 refs/heads/main $orgmain $newmain
688 refs/heads/next $orgnext $newnext
691 cat >post-receive.expect <<-EOF &&
692 $orgmain $newmain refs/heads/main
693 $orgnext $newnext refs/heads/next
696 cat >post-update.expect <<-EOF &&
697 refs/heads/main
698 refs/heads/next
701 test_cmp pre-receive.expect pre-receive.actual &&
702 test_cmp update.expect update.actual &&
703 test_cmp post-receive.expect post-receive.actual &&
704 test_cmp post-update.expect post-update.actual
708 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
709 mk_test_with_hooks testrepo heads/main &&
710 rm -f testrepo/.git/objects/??/* &&
711 git push testrepo :refs/heads/main &&
713 cd testrepo/.git &&
714 cat >pre-receive.expect <<-EOF &&
715 $ZERO_OID $ZERO_OID refs/heads/main
718 cat >update.expect <<-EOF &&
719 refs/heads/main $ZERO_OID $ZERO_OID
722 cat >post-receive.expect <<-EOF &&
723 $ZERO_OID $ZERO_OID refs/heads/main
726 cat >post-update.expect <<-EOF &&
727 refs/heads/main
730 test_cmp pre-receive.expect pre-receive.actual &&
731 test_cmp update.expect update.actual &&
732 test_cmp post-receive.expect post-receive.actual &&
733 test_cmp post-update.expect post-update.actual
737 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
738 mk_test_with_hooks testrepo heads/main &&
739 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
740 newmain=$(git show-ref -s --verify refs/heads/main) &&
741 git push testrepo main :refs/heads/nonexistent &&
743 cd testrepo/.git &&
744 cat >pre-receive.expect <<-EOF &&
745 $orgmain $newmain refs/heads/main
746 $ZERO_OID $ZERO_OID refs/heads/nonexistent
749 cat >update.expect <<-EOF &&
750 refs/heads/main $orgmain $newmain
751 refs/heads/nonexistent $ZERO_OID $ZERO_OID
754 cat >post-receive.expect <<-EOF &&
755 $orgmain $newmain refs/heads/main
758 cat >post-update.expect <<-EOF &&
759 refs/heads/main
762 test_cmp pre-receive.expect pre-receive.actual &&
763 test_cmp update.expect update.actual &&
764 test_cmp post-receive.expect post-receive.actual &&
765 test_cmp post-update.expect post-update.actual
769 test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
770 mk_test_with_hooks testrepo heads/main &&
771 git push testrepo :refs/heads/nonexistent &&
773 cd testrepo/.git &&
774 cat >pre-receive.expect <<-EOF &&
775 $ZERO_OID $ZERO_OID refs/heads/nonexistent
778 cat >update.expect <<-EOF &&
779 refs/heads/nonexistent $ZERO_OID $ZERO_OID
782 test_cmp pre-receive.expect pre-receive.actual &&
783 test_cmp update.expect update.actual &&
784 test_path_is_missing post-receive.actual &&
785 test_path_is_missing post-update.actual
789 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
790 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
791 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
792 newmain=$(git show-ref -s --verify refs/heads/main) &&
793 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
794 newnext=$ZERO_OID &&
795 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
796 newseen=$(git show-ref -s --verify refs/heads/main) &&
797 git push testrepo refs/heads/main:refs/heads/main \
798 refs/heads/main:refs/heads/seen :refs/heads/next \
799 :refs/heads/nonexistent &&
801 cd testrepo/.git &&
802 cat >pre-receive.expect <<-EOF &&
803 $orgmain $newmain refs/heads/main
804 $orgnext $newnext refs/heads/next
805 $orgseen $newseen refs/heads/seen
806 $ZERO_OID $ZERO_OID refs/heads/nonexistent
809 cat >update.expect <<-EOF &&
810 refs/heads/main $orgmain $newmain
811 refs/heads/next $orgnext $newnext
812 refs/heads/seen $orgseen $newseen
813 refs/heads/nonexistent $ZERO_OID $ZERO_OID
816 cat >post-receive.expect <<-EOF &&
817 $orgmain $newmain refs/heads/main
818 $orgnext $newnext refs/heads/next
819 $orgseen $newseen refs/heads/seen
822 cat >post-update.expect <<-EOF &&
823 refs/heads/main
824 refs/heads/next
825 refs/heads/seen
828 test_cmp pre-receive.expect pre-receive.actual &&
829 test_cmp update.expect update.actual &&
830 test_cmp post-receive.expect post-receive.actual &&
831 test_cmp post-update.expect post-update.actual
835 test_expect_success 'allow deleting a ref using --delete' '
836 mk_test testrepo heads/main &&
837 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
838 git push testrepo --delete main &&
839 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
842 test_expect_success 'allow deleting a tag using --delete' '
843 mk_test testrepo heads/main &&
844 git tag -a -m dummy_message deltag heads/main &&
845 git push testrepo --tags &&
846 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
847 git push testrepo --delete tag deltag &&
848 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
851 test_expect_success 'push --delete without args aborts' '
852 mk_test testrepo heads/main &&
853 test_must_fail git push testrepo --delete
856 test_expect_success 'push --delete refuses src:dest refspecs' '
857 mk_test testrepo heads/main &&
858 test_must_fail git push testrepo --delete main:foo
861 test_expect_success 'push --delete refuses empty string' '
862 mk_test testrepo heads/master &&
863 test_must_fail git push testrepo --delete ""
866 test_expect_success 'warn on push to HEAD of non-bare repository' '
867 mk_test testrepo heads/main &&
869 cd testrepo &&
870 git checkout main &&
871 git config receive.denyCurrentBranch warn
872 ) &&
873 git push testrepo main 2>stderr &&
874 grep "warning: updating the current branch" stderr
877 test_expect_success 'deny push to HEAD of non-bare repository' '
878 mk_test testrepo heads/main &&
880 cd testrepo &&
881 git checkout main &&
882 git config receive.denyCurrentBranch true
883 ) &&
884 test_must_fail git push testrepo main
887 test_expect_success 'allow push to HEAD of bare repository (bare)' '
888 mk_test testrepo heads/main &&
890 cd testrepo &&
891 git checkout main &&
892 git config receive.denyCurrentBranch true &&
893 git config core.bare true
894 ) &&
895 git push testrepo main 2>stderr &&
896 ! grep "warning: updating the current branch" stderr
899 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
900 mk_test testrepo heads/main &&
902 cd testrepo &&
903 git checkout main &&
904 git config receive.denyCurrentBranch false
905 ) &&
906 git push testrepo main 2>stderr &&
907 ! grep "warning: updating the current branch" stderr
910 test_expect_success 'fetch with branches' '
911 mk_empty testrepo &&
912 git branch second $the_first_commit &&
913 git checkout second &&
914 echo ".." > testrepo/.git/branches/branch1 &&
916 cd testrepo &&
917 git fetch branch1 &&
918 echo "$the_commit commit refs/heads/branch1" >expect &&
919 git for-each-ref refs/heads >actual &&
920 test_cmp expect actual
921 ) &&
922 git checkout main
925 test_expect_success 'fetch with branches containing #' '
926 mk_empty testrepo &&
927 echo "..#second" > testrepo/.git/branches/branch2 &&
929 cd testrepo &&
930 git fetch branch2 &&
931 echo "$the_first_commit commit refs/heads/branch2" >expect &&
932 git for-each-ref refs/heads >actual &&
933 test_cmp expect actual
934 ) &&
935 git checkout main
938 test_expect_success 'push with branches' '
939 mk_empty testrepo &&
940 git checkout second &&
941 echo "testrepo" > .git/branches/branch1 &&
942 git push branch1 &&
944 cd testrepo &&
945 echo "$the_first_commit commit refs/heads/main" >expect &&
946 git for-each-ref refs/heads >actual &&
947 test_cmp expect actual
951 test_expect_success 'push with branches containing #' '
952 mk_empty testrepo &&
953 echo "testrepo#branch3" > .git/branches/branch2 &&
954 git push branch2 &&
956 cd testrepo &&
957 echo "$the_first_commit commit refs/heads/branch3" >expect &&
958 git for-each-ref refs/heads >actual &&
959 test_cmp expect actual
960 ) &&
961 git checkout main
964 test_expect_success 'push into aliased refs (consistent)' '
965 mk_test testrepo heads/main &&
966 mk_child testrepo child1 &&
967 mk_child testrepo child2 &&
969 cd child1 &&
970 git branch foo &&
971 git symbolic-ref refs/heads/bar refs/heads/foo &&
972 git config receive.denyCurrentBranch false
973 ) &&
975 cd child2 &&
976 >path2 &&
977 git add path2 &&
978 test_tick &&
979 git commit -a -m child2 &&
980 git branch foo &&
981 git branch bar &&
982 git push ../child1 foo bar
986 test_expect_success 'push into aliased refs (inconsistent)' '
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 >path3 &&
1004 git add path3 &&
1005 test_tick &&
1006 git commit -a -m child2 &&
1007 git branch bar &&
1008 test_must_fail git push ../child1 foo bar 2>stderr &&
1009 grep "refusing inconsistent update" stderr
1013 test_force_push_tag () {
1014 tag_type_description=$1
1015 tag_args=$2
1017 test_expect_success "force pushing required to update $tag_type_description" "
1018 mk_test testrepo heads/main &&
1019 mk_child testrepo child1 &&
1020 mk_child testrepo child2 &&
1022 cd child1 &&
1023 git tag testTag &&
1024 git push ../child2 testTag &&
1025 >file1 &&
1026 git add file1 &&
1027 git commit -m 'file1' &&
1028 git tag $tag_args testTag &&
1029 test_must_fail git push ../child2 testTag &&
1030 git push --force ../child2 testTag &&
1031 git tag $tag_args testTag HEAD~ &&
1032 test_must_fail git push ../child2 testTag &&
1033 git push --force ../child2 testTag &&
1035 # Clobbering without + in refspec needs --force
1036 git tag -f testTag &&
1037 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1038 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1040 # Clobbering with + in refspec does not need --force
1041 git tag -f testTag HEAD~ &&
1042 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1044 # Clobbering with --no-force still obeys + in refspec
1045 git tag -f testTag &&
1046 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1048 # Clobbering with/without --force and 'tag <name>' format
1049 git tag -f testTag HEAD~ &&
1050 test_must_fail git push ../child2 tag testTag &&
1051 git push --force ../child2 tag testTag
1056 test_force_push_tag "lightweight tag" "-f"
1057 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1059 test_force_fetch_tag () {
1060 tag_type_description=$1
1061 tag_args=$2
1063 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1064 mk_test testrepo heads/main &&
1065 mk_child testrepo child1 &&
1066 mk_child testrepo child2 &&
1068 cd testrepo &&
1069 git tag testTag &&
1070 git -C ../child1 fetch origin tag testTag &&
1071 >file1 &&
1072 git add file1 &&
1073 git commit -m 'file1' &&
1074 git tag $tag_args testTag &&
1075 test_must_fail git -C ../child1 fetch origin tag testTag &&
1076 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1081 test_force_fetch_tag "lightweight tag" "-f"
1082 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1084 test_expect_success 'push --porcelain' '
1085 mk_empty testrepo &&
1086 echo >.git/foo "To testrepo" &&
1087 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1088 echo >>.git/foo "Done" &&
1089 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1091 cd testrepo &&
1092 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1093 git for-each-ref refs/remotes/origin >actual &&
1094 test_cmp expect actual
1095 ) &&
1096 test_cmp .git/foo .git/bar
1099 test_expect_success 'push --porcelain bad url' '
1100 mk_empty testrepo &&
1101 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1102 ! grep -q Done .git/bar
1105 test_expect_success 'push --porcelain rejected' '
1106 mk_empty testrepo &&
1107 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1108 (cd testrepo &&
1109 git reset --hard origin/main^ &&
1110 git config receive.denyCurrentBranch true) &&
1112 echo >.git/foo "To testrepo" &&
1113 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1114 echo >>.git/foo "Done" &&
1116 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1117 test_cmp .git/foo .git/bar
1120 test_expect_success 'push --porcelain --dry-run rejected' '
1121 mk_empty testrepo &&
1122 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1123 (cd testrepo &&
1124 git reset --hard origin/main &&
1125 git config receive.denyCurrentBranch true) &&
1127 echo >.git/foo "To testrepo" &&
1128 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1129 echo >>.git/foo "Done" &&
1131 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1132 test_cmp .git/foo .git/bar
1135 test_expect_success 'push --prune' '
1136 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1137 git push --prune testrepo : &&
1138 check_push_result testrepo $the_commit heads/main &&
1139 check_push_result testrepo $the_first_commit heads/second &&
1140 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1143 test_expect_success 'push --prune refspec' '
1144 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1145 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1146 check_push_result testrepo $the_commit tmp/main &&
1147 check_push_result testrepo $the_first_commit tmp/second &&
1148 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1151 for configsection in transfer receive
1153 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1154 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1156 cd testrepo &&
1157 git config $configsection.hiderefs refs/hidden
1158 ) &&
1160 # push to unhidden ref succeeds normally
1161 git push testrepo main:refs/heads/main &&
1162 check_push_result testrepo $the_commit heads/main &&
1164 # push to update a hidden ref should fail
1165 test_must_fail git push testrepo main:refs/hidden/one &&
1166 check_push_result testrepo $the_first_commit hidden/one &&
1168 # push to delete a hidden ref should fail
1169 test_must_fail git push testrepo :refs/hidden/two &&
1170 check_push_result testrepo $the_first_commit hidden/two &&
1172 # idempotent push to update a hidden ref should fail
1173 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1174 check_push_result testrepo $the_first_commit hidden/three
1176 done
1178 test_expect_success 'fetch exact SHA1' '
1179 mk_test testrepo heads/main hidden/one &&
1180 git push testrepo main:refs/hidden/one &&
1182 cd testrepo &&
1183 git config transfer.hiderefs refs/hidden
1184 ) &&
1185 check_push_result testrepo $the_commit hidden/one &&
1187 mk_child testrepo child &&
1189 cd child &&
1191 # make sure $the_commit does not exist here
1192 git repack -a -d &&
1193 git prune &&
1194 test_must_fail git cat-file -t $the_commit &&
1196 # Some protocol versions (e.g. 2) support fetching
1197 # unadvertised objects, so restrict this test to v0.
1199 # fetching the hidden object should fail by default
1200 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1201 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1202 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1203 test_must_fail git rev-parse --verify refs/heads/copy &&
1205 # the server side can allow it to succeed
1207 cd ../testrepo &&
1208 git config uploadpack.allowtipsha1inwant true
1209 ) &&
1211 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1212 cat >expect <<-EOF &&
1213 $the_commit
1214 $the_first_commit
1217 git rev-parse --verify refs/heads/copy &&
1218 git rev-parse --verify refs/heads/extra
1219 } >actual &&
1220 test_cmp expect actual
1224 test_expect_success 'fetch exact SHA1 in protocol v2' '
1225 mk_test testrepo heads/main hidden/one &&
1226 git push testrepo main:refs/hidden/one &&
1227 git -C testrepo config transfer.hiderefs refs/hidden &&
1228 check_push_result testrepo $the_commit hidden/one &&
1230 mk_child testrepo child &&
1231 git -C child config protocol.version 2 &&
1233 # make sure $the_commit does not exist here
1234 git -C child repack -a -d &&
1235 git -C child prune &&
1236 test_must_fail git -C child cat-file -t $the_commit &&
1238 # fetching the hidden object succeeds by default
1239 # NEEDSWORK: should this match the v0 behavior instead?
1240 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1243 for configallowtipsha1inwant in true false
1245 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1246 mk_empty testrepo &&
1248 cd testrepo &&
1249 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1250 git commit --allow-empty -m foo &&
1251 git commit --allow-empty -m bar
1252 ) &&
1253 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1254 mk_empty shallow &&
1256 cd shallow &&
1257 # Some protocol versions (e.g. 2) support fetching
1258 # unadvertised objects, so restrict this test to v0.
1259 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1260 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1261 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1262 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1263 git cat-file commit $SHA1
1267 test_expect_success "deny fetch unreachable SHA1, 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 git commit --allow-empty -m xyz
1275 ) &&
1276 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1277 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1278 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1280 cd testrepo &&
1281 git reset --hard $SHA1_2 &&
1282 git cat-file commit $SHA1_1 &&
1283 git cat-file commit $SHA1_3
1284 ) &&
1285 mk_empty shallow &&
1287 cd shallow &&
1288 # Some protocol versions (e.g. 2) support fetching
1289 # unadvertised objects, so restrict this test to v0.
1290 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1291 git fetch ../testrepo/.git $SHA1_3 &&
1292 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1293 git fetch ../testrepo/.git $SHA1_1 &&
1294 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1295 git fetch ../testrepo/.git $SHA1_1 &&
1296 git cat-file commit $SHA1_1 &&
1297 test_must_fail git cat-file commit $SHA1_2 &&
1298 git fetch ../testrepo/.git $SHA1_2 &&
1299 git cat-file commit $SHA1_2 &&
1300 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1301 git fetch ../testrepo/.git $SHA1_3 2>err &&
1302 # ideally we would insist this be on a "remote error:"
1303 # line, but it is racy; see the commit message
1304 test_i18ngrep "not our ref.*$SHA1_3\$" err
1307 done
1309 test_expect_success 'fetch follows tags by default' '
1310 mk_test testrepo heads/main &&
1311 rm -fr src dst &&
1312 git init src &&
1314 cd src &&
1315 git pull ../testrepo main &&
1316 git tag -m "annotated" tag &&
1317 git for-each-ref >tmp1 &&
1319 cat tmp1
1320 sed -n "s|refs/heads/main$|refs/remotes/origin/main|p" tmp1
1322 sort -k 3 >../expect
1323 ) &&
1324 git init dst &&
1326 cd dst &&
1327 git remote add origin ../src &&
1328 git config branch.main.remote origin &&
1329 git config branch.main.merge refs/heads/main &&
1330 git pull &&
1331 git for-each-ref >../actual
1332 ) &&
1333 test_cmp expect actual
1336 test_expect_success 'peeled advertisements are not considered ref tips' '
1337 mk_empty testrepo &&
1338 git -C testrepo commit --allow-empty -m one &&
1339 git -C testrepo commit --allow-empty -m two &&
1340 git -C testrepo tag -m foo mytag HEAD^ &&
1341 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1342 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1343 git fetch testrepo $oid 2>err &&
1344 test_i18ngrep "Server does not allow request for unadvertised object" err
1347 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1348 mk_test testrepo heads/main &&
1349 rm -fr src dst &&
1350 git init src &&
1351 git init --bare dst &&
1353 cd src &&
1354 git pull ../testrepo main &&
1355 git branch next &&
1356 git config remote.dst.url ../dst &&
1357 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1358 git push dst main &&
1359 git show-ref refs/heads/main |
1360 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1361 ) &&
1363 cd dst &&
1364 test_must_fail git show-ref refs/heads/next &&
1365 test_must_fail git show-ref refs/heads/main &&
1366 git show-ref refs/remotes/src/main >actual
1367 ) &&
1368 test_cmp dst/expect dst/actual
1371 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1372 mk_test testrepo heads/main &&
1373 rm -fr src dst &&
1374 git init src &&
1375 git init --bare dst &&
1377 cd src &&
1378 git pull ../testrepo main &&
1379 git branch next &&
1380 git config remote.dst.url ../dst &&
1381 git config push.default matching &&
1382 git push dst main &&
1383 git show-ref refs/heads/main >../dst/expect
1384 ) &&
1386 cd dst &&
1387 test_must_fail git show-ref refs/heads/next &&
1388 git show-ref refs/heads/main >actual
1389 ) &&
1390 test_cmp dst/expect dst/actual
1393 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1394 mk_test testrepo heads/main &&
1395 rm -fr src dst &&
1396 git init src &&
1397 git init --bare dst &&
1399 cd src &&
1400 git pull ../testrepo main &&
1401 git branch next &&
1402 git config remote.dst.url ../dst &&
1403 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1404 git config push.default upstream &&
1406 git config branch.main.merge refs/heads/trunk &&
1407 git config branch.main.remote dst &&
1409 git push dst main &&
1410 git show-ref refs/heads/main |
1411 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1412 ) &&
1414 cd dst &&
1415 test_must_fail git show-ref refs/heads/main &&
1416 test_must_fail git show-ref refs/heads/next &&
1417 git show-ref refs/heads/trunk >actual
1418 ) &&
1419 test_cmp dst/expect dst/actual
1422 test_expect_success 'push does not follow tags by default' '
1423 mk_test testrepo heads/main &&
1424 rm -fr src dst &&
1425 git init src &&
1426 git init --bare dst &&
1428 cd src &&
1429 git pull ../testrepo main &&
1430 git tag -m "annotated" tag &&
1431 git checkout -b another &&
1432 git commit --allow-empty -m "future commit" &&
1433 git tag -m "future" future &&
1434 git checkout main &&
1435 git for-each-ref refs/heads/main >../expect &&
1436 git push ../dst main
1437 ) &&
1439 cd dst &&
1440 git for-each-ref >../actual
1441 ) &&
1442 test_cmp expect actual
1445 test_expect_success 'push --follow-tags only pushes relevant tags' '
1446 mk_test testrepo heads/main &&
1447 rm -fr src dst &&
1448 git init src &&
1449 git init --bare dst &&
1451 cd src &&
1452 git pull ../testrepo main &&
1453 git tag -m "annotated" tag &&
1454 git checkout -b another &&
1455 git commit --allow-empty -m "future commit" &&
1456 git tag -m "future" future &&
1457 git checkout main &&
1458 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1459 git push --follow-tags ../dst main
1460 ) &&
1462 cd dst &&
1463 git for-each-ref >../actual
1464 ) &&
1465 test_cmp expect actual
1468 test_expect_success 'push --no-thin must produce non-thin pack' '
1469 cat >>path1 <<\EOF &&
1470 keep base version of path1 big enough, compared to the new changes
1471 later, in order to pass size heuristics in
1472 builtin/pack-objects.c:try_delta()
1474 git commit -am initial &&
1475 git init no-thin &&
1476 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1477 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1478 echo modified >> path1 &&
1479 git commit -am modified &&
1480 git repack -adf &&
1481 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1482 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1485 test_expect_success 'pushing a tag pushes the tagged object' '
1486 rm -rf dst.git &&
1487 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1488 git tag -m foo tag-of-blob $blob &&
1489 git init --bare dst.git &&
1490 git push dst.git tag-of-blob &&
1491 # the receiving index-pack should have noticed
1492 # any problems, but we double check
1493 echo unreferenced >expect &&
1494 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1495 test_cmp expect actual
1498 test_expect_success 'push into bare respects core.logallrefupdates' '
1499 rm -rf dst.git &&
1500 git init --bare dst.git &&
1501 git -C dst.git config core.logallrefupdates true &&
1503 # double push to test both with and without
1504 # the actual pack transfer
1505 git push dst.git main:one &&
1506 echo "one@{0} push" >expect &&
1507 git -C dst.git log -g --format="%gd %gs" one >actual &&
1508 test_cmp expect actual &&
1510 git push dst.git main:two &&
1511 echo "two@{0} push" >expect &&
1512 git -C dst.git log -g --format="%gd %gs" two >actual &&
1513 test_cmp expect actual
1516 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1517 rm -rf dst.git &&
1518 git init --bare dst.git &&
1520 cd dst.git &&
1521 git config core.logallrefupdates true &&
1523 # as above, we double-fetch to test both
1524 # with and without pack transfer
1525 git fetch .. main:one &&
1526 echo "one@{0} fetch .. main:one: storing head" >expect &&
1527 git log -g --format="%gd %gs" one >actual &&
1528 test_cmp expect actual &&
1530 git fetch .. main:two &&
1531 echo "two@{0} fetch .. main:two: storing head" >expect &&
1532 git log -g --format="%gd %gs" two >actual &&
1533 test_cmp expect actual
1537 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1538 git push testrepo main &&
1540 cd testrepo &&
1541 git reset --hard &&
1542 git config receive.denyCurrentBranch updateInstead
1543 ) &&
1544 test_commit third path2 &&
1546 # Try pushing into a repository with pristine working tree
1547 git push testrepo main &&
1549 cd testrepo &&
1550 git update-index -q --refresh &&
1551 git diff-files --quiet -- &&
1552 git diff-index --quiet --cached HEAD -- &&
1553 test third = "$(cat path2)" &&
1554 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1555 ) &&
1557 # Try pushing into a repository with working tree needing a refresh
1559 cd testrepo &&
1560 git reset --hard HEAD^ &&
1561 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1562 test-tool chmtime +100 path1
1563 ) &&
1564 git push testrepo main &&
1566 cd testrepo &&
1567 git update-index -q --refresh &&
1568 git diff-files --quiet -- &&
1569 git diff-index --quiet --cached HEAD -- &&
1570 test_cmp ../path1 path1 &&
1571 test third = "$(cat path2)" &&
1572 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1573 ) &&
1575 # Update what is to be pushed
1576 test_commit fourth path2 &&
1578 # Try pushing into a repository with a dirty working tree
1579 # (1) the working tree updated
1581 cd testrepo &&
1582 echo changed >path1
1583 ) &&
1584 test_must_fail git push testrepo main &&
1586 cd testrepo &&
1587 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1588 git diff --quiet --cached &&
1589 test changed = "$(cat path1)"
1590 ) &&
1592 # (2) the index updated
1594 cd testrepo &&
1595 echo changed >path1 &&
1596 git add path1
1597 ) &&
1598 test_must_fail git push testrepo main &&
1600 cd testrepo &&
1601 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1602 git diff --quiet &&
1603 test changed = "$(cat path1)"
1604 ) &&
1606 # Introduce a new file in the update
1607 test_commit fifth path3 &&
1609 # (3) the working tree has an untracked file that would interfere
1611 cd testrepo &&
1612 git reset --hard &&
1613 echo changed >path3
1614 ) &&
1615 test_must_fail git push testrepo main &&
1617 cd testrepo &&
1618 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1619 git diff --quiet &&
1620 git diff --quiet --cached &&
1621 test changed = "$(cat path3)"
1622 ) &&
1624 # (4) the target changes to what gets pushed but it still is a change
1626 cd testrepo &&
1627 git reset --hard &&
1628 echo fifth >path3 &&
1629 git add path3
1630 ) &&
1631 test_must_fail git push testrepo main &&
1633 cd testrepo &&
1634 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1635 git diff --quiet &&
1636 test fifth = "$(cat path3)"
1637 ) &&
1639 # (5) push into void
1640 rm -fr void &&
1641 git init void &&
1643 cd void &&
1644 git config receive.denyCurrentBranch updateInstead
1645 ) &&
1646 git push void main &&
1648 cd void &&
1649 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1650 git diff --quiet &&
1651 git diff --cached --quiet
1652 ) &&
1654 # (6) updateInstead intervened by fast-forward check
1655 test_must_fail git push void main^:main &&
1656 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1657 git -C void diff --quiet &&
1658 git -C void diff --cached --quiet
1661 test_expect_success 'updateInstead with push-to-checkout hook' '
1662 rm -fr testrepo &&
1663 git init testrepo &&
1665 cd testrepo &&
1666 git pull .. main &&
1667 git reset --hard HEAD^^ &&
1668 git tag initial &&
1669 git config receive.denyCurrentBranch updateInstead &&
1670 write_script .git/hooks/push-to-checkout <<-\EOF
1671 echo >&2 updating from $(git rev-parse HEAD)
1672 echo >&2 updating to "$1"
1674 git update-index -q --refresh &&
1675 git read-tree -u -m HEAD "$1" || {
1676 status=$?
1677 echo >&2 read-tree failed
1678 exit $status
1681 ) &&
1683 # Try pushing into a pristine
1684 git push testrepo main &&
1686 cd testrepo &&
1687 git diff --quiet &&
1688 git diff HEAD --quiet &&
1689 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1690 ) &&
1692 # Try pushing into a repository with conflicting change
1694 cd testrepo &&
1695 git reset --hard initial &&
1696 echo conflicting >path2
1697 ) &&
1698 test_must_fail git push testrepo main &&
1700 cd testrepo &&
1701 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1702 test conflicting = "$(cat path2)" &&
1703 git diff-index --quiet --cached HEAD
1704 ) &&
1706 # Try pushing into a repository with unrelated change
1708 cd testrepo &&
1709 git reset --hard initial &&
1710 echo unrelated >path1 &&
1711 echo irrelevant >path5 &&
1712 git add path5
1713 ) &&
1714 git push testrepo main &&
1716 cd testrepo &&
1717 test "$(cat path1)" = unrelated &&
1718 test "$(cat path5)" = irrelevant &&
1719 test "$(git diff --name-only --cached HEAD)" = path5 &&
1720 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1721 ) &&
1723 # push into void
1724 rm -fr void &&
1725 git init void &&
1727 cd void &&
1728 git config receive.denyCurrentBranch updateInstead &&
1729 write_script .git/hooks/push-to-checkout <<-\EOF
1730 if git rev-parse --quiet --verify HEAD
1731 then
1732 has_head=yes
1733 echo >&2 updating from $(git rev-parse HEAD)
1734 else
1735 has_head=no
1736 echo >&2 pushing into void
1738 echo >&2 updating to "$1"
1740 git update-index -q --refresh &&
1741 case "$has_head" in
1742 yes)
1743 git read-tree -u -m HEAD "$1" ;;
1745 git read-tree -u -m "$1" ;;
1746 esac || {
1747 status=$?
1748 echo >&2 read-tree failed
1749 exit $status
1752 ) &&
1754 git push void main &&
1756 cd void &&
1757 git diff --quiet &&
1758 git diff --cached --quiet &&
1759 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1763 test_expect_success 'denyCurrentBranch and worktrees' '
1764 git worktree add new-wt &&
1765 git clone . cloned &&
1766 test_commit -C cloned first &&
1767 test_config receive.denyCurrentBranch refuse &&
1768 test_must_fail git -C cloned push origin HEAD:new-wt &&
1769 test_config receive.denyCurrentBranch updateInstead &&
1770 git -C cloned push origin HEAD:new-wt &&
1771 test_must_fail git -C cloned push --delete origin new-wt
1773 test_done