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