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