1#!/bin/sh
   2test_description='fetching and pushing, with or without wildcard'
   4. ./test-lib.sh
   6D=`pwd`
   8mk_empty () {
  10        rm -fr testrepo &&
  11        mkdir testrepo &&
  12        (
  13                cd testrepo &&
  14                git init &&
  15                mv .git/hooks .git/hooks-disabled
  16        )
  17}
  18mk_test () {
  20        mk_empty &&
  21        (
  22                for ref in "$@"
  23                do
  24                        git push testrepo $the_first_commit:refs/$ref || {
  25                                echo "Oops, push refs/$ref failure"
  26                                exit 1
  27                        }
  28                done &&
  29                cd testrepo &&
  30                for ref in "$@"
  31                do
  32                        r=$(git show-ref -s --verify refs/$ref) &&
  33                        test "z$r" = "z$the_first_commit" || {
  34                                echo "Oops, refs/$ref is wrong"
  35                                exit 1
  36                        }
  37                done &&
  38                git fsck --full
  39        )
  40}
  41mk_child() {
  43        rm -rf "$1" &&
  44        git clone testrepo "$1"
  45}
  46check_push_result () {
  48        (
  49                cd testrepo &&
  50                it="$1" &&
  51                shift
  52                for ref in "$@"
  53                do
  54                        r=$(git show-ref -s --verify refs/$ref) &&
  55                        test "z$r" = "z$it" || {
  56                                echo "Oops, refs/$ref is wrong"
  57                                exit 1
  58                        }
  59                done &&
  60                git fsck --full
  61        )
  62}
  63test_expect_success setup '
  65        : >path1 &&
  67        git add path1 &&
  68        test_tick &&
  69        git commit -a -m repo &&
  70        the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
  71        : >path2 &&
  73        git add path2 &&
  74        test_tick &&
  75        git commit -a -m second &&
  76        the_commit=$(git show-ref -s --verify refs/heads/master)
  77'
  79test_expect_success 'fetch without wildcard' '
  81        mk_empty &&
  82        (
  83                cd testrepo &&
  84                git fetch .. refs/heads/master:refs/remotes/origin/master &&
  85                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
  87                test "z$r" = "z$the_commit" &&
  88                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
  90        )
  91'
  92test_expect_success 'fetch with wildcard' '
  94        mk_empty &&
  95        (
  96                cd testrepo &&
  97                git config remote.up.url .. &&
  98                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
  99                git fetch up &&
 100                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 102                test "z$r" = "z$the_commit" &&
 103                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 105        )
 106'
 107test_expect_success 'fetch with insteadOf' '
 109        mk_empty &&
 110        (
 111                TRASH=$(pwd)/ &&
 112                cd testrepo &&
 113                git config "url.$TRASH.insteadOf" trash/ &&
 114                git config remote.up.url trash/. &&
 115                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
 116                git fetch up &&
 117                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 119                test "z$r" = "z$the_commit" &&
 120                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 122        )
 123'
 124test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
 126        mk_empty &&
 127        (
 128                TRASH=$(pwd)/ &&
 129                cd testrepo &&
 130                git config "url.trash/.pushInsteadOf" "$TRASH" &&
 131                git config remote.up.url "$TRASH." &&
 132                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
 133                git fetch up &&
 134                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 136                test "z$r" = "z$the_commit" &&
 137                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 139        )
 140'
 141test_expect_success 'push without wildcard' '
 143        mk_empty &&
 144        git push testrepo refs/heads/master:refs/remotes/origin/master &&
 146        (
 147                cd testrepo &&
 148                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 149                test "z$r" = "z$the_commit" &&
 150                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 152        )
 153'
 154test_expect_success 'push with wildcard' '
 156        mk_empty &&
 157        git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
 159        (
 160                cd testrepo &&
 161                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 162                test "z$r" = "z$the_commit" &&
 163                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 165        )
 166'
 167test_expect_success 'push with insteadOf' '
 169        mk_empty &&
 170        TRASH="$(pwd)/" &&
 171        git config "url.$TRASH.insteadOf" trash/ &&
 172        git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
 173        (
 174                cd testrepo &&
 175                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 176                test "z$r" = "z$the_commit" &&
 177                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 179        )
 180'
 181test_expect_success 'push with pushInsteadOf' '
 183        mk_empty &&
 184        TRASH="$(pwd)/" &&
 185        git config "url.$TRASH.pushInsteadOf" trash/ &&
 186        git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
 187        (
 188                cd testrepo &&
 189                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 190                test "z$r" = "z$the_commit" &&
 191                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 193        )
 194'
 195test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
 197        mk_empty &&
 198        TRASH="$(pwd)/" &&
 199        git config "url.trash2/.pushInsteadOf" trash/ &&
 200        git config remote.r.url trash/wrong &&
 201        git config remote.r.pushurl "$TRASH/testrepo" &&
 202        git push r refs/heads/master:refs/remotes/origin/master &&
 203        (
 204                cd testrepo &&
 205                r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 206                test "z$r" = "z$the_commit" &&
 207                test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 209        )
 210'
 211test_expect_success 'push with matching heads' '
 213        mk_test heads/master &&
 215        git push testrepo &&
 216        check_push_result $the_commit heads/master
 217'
 219test_expect_success 'push with matching heads on the command line' '
 221        mk_test heads/master &&
 223        git push testrepo : &&
 224        check_push_result $the_commit heads/master
 225'
 227test_expect_success 'failed (non-fast-forward) push with matching heads' '
 229        mk_test heads/master &&
 231        git push testrepo : &&
 232        git commit --amend -massaged &&
 233        test_must_fail git push testrepo &&
 234        check_push_result $the_commit heads/master &&
 235        git reset --hard $the_commit
 236'
 238test_expect_success 'push --force with matching heads' '
 240        mk_test heads/master &&
 242        git push testrepo : &&
 243        git commit --amend -massaged &&
 244        git push --force testrepo &&
 245        ! check_push_result $the_commit heads/master &&
 246        git reset --hard $the_commit
 247'
 249test_expect_success 'push with matching heads and forced update' '
 251        mk_test heads/master &&
 253        git push testrepo : &&
 254        git commit --amend -massaged &&
 255        git push testrepo +: &&
 256        ! check_push_result $the_commit heads/master &&
 257        git reset --hard $the_commit
 258'
 260test_expect_success 'push with no ambiguity (1)' '
 262        mk_test heads/master &&
 264        git push testrepo master:master &&
 265        check_push_result $the_commit heads/master
 266'
 268test_expect_success 'push with no ambiguity (2)' '
 270        mk_test remotes/origin/master &&
 272        git push testrepo master:origin/master &&
 273        check_push_result $the_commit remotes/origin/master
 274'
 276test_expect_success 'push with colon-less refspec, no ambiguity' '
 278        mk_test heads/master heads/t/master &&
 280        git branch -f t/master master &&
 281        git push testrepo master &&
 282        check_push_result $the_commit heads/master &&
 283        check_push_result $the_first_commit heads/t/master
 284'
 286test_expect_success 'push with weak ambiguity (1)' '
 288        mk_test heads/master remotes/origin/master &&
 290        git push testrepo master:master &&
 291        check_push_result $the_commit heads/master &&
 292        check_push_result $the_first_commit remotes/origin/master
 293'
 295test_expect_success 'push with weak ambiguity (2)' '
 297        mk_test heads/master remotes/origin/master remotes/another/master &&
 299        git push testrepo master:master &&
 300        check_push_result $the_commit heads/master &&
 301        check_push_result $the_first_commit remotes/origin/master remotes/another/master
 302'
 304test_expect_success 'push with ambiguity' '
 306        mk_test heads/frotz tags/frotz &&
 308        if git push testrepo master:frotz
 309        then
 310                echo "Oops, should have failed"
 311                false
 312        else
 313                check_push_result $the_first_commit heads/frotz tags/frotz
 314        fi
 315'
 317test_expect_success 'push with colon-less refspec (1)' '
 319        mk_test heads/frotz tags/frotz &&
 321        git branch -f frotz master &&
 322        git push testrepo frotz &&
 323        check_push_result $the_commit heads/frotz &&
 324        check_push_result $the_first_commit tags/frotz
 325'
 327test_expect_success 'push with colon-less refspec (2)' '
 329        mk_test heads/frotz tags/frotz &&
 331        if git show-ref --verify -q refs/heads/frotz
 332        then
 333                git branch -D frotz
 334        fi &&
 335        git tag -f frotz &&
 336        git push testrepo frotz &&
 337        check_push_result $the_commit tags/frotz &&
 338        check_push_result $the_first_commit heads/frotz
 339'
 341test_expect_success 'push with colon-less refspec (3)' '
 343        mk_test &&
 345        if git show-ref --verify -q refs/tags/frotz
 346        then
 347                git tag -d frotz
 348        fi &&
 349        git branch -f frotz master &&
 350        git push testrepo frotz &&
 351        check_push_result $the_commit heads/frotz &&
 352        test 1 = $( cd testrepo && git show-ref | wc -l )
 353'
 354test_expect_success 'push with colon-less refspec (4)' '
 356        mk_test &&
 358        if git show-ref --verify -q refs/heads/frotz
 359        then
 360                git branch -D frotz
 361        fi &&
 362        git tag -f frotz &&
 363        git push testrepo frotz &&
 364        check_push_result $the_commit tags/frotz &&
 365        test 1 = $( cd testrepo && git show-ref | wc -l )
 366'
 368test_expect_success 'push head with non-existant, incomplete dest' '
 370        mk_test &&
 372        git push testrepo master:branch &&
 373        check_push_result $the_commit heads/branch
 374'
 376test_expect_success 'push tag with non-existant, incomplete dest' '
 378        mk_test &&
 380        git tag -f v1.0 &&
 381        git push testrepo v1.0:tag &&
 382        check_push_result $the_commit tags/tag
 383'
 385test_expect_success 'push sha1 with non-existant, incomplete dest' '
 387        mk_test &&
 389        test_must_fail git push testrepo `git rev-parse master`:foo
 390'
 392test_expect_success 'push ref expression with non-existant, incomplete dest' '
 394        mk_test &&
 396        test_must_fail git push testrepo master^:branch
 397'
 399test_expect_success 'push with HEAD' '
 401        mk_test heads/master &&
 403        git checkout master &&
 404        git push testrepo HEAD &&
 405        check_push_result $the_commit heads/master
 406'
 408test_expect_success 'push with HEAD nonexisting at remote' '
 410        mk_test heads/master &&
 412        git checkout -b local master &&
 413        git push testrepo HEAD &&
 414        check_push_result $the_commit heads/local
 415'
 416test_expect_success 'push with +HEAD' '
 418        mk_test heads/master &&
 420        git checkout master &&
 421        git branch -D local &&
 422        git checkout -b local &&
 423        git push testrepo master local &&
 424        check_push_result $the_commit heads/master &&
 425        check_push_result $the_commit heads/local &&
 426        # Without force rewinding should fail
 428        git reset --hard HEAD^ &&
 429        test_must_fail git push testrepo HEAD &&
 430        check_push_result $the_commit heads/local &&
 431        # With force rewinding should succeed
 433        git push testrepo +HEAD &&
 434        check_push_result $the_first_commit heads/local
 435'
 437test_expect_success 'push HEAD with non-existant, incomplete dest' '
 439        mk_test &&
 441        git checkout master &&
 442        git push testrepo HEAD:branch &&
 443        check_push_result $the_commit heads/branch
 444'
 446test_expect_success 'push with config remote.*.push = HEAD' '
 448        mk_test heads/local &&
 450        git checkout master &&
 451        git branch -f local $the_commit &&
 452        (
 453                cd testrepo &&
 454                git checkout local &&
 455                git reset --hard $the_first_commit
 456        ) &&
 457        git config remote.there.url testrepo &&
 458        git config remote.there.push HEAD &&
 459        git config branch.master.remote there &&
 460        git push &&
 461        check_push_result $the_commit heads/master &&
 462        check_push_result $the_first_commit heads/local
 463'
 464# clean up the cruft left with the previous one
 466git config --remove-section remote.there
 467git config --remove-section branch.master
 468test_expect_success 'push with config remote.*.pushurl' '
 470        mk_test heads/master &&
 472        git checkout master &&
 473        git config remote.there.url test2repo &&
 474        git config remote.there.pushurl testrepo &&
 475        git push there &&
 476        check_push_result $the_commit heads/master
 477'
 478# clean up the cruft left with the previous one
 480git config --remove-section remote.there
 481test_expect_success 'push with dry-run' '
 483        mk_test heads/master &&
 485        (cd testrepo &&
 486         old_commit=$(git show-ref -s --verify refs/heads/master)) &&
 487        git push --dry-run testrepo &&
 488        check_push_result $old_commit heads/master
 489'
 490test_expect_success 'push updates local refs' '
 492        mk_test heads/master &&
 494        mk_child child &&
 495        (cd child &&
 496                git pull .. master &&
 497                git push &&
 498        test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
 499'
 501test_expect_success 'push updates up-to-date local refs' '
 503        mk_test heads/master &&
 505        mk_child child1 &&
 506        mk_child child2 &&
 507        (cd child1 && git pull .. master && git push) &&
 508        (cd child2 &&
 509                git pull ../child1 master &&
 510                git push &&
 511        test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
 512'
 514test_expect_success 'push preserves up-to-date packed refs' '
 516        mk_test heads/master &&
 518        mk_child child &&
 519        (cd child &&
 520                git push &&
 521        ! test -f .git/refs/remotes/origin/master)
 522'
 524test_expect_success 'push does not update local refs on failure' '
 526        mk_test heads/master &&
 528        mk_child child &&
 529        mkdir testrepo/.git/hooks &&
 530        echo exit 1 >testrepo/.git/hooks/pre-receive &&
 531        chmod +x testrepo/.git/hooks/pre-receive &&
 532        (cd child &&
 533                git pull .. master
 534                test_must_fail git push &&
 535                test $(git rev-parse master) != \
 536                        $(git rev-parse remotes/origin/master))
 537'
 539test_expect_success 'allow deleting an invalid remote ref' '
 541        mk_test heads/master &&
 543        rm -f testrepo/.git/objects/??/* &&
 544        git push testrepo :refs/heads/master &&
 545        (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
 546'
 548test_expect_success 'warn on push to HEAD of non-bare repository' '
 550        mk_test heads/master
 551        (cd testrepo &&
 552                git checkout master &&
 553                git config receive.denyCurrentBranch warn) &&
 554        git push testrepo master 2>stderr &&
 555        grep "warning: updating the current branch" stderr
 556'
 557test_expect_success 'deny push to HEAD of non-bare repository' '
 559        mk_test heads/master
 560        (cd testrepo &&
 561                git checkout master &&
 562                git config receive.denyCurrentBranch true) &&
 563        test_must_fail git push testrepo master
 564'
 565test_expect_success 'allow push to HEAD of bare repository (bare)' '
 567        mk_test heads/master
 568        (cd testrepo &&
 569                git checkout master &&
 570                git config receive.denyCurrentBranch true &&
 571                git config core.bare true) &&
 572        git push testrepo master 2>stderr &&
 573        ! grep "warning: updating the current branch" stderr
 574'
 575test_expect_success 'allow push to HEAD of non-bare repository (config)' '
 577        mk_test heads/master
 578        (cd testrepo &&
 579                git checkout master &&
 580                git config receive.denyCurrentBranch false
 581        ) &&
 582        git push testrepo master 2>stderr &&
 583        ! grep "warning: updating the current branch" stderr
 584'
 585test_expect_success 'fetch with branches' '
 587        mk_empty &&
 588        git branch second $the_first_commit &&
 589        git checkout second &&
 590        echo ".." > testrepo/.git/branches/branch1 &&
 591        (cd testrepo &&
 592                git fetch branch1 &&
 593                r=$(git show-ref -s --verify refs/heads/branch1) &&
 594                test "z$r" = "z$the_commit" &&
 595                test 1 = $(git for-each-ref refs/heads | wc -l)
 596        ) &&
 597        git checkout master
 598'
 599test_expect_success 'fetch with branches containing #' '
 601        mk_empty &&
 602        echo "..#second" > testrepo/.git/branches/branch2 &&
 603        (cd testrepo &&
 604                git fetch branch2 &&
 605                r=$(git show-ref -s --verify refs/heads/branch2) &&
 606                test "z$r" = "z$the_first_commit" &&
 607                test 1 = $(git for-each-ref refs/heads | wc -l)
 608        ) &&
 609        git checkout master
 610'
 611test_expect_success 'push with branches' '
 613        mk_empty &&
 614        git checkout second &&
 615        echo "testrepo" > .git/branches/branch1 &&
 616        git push branch1 &&
 617        (cd testrepo &&
 618                r=$(git show-ref -s --verify refs/heads/master) &&
 619                test "z$r" = "z$the_first_commit" &&
 620                test 1 = $(git for-each-ref refs/heads | wc -l)
 621        )
 622'
 623test_expect_success 'push with branches containing #' '
 625        mk_empty &&
 626        echo "testrepo#branch3" > .git/branches/branch2 &&
 627        git push branch2 &&
 628        (cd testrepo &&
 629                r=$(git show-ref -s --verify refs/heads/branch3) &&
 630                test "z$r" = "z$the_first_commit" &&
 631                test 1 = $(git for-each-ref refs/heads | wc -l)
 632        ) &&
 633        git checkout master
 634'
 635test_done