rev-list-options.txt: fix simple typo
[git/debian.git] / t / t5516-fetch-push.sh
blobf3356f9ea8cc1d893f756bf9af9806d210904124
1 #!/bin/sh
3 test_description='Basic fetch/push functionality.
5 This test checks the following functionality:
7 * command-line syntax
8 * refspecs
9 * fast-forward detection, and overriding it
10 * configuration
11 * hooks
12 * --porcelain output format
13 * hiderefs
14 * reflogs
15 * URL validation
18 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
19 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
21 TEST_CREATE_REPO_NO_TEMPLATE=1
22 . ./test-lib.sh
24 D=$(pwd)
26 mk_empty () {
27 repo_name="$1"
28 test_when_finished "rm -rf \"$repo_name\"" &&
29 test_path_is_missing "$repo_name" &&
30 git init --template= "$repo_name" &&
31 mkdir "$repo_name"/.git/hooks &&
32 git -C "$repo_name" config receive.denyCurrentBranch warn
35 mk_test () {
36 repo_name="$1"
37 shift
39 mk_empty "$repo_name" &&
41 for ref in "$@"
43 git push "$repo_name" $the_first_commit:refs/$ref ||
44 exit
45 done &&
46 cd "$repo_name" &&
47 for ref in "$@"
49 echo "$the_first_commit" >expect &&
50 git show-ref -s --verify refs/$ref >actual &&
51 test_cmp expect actual ||
52 exit
53 done &&
54 git fsck --full
58 mk_test_with_hooks() {
59 repo_name=$1
60 mk_test "$@" &&
61 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
62 cat - >>pre-receive.actual
63 EOF
65 test_hook -C "$repo_name" update <<-'EOF' &&
66 printf "%s %s %s\n" "$@" >>update.actual
67 EOF
69 test_hook -C "$repo_name" post-receive <<-'EOF' &&
70 cat - >>post-receive.actual
71 EOF
73 test_hook -C "$repo_name" post-update <<-'EOF'
74 for ref in "$@"
76 printf "%s\n" "$ref" >>post-update.actual
77 done
78 EOF
81 mk_child() {
82 test_when_finished "rm -rf \"$2\"" &&
83 git clone --template= "$1" "$2"
86 check_push_result () {
87 test $# -ge 3 ||
88 BUG "check_push_result requires at least 3 parameters"
90 repo_name="$1"
91 shift
94 cd "$repo_name" &&
95 echo "$1" >expect &&
96 shift &&
97 for ref in "$@"
99 git show-ref -s --verify refs/$ref >actual &&
100 test_cmp expect actual ||
101 exit
102 done &&
103 git fsck --full
107 test_expect_success setup '
109 >path1 &&
110 git add path1 &&
111 test_tick &&
112 git commit -a -m repo &&
113 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
115 >path2 &&
116 git add path2 &&
117 test_tick &&
118 git commit -a -m second &&
119 the_commit=$(git show-ref -s --verify refs/heads/main)
123 test_expect_success 'fetch without wildcard' '
124 mk_empty testrepo &&
126 cd testrepo &&
127 git fetch .. refs/heads/main:refs/remotes/origin/main &&
129 echo "$the_commit commit refs/remotes/origin/main" >expect &&
130 git for-each-ref refs/remotes/origin >actual &&
131 test_cmp expect actual
135 test_expect_success 'fetch with wildcard' '
136 mk_empty testrepo &&
138 cd testrepo &&
139 git config remote.up.url .. &&
140 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
141 git fetch up &&
143 echo "$the_commit commit refs/remotes/origin/main" >expect &&
144 git for-each-ref refs/remotes/origin >actual &&
145 test_cmp expect actual
149 test_expect_success 'fetch with insteadOf' '
150 mk_empty testrepo &&
152 TRASH=$(pwd)/ &&
153 cd testrepo &&
154 git config "url.$TRASH.insteadOf" trash/ &&
155 git config remote.up.url trash/. &&
156 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
157 git fetch up &&
159 echo "$the_commit commit refs/remotes/origin/main" >expect &&
160 git for-each-ref refs/remotes/origin >actual &&
161 test_cmp expect actual
165 test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
166 mk_empty testrepo &&
168 TRASH=$(pwd)/ &&
169 cd testrepo &&
170 git config "url.trash/.pushInsteadOf" "$TRASH" &&
171 git config remote.up.url "$TRASH." &&
172 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
173 git fetch up &&
175 echo "$the_commit commit refs/remotes/origin/main" >expect &&
176 git for-each-ref refs/remotes/origin >actual &&
177 test_cmp expect actual
181 grep_wrote () {
182 object_count=$1
183 file_name=$2
184 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
187 test_expect_success 'push without negotiation' '
188 mk_empty testrepo &&
189 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
190 test_commit -C testrepo unrelated_commit &&
191 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
192 test_when_finished "rm event" &&
193 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
194 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
197 test_expect_success 'push with negotiation' '
198 mk_empty testrepo &&
199 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
200 test_commit -C testrepo unrelated_commit &&
201 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
202 test_when_finished "rm event" &&
203 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
204 grep_wrote 2 event # 1 commit, 1 tree
207 test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
208 mk_empty testrepo &&
209 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
210 test_commit -C testrepo unrelated_commit &&
211 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
212 test_when_finished "rm event" &&
213 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
214 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
215 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
216 test_i18ngrep "push negotiation failed" err
219 test_expect_success 'push with negotiation does not attempt to fetch submodules' '
220 mk_empty submodule_upstream &&
221 test_commit -C submodule_upstream submodule_commit &&
222 git submodule add ./submodule_upstream submodule &&
223 mk_empty testrepo &&
224 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
225 test_commit -C testrepo unrelated_commit &&
226 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
227 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
228 ! grep "Fetching submodule" err
231 test_expect_success 'push without wildcard' '
232 mk_empty testrepo &&
234 git push testrepo refs/heads/main:refs/remotes/origin/main &&
236 cd testrepo &&
237 echo "$the_commit commit refs/remotes/origin/main" >expect &&
238 git for-each-ref refs/remotes/origin >actual &&
239 test_cmp expect actual
243 test_expect_success 'push with wildcard' '
244 mk_empty testrepo &&
246 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
248 cd testrepo &&
249 echo "$the_commit commit refs/remotes/origin/main" >expect &&
250 git for-each-ref refs/remotes/origin >actual &&
251 test_cmp expect actual
255 test_expect_success 'push with insteadOf' '
256 mk_empty testrepo &&
257 TRASH="$(pwd)/" &&
258 test_config "url.$TRASH.insteadOf" trash/ &&
259 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
261 cd testrepo &&
262 echo "$the_commit commit refs/remotes/origin/main" >expect &&
263 git for-each-ref refs/remotes/origin >actual &&
264 test_cmp expect actual
268 test_expect_success 'push with pushInsteadOf' '
269 mk_empty testrepo &&
270 TRASH="$(pwd)/" &&
271 test_config "url.$TRASH.pushInsteadOf" trash/ &&
272 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
274 cd testrepo &&
275 echo "$the_commit commit refs/remotes/origin/main" >expect &&
276 git for-each-ref refs/remotes/origin >actual &&
277 test_cmp expect actual
281 test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
282 mk_empty testrepo &&
283 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
284 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
285 test_config remote.r.url trash/wrong &&
286 test_config remote.r.pushurl "testrepo/" &&
287 git push r refs/heads/main:refs/remotes/origin/main &&
289 cd testrepo &&
290 echo "$the_commit commit refs/remotes/origin/main" >expect &&
291 git for-each-ref refs/remotes/origin >actual &&
292 test_cmp expect actual
296 test_expect_success 'push with matching heads' '
298 mk_test testrepo heads/main &&
299 git push testrepo : &&
300 check_push_result testrepo $the_commit heads/main
304 test_expect_success 'push with matching heads on the command line' '
306 mk_test testrepo heads/main &&
307 git push testrepo : &&
308 check_push_result testrepo $the_commit heads/main
312 test_expect_success 'failed (non-fast-forward) push with matching heads' '
314 mk_test testrepo heads/main &&
315 git push testrepo : &&
316 git commit --amend -massaged &&
317 test_must_fail git push testrepo &&
318 check_push_result testrepo $the_commit heads/main &&
319 git reset --hard $the_commit
323 test_expect_success 'push --force with matching heads' '
325 mk_test testrepo heads/main &&
326 git push testrepo : &&
327 git commit --amend -massaged &&
328 git push --force testrepo : &&
329 ! check_push_result testrepo $the_commit heads/main &&
330 git reset --hard $the_commit
334 test_expect_success 'push with matching heads and forced update' '
336 mk_test testrepo heads/main &&
337 git push testrepo : &&
338 git commit --amend -massaged &&
339 git push testrepo +: &&
340 ! check_push_result testrepo $the_commit heads/main &&
341 git reset --hard $the_commit
345 test_expect_success 'push with no ambiguity (1)' '
347 mk_test testrepo heads/main &&
348 git push testrepo main:main &&
349 check_push_result testrepo $the_commit heads/main
353 test_expect_success 'push with no ambiguity (2)' '
355 mk_test testrepo remotes/origin/main &&
356 git push testrepo main:origin/main &&
357 check_push_result testrepo $the_commit remotes/origin/main
361 test_expect_success 'push with colon-less refspec, no ambiguity' '
363 mk_test testrepo heads/main heads/t/main &&
364 git branch -f t/main main &&
365 git push testrepo main &&
366 check_push_result testrepo $the_commit heads/main &&
367 check_push_result testrepo $the_first_commit heads/t/main
371 test_expect_success 'push with weak ambiguity (1)' '
373 mk_test testrepo heads/main remotes/origin/main &&
374 git push testrepo main:main &&
375 check_push_result testrepo $the_commit heads/main &&
376 check_push_result testrepo $the_first_commit remotes/origin/main
380 test_expect_success 'push with weak ambiguity (2)' '
382 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
383 git push testrepo main:main &&
384 check_push_result testrepo $the_commit heads/main &&
385 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
389 test_expect_success 'push with ambiguity' '
391 mk_test testrepo heads/frotz tags/frotz &&
392 test_must_fail git push testrepo main:frotz &&
393 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
397 test_expect_success 'push with colon-less refspec (1)' '
399 mk_test testrepo heads/frotz tags/frotz &&
400 git branch -f frotz main &&
401 git push testrepo frotz &&
402 check_push_result testrepo $the_commit heads/frotz &&
403 check_push_result testrepo $the_first_commit tags/frotz
407 test_expect_success 'push with colon-less refspec (2)' '
409 mk_test testrepo heads/frotz tags/frotz &&
410 if git show-ref --verify -q refs/heads/frotz
411 then
412 git branch -D frotz
413 fi &&
414 git tag -f frotz &&
415 git push -f testrepo frotz &&
416 check_push_result testrepo $the_commit tags/frotz &&
417 check_push_result testrepo $the_first_commit heads/frotz
421 test_expect_success 'push with colon-less refspec (3)' '
423 mk_test testrepo &&
424 if git show-ref --verify -q refs/tags/frotz
425 then
426 git tag -d frotz
427 fi &&
428 git branch -f frotz main &&
429 git push testrepo frotz &&
430 check_push_result testrepo $the_commit heads/frotz &&
431 test 1 = $( cd testrepo && git show-ref | wc -l )
434 test_expect_success 'push with colon-less refspec (4)' '
436 mk_test testrepo &&
437 if git show-ref --verify -q refs/heads/frotz
438 then
439 git branch -D frotz
440 fi &&
441 git tag -f frotz &&
442 git push testrepo frotz &&
443 check_push_result testrepo $the_commit tags/frotz &&
444 test 1 = $( cd testrepo && git show-ref | wc -l )
448 test_expect_success 'push head with non-existent, incomplete dest' '
450 mk_test testrepo &&
451 git push testrepo main:branch &&
452 check_push_result testrepo $the_commit heads/branch
456 test_expect_success 'push tag with non-existent, incomplete dest' '
458 mk_test testrepo &&
459 git tag -f v1.0 &&
460 git push testrepo v1.0:tag &&
461 check_push_result testrepo $the_commit tags/tag
465 test_expect_success 'push sha1 with non-existent, incomplete dest' '
467 mk_test testrepo &&
468 test_must_fail git push testrepo $(git rev-parse main):foo
472 test_expect_success 'push ref expression with non-existent, incomplete dest' '
474 mk_test testrepo &&
475 test_must_fail git push testrepo main^:branch
479 for head in HEAD @
482 test_expect_success "push with $head" '
483 mk_test testrepo heads/main &&
484 git checkout main &&
485 git push testrepo $head &&
486 check_push_result testrepo $the_commit heads/main
489 test_expect_success "push with $head nonexisting at remote" '
490 mk_test testrepo heads/main &&
491 git checkout -b local main &&
492 test_when_finished "git checkout main; git branch -D local" &&
493 git push testrepo $head &&
494 check_push_result testrepo $the_commit heads/local
497 test_expect_success "push with +$head" '
498 mk_test testrepo heads/main &&
499 git checkout -b local main &&
500 test_when_finished "git checkout main; git branch -D local" &&
501 git push testrepo main local &&
502 check_push_result testrepo $the_commit heads/main &&
503 check_push_result testrepo $the_commit heads/local &&
505 # Without force rewinding should fail
506 git reset --hard $head^ &&
507 test_must_fail git push testrepo $head &&
508 check_push_result testrepo $the_commit heads/local &&
510 # With force rewinding should succeed
511 git push testrepo +$head &&
512 check_push_result testrepo $the_first_commit heads/local
515 test_expect_success "push $head with non-existent, incomplete dest" '
516 mk_test testrepo &&
517 git checkout main &&
518 git push testrepo $head:branch &&
519 check_push_result testrepo $the_commit heads/branch
523 test_expect_success "push with config remote.*.push = $head" '
524 mk_test testrepo heads/local &&
525 git checkout main &&
526 git branch -f local $the_commit &&
527 test_when_finished "git branch -D local" &&
529 cd testrepo &&
530 git checkout local &&
531 git reset --hard $the_first_commit
532 ) &&
533 test_config remote.there.url testrepo &&
534 test_config remote.there.push $head &&
535 test_config branch.main.remote there &&
536 git push &&
537 check_push_result testrepo $the_commit heads/main &&
538 check_push_result testrepo $the_first_commit heads/local
541 done
543 test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
544 mk_test testrepo heads/main &&
545 git checkout $the_first_commit &&
546 test_config remote.there.url testrepo &&
547 test_config remote.there.push refs/heads/main:refs/heads/main &&
548 git push there &&
549 check_push_result testrepo $the_commit heads/main
552 test_expect_success 'push with remote.pushdefault' '
553 mk_test up_repo heads/main &&
554 mk_test down_repo heads/main &&
555 test_config remote.up.url up_repo &&
556 test_config remote.down.url down_repo &&
557 test_config branch.main.remote up &&
558 test_config remote.pushdefault down &&
559 test_config push.default matching &&
560 git push &&
561 check_push_result up_repo $the_first_commit heads/main &&
562 check_push_result down_repo $the_commit heads/main
565 test_expect_success 'push with config remote.*.pushurl' '
567 mk_test testrepo heads/main &&
568 git checkout main &&
569 test_config remote.there.url test2repo &&
570 test_config remote.there.pushurl testrepo &&
571 git push there : &&
572 check_push_result testrepo $the_commit heads/main
575 test_expect_success 'push with config branch.*.pushremote' '
576 mk_test up_repo heads/main &&
577 mk_test side_repo heads/main &&
578 mk_test down_repo heads/main &&
579 test_config remote.up.url up_repo &&
580 test_config remote.pushdefault side_repo &&
581 test_config remote.down.url down_repo &&
582 test_config branch.main.remote up &&
583 test_config branch.main.pushremote down &&
584 test_config push.default matching &&
585 git push &&
586 check_push_result up_repo $the_first_commit heads/main &&
587 check_push_result side_repo $the_first_commit heads/main &&
588 check_push_result down_repo $the_commit heads/main
591 test_expect_success 'branch.*.pushremote config order is irrelevant' '
592 mk_test one_repo heads/main &&
593 mk_test two_repo heads/main &&
594 test_config remote.one.url one_repo &&
595 test_config remote.two.url two_repo &&
596 test_config branch.main.pushremote two_repo &&
597 test_config remote.pushdefault one_repo &&
598 test_config push.default matching &&
599 git push &&
600 check_push_result one_repo $the_first_commit heads/main &&
601 check_push_result two_repo $the_commit heads/main
604 test_expect_success 'push rejects empty branch name entries' '
605 mk_test one_repo heads/main &&
606 test_config remote.one.url one_repo &&
607 test_config branch..remote one &&
608 test_config branch..merge refs/heads/ &&
609 test_config branch.main.remote one &&
610 test_config branch.main.merge refs/heads/main &&
611 test_must_fail git push 2>err &&
612 grep "bad config variable .branch\.\." err
615 test_expect_success 'push ignores "branch." config without subsection' '
616 mk_test one_repo heads/main &&
617 test_config remote.one.url one_repo &&
618 test_config branch.autoSetupMerge true &&
619 test_config branch.main.remote one &&
620 test_config branch.main.merge refs/heads/main &&
621 git push
624 test_expect_success 'push with dry-run' '
626 mk_test testrepo heads/main &&
627 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
628 git push --dry-run testrepo : &&
629 check_push_result testrepo $old_commit heads/main
632 test_expect_success 'push updates local refs' '
634 mk_test testrepo heads/main &&
635 mk_child testrepo child &&
637 cd child &&
638 git pull .. main &&
639 git push &&
640 test $(git rev-parse main) = \
641 $(git rev-parse remotes/origin/main)
646 test_expect_success 'push updates up-to-date local refs' '
648 mk_test testrepo heads/main &&
649 mk_child testrepo child1 &&
650 mk_child testrepo child2 &&
651 (cd child1 && git pull .. main && git push) &&
653 cd child2 &&
654 git pull ../child1 main &&
655 git push &&
656 test $(git rev-parse main) = \
657 $(git rev-parse remotes/origin/main)
662 test_expect_success 'push preserves up-to-date packed refs' '
664 mk_test testrepo heads/main &&
665 mk_child testrepo child &&
667 cd child &&
668 git push &&
669 ! test -f .git/refs/remotes/origin/main
674 test_expect_success 'push does not update local refs on failure' '
676 mk_test testrepo heads/main &&
677 mk_child testrepo child &&
678 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
679 chmod +x testrepo/.git/hooks/pre-receive &&
681 cd child &&
682 git pull .. main &&
683 test_must_fail git push &&
684 test $(git rev-parse main) != \
685 $(git rev-parse remotes/origin/main)
690 test_expect_success 'allow deleting an invalid remote ref' '
692 mk_test testrepo heads/branch &&
693 rm -f testrepo/.git/objects/??/* &&
694 git push testrepo :refs/heads/branch &&
695 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
699 test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
700 mk_test_with_hooks testrepo heads/main heads/next &&
701 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
702 newmain=$(git show-ref -s --verify refs/heads/main) &&
703 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
704 newnext=$ZERO_OID &&
705 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
707 cd testrepo/.git &&
708 cat >pre-receive.expect <<-EOF &&
709 $orgmain $newmain refs/heads/main
710 $orgnext $newnext refs/heads/next
713 cat >update.expect <<-EOF &&
714 refs/heads/main $orgmain $newmain
715 refs/heads/next $orgnext $newnext
718 cat >post-receive.expect <<-EOF &&
719 $orgmain $newmain refs/heads/main
720 $orgnext $newnext refs/heads/next
723 cat >post-update.expect <<-EOF &&
724 refs/heads/main
725 refs/heads/next
728 test_cmp pre-receive.expect pre-receive.actual &&
729 test_cmp update.expect update.actual &&
730 test_cmp post-receive.expect post-receive.actual &&
731 test_cmp post-update.expect post-update.actual
735 test_expect_success 'deleting dangling ref triggers hooks with correct args' '
736 mk_test_with_hooks testrepo heads/branch &&
737 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
738 rm -f testrepo/.git/objects/??/* &&
739 git push testrepo :refs/heads/branch &&
741 cd testrepo/.git &&
742 cat >pre-receive.expect <<-EOF &&
743 $orig $ZERO_OID refs/heads/branch
746 cat >update.expect <<-EOF &&
747 refs/heads/branch $orig $ZERO_OID
750 cat >post-receive.expect <<-EOF &&
751 $orig $ZERO_OID refs/heads/branch
754 cat >post-update.expect <<-EOF &&
755 refs/heads/branch
758 test_cmp pre-receive.expect pre-receive.actual &&
759 test_cmp update.expect update.actual &&
760 test_cmp post-receive.expect post-receive.actual &&
761 test_cmp post-update.expect post-update.actual
765 test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
766 mk_test_with_hooks testrepo heads/main &&
767 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
768 newmain=$(git show-ref -s --verify refs/heads/main) &&
769 git push testrepo main :refs/heads/nonexistent &&
771 cd testrepo/.git &&
772 cat >pre-receive.expect <<-EOF &&
773 $orgmain $newmain refs/heads/main
774 $ZERO_OID $ZERO_OID refs/heads/nonexistent
777 cat >update.expect <<-EOF &&
778 refs/heads/main $orgmain $newmain
779 refs/heads/nonexistent $ZERO_OID $ZERO_OID
782 cat >post-receive.expect <<-EOF &&
783 $orgmain $newmain refs/heads/main
786 cat >post-update.expect <<-EOF &&
787 refs/heads/main
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 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
798 mk_test_with_hooks testrepo heads/main &&
799 git push testrepo :refs/heads/nonexistent &&
801 cd testrepo/.git &&
802 cat >pre-receive.expect <<-EOF &&
803 $ZERO_OID $ZERO_OID refs/heads/nonexistent
806 cat >update.expect <<-EOF &&
807 refs/heads/nonexistent $ZERO_OID $ZERO_OID
810 test_cmp pre-receive.expect pre-receive.actual &&
811 test_cmp update.expect update.actual &&
812 test_path_is_missing post-receive.actual &&
813 test_path_is_missing post-update.actual
817 test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
818 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
819 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
820 newmain=$(git show-ref -s --verify refs/heads/main) &&
821 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
822 newnext=$ZERO_OID &&
823 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
824 newseen=$(git show-ref -s --verify refs/heads/main) &&
825 git push testrepo refs/heads/main:refs/heads/main \
826 refs/heads/main:refs/heads/seen :refs/heads/next \
827 :refs/heads/nonexistent &&
829 cd testrepo/.git &&
830 cat >pre-receive.expect <<-EOF &&
831 $orgmain $newmain refs/heads/main
832 $orgnext $newnext refs/heads/next
833 $orgseen $newseen refs/heads/seen
834 $ZERO_OID $ZERO_OID refs/heads/nonexistent
837 cat >update.expect <<-EOF &&
838 refs/heads/main $orgmain $newmain
839 refs/heads/next $orgnext $newnext
840 refs/heads/seen $orgseen $newseen
841 refs/heads/nonexistent $ZERO_OID $ZERO_OID
844 cat >post-receive.expect <<-EOF &&
845 $orgmain $newmain refs/heads/main
846 $orgnext $newnext refs/heads/next
847 $orgseen $newseen refs/heads/seen
850 cat >post-update.expect <<-EOF &&
851 refs/heads/main
852 refs/heads/next
853 refs/heads/seen
856 test_cmp pre-receive.expect pre-receive.actual &&
857 test_cmp update.expect update.actual &&
858 test_cmp post-receive.expect post-receive.actual &&
859 test_cmp post-update.expect post-update.actual
863 test_expect_success 'allow deleting a ref using --delete' '
864 mk_test testrepo heads/main &&
865 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
866 git push testrepo --delete main &&
867 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
870 test_expect_success 'allow deleting a tag using --delete' '
871 mk_test testrepo heads/main &&
872 git tag -a -m dummy_message deltag heads/main &&
873 git push testrepo --tags &&
874 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
875 git push testrepo --delete tag deltag &&
876 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
879 test_expect_success 'push --delete without args aborts' '
880 mk_test testrepo heads/main &&
881 test_must_fail git push testrepo --delete
884 test_expect_success 'push --delete refuses src:dest refspecs' '
885 mk_test testrepo heads/main &&
886 test_must_fail git push testrepo --delete main:foo
889 test_expect_success 'push --delete refuses empty string' '
890 mk_test testrepo heads/master &&
891 test_must_fail git push testrepo --delete ""
894 test_expect_success 'warn on push to HEAD of non-bare repository' '
895 mk_test testrepo heads/main &&
897 cd testrepo &&
898 git checkout main &&
899 git config receive.denyCurrentBranch warn
900 ) &&
901 git push testrepo main 2>stderr &&
902 grep "warning: updating the current branch" stderr
905 test_expect_success 'deny push to HEAD of non-bare repository' '
906 mk_test testrepo heads/main &&
908 cd testrepo &&
909 git checkout main &&
910 git config receive.denyCurrentBranch true
911 ) &&
912 test_must_fail git push testrepo main
915 test_expect_success 'allow push to HEAD of bare repository (bare)' '
916 mk_test testrepo heads/main &&
918 cd testrepo &&
919 git checkout main &&
920 git config receive.denyCurrentBranch true &&
921 git config core.bare true
922 ) &&
923 git push testrepo main 2>stderr &&
924 ! grep "warning: updating the current branch" stderr
927 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
928 mk_test testrepo heads/main &&
930 cd testrepo &&
931 git checkout main &&
932 git config receive.denyCurrentBranch false
933 ) &&
934 git push testrepo main 2>stderr &&
935 ! grep "warning: updating the current branch" stderr
938 test_expect_success 'fetch with branches' '
939 mk_empty testrepo &&
940 git branch second $the_first_commit &&
941 git checkout second &&
942 mkdir testrepo/.git/branches &&
943 echo ".." > testrepo/.git/branches/branch1 &&
945 cd testrepo &&
946 git fetch branch1 &&
947 echo "$the_commit commit refs/heads/branch1" >expect &&
948 git for-each-ref refs/heads >actual &&
949 test_cmp expect actual
950 ) &&
951 git checkout main
954 test_expect_success 'fetch with branches containing #' '
955 mk_empty testrepo &&
956 mkdir testrepo/.git/branches &&
957 echo "..#second" > testrepo/.git/branches/branch2 &&
959 cd testrepo &&
960 git fetch branch2 &&
961 echo "$the_first_commit commit refs/heads/branch2" >expect &&
962 git for-each-ref refs/heads >actual &&
963 test_cmp expect actual
964 ) &&
965 git checkout main
968 test_expect_success 'push with branches' '
969 mk_empty testrepo &&
970 git checkout second &&
972 test_when_finished "rm -rf .git/branches" &&
973 mkdir .git/branches &&
974 echo "testrepo" > .git/branches/branch1 &&
976 git push branch1 &&
978 cd testrepo &&
979 echo "$the_first_commit commit refs/heads/main" >expect &&
980 git for-each-ref refs/heads >actual &&
981 test_cmp expect actual
985 test_expect_success 'push with branches containing #' '
986 mk_empty testrepo &&
988 test_when_finished "rm -rf .git/branches" &&
989 mkdir .git/branches &&
990 echo "testrepo#branch3" > .git/branches/branch2 &&
992 git push branch2 &&
994 cd testrepo &&
995 echo "$the_first_commit commit refs/heads/branch3" >expect &&
996 git for-each-ref refs/heads >actual &&
997 test_cmp expect actual
998 ) &&
999 git checkout main
1002 test_expect_success 'push into aliased refs (consistent)' '
1003 mk_test testrepo heads/main &&
1004 mk_child testrepo child1 &&
1005 mk_child testrepo child2 &&
1007 cd child1 &&
1008 git branch foo &&
1009 git symbolic-ref refs/heads/bar refs/heads/foo &&
1010 git config receive.denyCurrentBranch false
1011 ) &&
1013 cd child2 &&
1014 >path2 &&
1015 git add path2 &&
1016 test_tick &&
1017 git commit -a -m child2 &&
1018 git branch foo &&
1019 git branch bar &&
1020 git push ../child1 foo bar
1024 test_expect_success 'push into aliased refs (inconsistent)' '
1025 mk_test testrepo heads/main &&
1026 mk_child testrepo child1 &&
1027 mk_child testrepo child2 &&
1029 cd child1 &&
1030 git branch foo &&
1031 git symbolic-ref refs/heads/bar refs/heads/foo &&
1032 git config receive.denyCurrentBranch false
1033 ) &&
1035 cd child2 &&
1036 >path2 &&
1037 git add path2 &&
1038 test_tick &&
1039 git commit -a -m child2 &&
1040 git branch foo &&
1041 >path3 &&
1042 git add path3 &&
1043 test_tick &&
1044 git commit -a -m child2 &&
1045 git branch bar &&
1046 test_must_fail git push ../child1 foo bar 2>stderr &&
1047 grep "refusing inconsistent update" stderr
1051 test_force_push_tag () {
1052 tag_type_description=$1
1053 tag_args=$2
1055 test_expect_success "force pushing required to update $tag_type_description" "
1056 mk_test testrepo heads/main &&
1057 mk_child testrepo child1 &&
1058 mk_child testrepo child2 &&
1060 cd child1 &&
1061 git tag testTag &&
1062 git push ../child2 testTag &&
1063 >file1 &&
1064 git add file1 &&
1065 git commit -m 'file1' &&
1066 git tag $tag_args testTag &&
1067 test_must_fail git push ../child2 testTag &&
1068 git push --force ../child2 testTag &&
1069 git tag $tag_args testTag HEAD~ &&
1070 test_must_fail git push ../child2 testTag &&
1071 git push --force ../child2 testTag &&
1073 # Clobbering without + in refspec needs --force
1074 git tag -f testTag &&
1075 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1076 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1078 # Clobbering with + in refspec does not need --force
1079 git tag -f testTag HEAD~ &&
1080 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1082 # Clobbering with --no-force still obeys + in refspec
1083 git tag -f testTag &&
1084 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1086 # Clobbering with/without --force and 'tag <name>' format
1087 git tag -f testTag HEAD~ &&
1088 test_must_fail git push ../child2 tag testTag &&
1089 git push --force ../child2 tag testTag
1094 test_force_push_tag "lightweight tag" "-f"
1095 test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1097 test_force_fetch_tag () {
1098 tag_type_description=$1
1099 tag_args=$2
1101 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1102 mk_test testrepo heads/main &&
1103 mk_child testrepo child1 &&
1104 mk_child testrepo child2 &&
1106 cd testrepo &&
1107 git tag testTag &&
1108 git -C ../child1 fetch origin tag testTag &&
1109 >file1 &&
1110 git add file1 &&
1111 git commit -m 'file1' &&
1112 git tag $tag_args testTag &&
1113 test_must_fail git -C ../child1 fetch origin tag testTag &&
1114 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1119 test_force_fetch_tag "lightweight tag" "-f"
1120 test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
1122 test_expect_success 'push --porcelain' '
1123 mk_empty testrepo &&
1124 echo >.git/foo "To testrepo" &&
1125 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
1126 echo >>.git/foo "Done" &&
1127 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
1129 cd testrepo &&
1130 echo "$the_commit commit refs/remotes/origin/main" >expect &&
1131 git for-each-ref refs/remotes/origin >actual &&
1132 test_cmp expect actual
1133 ) &&
1134 test_cmp .git/foo .git/bar
1137 test_expect_success 'push --porcelain bad url' '
1138 mk_empty testrepo &&
1139 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
1140 ! grep -q Done .git/bar
1143 test_expect_success 'push --porcelain rejected' '
1144 mk_empty testrepo &&
1145 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1146 (cd testrepo &&
1147 git reset --hard origin/main^ &&
1148 git config receive.denyCurrentBranch true) &&
1150 echo >.git/foo "To testrepo" &&
1151 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
1152 echo >>.git/foo "Done" &&
1154 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
1155 test_cmp .git/foo .git/bar
1158 test_expect_success 'push --porcelain --dry-run rejected' '
1159 mk_empty testrepo &&
1160 git push testrepo refs/heads/main:refs/remotes/origin/main &&
1161 (cd testrepo &&
1162 git reset --hard origin/main &&
1163 git config receive.denyCurrentBranch true) &&
1165 echo >.git/foo "To testrepo" &&
1166 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
1167 echo >>.git/foo "Done" &&
1169 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
1170 test_cmp .git/foo .git/bar
1173 test_expect_success 'push --prune' '
1174 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
1175 git push --prune testrepo : &&
1176 check_push_result testrepo $the_commit heads/main &&
1177 check_push_result testrepo $the_first_commit heads/second &&
1178 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1181 test_expect_success 'push --prune refspec' '
1182 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
1183 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1184 check_push_result testrepo $the_commit tmp/main &&
1185 check_push_result testrepo $the_first_commit tmp/second &&
1186 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1189 for configsection in transfer receive
1191 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1192 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
1194 cd testrepo &&
1195 git config $configsection.hiderefs refs/hidden
1196 ) &&
1198 # push to unhidden ref succeeds normally
1199 git push testrepo main:refs/heads/main &&
1200 check_push_result testrepo $the_commit heads/main &&
1202 # push to update a hidden ref should fail
1203 test_must_fail git push testrepo main:refs/hidden/one &&
1204 check_push_result testrepo $the_first_commit hidden/one &&
1206 # push to delete a hidden ref should fail
1207 test_must_fail git push testrepo :refs/hidden/two &&
1208 check_push_result testrepo $the_first_commit hidden/two &&
1210 # idempotent push to update a hidden ref should fail
1211 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1212 check_push_result testrepo $the_first_commit hidden/three
1214 done
1216 test_expect_success 'fetch exact SHA1' '
1217 mk_test testrepo heads/main hidden/one &&
1218 git push testrepo main:refs/hidden/one &&
1220 cd testrepo &&
1221 git config transfer.hiderefs refs/hidden
1222 ) &&
1223 check_push_result testrepo $the_commit hidden/one &&
1225 mk_child testrepo child &&
1227 cd child &&
1229 # make sure $the_commit does not exist here
1230 git repack -a -d &&
1231 git prune &&
1232 test_must_fail git cat-file -t $the_commit &&
1234 # Some protocol versions (e.g. 2) support fetching
1235 # unadvertised objects, so restrict this test to v0.
1237 # fetching the hidden object should fail by default
1238 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1239 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
1240 test_i18ngrep "Server does not allow request for unadvertised object" err &&
1241 test_must_fail git rev-parse --verify refs/heads/copy &&
1243 # the server side can allow it to succeed
1245 cd ../testrepo &&
1246 git config uploadpack.allowtipsha1inwant true
1247 ) &&
1249 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
1250 cat >expect <<-EOF &&
1251 $the_commit
1252 $the_first_commit
1255 git rev-parse --verify refs/heads/copy &&
1256 git rev-parse --verify refs/heads/extra
1257 } >actual &&
1258 test_cmp expect actual
1262 test_expect_success 'fetch exact SHA1 in protocol v2' '
1263 mk_test testrepo heads/main hidden/one &&
1264 git push testrepo main:refs/hidden/one &&
1265 git -C testrepo config transfer.hiderefs refs/hidden &&
1266 check_push_result testrepo $the_commit hidden/one &&
1268 mk_child testrepo child &&
1269 git -C child config protocol.version 2 &&
1271 # make sure $the_commit does not exist here
1272 git -C child repack -a -d &&
1273 git -C child prune &&
1274 test_must_fail git -C child cat-file -t $the_commit &&
1276 # fetching the hidden object succeeds by default
1277 # NEEDSWORK: should this match the v0 behavior instead?
1278 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1281 for configallowtipsha1inwant in true false
1283 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1284 mk_empty testrepo &&
1286 cd testrepo &&
1287 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1288 git commit --allow-empty -m foo &&
1289 git commit --allow-empty -m bar
1290 ) &&
1291 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1292 mk_empty shallow &&
1294 cd shallow &&
1295 # Some protocol versions (e.g. 2) support fetching
1296 # unadvertised objects, so restrict this test to v0.
1297 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1298 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1299 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1300 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1301 git cat-file commit $SHA1
1305 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1306 mk_empty testrepo &&
1308 cd testrepo &&
1309 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1310 git commit --allow-empty -m foo &&
1311 git commit --allow-empty -m bar &&
1312 git commit --allow-empty -m xyz
1313 ) &&
1314 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1315 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1316 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1318 cd testrepo &&
1319 git reset --hard $SHA1_2 &&
1320 git cat-file commit $SHA1_1 &&
1321 git cat-file commit $SHA1_3
1322 ) &&
1323 mk_empty shallow &&
1325 cd shallow &&
1326 # Some protocol versions (e.g. 2) support fetching
1327 # unadvertised objects, so restrict this test to v0.
1328 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1329 git fetch ../testrepo/.git $SHA1_3 &&
1330 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1331 git fetch ../testrepo/.git $SHA1_1 &&
1332 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1333 git fetch ../testrepo/.git $SHA1_1 &&
1334 git cat-file commit $SHA1_1 &&
1335 test_must_fail git cat-file commit $SHA1_2 &&
1336 git fetch ../testrepo/.git $SHA1_2 &&
1337 git cat-file commit $SHA1_2 &&
1338 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1339 git fetch ../testrepo/.git $SHA1_3 2>err &&
1340 # ideally we would insist this be on a "remote error:"
1341 # line, but it is racy; see the commit message
1342 test_i18ngrep "not our ref.*$SHA1_3\$" err
1345 done
1347 test_expect_success 'fetch follows tags by default' '
1348 mk_test testrepo heads/main &&
1349 test_when_finished "rm -rf src" &&
1350 git init src &&
1352 cd src &&
1353 git pull ../testrepo main &&
1354 git tag -m "annotated" tag &&
1355 git for-each-ref >tmp1 &&
1356 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
1357 sort -k 3 >../expect
1358 ) &&
1359 test_when_finished "rm -rf dst" &&
1360 git init dst &&
1362 cd dst &&
1363 git remote add origin ../src &&
1364 git config branch.main.remote origin &&
1365 git config branch.main.merge refs/heads/main &&
1366 git pull &&
1367 git for-each-ref >../actual
1368 ) &&
1369 test_cmp expect actual
1372 test_expect_success 'peeled advertisements are not considered ref tips' '
1373 mk_empty testrepo &&
1374 git -C testrepo commit --allow-empty -m one &&
1375 git -C testrepo commit --allow-empty -m two &&
1376 git -C testrepo tag -m foo mytag HEAD^ &&
1377 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1378 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1379 git fetch testrepo $oid 2>err &&
1380 test_i18ngrep "Server does not allow request for unadvertised object" err
1383 test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1384 mk_test testrepo heads/main &&
1385 test_when_finished "rm -rf src" &&
1386 git init src &&
1387 test_when_finished "rm -rf dst" &&
1388 git init --bare dst &&
1390 cd src &&
1391 git pull ../testrepo main &&
1392 git branch next &&
1393 git config remote.dst.url ../dst &&
1394 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1395 git push dst main &&
1396 git show-ref refs/heads/main |
1397 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1398 ) &&
1400 cd dst &&
1401 test_must_fail git show-ref refs/heads/next &&
1402 test_must_fail git show-ref refs/heads/main &&
1403 git show-ref refs/remotes/src/main >actual
1404 ) &&
1405 test_cmp dst/expect dst/actual
1408 test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1409 mk_test testrepo heads/main &&
1410 test_when_finished "rm -rf src" &&
1411 git init src &&
1412 test_when_finished "rm -rf dst" &&
1413 git init --bare dst &&
1415 cd src &&
1416 git pull ../testrepo main &&
1417 git branch next &&
1418 git config remote.dst.url ../dst &&
1419 git config push.default matching &&
1420 git push dst main &&
1421 git show-ref refs/heads/main >../dst/expect
1422 ) &&
1424 cd dst &&
1425 test_must_fail git show-ref refs/heads/next &&
1426 git show-ref refs/heads/main >actual
1427 ) &&
1428 test_cmp dst/expect dst/actual
1431 test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1432 mk_test testrepo heads/main &&
1433 test_when_finished "rm -rf src" &&
1434 git init src &&
1435 test_when_finished "rm -rf dst" &&
1436 git init --bare dst &&
1438 cd src &&
1439 git pull ../testrepo main &&
1440 git branch next &&
1441 git config remote.dst.url ../dst &&
1442 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1443 git config push.default upstream &&
1445 git config branch.main.merge refs/heads/trunk &&
1446 git config branch.main.remote dst &&
1448 git push dst main &&
1449 git show-ref refs/heads/main |
1450 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
1451 ) &&
1453 cd dst &&
1454 test_must_fail git show-ref refs/heads/main &&
1455 test_must_fail git show-ref refs/heads/next &&
1456 git show-ref refs/heads/trunk >actual
1457 ) &&
1458 test_cmp dst/expect dst/actual
1461 test_expect_success 'push does not follow tags by default' '
1462 mk_test testrepo heads/main &&
1463 test_when_finished "rm -rf src" &&
1464 git init src &&
1465 test_when_finished "rm -rf dst" &&
1466 git init --bare dst &&
1468 cd src &&
1469 git pull ../testrepo main &&
1470 git tag -m "annotated" tag &&
1471 git checkout -b another &&
1472 git commit --allow-empty -m "future commit" &&
1473 git tag -m "future" future &&
1474 git checkout main &&
1475 git for-each-ref refs/heads/main >../expect &&
1476 git push ../dst main
1477 ) &&
1479 cd dst &&
1480 git for-each-ref >../actual
1481 ) &&
1482 test_cmp expect actual
1485 test_expect_success 'push --follow-tags only pushes relevant tags' '
1486 mk_test testrepo heads/main &&
1487 test_when_finished "rm -rf src" &&
1488 git init src &&
1489 test_when_finished "rm -rf dst" &&
1490 git init --bare dst &&
1492 cd src &&
1493 git pull ../testrepo main &&
1494 git tag -m "annotated" tag &&
1495 git checkout -b another &&
1496 git commit --allow-empty -m "future commit" &&
1497 git tag -m "future" future &&
1498 git checkout main &&
1499 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1500 git push --follow-tags ../dst main
1501 ) &&
1503 cd dst &&
1504 git for-each-ref >../actual
1505 ) &&
1506 test_cmp expect actual
1509 test_expect_success 'push --no-thin must produce non-thin pack' '
1510 cat >>path1 <<\EOF &&
1511 keep base version of path1 big enough, compared to the new changes
1512 later, in order to pass size heuristics in
1513 builtin/pack-objects.c:try_delta()
1515 git commit -am initial &&
1516 git init no-thin &&
1517 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1518 git push no-thin/.git refs/heads/main:refs/heads/foo &&
1519 echo modified >> path1 &&
1520 git commit -am modified &&
1521 git repack -adf &&
1522 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1523 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
1526 test_expect_success 'pushing a tag pushes the tagged object' '
1527 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1528 git tag -m foo tag-of-blob $blob &&
1529 test_when_finished "rm -rf dst.git" &&
1530 git init --bare dst.git &&
1531 git push dst.git tag-of-blob &&
1532 # the receiving index-pack should have noticed
1533 # any problems, but we double check
1534 echo unreferenced >expect &&
1535 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1536 test_cmp expect actual
1539 test_expect_success 'push into bare respects core.logallrefupdates' '
1540 test_when_finished "rm -rf dst.git" &&
1541 git init --bare dst.git &&
1542 git -C dst.git config core.logallrefupdates true &&
1544 # double push to test both with and without
1545 # the actual pack transfer
1546 git push dst.git main:one &&
1547 echo "one@{0} push" >expect &&
1548 git -C dst.git log -g --format="%gd %gs" one >actual &&
1549 test_cmp expect actual &&
1551 git push dst.git main:two &&
1552 echo "two@{0} push" >expect &&
1553 git -C dst.git log -g --format="%gd %gs" two >actual &&
1554 test_cmp expect actual
1557 test_expect_success 'fetch into bare respects core.logallrefupdates' '
1558 test_when_finished "rm -rf dst.git" &&
1559 git init --bare dst.git &&
1561 cd dst.git &&
1562 git config core.logallrefupdates true &&
1564 # as above, we double-fetch to test both
1565 # with and without pack transfer
1566 git fetch .. main:one &&
1567 echo "one@{0} fetch .. main:one: storing head" >expect &&
1568 git log -g --format="%gd %gs" one >actual &&
1569 test_cmp expect actual &&
1571 git fetch .. main:two &&
1572 echo "two@{0} fetch .. main:two: storing head" >expect &&
1573 git log -g --format="%gd %gs" two >actual &&
1574 test_cmp expect actual
1578 test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1579 mk_empty testrepo &&
1580 git push testrepo main &&
1582 cd testrepo &&
1583 git reset --hard &&
1584 git config receive.denyCurrentBranch updateInstead
1585 ) &&
1586 test_commit third path2 &&
1588 # Try pushing into a repository with pristine working tree
1589 git push testrepo main &&
1591 cd testrepo &&
1592 git update-index -q --refresh &&
1593 git diff-files --quiet -- &&
1594 git diff-index --quiet --cached HEAD -- &&
1595 test third = "$(cat path2)" &&
1596 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1597 ) &&
1599 # Try pushing into a repository with working tree needing a refresh
1601 cd testrepo &&
1602 git reset --hard HEAD^ &&
1603 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1604 test-tool chmtime +100 path1
1605 ) &&
1606 git push testrepo main &&
1608 cd testrepo &&
1609 git update-index -q --refresh &&
1610 git diff-files --quiet -- &&
1611 git diff-index --quiet --cached HEAD -- &&
1612 test_cmp ../path1 path1 &&
1613 test third = "$(cat path2)" &&
1614 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1615 ) &&
1617 # Update what is to be pushed
1618 test_commit fourth path2 &&
1620 # Try pushing into a repository with a dirty working tree
1621 # (1) the working tree updated
1623 cd testrepo &&
1624 echo changed >path1
1625 ) &&
1626 test_must_fail git push testrepo main &&
1628 cd testrepo &&
1629 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1630 git diff --quiet --cached &&
1631 test changed = "$(cat path1)"
1632 ) &&
1634 # (2) the index updated
1636 cd testrepo &&
1637 echo changed >path1 &&
1638 git add path1
1639 ) &&
1640 test_must_fail git push testrepo main &&
1642 cd testrepo &&
1643 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1644 git diff --quiet &&
1645 test changed = "$(cat path1)"
1646 ) &&
1648 # Introduce a new file in the update
1649 test_commit fifth path3 &&
1651 # (3) the working tree has an untracked file that would interfere
1653 cd testrepo &&
1654 git reset --hard &&
1655 echo changed >path3
1656 ) &&
1657 test_must_fail git push testrepo main &&
1659 cd testrepo &&
1660 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1661 git diff --quiet &&
1662 git diff --quiet --cached &&
1663 test changed = "$(cat path3)"
1664 ) &&
1666 # (4) the target changes to what gets pushed but it still is a change
1668 cd testrepo &&
1669 git reset --hard &&
1670 echo fifth >path3 &&
1671 git add path3
1672 ) &&
1673 test_must_fail git push testrepo main &&
1675 cd testrepo &&
1676 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1677 git diff --quiet &&
1678 test fifth = "$(cat path3)"
1679 ) &&
1681 # (5) push into void
1682 test_when_finished "rm -rf void" &&
1683 git init void &&
1685 cd void &&
1686 git config receive.denyCurrentBranch updateInstead
1687 ) &&
1688 git push void main &&
1690 cd void &&
1691 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1692 git diff --quiet &&
1693 git diff --cached --quiet
1694 ) &&
1696 # (6) updateInstead intervened by fast-forward check
1697 test_must_fail git push void main^:main &&
1698 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
1699 git -C void diff --quiet &&
1700 git -C void diff --cached --quiet
1703 test_expect_success 'updateInstead with push-to-checkout hook' '
1704 test_when_finished "rm -rf testrepo" &&
1705 git init testrepo &&
1706 git -C testrepo pull .. main &&
1707 git -C testrepo reset --hard HEAD^^ &&
1708 git -C testrepo tag initial &&
1709 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1710 test_hook -C testrepo push-to-checkout <<-\EOF &&
1711 echo >&2 updating from $(git rev-parse HEAD)
1712 echo >&2 updating to "$1"
1714 git update-index -q --refresh &&
1715 git read-tree -u -m HEAD "$1" || {
1716 status=$?
1717 echo >&2 read-tree failed
1718 exit $status
1722 # Try pushing into a pristine
1723 git push testrepo main &&
1725 cd testrepo &&
1726 git diff --quiet &&
1727 git diff HEAD --quiet &&
1728 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1729 ) &&
1731 # Try pushing into a repository with conflicting change
1733 cd testrepo &&
1734 git reset --hard initial &&
1735 echo conflicting >path2
1736 ) &&
1737 test_must_fail git push testrepo main &&
1739 cd testrepo &&
1740 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1741 test conflicting = "$(cat path2)" &&
1742 git diff-index --quiet --cached HEAD
1743 ) &&
1745 # Try pushing into a repository with unrelated change
1747 cd testrepo &&
1748 git reset --hard initial &&
1749 echo unrelated >path1 &&
1750 echo irrelevant >path5 &&
1751 git add path5
1752 ) &&
1753 git push testrepo main &&
1755 cd testrepo &&
1756 test "$(cat path1)" = unrelated &&
1757 test "$(cat path5)" = irrelevant &&
1758 test "$(git diff --name-only --cached HEAD)" = path5 &&
1759 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1760 ) &&
1762 # push into void
1763 test_when_finished "rm -rf void" &&
1764 git init void &&
1765 git -C void config receive.denyCurrentBranch updateInstead &&
1766 test_hook -C void push-to-checkout <<-\EOF &&
1767 if git rev-parse --quiet --verify HEAD
1768 then
1769 has_head=yes
1770 echo >&2 updating from $(git rev-parse HEAD)
1771 else
1772 has_head=no
1773 echo >&2 pushing into void
1775 echo >&2 updating to "$1"
1777 git update-index -q --refresh &&
1778 case "$has_head" in
1779 yes)
1780 git read-tree -u -m HEAD "$1" ;;
1782 git read-tree -u -m "$1" ;;
1783 esac || {
1784 status=$?
1785 echo >&2 read-tree failed
1786 exit $status
1790 git push void main &&
1792 cd void &&
1793 git diff --quiet &&
1794 git diff --cached --quiet &&
1795 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1799 test_expect_success 'denyCurrentBranch and worktrees' '
1800 git worktree add new-wt &&
1801 git clone . cloned &&
1802 test_commit -C cloned first &&
1803 test_config receive.denyCurrentBranch refuse &&
1804 test_must_fail git -C cloned push origin HEAD:new-wt &&
1805 test_config receive.denyCurrentBranch updateInstead &&
1806 git -C cloned push origin HEAD:new-wt &&
1807 test_path_exists new-wt/first.t &&
1808 test_must_fail git -C cloned push --delete origin new-wt
1811 test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1812 test_when_finished "rm -fr bare.git" &&
1813 git clone --bare . bare.git &&
1814 git -C bare.git worktree add wt &&
1815 test_commit grape &&
1816 git -C bare.git config receive.denyCurrentBranch refuse &&
1817 test_must_fail git push bare.git HEAD:wt &&
1818 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1819 git push bare.git HEAD:wt &&
1820 test_path_exists bare.git/wt/grape.t &&
1821 test_must_fail git push --delete bare.git wt
1824 test_expect_success 'refuse fetch to current branch of worktree' '
1825 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1826 git worktree add wt &&
1827 test_commit apple &&
1828 test_must_fail git fetch . HEAD:wt &&
1829 git fetch -u . HEAD:wt
1832 test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1833 test_when_finished "rm -fr bare.git" &&
1834 git clone --bare . bare.git &&
1835 git -C bare.git worktree add wt &&
1836 test_commit banana &&
1837 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1838 git -C bare.git fetch -u .. HEAD:wt
1841 test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1842 mk_empty testrepo &&
1843 git -C testrepo config receive.hiderefs refs/hidden &&
1844 git -C testrepo config receive.unpackLimit 1 &&
1845 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1846 test_dir_is_empty testrepo/.git/objects/pack
1849 test_expect_success LIBCURL 'fetch warns or fails when using username:password' '
1850 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1851 test_must_fail git -c transfer.credentialsInUrl=allow fetch https://username:password@localhost 2>err &&
1852 ! grep "$message" err &&
1854 test_must_fail git -c transfer.credentialsInUrl=warn fetch https://username:password@localhost 2>err &&
1855 grep "warning: $message" err >warnings &&
1856 test_line_count = 3 warnings &&
1858 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:password@localhost 2>err &&
1859 grep "fatal: $message" err >warnings &&
1860 test_line_count = 1 warnings &&
1862 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:@localhost 2>err &&
1863 grep "fatal: $message" err >warnings &&
1864 test_line_count = 1 warnings
1868 test_expect_success LIBCURL 'push warns or fails when using username:password' '
1869 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
1870 test_must_fail git -c transfer.credentialsInUrl=allow push https://username:password@localhost 2>err &&
1871 ! grep "$message" err &&
1873 test_must_fail git -c transfer.credentialsInUrl=warn push https://username:password@localhost 2>err &&
1874 grep "warning: $message" err >warnings &&
1875 test_must_fail git -c transfer.credentialsInUrl=die push https://username:password@localhost 2>err &&
1876 grep "fatal: $message" err >warnings &&
1877 test_line_count = 1 warnings
1880 test_expect_success 'push with config push.useBitmaps' '
1881 mk_test testrepo heads/main &&
1882 git checkout main &&
1883 test_unconfig push.useBitmaps &&
1884 GIT_TRACE2_EVENT="$PWD/default" \
1885 git push testrepo main:test &&
1886 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1887 --thin --delta-base-offset -q <default &&
1889 test_config push.useBitmaps true &&
1890 GIT_TRACE2_EVENT="$PWD/true" \
1891 git push testrepo main:test2 &&
1892 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1893 --thin --delta-base-offset -q <true &&
1895 test_config push.useBitmaps false &&
1896 GIT_TRACE2_EVENT="$PWD/false" \
1897 git push testrepo main:test3 &&
1898 test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1899 --thin --delta-base-offset -q --no-use-bitmap-index <false
1902 test_done