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