797b537d8aa0be522c6530ceb000b704fc5a95f9
   1#!/bin/sh
   2
   3test_description='Basic fetch/push functionality.
   4
   5This test checks the following functionality:
   6
   7* command-line syntax
   8* refspecs
   9* fast-forward detection, and overriding it
  10* configuration
  11* hooks
  12* --porcelain output format
  13* hiderefs
  14'
  15
  16. ./test-lib.sh
  17
  18D=`pwd`
  19
  20mk_empty () {
  21        repo_name="$1"
  22        rm -fr "$repo_name" &&
  23        mkdir "$repo_name" &&
  24        (
  25                cd "$repo_name" &&
  26                git init &&
  27                git config receive.denyCurrentBranch warn &&
  28                mv .git/hooks .git/hooks-disabled
  29        )
  30}
  31
  32mk_test () {
  33        repo_name="$1"
  34        shift
  35
  36        mk_empty "$repo_name" &&
  37        (
  38                for ref in "$@"
  39                do
  40                        git push "$repo_name" $the_first_commit:refs/$ref ||
  41                        exit
  42                done &&
  43                cd "$repo_name" &&
  44                for ref in "$@"
  45                do
  46                        echo "$the_first_commit" >expect &&
  47                        git show-ref -s --verify refs/$ref >actual &&
  48                        test_cmp expect actual ||
  49                        exit
  50                done &&
  51                git fsck --full
  52        )
  53}
  54
  55mk_test_with_hooks() {
  56        repo_name=$1
  57        mk_test "$@" &&
  58        (
  59                cd "$repo_name" &&
  60                mkdir .git/hooks &&
  61                cd .git/hooks &&
  62
  63                cat >pre-receive <<-'EOF' &&
  64                #!/bin/sh
  65                cat - >>pre-receive.actual
  66                EOF
  67
  68                cat >update <<-'EOF' &&
  69                #!/bin/sh
  70                printf "%s %s %s\n" "$@" >>update.actual
  71                EOF
  72
  73                cat >post-receive <<-'EOF' &&
  74                #!/bin/sh
  75                cat - >>post-receive.actual
  76                EOF
  77
  78                cat >post-update <<-'EOF' &&
  79                #!/bin/sh
  80                for ref in "$@"
  81                do
  82                        printf "%s\n" "$ref" >>post-update.actual
  83                done
  84                EOF
  85
  86                chmod +x pre-receive update post-receive post-update
  87        )
  88}
  89
  90mk_child() {
  91        rm -rf "$2" &&
  92        git clone "$1" "$2"
  93}
  94
  95check_push_result () {
  96        repo_name="$1"
  97        shift
  98
  99        (
 100                cd "$repo_name" &&
 101                echo "$1" >expect &&
 102                shift &&
 103                for ref in "$@"
 104                do
 105                        git show-ref -s --verify refs/$ref >actual &&
 106                        test_cmp expect actual ||
 107                        exit
 108                done &&
 109                git fsck --full
 110        )
 111}
 112
 113test_expect_success setup '
 114
 115        >path1 &&
 116        git add path1 &&
 117        test_tick &&
 118        git commit -a -m repo &&
 119        the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
 120
 121        >path2 &&
 122        git add path2 &&
 123        test_tick &&
 124        git commit -a -m second &&
 125        the_commit=$(git show-ref -s --verify refs/heads/master)
 126
 127'
 128
 129test_expect_success 'fetch without wildcard' '
 130        mk_empty testrepo &&
 131        (
 132                cd testrepo &&
 133                git fetch .. refs/heads/master:refs/remotes/origin/master &&
 134
 135                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 136                git for-each-ref refs/remotes/origin >actual &&
 137                test_cmp expect actual
 138        )
 139'
 140
 141test_expect_success 'fetch with wildcard' '
 142        mk_empty testrepo &&
 143        (
 144                cd testrepo &&
 145                git config remote.up.url .. &&
 146                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
 147                git fetch up &&
 148
 149                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 150                git for-each-ref refs/remotes/origin >actual &&
 151                test_cmp expect actual
 152        )
 153'
 154
 155test_expect_success 'fetch with insteadOf' '
 156        mk_empty testrepo &&
 157        (
 158                TRASH=$(pwd)/ &&
 159                cd testrepo &&
 160                git config "url.$TRASH.insteadOf" trash/ &&
 161                git config remote.up.url trash/. &&
 162                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
 163                git fetch up &&
 164
 165                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 166                git for-each-ref refs/remotes/origin >actual &&
 167                test_cmp expect actual
 168        )
 169'
 170
 171test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
 172        mk_empty testrepo &&
 173        (
 174                TRASH=$(pwd)/ &&
 175                cd testrepo &&
 176                git config "url.trash/.pushInsteadOf" "$TRASH" &&
 177                git config remote.up.url "$TRASH." &&
 178                git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
 179                git fetch up &&
 180
 181                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 182                git for-each-ref refs/remotes/origin >actual &&
 183                test_cmp expect actual
 184        )
 185'
 186
 187test_expect_success 'push without wildcard' '
 188        mk_empty testrepo &&
 189
 190        git push testrepo refs/heads/master:refs/remotes/origin/master &&
 191        (
 192                cd testrepo &&
 193                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 194                git for-each-ref refs/remotes/origin >actual &&
 195                test_cmp expect actual
 196        )
 197'
 198
 199test_expect_success 'push with wildcard' '
 200        mk_empty testrepo &&
 201
 202        git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
 203        (
 204                cd testrepo &&
 205                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 206                git for-each-ref refs/remotes/origin >actual &&
 207                test_cmp expect actual
 208        )
 209'
 210
 211test_expect_success 'push with insteadOf' '
 212        mk_empty testrepo &&
 213        TRASH="$(pwd)/" &&
 214        test_config "url.$TRASH.insteadOf" trash/ &&
 215        git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
 216        (
 217                cd testrepo &&
 218                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 219                git for-each-ref refs/remotes/origin >actual &&
 220                test_cmp expect actual
 221        )
 222'
 223
 224test_expect_success 'push with pushInsteadOf' '
 225        mk_empty testrepo &&
 226        TRASH="$(pwd)/" &&
 227        test_config "url.$TRASH.pushInsteadOf" trash/ &&
 228        git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
 229        (
 230                cd testrepo &&
 231                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 232                git for-each-ref refs/remotes/origin >actual &&
 233                test_cmp expect actual
 234        )
 235'
 236
 237test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
 238        mk_empty testrepo &&
 239        TRASH="$(pwd)/" &&
 240        test_config "url.trash2/.pushInsteadOf" trash/ &&
 241        test_config remote.r.url trash/wrong &&
 242        test_config remote.r.pushurl "$TRASH/testrepo" &&
 243        git push r refs/heads/master:refs/remotes/origin/master &&
 244        (
 245                cd testrepo &&
 246                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 247                git for-each-ref refs/remotes/origin >actual &&
 248                test_cmp expect actual
 249        )
 250'
 251
 252test_expect_success 'push with matching heads' '
 253
 254        mk_test testrepo heads/master &&
 255        git push testrepo &&
 256        check_push_result testrepo $the_commit heads/master
 257
 258'
 259
 260test_expect_success 'push with matching heads on the command line' '
 261
 262        mk_test testrepo heads/master &&
 263        git push testrepo : &&
 264        check_push_result testrepo $the_commit heads/master
 265
 266'
 267
 268test_expect_success 'failed (non-fast-forward) push with matching heads' '
 269
 270        mk_test testrepo heads/master &&
 271        git push testrepo : &&
 272        git commit --amend -massaged &&
 273        test_must_fail git push testrepo &&
 274        check_push_result testrepo $the_commit heads/master &&
 275        git reset --hard $the_commit
 276
 277'
 278
 279test_expect_success 'push --force with matching heads' '
 280
 281        mk_test testrepo heads/master &&
 282        git push testrepo : &&
 283        git commit --amend -massaged &&
 284        git push --force testrepo &&
 285        ! check_push_result testrepo $the_commit heads/master &&
 286        git reset --hard $the_commit
 287
 288'
 289
 290test_expect_success 'push with matching heads and forced update' '
 291
 292        mk_test testrepo heads/master &&
 293        git push testrepo : &&
 294        git commit --amend -massaged &&
 295        git push testrepo +: &&
 296        ! check_push_result testrepo $the_commit heads/master &&
 297        git reset --hard $the_commit
 298
 299'
 300
 301test_expect_success 'push with no ambiguity (1)' '
 302
 303        mk_test testrepo heads/master &&
 304        git push testrepo master:master &&
 305        check_push_result testrepo $the_commit heads/master
 306
 307'
 308
 309test_expect_success 'push with no ambiguity (2)' '
 310
 311        mk_test testrepo remotes/origin/master &&
 312        git push testrepo master:origin/master &&
 313        check_push_result testrepo $the_commit remotes/origin/master
 314
 315'
 316
 317test_expect_success 'push with colon-less refspec, no ambiguity' '
 318
 319        mk_test testrepo heads/master heads/t/master &&
 320        git branch -f t/master master &&
 321        git push testrepo master &&
 322        check_push_result testrepo $the_commit heads/master &&
 323        check_push_result testrepo $the_first_commit heads/t/master
 324
 325'
 326
 327test_expect_success 'push with weak ambiguity (1)' '
 328
 329        mk_test testrepo heads/master remotes/origin/master &&
 330        git push testrepo master:master &&
 331        check_push_result testrepo $the_commit heads/master &&
 332        check_push_result testrepo $the_first_commit remotes/origin/master
 333
 334'
 335
 336test_expect_success 'push with weak ambiguity (2)' '
 337
 338        mk_test testrepo heads/master remotes/origin/master remotes/another/master &&
 339        git push testrepo master:master &&
 340        check_push_result testrepo $the_commit heads/master &&
 341        check_push_result testrepo $the_first_commit remotes/origin/master remotes/another/master
 342
 343'
 344
 345test_expect_success 'push with ambiguity' '
 346
 347        mk_test testrepo heads/frotz tags/frotz &&
 348        test_must_fail git push testrepo master:frotz &&
 349        check_push_result testrepo $the_first_commit heads/frotz tags/frotz
 350
 351'
 352
 353test_expect_success 'push with colon-less refspec (1)' '
 354
 355        mk_test testrepo heads/frotz tags/frotz &&
 356        git branch -f frotz master &&
 357        git push testrepo frotz &&
 358        check_push_result testrepo $the_commit heads/frotz &&
 359        check_push_result testrepo $the_first_commit tags/frotz
 360
 361'
 362
 363test_expect_success 'push with colon-less refspec (2)' '
 364
 365        mk_test testrepo heads/frotz tags/frotz &&
 366        if git show-ref --verify -q refs/heads/frotz
 367        then
 368                git branch -D frotz
 369        fi &&
 370        git tag -f frotz &&
 371        git push -f testrepo frotz &&
 372        check_push_result testrepo $the_commit tags/frotz &&
 373        check_push_result testrepo $the_first_commit heads/frotz
 374
 375'
 376
 377test_expect_success 'push with colon-less refspec (3)' '
 378
 379        mk_test testrepo &&
 380        if git show-ref --verify -q refs/tags/frotz
 381        then
 382                git tag -d frotz
 383        fi &&
 384        git branch -f frotz master &&
 385        git push testrepo frotz &&
 386        check_push_result testrepo $the_commit heads/frotz &&
 387        test 1 = $( cd testrepo && git show-ref | wc -l )
 388'
 389
 390test_expect_success 'push with colon-less refspec (4)' '
 391
 392        mk_test testrepo &&
 393        if git show-ref --verify -q refs/heads/frotz
 394        then
 395                git branch -D frotz
 396        fi &&
 397        git tag -f frotz &&
 398        git push testrepo frotz &&
 399        check_push_result testrepo $the_commit tags/frotz &&
 400        test 1 = $( cd testrepo && git show-ref | wc -l )
 401
 402'
 403
 404test_expect_success 'push head with non-existent, incomplete dest' '
 405
 406        mk_test testrepo &&
 407        git push testrepo master:branch &&
 408        check_push_result testrepo $the_commit heads/branch
 409
 410'
 411
 412test_expect_success 'push tag with non-existent, incomplete dest' '
 413
 414        mk_test testrepo &&
 415        git tag -f v1.0 &&
 416        git push testrepo v1.0:tag &&
 417        check_push_result testrepo $the_commit tags/tag
 418
 419'
 420
 421test_expect_success 'push sha1 with non-existent, incomplete dest' '
 422
 423        mk_test testrepo &&
 424        test_must_fail git push testrepo `git rev-parse master`:foo
 425
 426'
 427
 428test_expect_success 'push ref expression with non-existent, incomplete dest' '
 429
 430        mk_test testrepo &&
 431        test_must_fail git push testrepo master^:branch
 432
 433'
 434
 435test_expect_success 'push with HEAD' '
 436
 437        mk_test testrepo heads/master &&
 438        git checkout master &&
 439        git push testrepo HEAD &&
 440        check_push_result testrepo $the_commit heads/master
 441
 442'
 443
 444test_expect_success 'push with HEAD nonexisting at remote' '
 445
 446        mk_test testrepo heads/master &&
 447        git checkout -b local master &&
 448        git push testrepo HEAD &&
 449        check_push_result testrepo $the_commit heads/local
 450'
 451
 452test_expect_success 'push with +HEAD' '
 453
 454        mk_test testrepo heads/master &&
 455        git checkout master &&
 456        git branch -D local &&
 457        git checkout -b local &&
 458        git push testrepo master local &&
 459        check_push_result testrepo $the_commit heads/master &&
 460        check_push_result testrepo $the_commit heads/local &&
 461
 462        # Without force rewinding should fail
 463        git reset --hard HEAD^ &&
 464        test_must_fail git push testrepo HEAD &&
 465        check_push_result testrepo $the_commit heads/local &&
 466
 467        # With force rewinding should succeed
 468        git push testrepo +HEAD &&
 469        check_push_result testrepo $the_first_commit heads/local
 470
 471'
 472
 473test_expect_success 'push HEAD with non-existent, incomplete dest' '
 474
 475        mk_test testrepo &&
 476        git checkout master &&
 477        git push testrepo HEAD:branch &&
 478        check_push_result testrepo $the_commit heads/branch
 479
 480'
 481
 482test_expect_success 'push with config remote.*.push = HEAD' '
 483
 484        mk_test testrepo heads/local &&
 485        git checkout master &&
 486        git branch -f local $the_commit &&
 487        (
 488                cd testrepo &&
 489                git checkout local &&
 490                git reset --hard $the_first_commit
 491        ) &&
 492        test_config remote.there.url testrepo &&
 493        test_config remote.there.push HEAD &&
 494        test_config branch.master.remote there &&
 495        git push &&
 496        check_push_result testrepo $the_commit heads/master &&
 497        check_push_result testrepo $the_first_commit heads/local
 498'
 499
 500test_expect_success 'push with remote.pushdefault' '
 501        mk_test up_repo heads/master &&
 502        mk_test down_repo heads/master &&
 503        test_config remote.up.url up_repo &&
 504        test_config remote.down.url down_repo &&
 505        test_config branch.master.remote up &&
 506        test_config remote.pushdefault down &&
 507        git push &&
 508        check_push_result up_repo $the_first_commit heads/master &&
 509        check_push_result down_repo $the_commit heads/master
 510'
 511
 512test_expect_success 'push with config remote.*.pushurl' '
 513
 514        mk_test testrepo heads/master &&
 515        git checkout master &&
 516        test_config remote.there.url test2repo &&
 517        test_config remote.there.pushurl testrepo &&
 518        git push there &&
 519        check_push_result testrepo $the_commit heads/master
 520'
 521
 522test_expect_success 'push with dry-run' '
 523
 524        mk_test testrepo heads/master &&
 525        (
 526                cd testrepo &&
 527                old_commit=$(git show-ref -s --verify refs/heads/master)
 528        ) &&
 529        git push --dry-run testrepo &&
 530        check_push_result testrepo $old_commit heads/master
 531'
 532
 533test_expect_success 'push updates local refs' '
 534
 535        mk_test testrepo heads/master &&
 536        mk_child testrepo child &&
 537        (
 538                cd child &&
 539                git pull .. master &&
 540                git push &&
 541                test $(git rev-parse master) = \
 542                        $(git rev-parse remotes/origin/master)
 543        )
 544
 545'
 546
 547test_expect_success 'push updates up-to-date local refs' '
 548
 549        mk_test testrepo heads/master &&
 550        mk_child testrepo child1 &&
 551        mk_child testrepo child2 &&
 552        (cd child1 && git pull .. master && git push) &&
 553        (
 554                cd child2 &&
 555                git pull ../child1 master &&
 556                git push &&
 557                test $(git rev-parse master) = \
 558                        $(git rev-parse remotes/origin/master)
 559        )
 560
 561'
 562
 563test_expect_success 'push preserves up-to-date packed refs' '
 564
 565        mk_test testrepo heads/master &&
 566        mk_child testrepo child &&
 567        (
 568                cd child &&
 569                git push &&
 570                ! test -f .git/refs/remotes/origin/master
 571        )
 572
 573'
 574
 575test_expect_success 'push does not update local refs on failure' '
 576
 577        mk_test testrepo heads/master &&
 578        mk_child testrepo child &&
 579        mkdir testrepo/.git/hooks &&
 580        echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
 581        chmod +x testrepo/.git/hooks/pre-receive &&
 582        (
 583                cd child &&
 584                git pull .. master
 585                test_must_fail git push &&
 586                test $(git rev-parse master) != \
 587                        $(git rev-parse remotes/origin/master)
 588        )
 589
 590'
 591
 592test_expect_success 'allow deleting an invalid remote ref' '
 593
 594        mk_test testrepo heads/master &&
 595        rm -f testrepo/.git/objects/??/* &&
 596        git push testrepo :refs/heads/master &&
 597        (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
 598
 599'
 600
 601test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
 602        mk_test_with_hooks testrepo heads/master heads/next &&
 603        orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
 604        newmaster=$(git show-ref -s --verify refs/heads/master) &&
 605        orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
 606        newnext=$_z40 &&
 607        git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
 608        (
 609                cd testrepo/.git &&
 610                cat >pre-receive.expect <<-EOF &&
 611                $orgmaster $newmaster refs/heads/master
 612                $orgnext $newnext refs/heads/next
 613                EOF
 614
 615                cat >update.expect <<-EOF &&
 616                refs/heads/master $orgmaster $newmaster
 617                refs/heads/next $orgnext $newnext
 618                EOF
 619
 620                cat >post-receive.expect <<-EOF &&
 621                $orgmaster $newmaster refs/heads/master
 622                $orgnext $newnext refs/heads/next
 623                EOF
 624
 625                cat >post-update.expect <<-EOF &&
 626                refs/heads/master
 627                refs/heads/next
 628                EOF
 629
 630                test_cmp pre-receive.expect pre-receive.actual &&
 631                test_cmp update.expect update.actual &&
 632                test_cmp post-receive.expect post-receive.actual &&
 633                test_cmp post-update.expect post-update.actual
 634        )
 635'
 636
 637test_expect_success 'deleting dangling ref triggers hooks with correct args' '
 638        mk_test_with_hooks testrepo heads/master &&
 639        rm -f testrepo/.git/objects/??/* &&
 640        git push testrepo :refs/heads/master &&
 641        (
 642                cd testrepo/.git &&
 643                cat >pre-receive.expect <<-EOF &&
 644                $_z40 $_z40 refs/heads/master
 645                EOF
 646
 647                cat >update.expect <<-EOF &&
 648                refs/heads/master $_z40 $_z40
 649                EOF
 650
 651                cat >post-receive.expect <<-EOF &&
 652                $_z40 $_z40 refs/heads/master
 653                EOF
 654
 655                cat >post-update.expect <<-EOF &&
 656                refs/heads/master
 657                EOF
 658
 659                test_cmp pre-receive.expect pre-receive.actual &&
 660                test_cmp update.expect update.actual &&
 661                test_cmp post-receive.expect post-receive.actual &&
 662                test_cmp post-update.expect post-update.actual
 663        )
 664'
 665
 666test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
 667        mk_test_with_hooks testrepo heads/master &&
 668        orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
 669        newmaster=$(git show-ref -s --verify refs/heads/master) &&
 670        git push testrepo master :refs/heads/nonexistent &&
 671        (
 672                cd testrepo/.git &&
 673                cat >pre-receive.expect <<-EOF &&
 674                $orgmaster $newmaster refs/heads/master
 675                $_z40 $_z40 refs/heads/nonexistent
 676                EOF
 677
 678                cat >update.expect <<-EOF &&
 679                refs/heads/master $orgmaster $newmaster
 680                refs/heads/nonexistent $_z40 $_z40
 681                EOF
 682
 683                cat >post-receive.expect <<-EOF &&
 684                $orgmaster $newmaster refs/heads/master
 685                EOF
 686
 687                cat >post-update.expect <<-EOF &&
 688                refs/heads/master
 689                EOF
 690
 691                test_cmp pre-receive.expect pre-receive.actual &&
 692                test_cmp update.expect update.actual &&
 693                test_cmp post-receive.expect post-receive.actual &&
 694                test_cmp post-update.expect post-update.actual
 695        )
 696'
 697
 698test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
 699        mk_test_with_hooks testrepo heads/master &&
 700        git push testrepo :refs/heads/nonexistent &&
 701        (
 702                cd testrepo/.git &&
 703                cat >pre-receive.expect <<-EOF &&
 704                $_z40 $_z40 refs/heads/nonexistent
 705                EOF
 706
 707                cat >update.expect <<-EOF &&
 708                refs/heads/nonexistent $_z40 $_z40
 709                EOF
 710
 711                test_cmp pre-receive.expect pre-receive.actual &&
 712                test_cmp update.expect update.actual &&
 713                test_path_is_missing post-receive.actual &&
 714                test_path_is_missing post-update.actual
 715        )
 716'
 717
 718test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
 719        mk_test_with_hooks testrepo heads/master heads/next heads/pu &&
 720        orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
 721        newmaster=$(git show-ref -s --verify refs/heads/master) &&
 722        orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
 723        newnext=$_z40 &&
 724        orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
 725        newpu=$(git show-ref -s --verify refs/heads/master) &&
 726        git push testrepo refs/heads/master:refs/heads/master \
 727            refs/heads/master:refs/heads/pu :refs/heads/next \
 728            :refs/heads/nonexistent &&
 729        (
 730                cd testrepo/.git &&
 731                cat >pre-receive.expect <<-EOF &&
 732                $orgmaster $newmaster refs/heads/master
 733                $orgnext $newnext refs/heads/next
 734                $orgpu $newpu refs/heads/pu
 735                $_z40 $_z40 refs/heads/nonexistent
 736                EOF
 737
 738                cat >update.expect <<-EOF &&
 739                refs/heads/master $orgmaster $newmaster
 740                refs/heads/next $orgnext $newnext
 741                refs/heads/pu $orgpu $newpu
 742                refs/heads/nonexistent $_z40 $_z40
 743                EOF
 744
 745                cat >post-receive.expect <<-EOF &&
 746                $orgmaster $newmaster refs/heads/master
 747                $orgnext $newnext refs/heads/next
 748                $orgpu $newpu refs/heads/pu
 749                EOF
 750
 751                cat >post-update.expect <<-EOF &&
 752                refs/heads/master
 753                refs/heads/next
 754                refs/heads/pu
 755                EOF
 756
 757                test_cmp pre-receive.expect pre-receive.actual &&
 758                test_cmp update.expect update.actual &&
 759                test_cmp post-receive.expect post-receive.actual &&
 760                test_cmp post-update.expect post-update.actual
 761        )
 762'
 763
 764test_expect_success 'allow deleting a ref using --delete' '
 765        mk_test testrepo heads/master &&
 766        (cd testrepo && git config receive.denyDeleteCurrent warn) &&
 767        git push testrepo --delete master &&
 768        (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
 769'
 770
 771test_expect_success 'allow deleting a tag using --delete' '
 772        mk_test testrepo heads/master &&
 773        git tag -a -m dummy_message deltag heads/master &&
 774        git push testrepo --tags &&
 775        (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
 776        git push testrepo --delete tag deltag &&
 777        (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
 778'
 779
 780test_expect_success 'push --delete without args aborts' '
 781        mk_test testrepo heads/master &&
 782        test_must_fail git push testrepo --delete
 783'
 784
 785test_expect_success 'push --delete refuses src:dest refspecs' '
 786        mk_test testrepo heads/master &&
 787        test_must_fail git push testrepo --delete master:foo
 788'
 789
 790test_expect_success 'warn on push to HEAD of non-bare repository' '
 791        mk_test testrepo heads/master &&
 792        (
 793                cd testrepo &&
 794                git checkout master &&
 795                git config receive.denyCurrentBranch warn
 796        ) &&
 797        git push testrepo master 2>stderr &&
 798        grep "warning: updating the current branch" stderr
 799'
 800
 801test_expect_success 'deny push to HEAD of non-bare repository' '
 802        mk_test testrepo heads/master &&
 803        (
 804                cd testrepo &&
 805                git checkout master &&
 806                git config receive.denyCurrentBranch true
 807        ) &&
 808        test_must_fail git push testrepo master
 809'
 810
 811test_expect_success 'allow push to HEAD of bare repository (bare)' '
 812        mk_test testrepo heads/master &&
 813        (
 814                cd testrepo &&
 815                git checkout master &&
 816                git config receive.denyCurrentBranch true &&
 817                git config core.bare true
 818        ) &&
 819        git push testrepo master 2>stderr &&
 820        ! grep "warning: updating the current branch" stderr
 821'
 822
 823test_expect_success 'allow push to HEAD of non-bare repository (config)' '
 824        mk_test testrepo heads/master &&
 825        (
 826                cd testrepo &&
 827                git checkout master &&
 828                git config receive.denyCurrentBranch false
 829        ) &&
 830        git push testrepo master 2>stderr &&
 831        ! grep "warning: updating the current branch" stderr
 832'
 833
 834test_expect_success 'fetch with branches' '
 835        mk_empty testrepo &&
 836        git branch second $the_first_commit &&
 837        git checkout second &&
 838        echo ".." > testrepo/.git/branches/branch1 &&
 839        (
 840                cd testrepo &&
 841                git fetch branch1 &&
 842                echo "$the_commit commit        refs/heads/branch1" >expect &&
 843                git for-each-ref refs/heads >actual &&
 844                test_cmp expect actual
 845        ) &&
 846        git checkout master
 847'
 848
 849test_expect_success 'fetch with branches containing #' '
 850        mk_empty testrepo &&
 851        echo "..#second" > testrepo/.git/branches/branch2 &&
 852        (
 853                cd testrepo &&
 854                git fetch branch2 &&
 855                echo "$the_first_commit commit  refs/heads/branch2" >expect &&
 856                git for-each-ref refs/heads >actual &&
 857                test_cmp expect actual
 858        ) &&
 859        git checkout master
 860'
 861
 862test_expect_success 'push with branches' '
 863        mk_empty testrepo &&
 864        git checkout second &&
 865        echo "testrepo" > .git/branches/branch1 &&
 866        git push branch1 &&
 867        (
 868                cd testrepo &&
 869                echo "$the_first_commit commit  refs/heads/master" >expect &&
 870                git for-each-ref refs/heads >actual &&
 871                test_cmp expect actual
 872        )
 873'
 874
 875test_expect_success 'push with branches containing #' '
 876        mk_empty testrepo &&
 877        echo "testrepo#branch3" > .git/branches/branch2 &&
 878        git push branch2 &&
 879        (
 880                cd testrepo &&
 881                echo "$the_first_commit commit  refs/heads/branch3" >expect &&
 882                git for-each-ref refs/heads >actual &&
 883                test_cmp expect actual
 884        ) &&
 885        git checkout master
 886'
 887
 888test_expect_success 'push into aliased refs (consistent)' '
 889        mk_test testrepo heads/master &&
 890        mk_child testrepo child1 &&
 891        mk_child testrepo child2 &&
 892        (
 893                cd child1 &&
 894                git branch foo &&
 895                git symbolic-ref refs/heads/bar refs/heads/foo
 896                git config receive.denyCurrentBranch false
 897        ) &&
 898        (
 899                cd child2 &&
 900                >path2 &&
 901                git add path2 &&
 902                test_tick &&
 903                git commit -a -m child2 &&
 904                git branch foo &&
 905                git branch bar &&
 906                git push ../child1 foo bar
 907        )
 908'
 909
 910test_expect_success 'push into aliased refs (inconsistent)' '
 911        mk_test testrepo heads/master &&
 912        mk_child testrepo child1 &&
 913        mk_child testrepo child2 &&
 914        (
 915                cd child1 &&
 916                git branch foo &&
 917                git symbolic-ref refs/heads/bar refs/heads/foo
 918                git config receive.denyCurrentBranch false
 919        ) &&
 920        (
 921                cd child2 &&
 922                >path2 &&
 923                git add path2 &&
 924                test_tick &&
 925                git commit -a -m child2 &&
 926                git branch foo &&
 927                >path3 &&
 928                git add path3 &&
 929                test_tick &&
 930                git commit -a -m child2 &&
 931                git branch bar &&
 932                test_must_fail git push ../child1 foo bar 2>stderr &&
 933                grep "refusing inconsistent update" stderr
 934        )
 935'
 936
 937test_expect_success 'push requires --force to update lightweight tag' '
 938        mk_test testrepo heads/master &&
 939        mk_child testrepo child1 &&
 940        mk_child testrepo child2 &&
 941        (
 942                cd child1 &&
 943                git tag Tag &&
 944                git push ../child2 Tag &&
 945                git push ../child2 Tag &&
 946                >file1 &&
 947                git add file1 &&
 948                git commit -m "file1" &&
 949                git tag -f Tag &&
 950                test_must_fail git push ../child2 Tag &&
 951                git push --force ../child2 Tag &&
 952                git tag -f Tag &&
 953                test_must_fail git push ../child2 Tag HEAD~ &&
 954                git push --force ../child2 Tag
 955        )
 956'
 957
 958test_expect_success 'push --porcelain' '
 959        mk_empty testrepo &&
 960        echo >.git/foo  "To testrepo" &&
 961        echo >>.git/foo "*      refs/heads/master:refs/remotes/origin/master    [new branch]"  &&
 962        echo >>.git/foo "Done" &&
 963        git push >.git/bar --porcelain  testrepo refs/heads/master:refs/remotes/origin/master &&
 964        (
 965                cd testrepo &&
 966                echo "$the_commit commit        refs/remotes/origin/master" >expect &&
 967                git for-each-ref refs/remotes/origin >actual &&
 968                test_cmp expect actual
 969        ) &&
 970        test_cmp .git/foo .git/bar
 971'
 972
 973test_expect_success 'push --porcelain bad url' '
 974        mk_empty testrepo &&
 975        test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
 976        test_must_fail grep -q Done .git/bar
 977'
 978
 979test_expect_success 'push --porcelain rejected' '
 980        mk_empty testrepo &&
 981        git push testrepo refs/heads/master:refs/remotes/origin/master &&
 982        (cd testrepo &&
 983                git reset --hard origin/master^
 984                git config receive.denyCurrentBranch true) &&
 985
 986        echo >.git/foo  "To testrepo"  &&
 987        echo >>.git/foo "!      refs/heads/master:refs/heads/master     [remote rejected] (branch is currently checked out)" &&
 988
 989        test_must_fail git push >.git/bar --porcelain  testrepo refs/heads/master:refs/heads/master &&
 990        test_cmp .git/foo .git/bar
 991'
 992
 993test_expect_success 'push --porcelain --dry-run rejected' '
 994        mk_empty testrepo &&
 995        git push testrepo refs/heads/master:refs/remotes/origin/master &&
 996        (cd testrepo &&
 997                git reset --hard origin/master
 998                git config receive.denyCurrentBranch true) &&
 999
1000        echo >.git/foo  "To testrepo"  &&
1001        echo >>.git/foo "!      refs/heads/master^:refs/heads/master    [rejected] (non-fast-forward)" &&
1002        echo >>.git/foo "Done" &&
1003
1004        test_must_fail git push >.git/bar --porcelain  --dry-run testrepo refs/heads/master^:refs/heads/master &&
1005        test_cmp .git/foo .git/bar
1006'
1007
1008test_expect_success 'push --prune' '
1009        mk_test testrepo heads/master heads/second heads/foo heads/bar &&
1010        git push --prune testrepo &&
1011        check_push_result testrepo $the_commit heads/master &&
1012        check_push_result testrepo $the_first_commit heads/second &&
1013        ! check_push_result testrepo $the_first_commit heads/foo heads/bar
1014'
1015
1016test_expect_success 'push --prune refspec' '
1017        mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
1018        git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
1019        check_push_result testrepo $the_commit tmp/master &&
1020        check_push_result testrepo $the_first_commit tmp/second &&
1021        ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
1022'
1023
1024for configsection in transfer receive
1025do
1026        test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1027                mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
1028                (
1029                        cd testrepo &&
1030                        git config $configsection.hiderefs refs/hidden
1031                ) &&
1032
1033                # push to unhidden ref succeeds normally
1034                git push testrepo master:refs/heads/master &&
1035                check_push_result testrepo $the_commit heads/master &&
1036
1037                # push to update a hidden ref should fail
1038                test_must_fail git push testrepo master:refs/hidden/one &&
1039                check_push_result testrepo $the_first_commit hidden/one &&
1040
1041                # push to delete a hidden ref should fail
1042                test_must_fail git push testrepo :refs/hidden/two &&
1043                check_push_result testrepo $the_first_commit hidden/two &&
1044
1045                # idempotent push to update a hidden ref should fail
1046                test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1047                check_push_result testrepo $the_first_commit hidden/three
1048        '
1049done
1050
1051test_expect_success 'fetch exact SHA1' '
1052        mk_test testrepo heads/master hidden/one &&
1053        git push testrepo master:refs/hidden/one &&
1054        (
1055                cd testrepo &&
1056                git config transfer.hiderefs refs/hidden
1057        ) &&
1058        check_push_result testrepo $the_commit hidden/one &&
1059
1060        mk_child testrepo child &&
1061        (
1062                cd child &&
1063
1064                # make sure $the_commit does not exist here
1065                git repack -a -d &&
1066                git prune &&
1067                test_must_fail git cat-file -t $the_commit &&
1068
1069                # fetching the hidden object should fail by default
1070                test_must_fail git fetch -v ../testrepo $the_commit:refs/heads/copy &&
1071                test_must_fail git rev-parse --verify refs/heads/copy &&
1072
1073                # the server side can allow it to succeed
1074                (
1075                        cd ../testrepo &&
1076                        git config uploadpack.allowtipsha1inwant true
1077                ) &&
1078
1079                git fetch -v ../testrepo $the_commit:refs/heads/copy &&
1080                result=$(git rev-parse --verify refs/heads/copy) &&
1081                test "$the_commit" = "$result"
1082        )
1083'
1084
1085test_expect_success 'fetch follows tags by default' '
1086        mk_test testrepo heads/master &&
1087        rm -fr src dst &&
1088        git init src &&
1089        (
1090                cd src &&
1091                git pull ../testrepo master &&
1092                git tag -m "annotated" tag &&
1093                git for-each-ref >tmp1 &&
1094                (
1095                        cat tmp1
1096                        sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1097                ) |
1098                sort -k 3 >../expect
1099        ) &&
1100        git init dst &&
1101        (
1102                cd dst &&
1103                git remote add origin ../src &&
1104                git config branch.master.remote origin &&
1105                git config branch.master.merge refs/heads/master &&
1106                git pull &&
1107                git for-each-ref >../actual
1108        ) &&
1109        test_cmp expect actual
1110'
1111
1112test_expect_success 'push does not follow tags by default' '
1113        mk_test testrepo heads/master &&
1114        rm -fr src dst &&
1115        git init src &&
1116        git init --bare dst &&
1117        (
1118                cd src &&
1119                git pull ../testrepo master &&
1120                git tag -m "annotated" tag &&
1121                git checkout -b another &&
1122                git commit --allow-empty -m "future commit" &&
1123                git tag -m "future" future &&
1124                git checkout master &&
1125                git for-each-ref refs/heads/master >../expect &&
1126                git push ../dst master
1127        ) &&
1128        (
1129                cd dst &&
1130                git for-each-ref >../actual
1131        ) &&
1132        test_cmp expect actual
1133'
1134
1135test_expect_success 'push --follow-tag only pushes relevant tags' '
1136        mk_test testrepo heads/master &&
1137        rm -fr src dst &&
1138        git init src &&
1139        git init --bare dst &&
1140        (
1141                cd src &&
1142                git pull ../testrepo master &&
1143                git tag -m "annotated" tag &&
1144                git checkout -b another &&
1145                git commit --allow-empty -m "future commit" &&
1146                git tag -m "future" future &&
1147                git checkout master &&
1148                git for-each-ref refs/heads/master refs/tags/tag >../expect
1149                git push --follow-tag ../dst master
1150        ) &&
1151        (
1152                cd dst &&
1153                git for-each-ref >../actual
1154        ) &&
1155        test_cmp expect actual
1156'
1157
1158test_done