chainlint.pl: add POSIX shell parser
[git.git] / t / t5505-remote.sh
blob6c7370f87f5e4e06fa64d2992ad32c74a3d3d4e2
1 #!/bin/sh
3 test_description='git remote porcelain-ish'
5 . ./test-lib.sh
7 setup_repository () {
8 mkdir "$1" && (
9 cd "$1" &&
10 git init -b main &&
11 >file &&
12 git add file &&
13 test_tick &&
14 git commit -m "Initial" &&
15 git checkout -b side &&
16 >elif &&
17 git add elif &&
18 test_tick &&
19 git commit -m "Second" &&
20 git checkout main
24 tokens_match () {
25 echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
26 echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
27 test_cmp expect actual
30 check_remote_track () {
31 actual=$(git remote show "$1" | sed -ne 's|^ \(.*\) tracked$|\1|p')
32 shift &&
33 tokens_match "$*" "$actual"
36 check_tracking_branch () {
37 f="" &&
38 r=$(git for-each-ref "--format=%(refname)" |
39 sed -ne "s|^refs/remotes/$1/||p") &&
40 shift &&
41 tokens_match "$*" "$r"
44 test_expect_success setup '
45 setup_repository one &&
46 setup_repository two &&
48 cd two &&
49 git branch another
50 ) &&
51 git clone one test
54 test_expect_success 'add remote whose URL agrees with url.<...>.insteadOf' '
55 test_config url.git@host.com:team/repo.git.insteadOf myremote &&
56 git remote add myremote git@host.com:team/repo.git
59 test_expect_success 'remote information for the origin' '
61 cd test &&
62 tokens_match origin "$(git remote)" &&
63 check_remote_track origin main side &&
64 check_tracking_branch origin HEAD main side
68 test_expect_success 'add another remote' '
70 cd test &&
71 git remote add -f second ../two &&
72 tokens_match "origin second" "$(git remote)" &&
73 check_tracking_branch second main side another &&
74 git for-each-ref "--format=%(refname)" refs/remotes |
75 sed -e "/^refs\/remotes\/origin\//d" \
76 -e "/^refs\/remotes\/second\//d" >actual &&
77 test_must_be_empty actual
81 test_expect_success 'setup bare clone for server' '
82 git clone --bare "file://$(pwd)/one" srv.bare &&
83 git -C srv.bare config --local uploadpack.allowfilter 1 &&
84 git -C srv.bare config --local uploadpack.allowanysha1inwant 1
87 test_expect_success 'filters for promisor remotes are listed by git remote -v' '
88 test_when_finished "rm -rf pc" &&
89 git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
90 git -C pc remote -v >out &&
91 grep "srv.bare (fetch) \[blob:none\]" out &&
93 git -C pc config remote.origin.partialCloneFilter object:type=commit &&
94 git -C pc remote -v >out &&
95 grep "srv.bare (fetch) \[object:type=commit\]" out
98 test_expect_success 'filters should not be listed for non promisor remotes (remote -v)' '
99 test_when_finished "rm -rf pc" &&
100 git clone one pc &&
101 git -C pc remote -v >out &&
102 ! grep "(fetch) \[.*\]" out
105 test_expect_success 'filters are listed by git remote -v only' '
106 test_when_finished "rm -rf pc" &&
107 git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
108 git -C pc remote >out &&
109 ! grep "\[blob:none\]" out &&
111 git -C pc remote show >out &&
112 ! grep "\[blob:none\]" out
115 test_expect_success 'check remote-tracking' '
117 cd test &&
118 check_remote_track origin main side &&
119 check_remote_track second main side another
123 test_expect_success 'remote forces tracking branches' '
125 cd test &&
126 case $(git config remote.second.fetch) in
127 +*) true ;;
128 *) false ;;
129 esac
133 test_expect_success 'remove remote' '
135 cd test &&
136 git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/main &&
137 git remote rm second
141 test_expect_success 'remove remote' '
143 cd test &&
144 tokens_match origin "$(git remote)" &&
145 check_remote_track origin main side &&
146 git for-each-ref "--format=%(refname)" refs/remotes |
147 sed -e "/^refs\/remotes\/origin\//d" >actual &&
148 test_must_be_empty actual
152 test_expect_success 'remove remote protects local branches' '
154 cd test &&
155 cat >expect1 <<-\EOF &&
156 Note: A branch outside the refs/remotes/ hierarchy was not removed;
157 to delete it, use:
158 git branch -d main
160 cat >expect2 <<-\EOF &&
161 Note: Some branches outside the refs/remotes/ hierarchy were not removed;
162 to delete them, use:
163 git branch -d foobranch
164 git branch -d main
166 git tag footag &&
167 git config --add remote.oops.fetch "+refs/*:refs/*" &&
168 git remote remove oops 2>actual1 &&
169 git branch foobranch &&
170 git config --add remote.oops.fetch "+refs/*:refs/*" &&
171 git remote rm oops 2>actual2 &&
172 git branch -d foobranch &&
173 git tag -d footag &&
174 test_cmp expect1 actual1 &&
175 test_cmp expect2 actual2
179 test_expect_success 'remove errors out early when deleting non-existent branch' '
181 cd test &&
182 echo "error: No such remote: '\''foo'\''" >expect &&
183 test_expect_code 2 git remote rm foo 2>actual &&
184 test_cmp expect actual
188 test_expect_success 'remove remote with a branch without configured merge' '
189 test_when_finished "(
190 git -C test checkout main;
191 git -C test branch -D two;
192 git -C test config --remove-section remote.two;
193 git -C test config --remove-section branch.second;
194 true
195 )" &&
197 cd test &&
198 git remote add two ../two &&
199 git fetch two &&
200 git checkout -b second two/main^0 &&
201 git config branch.second.remote two &&
202 git checkout main &&
203 git remote rm two
207 test_expect_success 'rename errors out early when deleting non-existent branch' '
209 cd test &&
210 echo "error: No such remote: '\''foo'\''" >expect &&
211 test_expect_code 2 git remote rename foo bar 2>actual &&
212 test_cmp expect actual
216 test_expect_success 'rename errors out early when new name is invalid' '
217 test_config remote.foo.vcs bar &&
218 echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
219 test_must_fail git remote rename foo invalid...name 2>actual &&
220 test_cmp expect actual
223 test_expect_success 'add existing foreign_vcs remote' '
224 test_config remote.foo.vcs bar &&
225 echo "error: remote foo already exists." >expect &&
226 test_expect_code 3 git remote add foo bar 2>actual &&
227 test_cmp expect actual
230 test_expect_success 'add existing foreign_vcs remote' '
231 test_config remote.foo.vcs bar &&
232 test_config remote.bar.vcs bar &&
233 echo "error: remote bar already exists." >expect &&
234 test_expect_code 3 git remote rename foo bar 2>actual &&
235 test_cmp expect actual
238 test_expect_success 'add invalid foreign_vcs remote' '
239 echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
240 test_must_fail git remote add invalid...name bar 2>actual &&
241 test_cmp expect actual
244 cat >test/expect <<EOF
245 * remote origin
246 Fetch URL: $(pwd)/one
247 Push URL: $(pwd)/one
248 HEAD branch: main
249 Remote branches:
250 main new (next fetch will store in remotes/origin)
251 side tracked
252 Local branches configured for 'git pull':
253 ahead merges with remote main
254 main merges with remote main
255 octopus merges with remote topic-a
256 and with remote topic-b
257 and with remote topic-c
258 rebase rebases onto remote main
259 Local refs configured for 'git push':
260 main pushes to main (local out of date)
261 main pushes to upstream (create)
262 * remote two
263 Fetch URL: ../two
264 Push URL: ../three
265 HEAD branch: main
266 Local refs configured for 'git push':
267 ahead forces to main (fast-forwardable)
268 main pushes to another (up to date)
271 test_expect_success 'show' '
273 cd test &&
274 git config --add remote.origin.fetch refs/heads/main:refs/heads/upstream &&
275 git fetch &&
276 git checkout -b ahead origin/main &&
277 echo 1 >>file &&
278 test_tick &&
279 git commit -m update file &&
280 git checkout main &&
281 git branch --track octopus origin/main &&
282 git branch --track rebase origin/main &&
283 git branch -d -r origin/main &&
284 git config --add remote.two.url ../two &&
285 git config --add remote.two.pushurl ../three &&
286 git config branch.rebase.rebase true &&
287 git config branch.octopus.merge "topic-a topic-b topic-c" &&
289 cd ../one &&
290 echo 1 >file &&
291 test_tick &&
292 git commit -m update file
293 ) &&
294 git config --add remote.origin.push : &&
295 git config --add remote.origin.push refs/heads/main:refs/heads/upstream &&
296 git config --add remote.origin.push +refs/tags/lastbackup &&
297 git config --add remote.two.push +refs/heads/ahead:refs/heads/main &&
298 git config --add remote.two.push refs/heads/main:refs/heads/another &&
299 git remote show origin two >output &&
300 git branch -d rebase octopus &&
301 test_cmp expect output
305 cat >expect <<EOF
306 * remote origin
307 Fetch URL: $(pwd)/one
308 Push URL: $(pwd)/one
309 HEAD branch: main
310 Remote branches:
311 main skipped
312 side tracked
313 Local branches configured for 'git pull':
314 ahead merges with remote main
315 main merges with remote main
316 Local refs configured for 'git push':
317 main pushes to main (local out of date)
318 main pushes to upstream (create)
321 test_expect_success 'show with negative refspecs' '
322 test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/main" &&
323 git -C test config --add remote.origin.fetch ^refs/heads/main &&
324 git -C test remote show origin >output &&
325 test_cmp expect output
328 cat >expect <<EOF
329 * remote origin
330 Fetch URL: $(pwd)/one
331 Push URL: $(pwd)/one
332 HEAD branch: main
333 Remote branches:
334 main new (next fetch will store in remotes/origin)
335 side stale (use 'git remote prune' to remove)
336 Local branches configured for 'git pull':
337 ahead merges with remote main
338 main merges with remote main
339 Local refs configured for 'git push':
340 main pushes to main (local out of date)
341 main pushes to upstream (create)
344 test_expect_failure 'show stale with negative refspecs' '
345 test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/side" &&
346 git -C test config --add remote.origin.fetch ^refs/heads/side &&
347 git -C test remote show origin >output &&
348 test_cmp expect output
351 cat >test/expect <<EOF
352 * remote origin
353 Fetch URL: $(pwd)/one
354 Push URL: $(pwd)/one
355 HEAD branch: (not queried)
356 Remote branches: (status not queried)
357 main
358 side
359 Local branches configured for 'git pull':
360 ahead merges with remote main
361 main merges with remote main
362 Local refs configured for 'git push' (status not queried):
363 (matching) pushes to (matching)
364 refs/heads/main pushes to refs/heads/upstream
365 refs/tags/lastbackup forces to refs/tags/lastbackup
368 test_expect_success 'show -n' '
369 mv one one.unreachable &&
371 cd test &&
372 git remote show -n origin >output &&
373 mv ../one.unreachable ../one &&
374 test_cmp expect output
378 test_expect_success 'prune' '
380 cd one &&
381 git branch -m side side2
382 ) &&
384 cd test &&
385 git fetch origin &&
386 git remote prune origin &&
387 git rev-parse refs/remotes/origin/side2 &&
388 test_must_fail git rev-parse refs/remotes/origin/side
392 test_expect_success 'set-head --delete' '
394 cd test &&
395 git symbolic-ref refs/remotes/origin/HEAD &&
396 git remote set-head --delete origin &&
397 test_must_fail git symbolic-ref refs/remotes/origin/HEAD
401 test_expect_success 'set-head --auto' '
403 cd test &&
404 git remote set-head --auto origin &&
405 echo refs/remotes/origin/main >expect &&
406 git symbolic-ref refs/remotes/origin/HEAD >output &&
407 test_cmp expect output
411 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
413 cd test &&
414 git fetch two "refs/heads/*:refs/remotes/two/*" &&
415 git remote set-head --auto two >output 2>&1 &&
416 echo "two/HEAD set to main" >expect &&
417 test_cmp expect output
421 cat >test/expect <<\EOF
422 refs/remotes/origin/side2
425 test_expect_success 'set-head explicit' '
427 cd test &&
428 git remote set-head origin side2 &&
429 git symbolic-ref refs/remotes/origin/HEAD >output &&
430 git remote set-head origin main &&
431 test_cmp expect output
435 cat >test/expect <<EOF
436 Pruning origin
437 URL: $(pwd)/one
438 * [would prune] origin/side2
441 test_expect_success 'prune --dry-run' '
442 git -C one branch -m side2 side &&
443 test_when_finished "git -C one branch -m side side2" &&
445 cd test &&
446 git remote prune --dry-run origin >output &&
447 git rev-parse refs/remotes/origin/side2 &&
448 test_must_fail git rev-parse refs/remotes/origin/side &&
449 test_cmp expect output
453 test_expect_success 'add --mirror && prune' '
454 mkdir mirror &&
456 cd mirror &&
457 git init --bare &&
458 git remote add --mirror -f origin ../one
459 ) &&
461 cd one &&
462 git branch -m side2 side
463 ) &&
465 cd mirror &&
466 git rev-parse --verify refs/heads/side2 &&
467 test_must_fail git rev-parse --verify refs/heads/side &&
468 git fetch origin &&
469 git remote prune origin &&
470 test_must_fail git rev-parse --verify refs/heads/side2 &&
471 git rev-parse --verify refs/heads/side
475 test_expect_success 'add --mirror=fetch' '
476 mkdir mirror-fetch &&
477 git init -b main mirror-fetch/parent &&
479 cd mirror-fetch/parent &&
480 test_commit one
481 ) &&
482 git init --bare mirror-fetch/child &&
484 cd mirror-fetch/child &&
485 git remote add --mirror=fetch -f parent ../parent
489 test_expect_success 'fetch mirrors act as mirrors during fetch' '
491 cd mirror-fetch/parent &&
492 git branch new &&
493 git branch -m main renamed
494 ) &&
496 cd mirror-fetch/child &&
497 git fetch parent &&
498 git rev-parse --verify refs/heads/new &&
499 git rev-parse --verify refs/heads/renamed
503 test_expect_success 'fetch mirrors can prune' '
505 cd mirror-fetch/child &&
506 git remote prune parent &&
507 test_must_fail git rev-parse --verify refs/heads/main
511 test_expect_success 'fetch mirrors do not act as mirrors during push' '
513 cd mirror-fetch/parent &&
514 git checkout HEAD^0
515 ) &&
517 cd mirror-fetch/child &&
518 git branch -m renamed renamed2 &&
519 git push parent :
520 ) &&
522 cd mirror-fetch/parent &&
523 git rev-parse --verify renamed &&
524 test_must_fail git rev-parse --verify refs/heads/renamed2
528 test_expect_success 'add fetch mirror with specific branches' '
529 git init --bare mirror-fetch/track &&
531 cd mirror-fetch/track &&
532 git remote add --mirror=fetch -t heads/new parent ../parent
536 test_expect_success 'fetch mirror respects specific branches' '
538 cd mirror-fetch/track &&
539 git fetch parent &&
540 git rev-parse --verify refs/heads/new &&
541 test_must_fail git rev-parse --verify refs/heads/renamed
545 test_expect_success 'add --mirror=push' '
546 mkdir mirror-push &&
547 git init --bare mirror-push/public &&
548 git init -b main mirror-push/private &&
550 cd mirror-push/private &&
551 test_commit one &&
552 git remote add --mirror=push public ../public
556 test_expect_success 'push mirrors act as mirrors during push' '
558 cd mirror-push/private &&
559 git branch new &&
560 git branch -m main renamed &&
561 git push public
562 ) &&
564 cd mirror-push/private &&
565 git rev-parse --verify refs/heads/new &&
566 git rev-parse --verify refs/heads/renamed &&
567 test_must_fail git rev-parse --verify refs/heads/main
571 test_expect_success 'push mirrors do not act as mirrors during fetch' '
573 cd mirror-push/public &&
574 git branch -m renamed renamed2 &&
575 git symbolic-ref HEAD refs/heads/renamed2
576 ) &&
578 cd mirror-push/private &&
579 git fetch public &&
580 git rev-parse --verify refs/heads/renamed &&
581 test_must_fail git rev-parse --verify refs/heads/renamed2
585 test_expect_success 'push mirrors do not allow you to specify refs' '
586 git init mirror-push/track &&
588 cd mirror-push/track &&
589 test_must_fail git remote add --mirror=push -t new public ../public
593 test_expect_success 'add alt && prune' '
594 mkdir alttst &&
596 cd alttst &&
597 git init &&
598 git remote add -f origin ../one &&
599 git config remote.alt.url ../one &&
600 git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*"
601 ) &&
603 cd one &&
604 git branch -m side side2
605 ) &&
607 cd alttst &&
608 git rev-parse --verify refs/remotes/origin/side &&
609 test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
610 git fetch alt &&
611 git remote prune alt &&
612 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
613 git rev-parse --verify refs/remotes/origin/side2
617 cat >test/expect <<\EOF
618 some-tag
621 test_expect_success 'add with reachable tags (default)' '
623 cd one &&
624 >foobar &&
625 git add foobar &&
626 git commit -m "Foobar" &&
627 git tag -a -m "Foobar tag" foobar-tag &&
628 git reset --hard HEAD~1 &&
629 git tag -a -m "Some tag" some-tag
630 ) &&
631 mkdir add-tags &&
633 cd add-tags &&
634 git init &&
635 git remote add -f origin ../one &&
636 git tag -l some-tag >../test/output &&
637 git tag -l foobar-tag >>../test/output &&
638 test_must_fail git config remote.origin.tagopt
639 ) &&
640 test_cmp test/expect test/output
643 cat >test/expect <<\EOF
644 some-tag
645 foobar-tag
646 --tags
649 test_expect_success 'add --tags' '
650 rm -rf add-tags &&
652 mkdir add-tags &&
653 cd add-tags &&
654 git init &&
655 git remote add -f --tags origin ../one &&
656 git tag -l some-tag >../test/output &&
657 git tag -l foobar-tag >>../test/output &&
658 git config remote.origin.tagopt >>../test/output
659 ) &&
660 test_cmp test/expect test/output
663 cat >test/expect <<\EOF
664 --no-tags
667 test_expect_success 'add --no-tags' '
668 rm -rf add-tags &&
670 mkdir add-no-tags &&
671 cd add-no-tags &&
672 git init &&
673 git remote add -f --no-tags origin ../one &&
674 grep tagOpt .git/config &&
675 git tag -l some-tag >../test/output &&
676 git tag -l foobar-tag >../test/output &&
677 git config remote.origin.tagopt >>../test/output
678 ) &&
680 cd one &&
681 git tag -d some-tag foobar-tag
682 ) &&
683 test_cmp test/expect test/output
686 test_expect_success 'reject --no-no-tags' '
688 cd add-no-tags &&
689 test_must_fail git remote add -f --no-no-tags neworigin ../one
693 cat >one/expect <<\EOF
694 apis/main
695 apis/side
696 drosophila/another
697 drosophila/main
698 drosophila/side
701 test_expect_success 'update' '
703 cd one &&
704 git remote add drosophila ../two &&
705 git remote add apis ../mirror &&
706 git remote update &&
707 git branch -r >output &&
708 test_cmp expect output
712 cat >one/expect <<\EOF
713 drosophila/another
714 drosophila/main
715 drosophila/side
716 manduca/main
717 manduca/side
718 megaloprepus/main
719 megaloprepus/side
722 test_expect_success 'update with arguments' '
724 cd one &&
725 for b in $(git branch -r)
727 git branch -r -d $b || exit 1
728 done &&
729 git remote add manduca ../mirror &&
730 git remote add megaloprepus ../mirror &&
731 git config remotes.phobaeticus "drosophila megaloprepus" &&
732 git config remotes.titanus manduca &&
733 git remote update phobaeticus titanus &&
734 git branch -r >output &&
735 test_cmp expect output
739 test_expect_success 'update --prune' '
741 cd one &&
742 git branch -m side2 side3
743 ) &&
745 cd test &&
746 git remote update --prune &&
748 cd ../one &&
749 git branch -m side3 side2
750 ) &&
751 git rev-parse refs/remotes/origin/side3 &&
752 test_must_fail git rev-parse refs/remotes/origin/side2
756 cat >one/expect <<-\EOF
757 apis/main
758 apis/side
759 manduca/main
760 manduca/side
761 megaloprepus/main
762 megaloprepus/side
765 test_expect_success 'update default' '
767 cd one &&
768 for b in $(git branch -r)
770 git branch -r -d $b || exit 1
771 done &&
772 git config remote.drosophila.skipDefaultUpdate true &&
773 git remote update default &&
774 git branch -r >output &&
775 test_cmp expect output
779 cat >one/expect <<\EOF
780 drosophila/another
781 drosophila/main
782 drosophila/side
785 test_expect_success 'update default (overridden, with funny whitespace)' '
787 cd one &&
788 for b in $(git branch -r)
790 git branch -r -d $b || exit 1
791 done &&
792 git config remotes.default "$(printf "\t drosophila \n")" &&
793 git remote update default &&
794 git branch -r >output &&
795 test_cmp expect output
799 test_expect_success 'update (with remotes.default defined)' '
801 cd one &&
802 for b in $(git branch -r)
804 git branch -r -d $b || exit 1
805 done &&
806 git config remotes.default "drosophila" &&
807 git remote update &&
808 git branch -r >output &&
809 test_cmp expect output
813 test_expect_success '"remote show" does not show symbolic refs' '
814 git clone one three &&
816 cd three &&
817 git remote show origin >output &&
818 ! grep "^ *HEAD$" < output &&
819 ! grep -i stale < output
823 test_expect_success 'reject adding remote with an invalid name' '
824 test_must_fail git remote add some:url desired-name
827 # The first three test if the tracking branches are properly renamed,
828 # the last two ones check if the config is updated.
830 test_expect_success 'rename a remote' '
831 test_config_global remote.pushDefault origin &&
832 git clone one four &&
834 cd four &&
835 git config branch.main.pushRemote origin &&
836 GIT_TRACE2_EVENT=$(pwd)/trace \
837 git remote rename --progress origin upstream &&
838 test_region progress "Renaming remote references" trace &&
839 grep "pushRemote" .git/config &&
840 test -z "$(git for-each-ref refs/remotes/origin)" &&
841 test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/main" &&
842 test "$(git rev-parse upstream/main)" = "$(git rev-parse main)" &&
843 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
844 test "$(git config branch.main.remote)" = "upstream" &&
845 test "$(git config branch.main.pushRemote)" = "upstream" &&
846 test "$(git config --global remote.pushDefault)" = "origin"
850 test_expect_success 'rename a remote renames repo remote.pushDefault' '
851 git clone one four.1 &&
853 cd four.1 &&
854 git config remote.pushDefault origin &&
855 git remote rename origin upstream &&
856 grep pushDefault .git/config &&
857 test "$(git config --local remote.pushDefault)" = "upstream"
861 test_expect_success 'rename a remote renames repo remote.pushDefault but ignores global' '
862 test_config_global remote.pushDefault other &&
863 git clone one four.2 &&
865 cd four.2 &&
866 git config remote.pushDefault origin &&
867 git remote rename origin upstream &&
868 test "$(git config --global remote.pushDefault)" = "other" &&
869 test "$(git config --local remote.pushDefault)" = "upstream"
873 test_expect_success 'rename a remote renames repo remote.pushDefault but keeps global' '
874 test_config_global remote.pushDefault origin &&
875 git clone one four.3 &&
877 cd four.3 &&
878 git config remote.pushDefault origin &&
879 git remote rename origin upstream &&
880 test "$(git config --global remote.pushDefault)" = "origin" &&
881 test "$(git config --local remote.pushDefault)" = "upstream"
885 test_expect_success 'rename does not update a non-default fetch refspec' '
886 git clone one four.one &&
888 cd four.one &&
889 git config remote.origin.fetch +refs/heads/*:refs/heads/origin/* &&
890 git remote rename origin upstream &&
891 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/heads/origin/*" &&
892 git rev-parse -q origin/main
896 test_expect_success 'rename a remote with name part of fetch spec' '
897 git clone one four.two &&
899 cd four.two &&
900 git remote rename origin remote &&
901 git remote rename remote upstream &&
902 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*"
906 test_expect_success 'rename a remote with name prefix of other remote' '
907 git clone one four.three &&
909 cd four.three &&
910 git remote add o git://example.com/repo.git &&
911 git remote rename o upstream &&
912 test "$(git rev-parse origin/main)" = "$(git rev-parse main)"
916 test_expect_success 'rename succeeds with existing remote.<target>.prune' '
917 git clone one four.four &&
918 test_when_finished git config --global --unset remote.upstream.prune &&
919 git config --global remote.upstream.prune true &&
920 git -C four.four remote rename origin upstream
923 test_expect_success 'remove a remote' '
924 test_config_global remote.pushDefault origin &&
925 git clone one four.five &&
927 cd four.five &&
928 git config branch.main.pushRemote origin &&
929 git remote remove origin &&
930 test -z "$(git for-each-ref refs/remotes/origin)" &&
931 test_must_fail git config branch.main.remote &&
932 test_must_fail git config branch.main.pushRemote &&
933 test "$(git config --global remote.pushDefault)" = "origin"
937 test_expect_success 'remove a remote removes repo remote.pushDefault' '
938 git clone one four.five.1 &&
940 cd four.five.1 &&
941 git config remote.pushDefault origin &&
942 git remote remove origin &&
943 test_must_fail git config --local remote.pushDefault
947 test_expect_success 'remove a remote removes repo remote.pushDefault but ignores global' '
948 test_config_global remote.pushDefault other &&
949 git clone one four.five.2 &&
951 cd four.five.2 &&
952 git config remote.pushDefault origin &&
953 git remote remove origin &&
954 test "$(git config --global remote.pushDefault)" = "other" &&
955 test_must_fail git config --local remote.pushDefault
959 test_expect_success 'remove a remote removes repo remote.pushDefault but keeps global' '
960 test_config_global remote.pushDefault origin &&
961 git clone one four.five.3 &&
963 cd four.five.3 &&
964 git config remote.pushDefault origin &&
965 git remote remove origin &&
966 test "$(git config --global remote.pushDefault)" = "origin" &&
967 test_must_fail git config --local remote.pushDefault
971 cat >remotes_origin <<EOF
972 URL: $(pwd)/one
973 Push: refs/heads/main:refs/heads/upstream
974 Push: refs/heads/next:refs/heads/upstream2
975 Pull: refs/heads/main:refs/heads/origin
976 Pull: refs/heads/next:refs/heads/origin2
979 test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
980 git clone one five &&
981 origin_url=$(pwd)/one &&
983 cd five &&
984 git remote remove origin &&
985 mkdir -p .git/remotes &&
986 cat ../remotes_origin >.git/remotes/origin &&
987 git remote rename origin origin &&
988 test_path_is_missing .git/remotes/origin &&
989 test "$(git config remote.origin.url)" = "$origin_url" &&
990 cat >push_expected <<-\EOF &&
991 refs/heads/main:refs/heads/upstream
992 refs/heads/next:refs/heads/upstream2
994 cat >fetch_expected <<-\EOF &&
995 refs/heads/main:refs/heads/origin
996 refs/heads/next:refs/heads/origin2
998 git config --get-all remote.origin.push >push_actual &&
999 git config --get-all remote.origin.fetch >fetch_actual &&
1000 test_cmp push_expected push_actual &&
1001 test_cmp fetch_expected fetch_actual
1005 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
1006 git clone --template= one six &&
1007 origin_url=$(pwd)/one &&
1009 cd six &&
1010 git remote rm origin &&
1011 mkdir .git/branches &&
1012 echo "$origin_url#main" >.git/branches/origin &&
1013 git remote rename origin origin &&
1014 test_path_is_missing .git/branches/origin &&
1015 test "$(git config remote.origin.url)" = "$origin_url" &&
1016 test "$(git config remote.origin.fetch)" = "refs/heads/main:refs/heads/origin" &&
1017 test "$(git config remote.origin.push)" = "HEAD:refs/heads/main"
1021 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches (2)' '
1022 git clone --template= one seven &&
1024 cd seven &&
1025 git remote rm origin &&
1026 mkdir .git/branches &&
1027 echo "quux#foom" > .git/branches/origin &&
1028 git remote rename origin origin &&
1029 test_path_is_missing .git/branches/origin &&
1030 test "$(git config remote.origin.url)" = "quux" &&
1031 test "$(git config remote.origin.fetch)" = "refs/heads/foom:refs/heads/origin" &&
1032 test "$(git config remote.origin.push)" = "HEAD:refs/heads/foom"
1036 test_expect_success 'remote prune to cause a dangling symref' '
1037 git clone one eight &&
1039 cd one &&
1040 git checkout side2 &&
1041 git branch -D main
1042 ) &&
1044 cd eight &&
1045 git remote prune origin
1046 ) >err 2>&1 &&
1047 test_i18ngrep "has become dangling" err &&
1049 : And the dangling symref will not cause other annoying errors &&
1051 cd eight &&
1052 git branch -a
1053 ) 2>err &&
1054 ! grep "points nowhere" err &&
1056 cd eight &&
1057 test_must_fail git branch nomore origin
1058 ) 2>err &&
1059 test_i18ngrep "dangling symref" err
1062 test_expect_success 'show empty remote' '
1063 test_create_repo empty &&
1064 git clone empty empty-clone &&
1066 cd empty-clone &&
1067 git remote show origin
1071 test_expect_success 'remote set-branches requires a remote' '
1072 test_must_fail git remote set-branches &&
1073 test_must_fail git remote set-branches --add
1076 test_expect_success 'remote set-branches' '
1077 echo "+refs/heads/*:refs/remotes/scratch/*" >expect.initial &&
1078 sort <<-\EOF >expect.add &&
1079 +refs/heads/*:refs/remotes/scratch/*
1080 +refs/heads/other:refs/remotes/scratch/other
1082 sort <<-\EOF >expect.replace &&
1083 +refs/heads/maint:refs/remotes/scratch/maint
1084 +refs/heads/main:refs/remotes/scratch/main
1085 +refs/heads/next:refs/remotes/scratch/next
1087 sort <<-\EOF >expect.add-two &&
1088 +refs/heads/maint:refs/remotes/scratch/maint
1089 +refs/heads/main:refs/remotes/scratch/main
1090 +refs/heads/next:refs/remotes/scratch/next
1091 +refs/heads/seen:refs/remotes/scratch/seen
1092 +refs/heads/t/topic:refs/remotes/scratch/t/topic
1094 sort <<-\EOF >expect.setup-ffonly &&
1095 refs/heads/main:refs/remotes/scratch/main
1096 +refs/heads/next:refs/remotes/scratch/next
1098 sort <<-\EOF >expect.respect-ffonly &&
1099 refs/heads/main:refs/remotes/scratch/main
1100 +refs/heads/next:refs/remotes/scratch/next
1101 +refs/heads/seen:refs/remotes/scratch/seen
1104 git clone .git/ setbranches &&
1106 cd setbranches &&
1107 git remote rename origin scratch &&
1108 git config --get-all remote.scratch.fetch >config-result &&
1109 sort <config-result >../actual.initial &&
1111 git remote set-branches scratch --add other &&
1112 git config --get-all remote.scratch.fetch >config-result &&
1113 sort <config-result >../actual.add &&
1115 git remote set-branches scratch maint main next &&
1116 git config --get-all remote.scratch.fetch >config-result &&
1117 sort <config-result >../actual.replace &&
1119 git remote set-branches --add scratch seen t/topic &&
1120 git config --get-all remote.scratch.fetch >config-result &&
1121 sort <config-result >../actual.add-two &&
1123 git config --unset-all remote.scratch.fetch &&
1124 git config remote.scratch.fetch \
1125 refs/heads/main:refs/remotes/scratch/main &&
1126 git config --add remote.scratch.fetch \
1127 +refs/heads/next:refs/remotes/scratch/next &&
1128 git config --get-all remote.scratch.fetch >config-result &&
1129 sort <config-result >../actual.setup-ffonly &&
1131 git remote set-branches --add scratch seen &&
1132 git config --get-all remote.scratch.fetch >config-result &&
1133 sort <config-result >../actual.respect-ffonly
1134 ) &&
1135 test_cmp expect.initial actual.initial &&
1136 test_cmp expect.add actual.add &&
1137 test_cmp expect.replace actual.replace &&
1138 test_cmp expect.add-two actual.add-two &&
1139 test_cmp expect.setup-ffonly actual.setup-ffonly &&
1140 test_cmp expect.respect-ffonly actual.respect-ffonly
1143 test_expect_success 'remote set-branches with --mirror' '
1144 echo "+refs/*:refs/*" >expect.initial &&
1145 echo "+refs/heads/main:refs/heads/main" >expect.replace &&
1146 git clone --mirror .git/ setbranches-mirror &&
1148 cd setbranches-mirror &&
1149 git remote rename origin scratch &&
1150 git config --get-all remote.scratch.fetch >../actual.initial &&
1152 git remote set-branches scratch heads/main &&
1153 git config --get-all remote.scratch.fetch >../actual.replace
1154 ) &&
1155 test_cmp expect.initial actual.initial &&
1156 test_cmp expect.replace actual.replace
1159 test_expect_success 'new remote' '
1160 git remote add someremote foo &&
1161 echo foo >expect &&
1162 git config --get-all remote.someremote.url >actual &&
1163 cmp expect actual
1166 get_url_test () {
1167 cat >expect &&
1168 git remote get-url "$@" >actual &&
1169 test_cmp expect actual
1172 test_expect_success 'get-url on new remote' '
1173 echo foo | get_url_test someremote &&
1174 echo foo | get_url_test --all someremote &&
1175 echo foo | get_url_test --push someremote &&
1176 echo foo | get_url_test --push --all someremote
1179 test_expect_success 'remote set-url with locked config' '
1180 test_when_finished "rm -f .git/config.lock" &&
1181 git config --get-all remote.someremote.url >expect &&
1182 >.git/config.lock &&
1183 test_must_fail git remote set-url someremote baz &&
1184 git config --get-all remote.someremote.url >actual &&
1185 cmp expect actual
1188 test_expect_success 'remote set-url bar' '
1189 git remote set-url someremote bar &&
1190 echo bar >expect &&
1191 git config --get-all remote.someremote.url >actual &&
1192 cmp expect actual
1195 test_expect_success 'remote set-url baz bar' '
1196 git remote set-url someremote baz bar &&
1197 echo baz >expect &&
1198 git config --get-all remote.someremote.url >actual &&
1199 cmp expect actual
1202 test_expect_success 'remote set-url zot bar' '
1203 test_must_fail git remote set-url someremote zot bar &&
1204 echo baz >expect &&
1205 git config --get-all remote.someremote.url >actual &&
1206 cmp expect actual
1209 test_expect_success 'remote set-url --push zot baz' '
1210 test_must_fail git remote set-url --push someremote zot baz &&
1211 echo "YYY" >expect &&
1212 echo baz >>expect &&
1213 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1214 echo "YYY" >>actual &&
1215 git config --get-all remote.someremote.url >>actual &&
1216 cmp expect actual
1219 test_expect_success 'remote set-url --push zot' '
1220 git remote set-url --push someremote zot &&
1221 echo zot >expect &&
1222 echo "YYY" >>expect &&
1223 echo baz >>expect &&
1224 git config --get-all remote.someremote.pushurl >actual &&
1225 echo "YYY" >>actual &&
1226 git config --get-all remote.someremote.url >>actual &&
1227 cmp expect actual
1230 test_expect_success 'get-url with different urls' '
1231 echo baz | get_url_test someremote &&
1232 echo baz | get_url_test --all someremote &&
1233 echo zot | get_url_test --push someremote &&
1234 echo zot | get_url_test --push --all someremote
1237 test_expect_success 'remote set-url --push qux zot' '
1238 git remote set-url --push someremote qux zot &&
1239 echo qux >expect &&
1240 echo "YYY" >>expect &&
1241 echo baz >>expect &&
1242 git config --get-all remote.someremote.pushurl >actual &&
1243 echo "YYY" >>actual &&
1244 git config --get-all remote.someremote.url >>actual &&
1245 cmp expect actual
1248 test_expect_success 'remote set-url --push foo qu+x' '
1249 git remote set-url --push someremote foo qu+x &&
1250 echo foo >expect &&
1251 echo "YYY" >>expect &&
1252 echo baz >>expect &&
1253 git config --get-all remote.someremote.pushurl >actual &&
1254 echo "YYY" >>actual &&
1255 git config --get-all remote.someremote.url >>actual &&
1256 cmp expect actual
1259 test_expect_success 'remote set-url --push --add aaa' '
1260 git remote set-url --push --add someremote aaa &&
1261 echo foo >expect &&
1262 echo aaa >>expect &&
1263 echo "YYY" >>expect &&
1264 echo baz >>expect &&
1265 git config --get-all remote.someremote.pushurl >actual &&
1266 echo "YYY" >>actual &&
1267 git config --get-all remote.someremote.url >>actual &&
1268 cmp expect actual
1271 test_expect_success 'get-url on multi push remote' '
1272 echo foo | get_url_test --push someremote &&
1273 get_url_test --push --all someremote <<-\EOF
1279 test_expect_success 'remote set-url --push bar aaa' '
1280 git remote set-url --push someremote bar aaa &&
1281 echo foo >expect &&
1282 echo bar >>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 --delete bar' '
1292 git remote set-url --push --delete someremote bar &&
1293 echo foo >expect &&
1294 echo "YYY" >>expect &&
1295 echo baz >>expect &&
1296 git config --get-all remote.someremote.pushurl >actual &&
1297 echo "YYY" >>actual &&
1298 git config --get-all remote.someremote.url >>actual &&
1299 cmp expect actual
1302 test_expect_success 'remote set-url --push --delete foo' '
1303 git remote set-url --push --delete someremote foo &&
1304 echo "YYY" >expect &&
1305 echo baz >>expect &&
1306 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1307 echo "YYY" >>actual &&
1308 git config --get-all remote.someremote.url >>actual &&
1309 cmp expect actual
1312 test_expect_success 'remote set-url --add bbb' '
1313 git remote set-url --add someremote bbb &&
1314 echo "YYY" >expect &&
1315 echo baz >>expect &&
1316 echo bbb >>expect &&
1317 test_must_fail 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 'get-url on multi fetch remote' '
1324 echo baz | get_url_test someremote &&
1325 get_url_test --all someremote <<-\EOF
1331 test_expect_success 'remote set-url --delete .*' '
1332 test_must_fail git remote set-url --delete someremote .\* &&
1333 echo "YYY" >expect &&
1334 echo baz >>expect &&
1335 echo bbb >>expect &&
1336 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1337 echo "YYY" >>actual &&
1338 git config --get-all remote.someremote.url >>actual &&
1339 cmp expect actual
1342 test_expect_success 'remote set-url --delete bbb' '
1343 git remote set-url --delete someremote bbb &&
1344 echo "YYY" >expect &&
1345 echo baz >>expect &&
1346 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1347 echo "YYY" >>actual &&
1348 git config --get-all remote.someremote.url >>actual &&
1349 cmp expect actual
1352 test_expect_success 'remote set-url --delete baz' '
1353 test_must_fail git remote set-url --delete someremote baz &&
1354 echo "YYY" >expect &&
1355 echo baz >>expect &&
1356 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1357 echo "YYY" >>actual &&
1358 git config --get-all remote.someremote.url >>actual &&
1359 cmp expect actual
1362 test_expect_success 'remote set-url --add ccc' '
1363 git remote set-url --add someremote ccc &&
1364 echo "YYY" >expect &&
1365 echo baz >>expect &&
1366 echo ccc >>expect &&
1367 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1368 echo "YYY" >>actual &&
1369 git config --get-all remote.someremote.url >>actual &&
1370 cmp expect actual
1373 test_expect_success 'remote set-url --delete baz' '
1374 git remote set-url --delete someremote baz &&
1375 echo "YYY" >expect &&
1376 echo ccc >>expect &&
1377 test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1378 echo "YYY" >>actual &&
1379 git config --get-all remote.someremote.url >>actual &&
1380 cmp expect actual
1383 test_expect_success 'extra args: setup' '
1384 # add a dummy origin so that this does not trigger failure
1385 git remote add origin .
1388 test_extra_arg () {
1389 test_expect_success "extra args: $*" "
1390 test_must_fail git remote $* bogus_extra_arg 2>actual &&
1391 test_i18ngrep '^usage:' actual
1395 test_extra_arg add nick url
1396 test_extra_arg rename origin newname
1397 test_extra_arg remove origin
1398 test_extra_arg set-head origin main
1399 # set-branches takes any number of args
1400 test_extra_arg get-url origin newurl
1401 test_extra_arg set-url origin newurl oldurl
1402 # show takes any number of args
1403 # prune takes any number of args
1404 # update takes any number of args
1406 test_expect_success 'add remote matching the "insteadOf" URL' '
1407 git config url.xyz@example.com.insteadOf backup &&
1408 git remote add backup xyz@example.com
1411 test_expect_success 'unqualified <dst> refspec DWIM and advice' '
1412 test_when_finished "(cd test && git tag -d some-tag)" &&
1414 cd test &&
1415 git tag -a -m "Some tag" some-tag main &&
1416 for type in commit tag tree blob
1418 if test "$type" = "blob"
1419 then
1420 oid=$(git rev-parse some-tag:file)
1421 else
1422 oid=$(git rev-parse some-tag^{$type})
1423 fi &&
1424 test_must_fail git push origin $oid:dst 2>err &&
1425 test_i18ngrep "error: The destination you" err &&
1426 test_i18ngrep "hint: Did you mean" err &&
1427 test_must_fail git -c advice.pushUnqualifiedRefName=false \
1428 push origin $oid:dst 2>err &&
1429 test_i18ngrep "error: The destination you" err &&
1430 test_i18ngrep ! "hint: Did you mean" err ||
1431 exit 1
1432 done
1436 test_expect_success 'refs/remotes/* <src> refspec and unqualified <dst> DWIM and advice' '
1438 cd two &&
1439 git tag -a -m "Some tag" my-tag main &&
1440 git update-ref refs/trees/my-head-tree HEAD^{tree} &&
1441 git update-ref refs/blobs/my-file-blob HEAD:file
1442 ) &&
1444 cd test &&
1445 git config --add remote.two.fetch "+refs/tags/*:refs/remotes/tags-from-two/*" &&
1446 git config --add remote.two.fetch "+refs/trees/*:refs/remotes/trees-from-two/*" &&
1447 git config --add remote.two.fetch "+refs/blobs/*:refs/remotes/blobs-from-two/*" &&
1448 git fetch --no-tags two &&
1450 test_must_fail git push origin refs/remotes/two/another:dst 2>err &&
1451 test_i18ngrep "error: The destination you" err &&
1453 test_must_fail git push origin refs/remotes/tags-from-two/my-tag:dst-tag 2>err &&
1454 test_i18ngrep "error: The destination you" err &&
1456 test_must_fail git push origin refs/remotes/trees-from-two/my-head-tree:dst-tree 2>err &&
1457 test_i18ngrep "error: The destination you" err &&
1459 test_must_fail git push origin refs/remotes/blobs-from-two/my-file-blob:dst-blob 2>err &&
1460 test_i18ngrep "error: The destination you" err
1464 test_done