t / t5505-remote.shon commit remote: fix remote set-url usage (c49904e)
   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
  46        setup_repository one &&
  47        setup_repository two &&
  48        (
  49                cd two && git branch another
  50        ) &&
  51        git clone one test
  52
  53'
  54
  55test_expect_success 'remote information for the origin' '
  56(
  57        cd test &&
  58        tokens_match origin "$(git remote)" &&
  59        check_remote_track origin master side &&
  60        check_tracking_branch origin HEAD master side
  61)
  62'
  63
  64test_expect_success 'add another remote' '
  65(
  66        cd test &&
  67        git remote add -f second ../two &&
  68        tokens_match "origin second" "$(git remote)" &&
  69        check_remote_track origin master side &&
  70        check_remote_track second master side another &&
  71        check_tracking_branch second master side another &&
  72        git for-each-ref "--format=%(refname)" refs/remotes |
  73        sed -e "/^refs\/remotes\/origin\//d" \
  74            -e "/^refs\/remotes\/second\//d" >actual &&
  75        >expect &&
  76        test_cmp expect actual
  77)
  78'
  79
  80test_expect_success 'remote forces tracking branches' '
  81(
  82        cd test &&
  83        case `git config remote.second.fetch` in
  84        +*) true ;;
  85         *) false ;;
  86        esac
  87)
  88'
  89
  90test_expect_success 'remove remote' '
  91(
  92        cd test &&
  93        git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/master &&
  94        git remote rm second
  95)
  96'
  97
  98test_expect_success 'remove remote' '
  99(
 100        cd test &&
 101        tokens_match origin "$(git remote)" &&
 102        check_remote_track origin master side &&
 103        git for-each-ref "--format=%(refname)" refs/remotes |
 104        sed -e "/^refs\/remotes\/origin\//d" >actual &&
 105        >expect &&
 106        test_cmp expect actual
 107)
 108'
 109
 110test_expect_success 'remove remote protects non-remote branches' '
 111(
 112        cd test &&
 113        (cat >expect1 <<EOF
 114Note: A non-remote branch was not removed; to delete it, use:
 115  git branch -d master
 116EOF
 117    cat >expect2 <<EOF
 118Note: Non-remote branches were not removed; to delete them, use:
 119  git branch -d foobranch
 120  git branch -d master
 121EOF
 122) &&
 123        git tag footag
 124        git config --add remote.oops.fetch "+refs/*:refs/*" &&
 125        git remote rm oops 2>actual1 &&
 126        git branch foobranch &&
 127        git config --add remote.oops.fetch "+refs/*:refs/*" &&
 128        git remote rm oops 2>actual2 &&
 129        git branch -d foobranch &&
 130        git tag -d footag &&
 131        test_cmp expect1 actual1 &&
 132        test_cmp expect2 actual2
 133)
 134'
 135
 136cat > test/expect << EOF
 137* remote origin
 138  Fetch URL: $(pwd)/one
 139  Push  URL: $(pwd)/one
 140  HEAD branch: master
 141  Remote branches:
 142    master new (next fetch will store in remotes/origin)
 143    side   tracked
 144  Local branches configured for 'git pull':
 145    ahead    merges with remote master
 146    master   merges with remote master
 147    octopus  merges with remote topic-a
 148                and with remote topic-b
 149                and with remote topic-c
 150    rebase  rebases onto remote master
 151  Local refs configured for 'git push':
 152    master pushes to master   (local out of date)
 153    master pushes to upstream (create)
 154* remote two
 155  Fetch URL: ../two
 156  Push  URL: ../three
 157  HEAD branch (remote HEAD is ambiguous, may be one of the following):
 158    another
 159    master
 160  Local refs configured for 'git push':
 161    ahead  forces to master  (fast-forwardable)
 162    master pushes to another (up to date)
 163EOF
 164
 165test_expect_success 'show' '
 166        (cd test &&
 167         git config --add remote.origin.fetch refs/heads/master:refs/heads/upstream &&
 168         git fetch &&
 169         git checkout -b ahead origin/master &&
 170         echo 1 >> file &&
 171         test_tick &&
 172         git commit -m update file &&
 173         git checkout master &&
 174         git branch --track octopus origin/master &&
 175         git branch --track rebase origin/master &&
 176         git branch -d -r origin/master &&
 177         git config --add remote.two.url ../two &&
 178         git config --add remote.two.pushurl ../three &&
 179         git config branch.rebase.rebase true &&
 180         git config branch.octopus.merge "topic-a topic-b topic-c" &&
 181         (cd ../one &&
 182          echo 1 > file &&
 183          test_tick &&
 184          git commit -m update file) &&
 185         git config --add remote.origin.push : &&
 186         git config --add remote.origin.push refs/heads/master:refs/heads/upstream &&
 187         git config --add remote.origin.push +refs/tags/lastbackup &&
 188         git config --add remote.two.push +refs/heads/ahead:refs/heads/master &&
 189         git config --add remote.two.push refs/heads/master:refs/heads/another &&
 190         git remote show origin two > output &&
 191         git branch -d rebase octopus &&
 192         test_cmp expect output)
 193'
 194
 195cat > test/expect << EOF
 196* remote origin
 197  Fetch URL: $(pwd)/one
 198  Push  URL: $(pwd)/one
 199  HEAD branch: (not queried)
 200  Remote branches: (status not queried)
 201    master
 202    side
 203  Local branches configured for 'git pull':
 204    ahead  merges with remote master
 205    master merges with remote master
 206  Local refs configured for 'git push' (status not queried):
 207    (matching)           pushes to (matching)
 208    refs/heads/master    pushes to refs/heads/upstream
 209    refs/tags/lastbackup forces to refs/tags/lastbackup
 210EOF
 211
 212test_expect_success 'show -n' '
 213        (mv one one.unreachable &&
 214         cd test &&
 215         git remote show -n origin > output &&
 216         mv ../one.unreachable ../one &&
 217         test_cmp expect output)
 218'
 219
 220test_expect_success 'prune' '
 221        (cd one &&
 222         git branch -m side side2) &&
 223        (cd test &&
 224         git fetch origin &&
 225         git remote prune origin &&
 226         git rev-parse refs/remotes/origin/side2 &&
 227         test_must_fail git rev-parse refs/remotes/origin/side)
 228'
 229
 230test_expect_success 'set-head --delete' '
 231        (cd test &&
 232         git symbolic-ref refs/remotes/origin/HEAD &&
 233         git remote set-head --delete origin &&
 234         test_must_fail git symbolic-ref refs/remotes/origin/HEAD)
 235'
 236
 237test_expect_success 'set-head --auto' '
 238        (cd test &&
 239         git remote set-head --auto origin &&
 240         echo refs/remotes/origin/master >expect &&
 241         git symbolic-ref refs/remotes/origin/HEAD >output &&
 242         test_cmp expect output
 243        )
 244'
 245
 246cat >test/expect <<EOF
 247error: Multiple remote HEAD branches. Please choose one explicitly with:
 248  git remote set-head two another
 249  git remote set-head two master
 250EOF
 251
 252test_expect_success 'set-head --auto fails w/multiple HEADs' '
 253        (cd test &&
 254         test_must_fail git remote set-head --auto two >output 2>&1 &&
 255        test_cmp expect output)
 256'
 257
 258cat >test/expect <<EOF
 259refs/remotes/origin/side2
 260EOF
 261
 262test_expect_success 'set-head explicit' '
 263        (cd test &&
 264         git remote set-head origin side2 &&
 265         git symbolic-ref refs/remotes/origin/HEAD >output &&
 266         git remote set-head origin master &&
 267         test_cmp expect output)
 268'
 269
 270cat > test/expect << EOF
 271Pruning origin
 272URL: $(pwd)/one
 273 * [would prune] origin/side2
 274EOF
 275
 276test_expect_success 'prune --dry-run' '
 277        (cd one &&
 278         git branch -m side2 side) &&
 279        (cd test &&
 280         git remote prune --dry-run origin > output &&
 281         git rev-parse refs/remotes/origin/side2 &&
 282         test_must_fail git rev-parse refs/remotes/origin/side &&
 283        (cd ../one &&
 284         git branch -m side side2) &&
 285         test_cmp expect output)
 286'
 287
 288test_expect_success 'add --mirror && prune' '
 289        (mkdir mirror &&
 290         cd mirror &&
 291         git init --bare &&
 292         git remote add --mirror -f origin ../one) &&
 293        (cd one &&
 294         git branch -m side2 side) &&
 295        (cd mirror &&
 296         git rev-parse --verify refs/heads/side2 &&
 297         test_must_fail git rev-parse --verify refs/heads/side &&
 298         git fetch origin &&
 299         git remote prune origin &&
 300         test_must_fail git rev-parse --verify refs/heads/side2 &&
 301         git rev-parse --verify refs/heads/side)
 302'
 303
 304test_expect_success 'add alt && prune' '
 305        (mkdir alttst &&
 306         cd alttst &&
 307         git init &&
 308         git remote add -f origin ../one &&
 309         git config remote.alt.url ../one &&
 310         git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*") &&
 311        (cd one &&
 312         git branch -m side side2) &&
 313        (cd alttst &&
 314         git rev-parse --verify refs/remotes/origin/side &&
 315         test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
 316         git fetch alt &&
 317         git remote prune alt &&
 318         test_must_fail git rev-parse --verify refs/remotes/origin/side &&
 319         git rev-parse --verify refs/remotes/origin/side2)
 320'
 321
 322cat > one/expect << EOF
 323  apis/master
 324  apis/side
 325  drosophila/another
 326  drosophila/master
 327  drosophila/side
 328EOF
 329
 330test_expect_success 'update' '
 331
 332        (cd one &&
 333         git remote add drosophila ../two &&
 334         git remote add apis ../mirror &&
 335         git remote update &&
 336         git branch -r > output &&
 337         test_cmp expect output)
 338
 339'
 340
 341cat > one/expect << EOF
 342  drosophila/another
 343  drosophila/master
 344  drosophila/side
 345  manduca/master
 346  manduca/side
 347  megaloprepus/master
 348  megaloprepus/side
 349EOF
 350
 351test_expect_success 'update with arguments' '
 352
 353        (cd one &&
 354         for b in $(git branch -r)
 355         do
 356                git branch -r -d $b || break
 357         done &&
 358         git remote add manduca ../mirror &&
 359         git remote add megaloprepus ../mirror &&
 360         git config remotes.phobaeticus "drosophila megaloprepus" &&
 361         git config remotes.titanus manduca &&
 362         git remote update phobaeticus titanus &&
 363         git branch -r > output &&
 364         test_cmp expect output)
 365
 366'
 367
 368test_expect_success 'update --prune' '
 369
 370        (cd one &&
 371         git branch -m side2 side3) &&
 372        (cd test &&
 373         git remote update --prune &&
 374         (cd ../one && git branch -m side3 side2)
 375         git rev-parse refs/remotes/origin/side3 &&
 376         test_must_fail git rev-parse refs/remotes/origin/side2)
 377'
 378
 379cat > one/expect << EOF
 380  apis/master
 381  apis/side
 382  manduca/master
 383  manduca/side
 384  megaloprepus/master
 385  megaloprepus/side
 386EOF
 387
 388test_expect_success 'update default' '
 389
 390        (cd one &&
 391         for b in $(git branch -r)
 392         do
 393                git branch -r -d $b || break
 394         done &&
 395         git config remote.drosophila.skipDefaultUpdate true &&
 396         git remote update default &&
 397         git branch -r > output &&
 398         test_cmp expect output)
 399
 400'
 401
 402cat > one/expect << EOF
 403  drosophila/another
 404  drosophila/master
 405  drosophila/side
 406EOF
 407
 408test_expect_success 'update default (overridden, with funny whitespace)' '
 409
 410        (cd one &&
 411         for b in $(git branch -r)
 412         do
 413                git branch -r -d $b || break
 414         done &&
 415         git config remotes.default "$(printf "\t drosophila  \n")" &&
 416         git remote update default &&
 417         git branch -r > output &&
 418         test_cmp expect output)
 419
 420'
 421
 422test_expect_success 'update (with remotes.default defined)' '
 423
 424        (cd one &&
 425         for b in $(git branch -r)
 426         do
 427                git branch -r -d $b || break
 428         done &&
 429         git config remotes.default "drosophila" &&
 430         git remote update &&
 431         git branch -r > output &&
 432         test_cmp expect output)
 433
 434'
 435
 436test_expect_success '"remote show" does not show symbolic refs' '
 437
 438        git clone one three &&
 439        (cd three &&
 440         git remote show origin > output &&
 441         ! grep "^ *HEAD$" < output &&
 442         ! grep -i stale < output)
 443
 444'
 445
 446test_expect_success 'reject adding remote with an invalid name' '
 447
 448        test_must_fail git remote add some:url desired-name
 449
 450'
 451
 452# The first three test if the tracking branches are properly renamed,
 453# the last two ones check if the config is updated.
 454
 455test_expect_success 'rename a remote' '
 456
 457        git clone one four &&
 458        (cd four &&
 459         git remote rename origin upstream &&
 460         rmdir .git/refs/remotes/origin &&
 461         test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/master" &&
 462         test "$(git rev-parse upstream/master)" = "$(git rev-parse master)" &&
 463         test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
 464         test "$(git config branch.master.remote)" = "upstream")
 465
 466'
 467
 468cat > remotes_origin << EOF
 469URL: $(pwd)/one
 470Push: refs/heads/master:refs/heads/upstream
 471Pull: refs/heads/master:refs/heads/origin
 472EOF
 473
 474test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
 475        git clone one five &&
 476        origin_url=$(pwd)/one &&
 477        (cd five &&
 478         git remote rm origin &&
 479         mkdir -p .git/remotes &&
 480         cat ../remotes_origin > .git/remotes/origin &&
 481         git remote rename origin origin &&
 482         ! test -f .git/remotes/origin &&
 483         test "$(git config remote.origin.url)" = "$origin_url" &&
 484         test "$(git config remote.origin.push)" = "refs/heads/master:refs/heads/upstream" &&
 485         test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
 486'
 487
 488test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
 489        git clone one six &&
 490        origin_url=$(pwd)/one &&
 491        (cd six &&
 492         git remote rm origin &&
 493         echo "$origin_url" > .git/branches/origin &&
 494         git remote rename origin origin &&
 495         ! test -f .git/branches/origin &&
 496         test "$(git config remote.origin.url)" = "$origin_url" &&
 497         test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
 498'
 499
 500test_expect_success 'remote prune to cause a dangling symref' '
 501        git clone one seven &&
 502        (
 503                cd one &&
 504                git checkout side2 &&
 505                git branch -D master
 506        ) &&
 507        (
 508                cd seven &&
 509                git remote prune origin
 510        ) 2>err &&
 511        grep "has become dangling" err &&
 512
 513        : And the dangling symref will not cause other annoying errors
 514        (
 515                cd seven &&
 516                git branch -a
 517        ) 2>err &&
 518        ! grep "points nowhere" err
 519        (
 520                cd seven &&
 521                test_must_fail git branch nomore origin
 522        ) 2>err &&
 523        grep "dangling symref" err
 524'
 525
 526test_expect_success 'show empty remote' '
 527
 528        test_create_repo empty &&
 529        git clone empty empty-clone &&
 530        (
 531                cd empty-clone &&
 532                git remote show origin
 533        )
 534'
 535
 536test_expect_success 'new remote' '
 537(
 538        git remote add someremote foo &&
 539        echo foo >expect &&
 540        git config --get-all remote.someremote.url >actual &&
 541        cmp expect actual
 542)
 543'
 544
 545test_expect_success 'remote set-url bar' '
 546(
 547        git remote set-url someremote bar &&
 548        echo bar >expect &&
 549        git config --get-all remote.someremote.url >actual &&
 550        cmp expect actual
 551)
 552'
 553
 554test_expect_success 'remote set-url baz bar' '
 555(
 556        git remote set-url someremote baz bar &&
 557        echo baz >expect &&
 558        git config --get-all remote.someremote.url >actual &&
 559        cmp expect actual
 560)
 561'
 562
 563test_expect_success 'remote set-url zot bar' '
 564(
 565        test_must_fail git remote set-url someremote zot bar &&
 566        echo baz >expect &&
 567        git config --get-all remote.someremote.url >actual &&
 568        cmp expect actual
 569)
 570'
 571
 572test_expect_success 'remote set-url --push zot baz' '
 573(
 574        test_must_fail git remote set-url --push someremote zot baz &&
 575        echo "YYY" >expect &&
 576        echo baz >>expect &&
 577        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 578        echo "YYY" >>actual &&
 579        git config --get-all remote.someremote.url >>actual &&
 580        cmp expect actual
 581)
 582'
 583
 584test_expect_success 'remote set-url --push zot' '
 585(
 586        git remote set-url --push someremote zot &&
 587        echo zot >expect &&
 588        echo "YYY" >>expect &&
 589        echo baz >>expect &&
 590        git config --get-all remote.someremote.pushurl >actual &&
 591        echo "YYY" >>actual &&
 592        git config --get-all remote.someremote.url >>actual &&
 593        cmp expect actual
 594)
 595'
 596
 597test_expect_success 'remote set-url --push qux zot' '
 598(
 599        git remote set-url --push someremote qux zot &&
 600        echo qux >expect &&
 601        echo "YYY" >>expect &&
 602        echo baz >>expect &&
 603        git config --get-all remote.someremote.pushurl >actual &&
 604        echo "YYY" >>actual &&
 605        git config --get-all remote.someremote.url >>actual &&
 606        cmp expect actual
 607)
 608'
 609
 610test_expect_success 'remote set-url --push foo qu+x' '
 611(
 612        git remote set-url --push someremote foo qu+x &&
 613        echo foo >expect &&
 614        echo "YYY" >>expect &&
 615        echo baz >>expect &&
 616        git config --get-all remote.someremote.pushurl >actual &&
 617        echo "YYY" >>actual &&
 618        git config --get-all remote.someremote.url >>actual &&
 619        cmp expect actual
 620)
 621'
 622
 623test_expect_success 'remote set-url --push --add aaa' '
 624(
 625        git remote set-url --push --add someremote aaa &&
 626        echo foo >expect &&
 627        echo aaa >>expect &&
 628        echo "YYY" >>expect &&
 629        echo baz >>expect &&
 630        git config --get-all remote.someremote.pushurl >actual &&
 631        echo "YYY" >>actual &&
 632        git config --get-all remote.someremote.url >>actual &&
 633        cmp expect actual
 634)
 635'
 636
 637test_expect_success 'remote set-url --push bar aaa' '
 638(
 639        git remote set-url --push someremote bar aaa &&
 640        echo foo >expect &&
 641        echo bar >>expect &&
 642        echo "YYY" >>expect &&
 643        echo baz >>expect &&
 644        git config --get-all remote.someremote.pushurl >actual &&
 645        echo "YYY" >>actual &&
 646        git config --get-all remote.someremote.url >>actual &&
 647        cmp expect actual
 648)
 649'
 650
 651test_expect_success 'remote set-url --push --delete bar' '
 652(
 653        git remote set-url --push --delete someremote bar &&
 654        echo foo >expect &&
 655        echo "YYY" >>expect &&
 656        echo baz >>expect &&
 657        git config --get-all remote.someremote.pushurl >actual &&
 658        echo "YYY" >>actual &&
 659        git config --get-all remote.someremote.url >>actual &&
 660        cmp expect actual
 661)
 662'
 663
 664test_expect_success 'remote set-url --push --delete foo' '
 665(
 666        git remote set-url --push --delete someremote foo &&
 667        echo "YYY" >expect &&
 668        echo baz >>expect &&
 669        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 670        echo "YYY" >>actual &&
 671        git config --get-all remote.someremote.url >>actual &&
 672        cmp expect actual
 673)
 674'
 675
 676test_expect_success 'remote set-url --add bbb' '
 677(
 678        git remote set-url --add someremote bbb &&
 679        echo "YYY" >expect &&
 680        echo baz >>expect &&
 681        echo bbb >>expect &&
 682        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 683        echo "YYY" >>actual &&
 684        git config --get-all remote.someremote.url >>actual &&
 685        cmp expect actual
 686)
 687'
 688
 689test_expect_success 'remote set-url --delete .*' '
 690(
 691        test_must_fail git remote set-url --delete someremote .* &&
 692        echo "YYY" >expect &&
 693        echo baz >>expect &&
 694        echo bbb >>expect &&
 695        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 696        echo "YYY" >>actual &&
 697        git config --get-all remote.someremote.url >>actual &&
 698        cmp expect actual
 699)
 700'
 701
 702test_expect_success 'remote set-url --delete bbb' '
 703(
 704        git remote set-url --delete someremote bbb &&
 705        echo "YYY" >expect &&
 706        echo baz >>expect &&
 707        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 708        echo "YYY" >>actual &&
 709        git config --get-all remote.someremote.url >>actual &&
 710        cmp expect actual
 711)
 712'
 713
 714test_expect_success 'remote set-url --delete baz' '
 715(
 716        test_must_fail git remote set-url --delete someremote baz &&
 717        echo "YYY" >expect &&
 718        echo baz >>expect &&
 719        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 720        echo "YYY" >>actual &&
 721        git config --get-all remote.someremote.url >>actual &&
 722        cmp expect actual
 723)
 724'
 725
 726test_expect_success 'remote set-url --add ccc' '
 727(
 728        git remote set-url --add someremote ccc &&
 729        echo "YYY" >expect &&
 730        echo baz >>expect &&
 731        echo ccc >>expect &&
 732        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 733        echo "YYY" >>actual &&
 734        git config --get-all remote.someremote.url >>actual &&
 735        cmp expect actual
 736)
 737'
 738
 739test_expect_success 'remote set-url --delete baz' '
 740(
 741        git remote set-url --delete someremote baz &&
 742        echo "YYY" >expect &&
 743        echo ccc >>expect &&
 744        test_must_fail git config --get-all remote.someremote.pushurl >actual &&
 745        echo "YYY" >>actual &&
 746        git config --get-all remote.someremote.url >>actual &&
 747        cmp expect actual
 748)
 749'
 750
 751test_done