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