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