Sync with 'master'
[alt-git.git] / t / t5505-remote.sh
blob532035933f3326798741189300a1a6ef3b481c3f
1 #!/bin/sh
3 test_description='git remote porcelain-ish'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 setup_repository () {
9 mkdir "$1" && (
10 cd "$1" &&
11 git init -b main &&
12 >file &&
13 git add file &&
14 test_tick &&
15 git commit -m "Initial" &&
16 git checkout -b side &&
17 >elif &&
18 git add elif &&
19 test_tick &&
20 git commit -m "Second" &&
21 git checkout main
25 tokens_match () {
26 echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
27 echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
28 test_cmp expect actual
31 check_remote_track () {
32 actual=$(git remote show "$1" | sed -ne 's|^ \(.*\) tracked$|\1|p')
33 shift &&
34 tokens_match "$*" "$actual"
37 check_tracking_branch () {
38 f="" &&
39 r=$(git for-each-ref "--format=%(refname)" |
40 sed -ne "s|^refs/remotes/$1/||p") &&
41 shift &&
42 tokens_match "$*" "$r"
45 test_expect_success setup '
46 setup_repository one &&
47 setup_repository two &&
49 cd two &&
50 git branch another
51 ) &&
52 git clone one test
55 test_expect_success 'add remote whose URL agrees with url.<...>.insteadOf' '
56 test_config url.git@host.com:team/repo.git.insteadOf myremote &&
57 git remote add myremote git@host.com:team/repo.git
60 test_expect_success 'remote information for the origin' '
62 cd test &&
63 tokens_match origin "$(git remote)" &&
64 check_remote_track origin main side &&
65 check_tracking_branch origin HEAD main side
69 test_expect_success 'add another remote' '
71 cd test &&
72 git remote add -f second ../two &&
73 tokens_match "origin second" "$(git remote)" &&
74 check_tracking_branch second main side another &&
75 git for-each-ref "--format=%(refname)" refs/remotes |
76 sed -e "/^refs\/remotes\/origin\//d" \
77 -e "/^refs\/remotes\/second\//d" >actual &&
78 test_must_be_empty actual
82 test_expect_success 'setup bare clone for server' '
83 git clone --bare "file://$(pwd)/one" srv.bare &&
84 git -C srv.bare config --local uploadpack.allowfilter 1 &&
85 git -C srv.bare config --local uploadpack.allowanysha1inwant 1
88 test_expect_success 'filters for promisor remotes are listed by git remote -v' '
89 test_when_finished "rm -rf pc" &&
90 git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
91 git -C pc remote -v >out &&
92 grep "srv.bare (fetch) \[blob:none\]" out &&
94 git -C pc config remote.origin.partialCloneFilter object:type=commit &&
95 git -C pc remote -v >out &&
96 grep "srv.bare (fetch) \[object:type=commit\]" out
99 test_expect_success 'filters should not be listed for non promisor remotes (remote -v)' '
100 test_when_finished "rm -rf pc" &&
101 git clone one pc &&
102 git -C pc remote -v >out &&
103 ! grep "(fetch) \[.*\]" out
106 test_expect_success 'filters are listed by git remote -v only' '
107 test_when_finished "rm -rf pc" &&
108 git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
109 git -C pc remote >out &&
110 ! grep "\[blob:none\]" out &&
112 git -C pc remote show >out &&
113 ! grep "\[blob:none\]" out
116 test_expect_success 'check remote-tracking' '
118 cd test &&
119 check_remote_track origin main side &&
120 check_remote_track second main side another
124 test_expect_success 'remote forces tracking branches' '
126 cd test &&
127 case $(git config remote.second.fetch) in
128 +*) true ;;
129 *) false ;;
130 esac
134 test_expect_success 'remove remote' '
136 cd test &&
137 git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/main &&
138 git remote rm second
142 test_expect_success 'remove remote' '
144 cd test &&
145 tokens_match origin "$(git remote)" &&
146 check_remote_track origin main side &&
147 git for-each-ref "--format=%(refname)" refs/remotes |
148 sed -e "/^refs\/remotes\/origin\//d" >actual &&
149 test_must_be_empty actual
153 test_expect_success 'remove remote protects local branches' '
155 cd test &&
156 cat >expect1 <<-\EOF &&
157 Note: A branch outside the refs/remotes/ hierarchy was not removed;
158 to delete it, use:
159 git branch -d main
161 cat >expect2 <<-\EOF &&
162 Note: Some branches outside the refs/remotes/ hierarchy were not removed;
163 to delete them, use:
164 git branch -d foobranch
165 git branch -d main
167 git tag footag &&
168 git config --add remote.oops.fetch "+refs/*:refs/*" &&
169 git remote remove oops 2>actual1 &&
170 git branch foobranch &&
171 git config --add remote.oops.fetch "+refs/*:refs/*" &&
172 git remote rm oops 2>actual2 &&
173 git branch -d foobranch &&
174 git tag -d footag &&
175 test_cmp expect1 actual1 &&
176 test_cmp expect2 actual2
180 test_expect_success 'remove errors out early when deleting non-existent branch' '
182 cd test &&
183 echo "error: No such remote: '\''foo'\''" >expect &&
184 test_expect_code 2 git remote rm foo 2>actual &&
185 test_cmp expect actual
189 test_expect_success 'remove remote with a branch without configured merge' '
190 test_when_finished "(
191 git -C test checkout main;
192 git -C test branch -D two;
193 git -C test config --remove-section remote.two;
194 git -C test config --remove-section branch.second;
195 true
196 )" &&
198 cd test &&
199 git remote add two ../two &&
200 git fetch two &&
201 git checkout -b second two/main^0 &&
202 git config branch.second.remote two &&
203 git checkout main &&
204 git remote rm two
208 test_expect_success 'rename errors out early when deleting non-existent branch' '
210 cd test &&
211 echo "error: No such remote: '\''foo'\''" >expect &&
212 test_expect_code 2 git remote rename foo bar 2>actual &&
213 test_cmp expect actual
217 test_expect_success 'rename errors out early when new name is invalid' '
218 test_config remote.foo.vcs bar &&
219 echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
220 test_must_fail git remote rename foo invalid...name 2>actual &&
221 test_cmp expect actual
224 test_expect_success 'add existing foreign_vcs remote' '
225 test_config remote.foo.vcs bar &&
226 echo "error: remote foo already exists." >expect &&
227 test_expect_code 3 git remote add foo bar 2>actual &&
228 test_cmp expect actual
231 test_expect_success 'add existing foreign_vcs remote' '
232 test_config remote.foo.vcs bar &&
233 test_config remote.bar.vcs bar &&
234 echo "error: remote bar already exists." >expect &&
235 test_expect_code 3 git remote rename foo bar 2>actual &&
236 test_cmp expect actual
239 test_expect_success 'add invalid foreign_vcs remote' '
240 echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
241 test_must_fail git remote add invalid...name bar 2>actual &&
242 test_cmp expect actual
245 test_expect_success 'without subcommand' '
246 echo origin >expect &&
247 git -C test remote >actual &&
248 test_cmp expect actual
251 test_expect_success 'without subcommand accepts -v' '
252 cat >expect <<-EOF &&
253 origin $(pwd)/one (fetch)
254 origin $(pwd)/one (push)
256 git -C test remote -v >actual &&
257 test_cmp expect actual
260 test_expect_success 'without subcommand does not take arguments' '
261 test_expect_code 129 git -C test remote origin 2>err &&
262 grep "^error: unknown subcommand:" err
265 cat >test/expect <<EOF
266 * remote origin
267 Fetch URL: $(pwd)/one
268 Push URL: $(pwd)/one
269 HEAD branch: main
270 Remote branches:
271 main new (next fetch will store in remotes/origin)
272 side tracked
273 Local branches configured for 'git pull':
274 ahead merges with remote main
275 main merges with remote main
276 octopus merges with remote topic-a
277 and with remote topic-b
278 and with remote topic-c
279 rebase rebases onto remote main
280 Local refs configured for 'git push':
281 main pushes to main (local out of date)
282 main pushes to upstream (create)
283 * remote two
284 Fetch URL: ../two
285 Push URL: ../three
286 HEAD branch: main
287 Local refs configured for 'git push':
288 ahead forces to main (fast-forwardable)
289 main pushes to another (up to date)
292 test_expect_success 'show' '
294 cd test &&
295 git config --add remote.origin.fetch refs/heads/main:refs/heads/upstream &&
296 git fetch &&
297 git checkout -b ahead origin/main &&
298 echo 1 >>file &&
299 test_tick &&
300 git commit -m update file &&
301 git checkout main &&
302 git branch --track octopus origin/main &&
303 git branch --track rebase origin/main &&
304 git branch -d -r origin/main &&
305 git config --add remote.two.url ../two &&
306 git config --add remote.two.pushurl ../three &&
307 git config branch.rebase.rebase true &&
308 git config branch.octopus.merge "topic-a topic-b topic-c" &&
310 cd ../one &&
311 echo 1 >file &&
312 test_tick &&
313 git commit -m update file
314 ) &&
315 git config --add remote.origin.push : &&
316 git config --add remote.origin.push refs/heads/main:refs/heads/upstream &&
317 git config --add remote.origin.push +refs/tags/lastbackup &&
318 git config --add remote.two.push +refs/heads/ahead:refs/heads/main &&
319 git config --add remote.two.push refs/heads/main:refs/heads/another &&
320 git remote show origin two >output &&
321 git branch -d rebase octopus &&
322 test_cmp expect output
326 cat >expect <<EOF
327 * remote origin
328 Fetch URL: $(pwd)/one
329 Push URL: $(pwd)/one
330 HEAD branch: main
331 Remote branches:
332 main skipped
333 side tracked
334 Local branches configured for 'git pull':
335 ahead merges with remote main
336 main merges with remote main
337 Local refs configured for 'git push':
338 main pushes to main (local out of date)
339 main pushes to upstream (create)
342 test_expect_success 'show with negative refspecs' '
343 test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/main" &&
344 git -C test config --add remote.origin.fetch ^refs/heads/main &&
345 git -C test remote show origin >output &&
346 test_cmp expect output
349 cat >expect <<EOF
350 * remote origin
351 Fetch URL: $(pwd)/one
352 Push URL: $(pwd)/one
353 HEAD branch: main
354 Remote branches:
355 main new (next fetch will store in remotes/origin)
356 side stale (use 'git remote prune' to remove)
357 Local branches configured for 'git pull':
358 ahead merges with remote main
359 main merges with remote main
360 Local refs configured for 'git push':
361 main pushes to main (local out of date)
362 main pushes to upstream (create)
365 test_expect_failure 'show stale with negative refspecs' '
366 test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/side" &&
367 git -C test config --add remote.origin.fetch ^refs/heads/side &&
368 git -C test remote show origin >output &&
369 test_cmp expect output
372 cat >test/expect <<EOF
373 * remote origin
374 Fetch URL: $(pwd)/one
375 Push URL: $(pwd)/one
376 HEAD branch: (not queried)
377 Remote branches: (status not queried)
378 main
379 side
380 Local branches configured for 'git pull':
381 ahead merges with remote main
382 main merges with remote main
383 Local refs configured for 'git push' (status not queried):
384 (matching) pushes to (matching)
385 refs/heads/main pushes to refs/heads/upstream
386 refs/tags/lastbackup forces to refs/tags/lastbackup
389 test_expect_success 'show -n' '
390 mv one one.unreachable &&
392 cd test &&
393 git remote show -n origin >output &&
394 mv ../one.unreachable ../one &&
395 test_cmp expect output
399 test_expect_success 'prune' '
401 cd one &&
402 git branch -m side side2
403 ) &&
405 cd test &&
406 git fetch origin &&
407 git remote prune origin &&
408 git rev-parse refs/remotes/origin/side2 &&
409 test_must_fail git rev-parse refs/remotes/origin/side
413 test_expect_success 'set-head --delete' '
415 cd test &&
416 git symbolic-ref refs/remotes/origin/HEAD &&
417 git remote set-head --delete origin &&
418 test_must_fail git symbolic-ref refs/remotes/origin/HEAD
422 test_expect_success 'set-head --auto' '
424 cd test &&
425 git remote set-head --auto origin &&
426 echo refs/remotes/origin/main >expect &&
427 git symbolic-ref refs/remotes/origin/HEAD >output &&
428 test_cmp expect output
432 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
434 cd test &&
435 git fetch two "refs/heads/*:refs/remotes/two/*" &&
436 git remote set-head --auto two >output 2>&1 &&
437 echo "two/HEAD set to main" >expect &&
438 test_cmp expect output
442 cat >test/expect <<\EOF
443 refs/remotes/origin/side2
446 test_expect_success 'set-head explicit' '
448 cd test &&
449 git remote set-head origin side2 &&
450 git symbolic-ref refs/remotes/origin/HEAD >output &&
451 git remote set-head origin main &&
452 test_cmp expect output
456 cat >test/expect <<EOF
457 Pruning origin
458 URL: $(pwd)/one
459 * [would prune] origin/side2
462 test_expect_success 'prune --dry-run' '
463 git -C one branch -m side2 side &&
464 test_when_finished "git -C one branch -m side side2" &&
466 cd test &&
467 git remote prune --dry-run origin >output &&
468 git rev-parse refs/remotes/origin/side2 &&
469 test_must_fail git rev-parse refs/remotes/origin/side &&
470 test_cmp expect output
474 test_expect_success 'add --mirror && prune' '
475 mkdir mirror &&
477 cd mirror &&
478 git init --bare &&
479 git remote add --mirror -f origin ../one
480 ) &&
482 cd one &&
483 git branch -m side2 side
484 ) &&
486 cd mirror &&
487 git rev-parse --verify refs/heads/side2 &&
488 test_must_fail git rev-parse --verify refs/heads/side &&
489 git fetch origin &&
490 git remote prune origin &&
491 test_must_fail git rev-parse --verify refs/heads/side2 &&
492 git rev-parse --verify refs/heads/side
496 test_expect_success 'add --mirror=fetch' '
497 mkdir mirror-fetch &&
498 git init -b main mirror-fetch/parent &&
500 cd mirror-fetch/parent &&
501 test_commit one
502 ) &&
503 git init --bare mirror-fetch/child &&
505 cd mirror-fetch/child &&
506 git remote add --mirror=fetch -f parent ../parent
510 test_expect_success 'fetch mirrors act as mirrors during fetch' '
512 cd mirror-fetch/parent &&
513 git branch new &&
514 git branch -m main renamed
515 ) &&
517 cd mirror-fetch/child &&
518 git fetch parent &&
519 git rev-parse --verify refs/heads/new &&
520 git rev-parse --verify refs/heads/renamed
524 test_expect_success 'fetch mirrors can prune' '
526 cd mirror-fetch/child &&
527 git remote prune parent &&
528 test_must_fail git rev-parse --verify refs/heads/main
532 test_expect_success 'fetch mirrors do not act as mirrors during push' '
534 cd mirror-fetch/parent &&
535 git checkout HEAD^0
536 ) &&
538 cd mirror-fetch/child &&
539 git branch -m renamed renamed2 &&
540 git push parent :
541 ) &&
543 cd mirror-fetch/parent &&
544 git rev-parse --verify renamed &&
545 test_must_fail git rev-parse --verify refs/heads/renamed2
549 test_expect_success 'add fetch mirror with specific branches' '
550 git init --bare mirror-fetch/track &&
552 cd mirror-fetch/track &&
553 git remote add --mirror=fetch -t heads/new parent ../parent
557 test_expect_success 'fetch mirror respects specific branches' '
559 cd mirror-fetch/track &&
560 git fetch parent &&
561 git rev-parse --verify refs/heads/new &&
562 test_must_fail git rev-parse --verify refs/heads/renamed
566 test_expect_success 'add --mirror=push' '
567 mkdir mirror-push &&
568 git init --bare mirror-push/public &&
569 git init -b main mirror-push/private &&
571 cd mirror-push/private &&
572 test_commit one &&
573 git remote add --mirror=push public ../public
577 test_expect_success 'push mirrors act as mirrors during push' '
579 cd mirror-push/private &&
580 git branch new &&
581 git branch -m main renamed &&
582 git push public
583 ) &&
585 cd mirror-push/private &&
586 git rev-parse --verify refs/heads/new &&
587 git rev-parse --verify refs/heads/renamed &&
588 test_must_fail git rev-parse --verify refs/heads/main
592 test_expect_success 'push mirrors do not act as mirrors during fetch' '
594 cd mirror-push/public &&
595 git branch -m renamed renamed2 &&
596 git symbolic-ref HEAD refs/heads/renamed2
597 ) &&
599 cd mirror-push/private &&
600 git fetch public &&
601 git rev-parse --verify refs/heads/renamed &&
602 test_must_fail git rev-parse --verify refs/heads/renamed2
606 test_expect_success 'push mirrors do not allow you to specify refs' '
607 git init mirror-push/track &&
609 cd mirror-push/track &&
610 test_must_fail git remote add --mirror=push -t new public ../public
614 test_expect_success 'add alt && prune' '
615 mkdir alttst &&
617 cd alttst &&
618 git init &&
619 git remote add -f origin ../one &&
620 git config remote.alt.url ../one &&
621 git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*"
622 ) &&
624 cd one &&
625 git branch -m side side2
626 ) &&
628 cd alttst &&
629 git rev-parse --verify refs/remotes/origin/side &&
630 test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
631 git fetch alt &&
632 git remote prune alt &&
633 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
634 git rev-parse --verify refs/remotes/origin/side2
638 cat >test/expect <<\EOF
639 some-tag
642 test_expect_success 'add with reachable tags (default)' '
644 cd one &&
645 >foobar &&
646 git add foobar &&
647 git commit -m "Foobar" &&
648 git tag -a -m "Foobar tag" foobar-tag &&
649 git reset --hard HEAD~1 &&
650 git tag -a -m "Some tag" some-tag
651 ) &&
652 mkdir add-tags &&
654 cd add-tags &&
655 git init &&
656 git remote add -f origin ../one &&
657 git tag -l some-tag >../test/output &&
658 git tag -l foobar-tag >>../test/output &&
659 test_must_fail git config remote.origin.tagopt
660 ) &&
661 test_cmp test/expect test/output
664 cat >test/expect <<\EOF
665 some-tag
666 foobar-tag
667 --tags
670 test_expect_success 'add --tags' '
671 rm -rf add-tags &&
673 mkdir add-tags &&
674 cd add-tags &&
675 git init &&
676 git remote add -f --tags origin ../one &&
677 git tag -l some-tag >../test/output &&
678 git tag -l foobar-tag >>../test/output &&
679 git config remote.origin.tagopt >>../test/output
680 ) &&
681 test_cmp test/expect test/output
684 cat >test/expect <<\EOF
685 --no-tags
688 test_expect_success 'add --no-tags' '
689 rm -rf add-tags &&
691 mkdir add-no-tags &&
692 cd add-no-tags &&
693 git init &&
694 git remote add -f --no-tags origin ../one &&
695 grep tagOpt .git/config &&
696 git tag -l some-tag >../test/output &&
697 git tag -l foobar-tag >../test/output &&
698 git config remote.origin.tagopt >>../test/output
699 ) &&
701 cd one &&
702 git tag -d some-tag foobar-tag
703 ) &&
704 test_cmp test/expect test/output
707 test_expect_success 'reject --no-no-tags' '
709 cd add-no-tags &&
710 test_must_fail git remote add -f --no-no-tags neworigin ../one
714 cat >one/expect <<\EOF
715 apis/main
716 apis/side
717 drosophila/another
718 drosophila/main
719 drosophila/side
722 test_expect_success 'update' '
724 cd one &&
725 git remote add drosophila ../two &&
726 git remote add apis ../mirror &&
727 git remote update &&
728 git branch -r >output &&
729 test_cmp expect output
733 cat >one/expect <<\EOF
734 drosophila/another
735 drosophila/main
736 drosophila/side
737 manduca/main
738 manduca/side
739 megaloprepus/main
740 megaloprepus/side
743 test_expect_success 'update with arguments' '
745 cd one &&
746 for b in $(git branch -r)
748 git branch -r -d $b || exit 1
749 done &&
750 git remote add manduca ../mirror &&
751 git remote add megaloprepus ../mirror &&
752 git config remotes.phobaeticus "drosophila megaloprepus" &&
753 git config remotes.titanus manduca &&
754 git remote update phobaeticus titanus &&
755 git branch -r >output &&
756 test_cmp expect output
760 test_expect_success 'update --prune' '
762 cd one &&
763 git branch -m side2 side3
764 ) &&
766 cd test &&
767 git remote update --prune &&
769 cd ../one &&
770 git branch -m side3 side2
771 ) &&
772 git rev-parse refs/remotes/origin/side3 &&
773 test_must_fail git rev-parse refs/remotes/origin/side2
777 cat >one/expect <<-\EOF
778 apis/main
779 apis/side
780 manduca/main
781 manduca/side
782 megaloprepus/main
783 megaloprepus/side
786 test_expect_success 'update default' '
788 cd one &&
789 for b in $(git branch -r)
791 git branch -r -d $b || exit 1
792 done &&
793 git config remote.drosophila.skipDefaultUpdate true &&
794 git remote update default &&
795 git branch -r >output &&
796 test_cmp expect output
800 cat >one/expect <<\EOF
801 drosophila/another
802 drosophila/main
803 drosophila/side
806 test_expect_success 'update default (overridden, with funny whitespace)' '
808 cd one &&
809 for b in $(git branch -r)
811 git branch -r -d $b || exit 1
812 done &&
813 git config remotes.default "$(printf "\t drosophila \n")" &&
814 git remote update default &&
815 git branch -r >output &&
816 test_cmp expect output
820 test_expect_success 'update (with remotes.default defined)' '
822 cd one &&
823 for b in $(git branch -r)
825 git branch -r -d $b || exit 1
826 done &&
827 git config remotes.default "drosophila" &&
828 git remote update &&
829 git branch -r >output &&
830 test_cmp expect output
834 test_expect_success '"remote show" does not show symbolic refs' '
835 git clone one three &&
837 cd three &&
838 git remote show origin >output &&
839 ! grep "^ *HEAD$" < output &&
840 ! grep -i stale < output
844 test_expect_success 'reject adding remote with an invalid name' '
845 test_must_fail git remote add some:url desired-name
848 # The first three test if the tracking branches are properly renamed,
849 # the last two ones check if the config is updated.
851 test_expect_success 'rename a remote' '
852 test_config_global remote.pushDefault origin &&
853 git clone one four &&
855 cd four &&
856 git config branch.main.pushRemote origin &&
857 GIT_TRACE2_EVENT=$(pwd)/trace \
858 git remote rename --progress origin upstream &&
859 test_region progress "Renaming remote references" trace &&
860 grep "pushRemote" .git/config &&
861 test -z "$(git for-each-ref refs/remotes/origin)" &&
862 test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/main" &&
863 test "$(git rev-parse upstream/main)" = "$(git rev-parse main)" &&
864 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
865 test "$(git config branch.main.remote)" = "upstream" &&
866 test "$(git config branch.main.pushRemote)" = "upstream" &&
867 test "$(git config --global remote.pushDefault)" = "origin"
871 test_expect_success 'rename a remote renames repo remote.pushDefault' '
872 git clone one four.1 &&
874 cd four.1 &&
875 git config remote.pushDefault origin &&
876 git remote rename origin upstream &&
877 grep pushDefault .git/config &&
878 test "$(git config --local remote.pushDefault)" = "upstream"
882 test_expect_success 'rename a remote renames repo remote.pushDefault but ignores global' '
883 test_config_global remote.pushDefault other &&
884 git clone one four.2 &&
886 cd four.2 &&
887 git config remote.pushDefault origin &&
888 git remote rename origin upstream &&
889 test "$(git config --global remote.pushDefault)" = "other" &&
890 test "$(git config --local remote.pushDefault)" = "upstream"
894 test_expect_success 'rename a remote renames repo remote.pushDefault but keeps global' '
895 test_config_global remote.pushDefault origin &&
896 git clone one four.3 &&
898 cd four.3 &&
899 git config remote.pushDefault origin &&
900 git remote rename origin upstream &&
901 test "$(git config --global remote.pushDefault)" = "origin" &&
902 test "$(git config --local remote.pushDefault)" = "upstream"
906 test_expect_success 'rename handles remote without fetch refspec' '
907 git clone --bare one no-refspec.git &&
908 # confirm assumption that bare clone does not create refspec
909 test_expect_code 5 \
910 git -C no-refspec.git config --unset-all remote.origin.fetch &&
911 git -C no-refspec.git config remote.origin.url >expect &&
912 git -C no-refspec.git remote rename origin foo &&
913 git -C no-refspec.git config remote.foo.url >actual &&
914 test_cmp expect actual
917 test_expect_success 'rename does not update a non-default fetch refspec' '
918 git clone one four.one &&
920 cd four.one &&
921 git config remote.origin.fetch +refs/heads/*:refs/heads/origin/* &&
922 git remote rename origin upstream &&
923 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/heads/origin/*" &&
924 git rev-parse -q origin/main
928 test_expect_success 'rename a remote with name part of fetch spec' '
929 git clone one four.two &&
931 cd four.two &&
932 git remote rename origin remote &&
933 git remote rename remote upstream &&
934 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*"
938 test_expect_success 'rename a remote with name prefix of other remote' '
939 git clone one four.three &&
941 cd four.three &&
942 git remote add o git://example.com/repo.git &&
943 git remote rename o upstream &&
944 test "$(git rev-parse origin/main)" = "$(git rev-parse main)"
948 test_expect_success 'rename succeeds with existing remote.<target>.prune' '
949 git clone one four.four &&
950 test_when_finished git config --global --unset remote.upstream.prune &&
951 git config --global remote.upstream.prune true &&
952 git -C four.four remote rename origin upstream
955 test_expect_success 'remove a remote' '
956 test_config_global remote.pushDefault origin &&
957 git clone one four.five &&
959 cd four.five &&
960 git config branch.main.pushRemote origin &&
961 git remote remove origin &&
962 test -z "$(git for-each-ref refs/remotes/origin)" &&
963 test_must_fail git config branch.main.remote &&
964 test_must_fail git config branch.main.pushRemote &&
965 test "$(git config --global remote.pushDefault)" = "origin"
969 test_expect_success 'remove a remote removes repo remote.pushDefault' '
970 git clone one four.five.1 &&
972 cd four.five.1 &&
973 git config remote.pushDefault origin &&
974 git remote remove origin &&
975 test_must_fail git config --local remote.pushDefault
979 test_expect_success 'remove a remote removes repo remote.pushDefault but ignores global' '
980 test_config_global remote.pushDefault other &&
981 git clone one four.five.2 &&
983 cd four.five.2 &&
984 git config remote.pushDefault origin &&
985 git remote remove origin &&
986 test "$(git config --global remote.pushDefault)" = "other" &&
987 test_must_fail git config --local remote.pushDefault
991 test_expect_success 'remove a remote removes repo remote.pushDefault but keeps global' '
992 test_config_global remote.pushDefault origin &&
993 git clone one four.five.3 &&
995 cd four.five.3 &&
996 git config remote.pushDefault origin &&
997 git remote remove origin &&
998 test "$(git config --global remote.pushDefault)" = "origin" &&
999 test_must_fail git config --local remote.pushDefault
1003 cat >remotes_origin <<EOF
1004 URL: $(pwd)/one
1005 Push: refs/heads/main:refs/heads/upstream
1006 Push: refs/heads/next:refs/heads/upstream2
1007 Pull: refs/heads/main:refs/heads/origin
1008 Pull: refs/heads/next:refs/heads/origin2
1011 test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
1012 git clone one five &&
1013 origin_url=$(pwd)/one &&
1015 cd five &&
1016 git remote remove origin &&
1017 mkdir -p .git/remotes &&
1018 cat ../remotes_origin >.git/remotes/origin &&
1019 git remote rename origin origin &&
1020 test_path_is_missing .git/remotes/origin &&
1021 test "$(git config remote.origin.url)" = "$origin_url" &&
1022 cat >push_expected <<-\EOF &&
1023 refs/heads/main:refs/heads/upstream
1024 refs/heads/next:refs/heads/upstream2
1026 cat >fetch_expected <<-\EOF &&
1027 refs/heads/main:refs/heads/origin
1028 refs/heads/next:refs/heads/origin2
1030 git config --get-all remote.origin.push >push_actual &&
1031 git config --get-all remote.origin.fetch >fetch_actual &&
1032 test_cmp push_expected push_actual &&
1033 test_cmp fetch_expected fetch_actual
1037 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
1038 git clone --template= one six &&
1039 origin_url=$(pwd)/one &&
1041 cd six &&
1042 git remote rm origin &&
1043 mkdir .git/branches &&
1044 echo "$origin_url#main" >.git/branches/origin &&
1045 git remote rename origin origin &&
1046 test_path_is_missing .git/branches/origin &&
1047 test "$(git config remote.origin.url)" = "$origin_url" &&
1048 test "$(git config remote.origin.fetch)" = "refs/heads/main:refs/heads/origin" &&
1049 test "$(git config remote.origin.push)" = "HEAD:refs/heads/main"
1053 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches (2)' '
1054 git clone --template= one seven &&
1056 cd seven &&
1057 git remote rm origin &&
1058 mkdir .git/branches &&
1059 echo "quux#foom" > .git/branches/origin &&
1060 git remote rename origin origin &&
1061 test_path_is_missing .git/branches/origin &&
1062 test "$(git config remote.origin.url)" = "quux" &&
1063 test "$(git config remote.origin.fetch)" = "refs/heads/foom:refs/heads/origin" &&
1064 test "$(git config remote.origin.push)" = "HEAD:refs/heads/foom"
1068 test_expect_success 'remote prune to cause a dangling symref' '
1069 git clone one eight &&
1071 cd one &&
1072 git checkout side2 &&
1073 git branch -D main
1074 ) &&
1076 cd eight &&
1077 git remote prune origin
1078 ) >err 2>&1 &&
1079 test_grep "has become dangling" err &&
1081 : And the dangling symref will not cause other annoying errors &&
1083 cd eight &&
1084 git branch -a
1085 ) 2>err &&
1086 ! grep "points nowhere" err &&
1088 cd eight &&
1089 test_must_fail git branch nomore origin
1090 ) 2>err &&
1091 test_grep "dangling symref" err
1094 test_expect_success 'show empty remote' '
1095 test_create_repo empty &&
1096 git clone empty empty-clone &&
1098 cd empty-clone &&
1099 git remote show origin
1103 test_expect_success 'remote set-branches requires a remote' '
1104 test_must_fail git remote set-branches &&
1105 test_must_fail git remote set-branches --add
1108 test_expect_success 'remote set-branches' '
1109 echo "+refs/heads/*:refs/remotes/scratch/*" >expect.initial &&
1110 sort <<-\EOF >expect.add &&
1111 +refs/heads/*:refs/remotes/scratch/*
1112 +refs/heads/other:refs/remotes/scratch/other
1114 sort <<-\EOF >expect.replace &&
1115 +refs/heads/maint:refs/remotes/scratch/maint
1116 +refs/heads/main:refs/remotes/scratch/main
1117 +refs/heads/next:refs/remotes/scratch/next
1119 sort <<-\EOF >expect.add-two &&
1120 +refs/heads/maint:refs/remotes/scratch/maint
1121 +refs/heads/main:refs/remotes/scratch/main
1122 +refs/heads/next:refs/remotes/scratch/next
1123 +refs/heads/seen:refs/remotes/scratch/seen
1124 +refs/heads/t/topic:refs/remotes/scratch/t/topic
1126 sort <<-\EOF >expect.setup-ffonly &&
1127 refs/heads/main:refs/remotes/scratch/main
1128 +refs/heads/next:refs/remotes/scratch/next
1130 sort <<-\EOF >expect.respect-ffonly &&
1131 refs/heads/main:refs/remotes/scratch/main
1132 +refs/heads/next:refs/remotes/scratch/next
1133 +refs/heads/seen:refs/remotes/scratch/seen
1136 git clone .git/ setbranches &&
1138 cd setbranches &&
1139 git remote rename origin scratch &&
1140 git config --get-all remote.scratch.fetch >config-result &&
1141 sort <config-result >../actual.initial &&
1143 git remote set-branches scratch --add other &&
1144 git config --get-all remote.scratch.fetch >config-result &&
1145 sort <config-result >../actual.add &&
1147 git remote set-branches scratch maint main next &&
1148 git config --get-all remote.scratch.fetch >config-result &&
1149 sort <config-result >../actual.replace &&
1151 git remote set-branches --add scratch seen t/topic &&
1152 git config --get-all remote.scratch.fetch >config-result &&
1153 sort <config-result >../actual.add-two &&
1155 git config --unset-all remote.scratch.fetch &&
1156 git config remote.scratch.fetch \
1157 refs/heads/main:refs/remotes/scratch/main &&
1158 git config --add remote.scratch.fetch \
1159 +refs/heads/next:refs/remotes/scratch/next &&
1160 git config --get-all remote.scratch.fetch >config-result &&
1161 sort <config-result >../actual.setup-ffonly &&
1163 git remote set-branches --add scratch seen &&
1164 git config --get-all remote.scratch.fetch >config-result &&
1165 sort <config-result >../actual.respect-ffonly
1166 ) &&
1167 test_cmp expect.initial actual.initial &&
1168 test_cmp expect.add actual.add &&
1169 test_cmp expect.replace actual.replace &&
1170 test_cmp expect.add-two actual.add-two &&
1171 test_cmp expect.setup-ffonly actual.setup-ffonly &&
1172 test_cmp expect.respect-ffonly actual.respect-ffonly
1175 test_expect_success 'remote set-branches with --mirror' '
1176 echo "+refs/*:refs/*" >expect.initial &&
1177 echo "+refs/heads/main:refs/heads/main" >expect.replace &&
1178 git clone --mirror .git/ setbranches-mirror &&
1180 cd setbranches-mirror &&
1181 git remote rename origin scratch &&
1182 git config --get-all remote.scratch.fetch >../actual.initial &&
1184 git remote set-branches scratch heads/main &&
1185 git config --get-all remote.scratch.fetch >../actual.replace
1186 ) &&
1187 test_cmp expect.initial actual.initial &&
1188 test_cmp expect.replace actual.replace
1191 test_expect_success 'new remote' '
1192 git remote add someremote foo &&
1193 echo foo >expect &&
1194 git config --get-all remote.someremote.url >actual &&
1195 cmp expect actual
1198 get_url_test () {
1199 cat >expect &&
1200 git remote get-url "$@" >actual &&
1201 test_cmp expect actual
1204 test_expect_success 'get-url on new remote' '
1205 echo foo | get_url_test someremote &&
1206 echo foo | get_url_test --all someremote &&
1207 echo foo | get_url_test --push someremote &&
1208 echo foo | get_url_test --push --all someremote
1211 test_expect_success 'remote set-url with locked config' '
1212 test_when_finished "rm -f .git/config.lock" &&
1213 git config --get-all remote.someremote.url >expect &&
1214 >.git/config.lock &&
1215 test_must_fail git remote set-url someremote baz &&
1216 git config --get-all remote.someremote.url >actual &&
1217 cmp expect actual
1220 test_expect_success 'remote set-url bar' '
1221 git remote set-url someremote bar &&
1222 echo bar >expect &&
1223 git config --get-all remote.someremote.url >actual &&
1224 cmp expect actual
1227 test_expect_success 'remote set-url baz bar' '
1228 git remote set-url someremote baz bar &&
1229 echo baz >expect &&
1230 git config --get-all remote.someremote.url >actual &&
1231 cmp expect actual
1234 test_expect_success 'remote set-url zot bar' '
1235 test_must_fail git remote set-url someremote zot bar &&
1236 echo baz >expect &&
1237 git config --get-all remote.someremote.url >actual &&
1238 cmp expect actual
1241 test_expect_success 'remote set-url --push zot baz' '
1242 test_must_fail git remote set-url --push someremote zot baz &&
1243 echo "YYY" >expect &&
1244 echo baz >>expect &&
1245 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1246 echo "YYY" >>actual &&
1247 git config --get-all remote.someremote.url >>actual &&
1248 cmp expect actual
1251 test_expect_success 'remote set-url --push zot' '
1252 git remote set-url --push someremote zot &&
1253 echo zot >expect &&
1254 echo "YYY" >>expect &&
1255 echo baz >>expect &&
1256 git config --get-all remote.someremote.pushurl >actual &&
1257 echo "YYY" >>actual &&
1258 git config --get-all remote.someremote.url >>actual &&
1259 cmp expect actual
1262 test_expect_success 'get-url with different urls' '
1263 echo baz | get_url_test someremote &&
1264 echo baz | get_url_test --all someremote &&
1265 echo zot | get_url_test --push someremote &&
1266 echo zot | get_url_test --push --all someremote
1269 test_expect_success 'remote set-url --push qux zot' '
1270 git remote set-url --push someremote qux zot &&
1271 echo qux >expect &&
1272 echo "YYY" >>expect &&
1273 echo baz >>expect &&
1274 git config --get-all remote.someremote.pushurl >actual &&
1275 echo "YYY" >>actual &&
1276 git config --get-all remote.someremote.url >>actual &&
1277 cmp expect actual
1280 test_expect_success 'remote set-url --push foo qu+x' '
1281 git remote set-url --push someremote foo qu+x &&
1282 echo foo >expect &&
1283 echo "YYY" >>expect &&
1284 echo baz >>expect &&
1285 git config --get-all remote.someremote.pushurl >actual &&
1286 echo "YYY" >>actual &&
1287 git config --get-all remote.someremote.url >>actual &&
1288 cmp expect actual
1291 test_expect_success 'remote set-url --push --add aaa' '
1292 git remote set-url --push --add someremote aaa &&
1293 echo foo >expect &&
1294 echo aaa >>expect &&
1295 echo "YYY" >>expect &&
1296 echo baz >>expect &&
1297 git config --get-all remote.someremote.pushurl >actual &&
1298 echo "YYY" >>actual &&
1299 git config --get-all remote.someremote.url >>actual &&
1300 cmp expect actual
1303 test_expect_success 'get-url on multi push remote' '
1304 echo foo | get_url_test --push someremote &&
1305 get_url_test --push --all someremote <<-\EOF
1311 test_expect_success 'remote set-url --push bar aaa' '
1312 git remote set-url --push someremote bar aaa &&
1313 echo foo >expect &&
1314 echo bar >>expect &&
1315 echo "YYY" >>expect &&
1316 echo baz >>expect &&
1317 git config --get-all remote.someremote.pushurl >actual &&
1318 echo "YYY" >>actual &&
1319 git config --get-all remote.someremote.url >>actual &&
1320 cmp expect actual
1323 test_expect_success 'remote set-url --push --delete bar' '
1324 git remote set-url --push --delete someremote bar &&
1325 echo foo >expect &&
1326 echo "YYY" >>expect &&
1327 echo baz >>expect &&
1328 git config --get-all remote.someremote.pushurl >actual &&
1329 echo "YYY" >>actual &&
1330 git config --get-all remote.someremote.url >>actual &&
1331 cmp expect actual
1334 test_expect_success 'remote set-url --push --delete foo' '
1335 git remote set-url --push --delete someremote foo &&
1336 echo "YYY" >expect &&
1337 echo baz >>expect &&
1338 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1339 echo "YYY" >>actual &&
1340 git config --get-all remote.someremote.url >>actual &&
1341 cmp expect actual
1344 test_expect_success 'remote set-url --add bbb' '
1345 git remote set-url --add someremote bbb &&
1346 echo "YYY" >expect &&
1347 echo baz >>expect &&
1348 echo bbb >>expect &&
1349 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1350 echo "YYY" >>actual &&
1351 git config --get-all remote.someremote.url >>actual &&
1352 cmp expect actual
1355 test_expect_success 'get-url on multi fetch remote' '
1356 echo baz | get_url_test someremote &&
1357 get_url_test --all someremote <<-\EOF
1363 test_expect_success 'remote set-url --delete .*' '
1364 test_must_fail git remote set-url --delete someremote .\* &&
1365 echo "YYY" >expect &&
1366 echo baz >>expect &&
1367 echo bbb >>expect &&
1368 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1369 echo "YYY" >>actual &&
1370 git config --get-all remote.someremote.url >>actual &&
1371 cmp expect actual
1374 test_expect_success 'remote set-url --delete bbb' '
1375 git remote set-url --delete someremote bbb &&
1376 echo "YYY" >expect &&
1377 echo baz >>expect &&
1378 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1379 echo "YYY" >>actual &&
1380 git config --get-all remote.someremote.url >>actual &&
1381 cmp expect actual
1384 test_expect_success 'remote set-url --delete baz' '
1385 test_must_fail git remote set-url --delete someremote baz &&
1386 echo "YYY" >expect &&
1387 echo baz >>expect &&
1388 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1389 echo "YYY" >>actual &&
1390 git config --get-all remote.someremote.url >>actual &&
1391 cmp expect actual
1394 test_expect_success 'remote set-url --add ccc' '
1395 git remote set-url --add someremote ccc &&
1396 echo "YYY" >expect &&
1397 echo baz >>expect &&
1398 echo ccc >>expect &&
1399 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1400 echo "YYY" >>actual &&
1401 git config --get-all remote.someremote.url >>actual &&
1402 cmp expect actual
1405 test_expect_success 'remote set-url --delete baz' '
1406 git remote set-url --delete someremote baz &&
1407 echo "YYY" >expect &&
1408 echo ccc >>expect &&
1409 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1410 echo "YYY" >>actual &&
1411 git config --get-all remote.someremote.url >>actual &&
1412 cmp expect actual
1415 test_expect_success 'extra args: setup' '
1416 # add a dummy origin so that this does not trigger failure
1417 git remote add origin .
1420 test_extra_arg () {
1421 test_expect_success "extra args: $*" "
1422 test_must_fail git remote $* bogus_extra_arg 2>actual &&
1423 test_grep '^usage:' actual
1427 test_extra_arg add nick url
1428 test_extra_arg rename origin newname
1429 test_extra_arg remove origin
1430 test_extra_arg set-head origin main
1431 # set-branches takes any number of args
1432 test_extra_arg get-url origin newurl
1433 test_extra_arg set-url origin newurl oldurl
1434 # show takes any number of args
1435 # prune takes any number of args
1436 # update takes any number of args
1438 test_expect_success 'add remote matching the "insteadOf" URL' '
1439 git config url.xyz@example.com.insteadOf backup &&
1440 git remote add backup xyz@example.com
1443 test_expect_success 'unqualified <dst> refspec DWIM and advice' '
1444 test_when_finished "(cd test && git tag -d some-tag)" &&
1446 cd test &&
1447 git tag -a -m "Some tag" some-tag main &&
1448 for type in commit tag tree blob
1450 if test "$type" = "blob"
1451 then
1452 oid=$(git rev-parse some-tag:file)
1453 else
1454 oid=$(git rev-parse some-tag^{$type})
1455 fi &&
1456 test_must_fail git push origin $oid:dst 2>err &&
1457 test_grep "error: The destination you" err &&
1458 test_grep "hint: Did you mean" err &&
1459 test_must_fail git -c advice.pushUnqualifiedRefName=false \
1460 push origin $oid:dst 2>err &&
1461 test_grep "error: The destination you" err &&
1462 test_grep ! "hint: Did you mean" err ||
1463 exit 1
1464 done
1468 test_expect_success 'refs/remotes/* <src> refspec and unqualified <dst> DWIM and advice' '
1470 cd two &&
1471 git tag -a -m "Some tag" my-tag main &&
1472 git update-ref refs/trees/my-head-tree HEAD^{tree} &&
1473 git update-ref refs/blobs/my-file-blob HEAD:file
1474 ) &&
1476 cd test &&
1477 git config --add remote.two.fetch "+refs/tags/*:refs/remotes/tags-from-two/*" &&
1478 git config --add remote.two.fetch "+refs/trees/*:refs/remotes/trees-from-two/*" &&
1479 git config --add remote.two.fetch "+refs/blobs/*:refs/remotes/blobs-from-two/*" &&
1480 git fetch --no-tags two &&
1482 test_must_fail git push origin refs/remotes/two/another:dst 2>err &&
1483 test_grep "error: The destination you" err &&
1485 test_must_fail git push origin refs/remotes/tags-from-two/my-tag:dst-tag 2>err &&
1486 test_grep "error: The destination you" err &&
1488 test_must_fail git push origin refs/remotes/trees-from-two/my-head-tree:dst-tree 2>err &&
1489 test_grep "error: The destination you" err &&
1491 test_must_fail git push origin refs/remotes/blobs-from-two/my-file-blob:dst-blob 2>err &&
1492 test_grep "error: The destination you" err
1496 test_expect_success 'empty config clears remote.*.url list' '
1497 test_when_finished "git config --remove-section remote.multi" &&
1498 git config --add remote.multi.url wrong-one &&
1499 git config --add remote.multi.url wrong-two &&
1500 git -c remote.multi.url= \
1501 -c remote.multi.url=right-one \
1502 -c remote.multi.url=right-two \
1503 remote show -n multi >actual.raw &&
1504 grep URL actual.raw >actual &&
1505 cat >expect <<-\EOF &&
1506 Fetch URL: right-one
1507 Push URL: right-one
1508 Push URL: right-two
1510 test_cmp expect actual
1513 test_expect_success 'empty config clears remote.*.pushurl list' '
1514 test_when_finished "git config --remove-section remote.multi" &&
1515 git config --add remote.multi.url right &&
1516 git config --add remote.multi.url will-be-ignored &&
1517 git config --add remote.multi.pushurl wrong-push-one &&
1518 git config --add remote.multi.pushurl wrong-push-two &&
1519 git -c remote.multi.pushurl= \
1520 -c remote.multi.pushurl=right-push-one \
1521 -c remote.multi.pushurl=right-push-two \
1522 remote show -n multi >actual.raw &&
1523 grep URL actual.raw >actual &&
1524 cat >expect <<-\EOF &&
1525 Fetch URL: right
1526 Push URL: right-push-one
1527 Push URL: right-push-two
1529 test_cmp expect actual
1532 test_done