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