config: unify code paths to get global config paths
[git] / t / t5505-remote.sh
1 #!/bin/sh
2
3 test_description='git remote porcelain-ish'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 setup_repository () {
11         mkdir "$1" && (
12         cd "$1" &&
13         git init -b main &&
14         >file &&
15         git add file &&
16         test_tick &&
17         git commit -m "Initial" &&
18         git checkout -b side &&
19         >elif &&
20         git add elif &&
21         test_tick &&
22         git commit -m "Second" &&
23         git checkout main
24         )
25 }
26
27 tokens_match () {
28         echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
29         echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
30         test_cmp expect actual
31 }
32
33 check_remote_track () {
34         actual=$(git remote show "$1" | sed -ne 's|^    \(.*\) tracked$|\1|p')
35         shift &&
36         tokens_match "$*" "$actual"
37 }
38
39 check_tracking_branch () {
40         f="" &&
41         r=$(git for-each-ref "--format=%(refname)" |
42                 sed -ne "s|^refs/remotes/$1/||p") &&
43         shift &&
44         tokens_match "$*" "$r"
45 }
46
47 test_expect_success setup '
48         setup_repository one &&
49         setup_repository two &&
50         (
51                 cd two &&
52                 git branch another
53         ) &&
54         git clone one test
55 '
56
57 test_expect_success 'add remote whose URL agrees with url.<...>.insteadOf' '
58         test_config url.git@host.com:team/repo.git.insteadOf myremote &&
59         git remote add myremote git@host.com:team/repo.git
60 '
61
62 test_expect_success 'remote information for the origin' '
63         (
64                 cd test &&
65                 tokens_match origin "$(git remote)" &&
66                 check_remote_track origin main side &&
67                 check_tracking_branch origin HEAD main side
68         )
69 '
70
71 test_expect_success 'add another remote' '
72         (
73                 cd test &&
74                 git remote add -f second ../two &&
75                 tokens_match "origin second" "$(git remote)" &&
76                 check_tracking_branch second main side another &&
77                 git for-each-ref "--format=%(refname)" refs/remotes |
78                 sed -e "/^refs\/remotes\/origin\//d" \
79                     -e "/^refs\/remotes\/second\//d" >actual &&
80                 test_must_be_empty actual
81         )
82 '
83
84 test_expect_success 'check remote-tracking' '
85         (
86                 cd test &&
87                 check_remote_track origin main side &&
88                 check_remote_track second main side another
89         )
90 '
91
92 test_expect_success 'remote forces tracking branches' '
93         (
94                 cd test &&
95                 case $(git config remote.second.fetch) in
96                 +*) true ;;
97                  *) false ;;
98                 esac
99         )
100 '
101
102 test_expect_success 'remove remote' '
103         (
104                 cd test &&
105                 git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/main &&
106                 git remote rm second
107         )
108 '
109
110 test_expect_success 'remove remote' '
111         (
112                 cd test &&
113                 tokens_match origin "$(git remote)" &&
114                 check_remote_track origin main side &&
115                 git for-each-ref "--format=%(refname)" refs/remotes |
116                 sed -e "/^refs\/remotes\/origin\//d" >actual &&
117                 test_must_be_empty actual
118         )
119 '
120
121 test_expect_success 'remove remote protects local branches' '
122         (
123                 cd test &&
124                 cat >expect1 <<-\EOF &&
125                 Note: A branch outside the refs/remotes/ hierarchy was not removed;
126                 to delete it, use:
127                   git branch -d main
128                 EOF
129                 cat >expect2 <<-\EOF &&
130                 Note: Some branches outside the refs/remotes/ hierarchy were not removed;
131                 to delete them, use:
132                   git branch -d foobranch
133                   git branch -d main
134                 EOF
135                 git tag footag &&
136                 git config --add remote.oops.fetch "+refs/*:refs/*" &&
137                 git remote remove oops 2>actual1 &&
138                 git branch foobranch &&
139                 git config --add remote.oops.fetch "+refs/*:refs/*" &&
140                 git remote rm oops 2>actual2 &&
141                 git branch -d foobranch &&
142                 git tag -d footag &&
143                 test_cmp expect1 actual1 &&
144                 test_cmp expect2 actual2
145         )
146 '
147
148 test_expect_success 'remove errors out early when deleting non-existent branch' '
149         (
150                 cd test &&
151                 echo "error: No such remote: '\''foo'\''" >expect &&
152                 test_expect_code 2 git remote rm foo 2>actual &&
153                 test_cmp expect actual
154         )
155 '
156
157 test_expect_success 'remove remote with a branch without configured merge' '
158         test_when_finished "(
159                 git -C test checkout main;
160                 git -C test branch -D two;
161                 git -C test config --remove-section remote.two;
162                 git -C test config --remove-section branch.second;
163                 true
164         )" &&
165         (
166                 cd test &&
167                 git remote add two ../two &&
168                 git fetch two &&
169                 git checkout -b second two/main^0 &&
170                 git config branch.second.remote two &&
171                 git checkout main &&
172                 git remote rm two
173         )
174 '
175
176 test_expect_success 'rename errors out early when deleting non-existent branch' '
177         (
178                 cd test &&
179                 echo "error: No such remote: '\''foo'\''" >expect &&
180                 test_expect_code 2 git remote rename foo bar 2>actual &&
181                 test_cmp expect actual
182         )
183 '
184
185 test_expect_success 'rename errors out early when when new name is invalid' '
186         test_config remote.foo.vcs bar &&
187         echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
188         test_must_fail git remote rename foo invalid...name 2>actual &&
189         test_cmp expect actual
190 '
191
192 test_expect_success 'add existing foreign_vcs remote' '
193         test_config remote.foo.vcs bar &&
194         echo "error: remote foo already exists." >expect &&
195         test_expect_code 3 git remote add foo bar 2>actual &&
196         test_cmp expect actual
197 '
198
199 test_expect_success 'add existing foreign_vcs remote' '
200         test_config remote.foo.vcs bar &&
201         test_config remote.bar.vcs bar &&
202         echo "error: remote bar already exists." >expect &&
203         test_expect_code 3 git remote rename foo bar 2>actual &&
204         test_cmp expect actual
205 '
206
207 test_expect_success 'add invalid foreign_vcs remote' '
208         echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
209         test_must_fail git remote add invalid...name bar 2>actual &&
210         test_cmp expect actual
211 '
212
213 cat >test/expect <<EOF
214 * remote origin
215   Fetch URL: $(pwd)/one
216   Push  URL: $(pwd)/one
217   HEAD branch: main
218   Remote branches:
219     main new (next fetch will store in remotes/origin)
220     side tracked
221   Local branches configured for 'git pull':
222     ahead    merges with remote main
223     main     merges with remote main
224     octopus  merges with remote topic-a
225                 and with remote topic-b
226                 and with remote topic-c
227     rebase  rebases onto remote main
228   Local refs configured for 'git push':
229     main pushes to main     (local out of date)
230     main pushes to upstream (create)
231 * remote two
232   Fetch URL: ../two
233   Push  URL: ../three
234   HEAD branch: main
235   Local refs configured for 'git push':
236     ahead forces to main    (fast-forwardable)
237     main  pushes to another (up to date)
238 EOF
239
240 test_expect_success 'show' '
241         (
242                 cd test &&
243                 git config --add remote.origin.fetch refs/heads/main:refs/heads/upstream &&
244                 git fetch &&
245                 git checkout -b ahead origin/main &&
246                 echo 1 >>file &&
247                 test_tick &&
248                 git commit -m update file &&
249                 git checkout main &&
250                 git branch --track octopus origin/main &&
251                 git branch --track rebase origin/main &&
252                 git branch -d -r origin/main &&
253                 git config --add remote.two.url ../two &&
254                 git config --add remote.two.pushurl ../three &&
255                 git config branch.rebase.rebase true &&
256                 git config branch.octopus.merge "topic-a topic-b topic-c" &&
257                 (
258                         cd ../one &&
259                         echo 1 >file &&
260                         test_tick &&
261                         git commit -m update file
262                 ) &&
263                 git config --add remote.origin.push : &&
264                 git config --add remote.origin.push refs/heads/main:refs/heads/upstream &&
265                 git config --add remote.origin.push +refs/tags/lastbackup &&
266                 git config --add remote.two.push +refs/heads/ahead:refs/heads/main &&
267                 git config --add remote.two.push refs/heads/main:refs/heads/another &&
268                 git remote show origin two >output &&
269                 git branch -d rebase octopus &&
270                 test_cmp expect output
271         )
272 '
273
274 cat >test/expect <<EOF
275 * remote origin
276   Fetch URL: $(pwd)/one
277   Push  URL: $(pwd)/one
278   HEAD branch: (not queried)
279   Remote branches: (status not queried)
280     main
281     side
282   Local branches configured for 'git pull':
283     ahead merges with remote main
284     main  merges with remote main
285   Local refs configured for 'git push' (status not queried):
286     (matching)           pushes to (matching)
287     refs/heads/main      pushes to refs/heads/upstream
288     refs/tags/lastbackup forces to refs/tags/lastbackup
289 EOF
290
291 test_expect_success 'show -n' '
292         mv one one.unreachable &&
293         (
294                 cd test &&
295                 git remote show -n origin >output &&
296                 mv ../one.unreachable ../one &&
297                 test_cmp expect output
298         )
299 '
300
301 test_expect_success 'prune' '
302         (
303                 cd one &&
304                 git branch -m side side2
305         ) &&
306         (
307                 cd test &&
308                 git fetch origin &&
309                 git remote prune origin &&
310                 git rev-parse refs/remotes/origin/side2 &&
311                 test_must_fail git rev-parse refs/remotes/origin/side
312         )
313 '
314
315 test_expect_success 'set-head --delete' '
316         (
317                 cd test &&
318                 git symbolic-ref refs/remotes/origin/HEAD &&
319                 git remote set-head --delete origin &&
320                 test_must_fail git symbolic-ref refs/remotes/origin/HEAD
321         )
322 '
323
324 test_expect_success 'set-head --auto' '
325         (
326                 cd test &&
327                 git remote set-head --auto origin &&
328                 echo refs/remotes/origin/main >expect &&
329                 git symbolic-ref refs/remotes/origin/HEAD >output &&
330                 test_cmp expect output
331         )
332 '
333
334 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
335         (
336                 cd test &&
337                 git fetch two "refs/heads/*:refs/remotes/two/*" &&
338                 git remote set-head --auto two >output 2>&1 &&
339                 echo "two/HEAD set to main" >expect &&
340                 test_cmp expect output
341         )
342 '
343
344 cat >test/expect <<\EOF
345 refs/remotes/origin/side2
346 EOF
347
348 test_expect_success 'set-head explicit' '
349         (
350                 cd test &&
351                 git remote set-head origin side2 &&
352                 git symbolic-ref refs/remotes/origin/HEAD >output &&
353                 git remote set-head origin main &&
354                 test_cmp expect output
355         )
356 '
357
358 cat >test/expect <<EOF
359 Pruning origin
360 URL: $(pwd)/one
361  * [would prune] origin/side2
362 EOF
363
364 test_expect_success 'prune --dry-run' '
365         git -C one branch -m side2 side &&
366         test_when_finished "git -C one branch -m side side2" &&
367         (
368                 cd test &&
369                 git remote prune --dry-run origin >output &&
370                 git rev-parse refs/remotes/origin/side2 &&
371                 test_must_fail git rev-parse refs/remotes/origin/side &&
372                 test_cmp expect output
373         )
374 '
375
376 test_expect_success 'add --mirror && prune' '
377         mkdir mirror &&
378         (
379                 cd mirror &&
380                 git init --bare &&
381                 git remote add --mirror -f origin ../one
382         ) &&
383         (
384                 cd one &&
385                 git branch -m side2 side
386         ) &&
387         (
388                 cd mirror &&
389                 git rev-parse --verify refs/heads/side2 &&
390                 test_must_fail git rev-parse --verify refs/heads/side &&
391                 git fetch origin &&
392                 git remote prune origin &&
393                 test_must_fail git rev-parse --verify refs/heads/side2 &&
394                 git rev-parse --verify refs/heads/side
395         )
396 '
397
398 test_expect_success 'add --mirror=fetch' '
399         mkdir mirror-fetch &&
400         git init -b main mirror-fetch/parent &&
401         (
402                 cd mirror-fetch/parent &&
403                 test_commit one
404         ) &&
405         git init --bare mirror-fetch/child &&
406         (
407                 cd mirror-fetch/child &&
408                 git remote add --mirror=fetch -f parent ../parent
409         )
410 '
411
412 test_expect_success 'fetch mirrors act as mirrors during fetch' '
413         (
414                 cd mirror-fetch/parent &&
415                 git branch new &&
416                 git branch -m main renamed
417         ) &&
418         (
419                 cd mirror-fetch/child &&
420                 git fetch parent &&
421                 git rev-parse --verify refs/heads/new &&
422                 git rev-parse --verify refs/heads/renamed
423         )
424 '
425
426 test_expect_success 'fetch mirrors can prune' '
427         (
428                 cd mirror-fetch/child &&
429                 git remote prune parent &&
430                 test_must_fail git rev-parse --verify refs/heads/main
431         )
432 '
433
434 test_expect_success 'fetch mirrors do not act as mirrors during push' '
435         (
436                 cd mirror-fetch/parent &&
437                 git checkout HEAD^0
438         ) &&
439         (
440                 cd mirror-fetch/child &&
441                 git branch -m renamed renamed2 &&
442                 git push parent :
443         ) &&
444         (
445                 cd mirror-fetch/parent &&
446                 git rev-parse --verify renamed &&
447                 test_must_fail git rev-parse --verify refs/heads/renamed2
448         )
449 '
450
451 test_expect_success 'add fetch mirror with specific branches' '
452         git init --bare mirror-fetch/track &&
453         (
454                 cd mirror-fetch/track &&
455                 git remote add --mirror=fetch -t heads/new parent ../parent
456         )
457 '
458
459 test_expect_success 'fetch mirror respects specific branches' '
460         (
461                 cd mirror-fetch/track &&
462                 git fetch parent &&
463                 git rev-parse --verify refs/heads/new &&
464                 test_must_fail git rev-parse --verify refs/heads/renamed
465         )
466 '
467
468 test_expect_success 'add --mirror=push' '
469         mkdir mirror-push &&
470         git init --bare mirror-push/public &&
471         git init -b main mirror-push/private &&
472         (
473                 cd mirror-push/private &&
474                 test_commit one &&
475                 git remote add --mirror=push public ../public
476         )
477 '
478
479 test_expect_success 'push mirrors act as mirrors during push' '
480         (
481                 cd mirror-push/private &&
482                 git branch new &&
483                 git branch -m main renamed &&
484                 git push public
485         ) &&
486         (
487                 cd mirror-push/private &&
488                 git rev-parse --verify refs/heads/new &&
489                 git rev-parse --verify refs/heads/renamed &&
490                 test_must_fail git rev-parse --verify refs/heads/main
491         )
492 '
493
494 test_expect_success 'push mirrors do not act as mirrors during fetch' '
495         (
496                 cd mirror-push/public &&
497                 git branch -m renamed renamed2 &&
498                 git symbolic-ref HEAD refs/heads/renamed2
499         ) &&
500         (
501                 cd mirror-push/private &&
502                 git fetch public &&
503                 git rev-parse --verify refs/heads/renamed &&
504                 test_must_fail git rev-parse --verify refs/heads/renamed2
505         )
506 '
507
508 test_expect_success 'push mirrors do not allow you to specify refs' '
509         git init mirror-push/track &&
510         (
511                 cd mirror-push/track &&
512                 test_must_fail git remote add --mirror=push -t new public ../public
513         )
514 '
515
516 test_expect_success 'add alt && prune' '
517         mkdir alttst &&
518         (
519                 cd alttst &&
520                 git init &&
521                 git remote add -f origin ../one &&
522                 git config remote.alt.url ../one &&
523                 git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*"
524         ) &&
525         (
526                 cd one &&
527                 git branch -m side side2
528         ) &&
529         (
530                 cd alttst &&
531                 git rev-parse --verify refs/remotes/origin/side &&
532                 test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
533                 git fetch alt &&
534                 git remote prune alt &&
535                 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
536                 git rev-parse --verify refs/remotes/origin/side2
537         )
538 '
539
540 cat >test/expect <<\EOF
541 some-tag
542 EOF
543
544 test_expect_success 'add with reachable tags (default)' '
545         (
546                 cd one &&
547                 >foobar &&
548                 git add foobar &&
549                 git commit -m "Foobar" &&
550                 git tag -a -m "Foobar tag" foobar-tag &&
551                 git reset --hard HEAD~1 &&
552                 git tag -a -m "Some tag" some-tag
553         ) &&
554         mkdir add-tags &&
555         (
556                 cd add-tags &&
557                 git init &&
558                 git remote add -f origin ../one &&
559                 git tag -l some-tag >../test/output &&
560                 git tag -l foobar-tag >>../test/output &&
561                 test_must_fail git config remote.origin.tagopt
562         ) &&
563         test_cmp test/expect test/output
564 '
565
566 cat >test/expect <<\EOF
567 some-tag
568 foobar-tag
569 --tags
570 EOF
571
572 test_expect_success 'add --tags' '
573         rm -rf add-tags &&
574         (
575                 mkdir add-tags &&
576                 cd add-tags &&
577                 git init &&
578                 git remote add -f --tags origin ../one &&
579                 git tag -l some-tag >../test/output &&
580                 git tag -l foobar-tag >>../test/output &&
581                 git config remote.origin.tagopt >>../test/output
582         ) &&
583         test_cmp test/expect test/output
584 '
585
586 cat >test/expect <<\EOF
587 --no-tags
588 EOF
589
590 test_expect_success 'add --no-tags' '
591         rm -rf add-tags &&
592         (
593                 mkdir add-no-tags &&
594                 cd add-no-tags &&
595                 git init &&
596                 git remote add -f --no-tags origin ../one &&
597                 grep tagOpt .git/config &&
598                 git tag -l some-tag >../test/output &&
599                 git tag -l foobar-tag >../test/output &&
600                 git config remote.origin.tagopt >>../test/output
601         ) &&
602         (
603                 cd one &&
604                 git tag -d some-tag foobar-tag
605         ) &&
606         test_cmp test/expect test/output
607 '
608
609 test_expect_success 'reject --no-no-tags' '
610         (
611                 cd add-no-tags &&
612                 test_must_fail git remote add -f --no-no-tags neworigin ../one
613         )
614 '
615
616 cat >one/expect <<\EOF
617   apis/main
618   apis/side
619   drosophila/another
620   drosophila/main
621   drosophila/side
622 EOF
623
624 test_expect_success 'update' '
625         (
626                 cd one &&
627                 git remote add drosophila ../two &&
628                 git remote add apis ../mirror &&
629                 git remote update &&
630                 git branch -r >output &&
631                 test_cmp expect output
632         )
633 '
634
635 cat >one/expect <<\EOF
636   drosophila/another
637   drosophila/main
638   drosophila/side
639   manduca/main
640   manduca/side
641   megaloprepus/main
642   megaloprepus/side
643 EOF
644
645 test_expect_success 'update with arguments' '
646         (
647                 cd one &&
648                 for b in $(git branch -r)
649                 do
650                 git branch -r -d $b || exit 1
651                 done &&
652                 git remote add manduca ../mirror &&
653                 git remote add megaloprepus ../mirror &&
654                 git config remotes.phobaeticus "drosophila megaloprepus" &&
655                 git config remotes.titanus manduca &&
656                 git remote update phobaeticus titanus &&
657                 git branch -r >output &&
658                 test_cmp expect output
659         )
660 '
661
662 test_expect_success 'update --prune' '
663         (
664                 cd one &&
665                 git branch -m side2 side3
666         ) &&
667         (
668                 cd test &&
669                 git remote update --prune &&
670                 (
671                         cd ../one &&
672                         git branch -m side3 side2
673                 ) &&
674                 git rev-parse refs/remotes/origin/side3 &&
675                 test_must_fail git rev-parse refs/remotes/origin/side2
676         )
677 '
678
679 cat >one/expect <<-\EOF
680   apis/main
681   apis/side
682   manduca/main
683   manduca/side
684   megaloprepus/main
685   megaloprepus/side
686 EOF
687
688 test_expect_success 'update default' '
689         (
690                 cd one &&
691                 for b in $(git branch -r)
692                 do
693                 git branch -r -d $b || exit 1
694                 done &&
695                 git config remote.drosophila.skipDefaultUpdate true &&
696                 git remote update default &&
697                 git branch -r >output &&
698                 test_cmp expect output
699         )
700 '
701
702 cat >one/expect <<\EOF
703   drosophila/another
704   drosophila/main
705   drosophila/side
706 EOF
707
708 test_expect_success 'update default (overridden, with funny whitespace)' '
709         (
710                 cd one &&
711                 for b in $(git branch -r)
712                 do
713                 git branch -r -d $b || exit 1
714                 done &&
715                 git config remotes.default "$(printf "\t drosophila  \n")" &&
716                 git remote update default &&
717                 git branch -r >output &&
718                 test_cmp expect output
719         )
720 '
721
722 test_expect_success 'update (with remotes.default defined)' '
723         (
724                 cd one &&
725                 for b in $(git branch -r)
726                 do
727                 git branch -r -d $b || exit 1
728                 done &&
729                 git config remotes.default "drosophila" &&
730                 git remote update &&
731                 git branch -r >output &&
732                 test_cmp expect output
733         )
734 '
735
736 test_expect_success '"remote show" does not show symbolic refs' '
737         git clone one three &&
738         (
739                 cd three &&
740                 git remote show origin >output &&
741                 ! grep "^ *HEAD$" < output &&
742                 ! grep -i stale < output
743         )
744 '
745
746 test_expect_success 'reject adding remote with an invalid name' '
747         test_must_fail git remote add some:url desired-name
748 '
749
750 # The first three test if the tracking branches are properly renamed,
751 # the last two ones check if the config is updated.
752
753 test_expect_success 'rename a remote' '
754         test_config_global remote.pushDefault origin &&
755         git clone one four &&
756         (
757                 cd four &&
758                 git config branch.main.pushRemote origin &&
759                 git remote rename origin upstream &&
760                 grep "pushRemote" .git/config &&
761                 test -z "$(git for-each-ref refs/remotes/origin)" &&
762                 test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/main" &&
763                 test "$(git rev-parse upstream/main)" = "$(git rev-parse main)" &&
764                 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
765                 test "$(git config branch.main.remote)" = "upstream" &&
766                 test "$(git config branch.main.pushRemote)" = "upstream" &&
767                 test "$(git config --global remote.pushDefault)" = "origin"
768         )
769 '
770
771 test_expect_success 'rename a remote renames repo remote.pushDefault' '
772         git clone one four.1 &&
773         (
774                 cd four.1 &&
775                 git config remote.pushDefault origin &&
776                 git remote rename origin upstream &&
777                 grep pushDefault .git/config &&
778                 test "$(git config --local remote.pushDefault)" = "upstream"
779         )
780 '
781
782 test_expect_success 'rename a remote renames repo remote.pushDefault but ignores global' '
783         test_config_global remote.pushDefault other &&
784         git clone one four.2 &&
785         (
786                 cd four.2 &&
787                 git config remote.pushDefault origin &&
788                 git remote rename origin upstream &&
789                 test "$(git config --global remote.pushDefault)" = "other" &&
790                 test "$(git config --local remote.pushDefault)" = "upstream"
791         )
792 '
793
794 test_expect_success 'rename a remote renames repo remote.pushDefault but keeps global' '
795         test_config_global remote.pushDefault origin &&
796         git clone one four.3 &&
797         (
798                 cd four.3 &&
799                 git config remote.pushDefault origin &&
800                 git remote rename origin upstream &&
801                 test "$(git config --global remote.pushDefault)" = "origin" &&
802                 test "$(git config --local remote.pushDefault)" = "upstream"
803         )
804 '
805
806 test_expect_success 'rename does not update a non-default fetch refspec' '
807         git clone one four.one &&
808         (
809                 cd four.one &&
810                 git config remote.origin.fetch +refs/heads/*:refs/heads/origin/* &&
811                 git remote rename origin upstream &&
812                 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/heads/origin/*" &&
813                 git rev-parse -q origin/main
814         )
815 '
816
817 test_expect_success 'rename a remote with name part of fetch spec' '
818         git clone one four.two &&
819         (
820                 cd four.two &&
821                 git remote rename origin remote &&
822                 git remote rename remote upstream &&
823                 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*"
824         )
825 '
826
827 test_expect_success 'rename a remote with name prefix of other remote' '
828         git clone one four.three &&
829         (
830                 cd four.three &&
831                 git remote add o git://example.com/repo.git &&
832                 git remote rename o upstream &&
833                 test "$(git rev-parse origin/main)" = "$(git rev-parse main)"
834         )
835 '
836
837 test_expect_success 'rename succeeds with existing remote.<target>.prune' '
838         git clone one four.four &&
839         test_when_finished git config --global --unset remote.upstream.prune &&
840         git config --global remote.upstream.prune true &&
841         git -C four.four remote rename origin upstream
842 '
843
844 test_expect_success 'remove a remote' '
845         test_config_global remote.pushDefault origin &&
846         git clone one four.five &&
847         (
848                 cd four.five &&
849                 git config branch.main.pushRemote origin &&
850                 git remote remove origin &&
851                 test -z "$(git for-each-ref refs/remotes/origin)" &&
852                 test_must_fail git config branch.main.remote &&
853                 test_must_fail git config branch.main.pushRemote &&
854                 test "$(git config --global remote.pushDefault)" = "origin"
855         )
856 '
857
858 test_expect_success 'remove a remote removes repo remote.pushDefault' '
859         git clone one four.five.1 &&
860         (
861                 cd four.five.1 &&
862                 git config remote.pushDefault origin &&
863                 git remote remove origin &&
864                 test_must_fail git config --local remote.pushDefault
865         )
866 '
867
868 test_expect_success 'remove a remote removes repo remote.pushDefault but ignores global' '
869         test_config_global remote.pushDefault other &&
870         git clone one four.five.2 &&
871         (
872                 cd four.five.2 &&
873                 git config remote.pushDefault origin &&
874                 git remote remove origin &&
875                 test "$(git config --global remote.pushDefault)" = "other" &&
876                 test_must_fail git config --local remote.pushDefault
877         )
878 '
879
880 test_expect_success 'remove a remote removes repo remote.pushDefault but keeps global' '
881         test_config_global remote.pushDefault origin &&
882         git clone one four.five.3 &&
883         (
884                 cd four.five.3 &&
885                 git config remote.pushDefault origin &&
886                 git remote remove origin &&
887                 test "$(git config --global remote.pushDefault)" = "origin" &&
888                 test_must_fail git config --local remote.pushDefault
889         )
890 '
891
892 cat >remotes_origin <<EOF
893 URL: $(pwd)/one
894 Push: refs/heads/main:refs/heads/upstream
895 Push: refs/heads/next:refs/heads/upstream2
896 Pull: refs/heads/main:refs/heads/origin
897 Pull: refs/heads/next:refs/heads/origin2
898 EOF
899
900 test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
901         git clone one five &&
902         origin_url=$(pwd)/one &&
903         (
904                 cd five &&
905                 git remote remove origin &&
906                 mkdir -p .git/remotes &&
907                 cat ../remotes_origin >.git/remotes/origin &&
908                 git remote rename origin origin &&
909                 test_path_is_missing .git/remotes/origin &&
910                 test "$(git config remote.origin.url)" = "$origin_url" &&
911                 cat >push_expected <<-\EOF &&
912                 refs/heads/main:refs/heads/upstream
913                 refs/heads/next:refs/heads/upstream2
914                 EOF
915                 cat >fetch_expected <<-\EOF &&
916                 refs/heads/main:refs/heads/origin
917                 refs/heads/next:refs/heads/origin2
918                 EOF
919                 git config --get-all remote.origin.push >push_actual &&
920                 git config --get-all remote.origin.fetch >fetch_actual &&
921                 test_cmp push_expected push_actual &&
922                 test_cmp fetch_expected fetch_actual
923         )
924 '
925
926 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
927         git clone one six &&
928         origin_url=$(pwd)/one &&
929         (
930                 cd six &&
931                 git remote rm origin &&
932                 echo "$origin_url#main" >.git/branches/origin &&
933                 git remote rename origin origin &&
934                 test_path_is_missing .git/branches/origin &&
935                 test "$(git config remote.origin.url)" = "$origin_url" &&
936                 test "$(git config remote.origin.fetch)" = "refs/heads/main:refs/heads/origin" &&
937                 test "$(git config remote.origin.push)" = "HEAD:refs/heads/main"
938         )
939 '
940
941 test_expect_success 'migrate a remote from named file in $GIT_DIR/branches (2)' '
942         git clone one seven &&
943         (
944                 cd seven &&
945                 git remote rm origin &&
946                 echo "quux#foom" > .git/branches/origin &&
947                 git remote rename origin origin &&
948                 test_path_is_missing .git/branches/origin &&
949                 test "$(git config remote.origin.url)" = "quux" &&
950                 test "$(git config remote.origin.fetch)" = "refs/heads/foom:refs/heads/origin" &&
951                 test "$(git config remote.origin.push)" = "HEAD:refs/heads/foom"
952         )
953 '
954
955 test_expect_success 'remote prune to cause a dangling symref' '
956         git clone one eight &&
957         (
958                 cd one &&
959                 git checkout side2 &&
960                 git branch -D main
961         ) &&
962         (
963                 cd eight &&
964                 git remote prune origin
965         ) >err 2>&1 &&
966         test_i18ngrep "has become dangling" err &&
967
968         : And the dangling symref will not cause other annoying errors &&
969         (
970                 cd eight &&
971                 git branch -a
972         ) 2>err &&
973         ! grep "points nowhere" err &&
974         (
975                 cd eight &&
976                 test_must_fail git branch nomore origin
977         ) 2>err &&
978         test_i18ngrep "dangling symref" err
979 '
980
981 test_expect_success 'show empty remote' '
982         test_create_repo empty &&
983         git clone empty empty-clone &&
984         (
985                 cd empty-clone &&
986                 git remote show origin
987         )
988 '
989
990 test_expect_success 'remote set-branches requires a remote' '
991         test_must_fail git remote set-branches &&
992         test_must_fail git remote set-branches --add
993 '
994
995 test_expect_success 'remote set-branches' '
996         echo "+refs/heads/*:refs/remotes/scratch/*" >expect.initial &&
997         sort <<-\EOF >expect.add &&
998         +refs/heads/*:refs/remotes/scratch/*
999         +refs/heads/other:refs/remotes/scratch/other
1000         EOF
1001         sort <<-\EOF >expect.replace &&
1002         +refs/heads/maint:refs/remotes/scratch/maint
1003         +refs/heads/main:refs/remotes/scratch/main
1004         +refs/heads/next:refs/remotes/scratch/next
1005         EOF
1006         sort <<-\EOF >expect.add-two &&
1007         +refs/heads/maint:refs/remotes/scratch/maint
1008         +refs/heads/main:refs/remotes/scratch/main
1009         +refs/heads/next:refs/remotes/scratch/next
1010         +refs/heads/seen:refs/remotes/scratch/seen
1011         +refs/heads/t/topic:refs/remotes/scratch/t/topic
1012         EOF
1013         sort <<-\EOF >expect.setup-ffonly &&
1014         refs/heads/main:refs/remotes/scratch/main
1015         +refs/heads/next:refs/remotes/scratch/next
1016         EOF
1017         sort <<-\EOF >expect.respect-ffonly &&
1018         refs/heads/main:refs/remotes/scratch/main
1019         +refs/heads/next:refs/remotes/scratch/next
1020         +refs/heads/seen:refs/remotes/scratch/seen
1021         EOF
1022
1023         git clone .git/ setbranches &&
1024         (
1025                 cd setbranches &&
1026                 git remote rename origin scratch &&
1027                 git config --get-all remote.scratch.fetch >config-result &&
1028                 sort <config-result >../actual.initial &&
1029
1030                 git remote set-branches scratch --add other &&
1031                 git config --get-all remote.scratch.fetch >config-result &&
1032                 sort <config-result >../actual.add &&
1033
1034                 git remote set-branches scratch maint main next &&
1035                 git config --get-all remote.scratch.fetch >config-result &&
1036                 sort <config-result >../actual.replace &&
1037
1038                 git remote set-branches --add scratch seen t/topic &&
1039                 git config --get-all remote.scratch.fetch >config-result &&
1040                 sort <config-result >../actual.add-two &&
1041
1042                 git config --unset-all remote.scratch.fetch &&
1043                 git config remote.scratch.fetch \
1044                         refs/heads/main:refs/remotes/scratch/main &&
1045                 git config --add remote.scratch.fetch \
1046                         +refs/heads/next:refs/remotes/scratch/next &&
1047                 git config --get-all remote.scratch.fetch >config-result &&
1048                 sort <config-result >../actual.setup-ffonly &&
1049
1050                 git remote set-branches --add scratch seen &&
1051                 git config --get-all remote.scratch.fetch >config-result &&
1052                 sort <config-result >../actual.respect-ffonly
1053         ) &&
1054         test_cmp expect.initial actual.initial &&
1055         test_cmp expect.add actual.add &&
1056         test_cmp expect.replace actual.replace &&
1057         test_cmp expect.add-two actual.add-two &&
1058         test_cmp expect.setup-ffonly actual.setup-ffonly &&
1059         test_cmp expect.respect-ffonly actual.respect-ffonly
1060 '
1061
1062 test_expect_success 'remote set-branches with --mirror' '
1063         echo "+refs/*:refs/*" >expect.initial &&
1064         echo "+refs/heads/main:refs/heads/main" >expect.replace &&
1065         git clone --mirror .git/ setbranches-mirror &&
1066         (
1067                 cd setbranches-mirror &&
1068                 git remote rename origin scratch &&
1069                 git config --get-all remote.scratch.fetch >../actual.initial &&
1070
1071                 git remote set-branches scratch heads/main &&
1072                 git config --get-all remote.scratch.fetch >../actual.replace
1073         ) &&
1074         test_cmp expect.initial actual.initial &&
1075         test_cmp expect.replace actual.replace
1076 '
1077
1078 test_expect_success 'new remote' '
1079         git remote add someremote foo &&
1080         echo foo >expect &&
1081         git config --get-all remote.someremote.url >actual &&
1082         cmp expect actual
1083 '
1084
1085 get_url_test () {
1086         cat >expect &&
1087         git remote get-url "$@" >actual &&
1088         test_cmp expect actual
1089 }
1090
1091 test_expect_success 'get-url on new remote' '
1092         echo foo | get_url_test someremote &&
1093         echo foo | get_url_test --all someremote &&
1094         echo foo | get_url_test --push someremote &&
1095         echo foo | get_url_test --push --all someremote
1096 '
1097
1098 test_expect_success 'remote set-url with locked config' '
1099         test_when_finished "rm -f .git/config.lock" &&
1100         git config --get-all remote.someremote.url >expect &&
1101         >.git/config.lock &&
1102         test_must_fail git remote set-url someremote baz &&
1103         git config --get-all remote.someremote.url >actual &&
1104         cmp expect actual
1105 '
1106
1107 test_expect_success 'remote set-url bar' '
1108         git remote set-url someremote bar &&
1109         echo bar >expect &&
1110         git config --get-all remote.someremote.url >actual &&
1111         cmp expect actual
1112 '
1113
1114 test_expect_success 'remote set-url baz bar' '
1115         git remote set-url someremote baz bar &&
1116         echo baz >expect &&
1117         git config --get-all remote.someremote.url >actual &&
1118         cmp expect actual
1119 '
1120
1121 test_expect_success 'remote set-url zot bar' '
1122         test_must_fail git remote set-url someremote zot bar &&
1123         echo baz >expect &&
1124         git config --get-all remote.someremote.url >actual &&
1125         cmp expect actual
1126 '
1127
1128 test_expect_success 'remote set-url --push zot baz' '
1129         test_must_fail git remote set-url --push someremote zot baz &&
1130         echo "YYY" >expect &&
1131         echo baz >>expect &&
1132         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1133         echo "YYY" >>actual &&
1134         git config --get-all remote.someremote.url >>actual &&
1135         cmp expect actual
1136 '
1137
1138 test_expect_success 'remote set-url --push zot' '
1139         git remote set-url --push someremote zot &&
1140         echo zot >expect &&
1141         echo "YYY" >>expect &&
1142         echo baz >>expect &&
1143         git config --get-all remote.someremote.pushurl >actual &&
1144         echo "YYY" >>actual &&
1145         git config --get-all remote.someremote.url >>actual &&
1146         cmp expect actual
1147 '
1148
1149 test_expect_success 'get-url with different urls' '
1150         echo baz | get_url_test someremote &&
1151         echo baz | get_url_test --all someremote &&
1152         echo zot | get_url_test --push someremote &&
1153         echo zot | get_url_test --push --all someremote
1154 '
1155
1156 test_expect_success 'remote set-url --push qux zot' '
1157         git remote set-url --push someremote qux zot &&
1158         echo qux >expect &&
1159         echo "YYY" >>expect &&
1160         echo baz >>expect &&
1161         git config --get-all remote.someremote.pushurl >actual &&
1162         echo "YYY" >>actual &&
1163         git config --get-all remote.someremote.url >>actual &&
1164         cmp expect actual
1165 '
1166
1167 test_expect_success 'remote set-url --push foo qu+x' '
1168         git remote set-url --push someremote foo qu+x &&
1169         echo foo >expect &&
1170         echo "YYY" >>expect &&
1171         echo baz >>expect &&
1172         git config --get-all remote.someremote.pushurl >actual &&
1173         echo "YYY" >>actual &&
1174         git config --get-all remote.someremote.url >>actual &&
1175         cmp expect actual
1176 '
1177
1178 test_expect_success 'remote set-url --push --add aaa' '
1179         git remote set-url --push --add someremote aaa &&
1180         echo foo >expect &&
1181         echo aaa >>expect &&
1182         echo "YYY" >>expect &&
1183         echo baz >>expect &&
1184         git config --get-all remote.someremote.pushurl >actual &&
1185         echo "YYY" >>actual &&
1186         git config --get-all remote.someremote.url >>actual &&
1187         cmp expect actual
1188 '
1189
1190 test_expect_success 'get-url on multi push remote' '
1191         echo foo | get_url_test --push someremote &&
1192         get_url_test --push --all someremote <<-\EOF
1193         foo
1194         aaa
1195         EOF
1196 '
1197
1198 test_expect_success 'remote set-url --push bar aaa' '
1199         git remote set-url --push someremote bar aaa &&
1200         echo foo >expect &&
1201         echo bar >>expect &&
1202         echo "YYY" >>expect &&
1203         echo baz >>expect &&
1204         git config --get-all remote.someremote.pushurl >actual &&
1205         echo "YYY" >>actual &&
1206         git config --get-all remote.someremote.url >>actual &&
1207         cmp expect actual
1208 '
1209
1210 test_expect_success 'remote set-url --push --delete bar' '
1211         git remote set-url --push --delete someremote bar &&
1212         echo foo >expect &&
1213         echo "YYY" >>expect &&
1214         echo baz >>expect &&
1215         git config --get-all remote.someremote.pushurl >actual &&
1216         echo "YYY" >>actual &&
1217         git config --get-all remote.someremote.url >>actual &&
1218         cmp expect actual
1219 '
1220
1221 test_expect_success 'remote set-url --push --delete foo' '
1222         git remote set-url --push --delete someremote foo &&
1223         echo "YYY" >expect &&
1224         echo baz >>expect &&
1225         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1226         echo "YYY" >>actual &&
1227         git config --get-all remote.someremote.url >>actual &&
1228         cmp expect actual
1229 '
1230
1231 test_expect_success 'remote set-url --add bbb' '
1232         git remote set-url --add someremote bbb &&
1233         echo "YYY" >expect &&
1234         echo baz >>expect &&
1235         echo bbb >>expect &&
1236         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1237         echo "YYY" >>actual &&
1238         git config --get-all remote.someremote.url >>actual &&
1239         cmp expect actual
1240 '
1241
1242 test_expect_success 'get-url on multi fetch remote' '
1243         echo baz | get_url_test someremote &&
1244         get_url_test --all someremote <<-\EOF
1245         baz
1246         bbb
1247         EOF
1248 '
1249
1250 test_expect_success 'remote set-url --delete .*' '
1251         test_must_fail git remote set-url --delete someremote .\* &&
1252         echo "YYY" >expect &&
1253         echo baz >>expect &&
1254         echo bbb >>expect &&
1255         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1256         echo "YYY" >>actual &&
1257         git config --get-all remote.someremote.url >>actual &&
1258         cmp expect actual
1259 '
1260
1261 test_expect_success 'remote set-url --delete bbb' '
1262         git remote set-url --delete someremote bbb &&
1263         echo "YYY" >expect &&
1264         echo baz >>expect &&
1265         test_must_fail 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
1269 '
1270
1271 test_expect_success 'remote set-url --delete baz' '
1272         test_must_fail git remote set-url --delete someremote baz &&
1273         echo "YYY" >expect &&
1274         echo baz >>expect &&
1275         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1276         echo "YYY" >>actual &&
1277         git config --get-all remote.someremote.url >>actual &&
1278         cmp expect actual
1279 '
1280
1281 test_expect_success 'remote set-url --add ccc' '
1282         git remote set-url --add someremote ccc &&
1283         echo "YYY" >expect &&
1284         echo baz >>expect &&
1285         echo ccc >>expect &&
1286         test_must_fail git config --get-all remote.someremote.pushurl >actual &&
1287         echo "YYY" >>actual &&
1288         git config --get-all remote.someremote.url >>actual &&
1289         cmp expect actual
1290 '
1291
1292 test_expect_success 'remote set-url --delete baz' '
1293         git remote set-url --delete someremote baz &&
1294         echo "YYY" >expect &&
1295         echo ccc >>expect &&
1296         test_must_fail 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
1300 '
1301
1302 test_expect_success 'extra args: setup' '
1303         # add a dummy origin so that this does not trigger failure
1304         git remote add origin .
1305 '
1306
1307 test_extra_arg () {
1308         test_expect_success "extra args: $*" "
1309                 test_must_fail git remote $* bogus_extra_arg 2>actual &&
1310                 test_i18ngrep '^usage:' actual
1311         "
1312 }
1313
1314 test_extra_arg add nick url
1315 test_extra_arg rename origin newname
1316 test_extra_arg remove origin
1317 test_extra_arg set-head origin main
1318 # set-branches takes any number of args
1319 test_extra_arg get-url origin newurl
1320 test_extra_arg set-url origin newurl oldurl
1321 # show takes any number of args
1322 # prune takes any number of args
1323 # update takes any number of args
1324
1325 test_expect_success 'add remote matching the "insteadOf" URL' '
1326         git config url.xyz@example.com.insteadOf backup &&
1327         git remote add backup xyz@example.com
1328 '
1329
1330 test_expect_success 'unqualified <dst> refspec DWIM and advice' '
1331         test_when_finished "(cd test && git tag -d some-tag)" &&
1332         (
1333                 cd test &&
1334                 git tag -a -m "Some tag" some-tag main &&
1335                 exit_with=true &&
1336                 for type in commit tag tree blob
1337                 do
1338                         if test "$type" = "blob"
1339                         then
1340                                 oid=$(git rev-parse some-tag:file)
1341                         else
1342                                 oid=$(git rev-parse some-tag^{$type})
1343                         fi &&
1344                         test_must_fail git push origin $oid:dst 2>err &&
1345                         test_i18ngrep "error: The destination you" err &&
1346                         test_i18ngrep "hint: Did you mean" err &&
1347                         test_must_fail git -c advice.pushUnqualifiedRefName=false \
1348                                 push origin $oid:dst 2>err &&
1349                         test_i18ngrep "error: The destination you" err &&
1350                         test_i18ngrep ! "hint: Did you mean" err ||
1351                         exit_with=false
1352                 done &&
1353                 $exit_with
1354         )
1355 '
1356
1357 test_expect_success 'refs/remotes/* <src> refspec and unqualified <dst> DWIM and advice' '
1358         (
1359                 cd two &&
1360                 git tag -a -m "Some tag" my-tag main &&
1361                 git update-ref refs/trees/my-head-tree HEAD^{tree} &&
1362                 git update-ref refs/blobs/my-file-blob HEAD:file
1363         ) &&
1364         (
1365                 cd test &&
1366                 git config --add remote.two.fetch "+refs/tags/*:refs/remotes/tags-from-two/*" &&
1367                 git config --add remote.two.fetch "+refs/trees/*:refs/remotes/trees-from-two/*" &&
1368                 git config --add remote.two.fetch "+refs/blobs/*:refs/remotes/blobs-from-two/*" &&
1369                 git fetch --no-tags two &&
1370
1371                 test_must_fail git push origin refs/remotes/two/another:dst 2>err &&
1372                 test_i18ngrep "error: The destination you" err &&
1373
1374                 test_must_fail git push origin refs/remotes/tags-from-two/my-tag:dst-tag 2>err &&
1375                 test_i18ngrep "error: The destination you" err &&
1376
1377                 test_must_fail git push origin refs/remotes/trees-from-two/my-head-tree:dst-tree 2>err &&
1378                 test_i18ngrep "error: The destination you" err &&
1379
1380                 test_must_fail git push origin refs/remotes/blobs-from-two/my-file-blob:dst-blob 2>err &&
1381                 test_i18ngrep "error: The destination you" err
1382         )
1383 '
1384
1385 test_done