0b7df110ad6e52f16a2c15d3c124666eecc102f6
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 Felipe Contreras
   4#
   5# Base commands from hg-git tests:
   6# https://bitbucket.org/durin42/hg-git/src
   7#
   8
   9test_description='Test remote-hg'
  10
  11test -n "$TEST_DIRECTORY" || TEST_DIRECTORY=${0%/*}/../../t
  12. "$TEST_DIRECTORY"/test-lib.sh
  13
  14if ! test_have_prereq PYTHON
  15then
  16        skip_all='skipping remote-hg tests; python not available'
  17        test_done
  18fi
  19
  20if ! python -c 'import mercurial'
  21then
  22        skip_all='skipping remote-hg tests; mercurial not available'
  23        test_done
  24fi
  25
  26check () {
  27        echo $3 >expected &&
  28        git --git-dir=$1/.git log --format='%s' -1 $2 >actual
  29        test_cmp expected actual
  30}
  31
  32check_branch () {
  33        if test -n "$3"
  34        then
  35                echo $3 >expected &&
  36                hg -R $1 log -r $2 --template '{desc}\n' >actual &&
  37                test_cmp expected actual
  38        else
  39                hg -R $1 branches >out &&
  40                ! grep $2 out
  41        fi
  42}
  43
  44check_bookmark () {
  45        if test -n "$3"
  46        then
  47                echo $3 >expected &&
  48                hg -R $1 log -r "bookmark('$2')" --template '{desc}\n' >actual &&
  49                test_cmp expected actual
  50        else
  51                hg -R $1 bookmarks >out &&
  52                ! grep $2 out
  53        fi
  54}
  55
  56check_push () {
  57        expected_ret=$1 ret=0 ref_ret=0
  58
  59        shift
  60        git push origin "$@" 2>error
  61        ret=$?
  62        cat error
  63
  64        while IFS=':' read branch kind
  65        do
  66                case "$kind" in
  67                'new')
  68                        grep "^ \* \[new branch\] *${branch} -> ${branch}$" error || ref_ret=1
  69                        ;;
  70                'non-fast-forward')
  71                        grep "^ ! \[rejected\] *${branch} -> ${branch} (non-fast-forward)$" error || ref_ret=1
  72                        ;;
  73                'fetch-first')
  74                        grep "^ ! \[rejected\] *${branch} -> ${branch} (fetch first)$" error || ref_ret=1
  75                        ;;
  76                'forced-update')
  77                        grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *${branch} -> ${branch} (forced update)$" error || ref_ret=1
  78                        ;;
  79                '')
  80                        grep "^   [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
  81                        ;;
  82                esac
  83                test $ref_ret -ne 0 && echo "match for '$branch' failed" && break
  84        done
  85
  86        if test $expected_ret -ne $ret || test $ref_ret -ne 0
  87        then
  88                return 1
  89        fi
  90
  91        return 0
  92}
  93
  94setup () {
  95        (
  96        echo "[ui]"
  97        echo "username = H G Wells <wells@example.com>"
  98        echo "[extensions]"
  99        echo "mq ="
 100        ) >>"$HOME"/.hgrc &&
 101
 102        GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
 103        GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
 104        export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 105}
 106
 107setup
 108
 109test_expect_success 'cloning' '
 110        test_when_finished "rm -rf gitrepo*" &&
 111
 112        (
 113        hg init hgrepo &&
 114        cd hgrepo &&
 115        echo zero >content &&
 116        hg add content &&
 117        hg commit -m zero
 118        ) &&
 119
 120        git clone "hg::hgrepo" gitrepo &&
 121        check gitrepo HEAD zero
 122'
 123
 124test_expect_success 'cloning with branches' '
 125        test_when_finished "rm -rf gitrepo*" &&
 126
 127        (
 128        cd hgrepo &&
 129        hg branch next &&
 130        echo next >content &&
 131        hg commit -m next
 132        ) &&
 133
 134        git clone "hg::hgrepo" gitrepo &&
 135        check gitrepo origin/branches/next next
 136'
 137
 138test_expect_success 'cloning with bookmarks' '
 139        test_when_finished "rm -rf gitrepo*" &&
 140
 141        (
 142        cd hgrepo &&
 143        hg checkout default &&
 144        hg bookmark feature-a &&
 145        echo feature-a >content &&
 146        hg commit -m feature-a
 147        ) &&
 148
 149        git clone "hg::hgrepo" gitrepo &&
 150        check gitrepo origin/feature-a feature-a
 151'
 152
 153test_expect_success 'update bookmark' '
 154        test_when_finished "rm -rf gitrepo*" &&
 155
 156        (
 157        cd hgrepo &&
 158        hg bookmark devel
 159        ) &&
 160
 161        (
 162        git clone "hg::hgrepo" gitrepo &&
 163        cd gitrepo &&
 164        git checkout --quiet devel &&
 165        echo devel >content &&
 166        git commit -a -m devel &&
 167        git push --quiet
 168        ) &&
 169
 170        check_bookmark hgrepo devel devel
 171'
 172
 173test_expect_success 'new bookmark' '
 174        test_when_finished "rm -rf gitrepo*" &&
 175
 176        (
 177        git clone "hg::hgrepo" gitrepo &&
 178        cd gitrepo &&
 179        git checkout --quiet -b feature-b &&
 180        echo feature-b >content &&
 181        git commit -a -m feature-b &&
 182        git push --quiet origin feature-b
 183        ) &&
 184
 185        check_bookmark hgrepo feature-b feature-b
 186'
 187
 188# cleanup previous stuff
 189rm -rf hgrepo
 190
 191author_test () {
 192        echo $1 >>content &&
 193        hg commit -u "$2" -m "add $1" &&
 194        echo "$3" >>../expected
 195}
 196
 197test_expect_success 'authors' '
 198        test_when_finished "rm -rf hgrepo gitrepo" &&
 199
 200        (
 201        hg init hgrepo &&
 202        cd hgrepo &&
 203
 204        touch content &&
 205        hg add content &&
 206
 207        >../expected &&
 208        author_test alpha "" "H G Wells <wells@example.com>" &&
 209        author_test beta "beta" "beta <unknown>" &&
 210        author_test gamma "gamma <test@example.com> (comment)" "gamma <test@example.com>" &&
 211        author_test delta "<delta@example.com>" "Unknown <delta@example.com>" &&
 212        author_test epsilon "epsilon<test@example.com>" "epsilon <test@example.com>" &&
 213        author_test zeta "zeta <test@example.com" "zeta <test@example.com>" &&
 214        author_test eta " eta " "eta <unknown>" &&
 215        author_test theta "theta < test@example.com >" "theta <test@example.com>" &&
 216        author_test iota "iota >test@example.com>" "iota <test@example.com>" &&
 217        author_test kappa "kappa < test <at> example <dot> com>" "kappa <unknown>" &&
 218        author_test lambda "lambda@example.com" "Unknown <lambda@example.com>" &&
 219        author_test mu "mu.mu@example.com" "Unknown <mu.mu@example.com>"
 220        ) &&
 221
 222        git clone "hg::hgrepo" gitrepo &&
 223        git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" >actual &&
 224
 225        test_cmp expected actual
 226'
 227
 228test_expect_success 'strip' '
 229        test_when_finished "rm -rf hgrepo gitrepo" &&
 230
 231        (
 232        hg init hgrepo &&
 233        cd hgrepo &&
 234
 235        echo one >>content &&
 236        hg add content &&
 237        hg commit -m one &&
 238
 239        echo two >>content &&
 240        hg commit -m two
 241        ) &&
 242
 243        git clone "hg::hgrepo" gitrepo &&
 244
 245        (
 246        cd hgrepo &&
 247        hg strip 1 &&
 248
 249        echo three >>content &&
 250        hg commit -m three &&
 251
 252        echo four >>content &&
 253        hg commit -m four
 254        ) &&
 255
 256        (
 257        cd gitrepo &&
 258        git fetch &&
 259        git log --format="%s" origin/master >../actual
 260        ) &&
 261
 262        hg -R hgrepo log --template "{desc}\n" >expected &&
 263        test_cmp actual expected
 264'
 265
 266test_expect_success 'remote push with master bookmark' '
 267        test_when_finished "rm -rf hgrepo gitrepo*" &&
 268
 269        (
 270        hg init hgrepo &&
 271        cd hgrepo &&
 272        echo zero >content &&
 273        hg add content &&
 274        hg commit -m zero &&
 275        hg bookmark master &&
 276        echo one >content &&
 277        hg commit -m one
 278        ) &&
 279
 280        (
 281        git clone "hg::hgrepo" gitrepo &&
 282        cd gitrepo &&
 283        echo two >content &&
 284        git commit -a -m two &&
 285        git push
 286        ) &&
 287
 288        check_branch hgrepo default two
 289'
 290
 291cat >expected <<\EOF
 292changeset:   0:6e2126489d3d
 293tag:         tip
 294user:        A U Thor <author@example.com>
 295date:        Mon Jan 01 00:00:00 2007 +0230
 296summary:     one
 297
 298EOF
 299
 300test_expect_success 'remote push from master branch' '
 301        test_when_finished "rm -rf hgrepo gitrepo*" &&
 302
 303        hg init hgrepo &&
 304
 305        (
 306        git init gitrepo &&
 307        cd gitrepo &&
 308        git remote add origin "hg::../hgrepo" &&
 309        echo one >content &&
 310        git add content &&
 311        git commit -a -m one &&
 312        git push origin master
 313        ) &&
 314
 315        hg -R hgrepo log >actual &&
 316        cat actual &&
 317        test_cmp expected actual &&
 318
 319        check_branch hgrepo default one
 320'
 321
 322GIT_REMOTE_HG_TEST_REMOTE=1
 323export GIT_REMOTE_HG_TEST_REMOTE
 324
 325test_expect_success 'remote cloning' '
 326        test_when_finished "rm -rf gitrepo*" &&
 327
 328        (
 329        hg init hgrepo &&
 330        cd hgrepo &&
 331        echo zero >content &&
 332        hg add content &&
 333        hg commit -m zero
 334        ) &&
 335
 336        git clone "hg::hgrepo" gitrepo &&
 337        check gitrepo HEAD zero
 338'
 339
 340test_expect_success 'remote update bookmark' '
 341        test_when_finished "rm -rf gitrepo*" &&
 342
 343        (
 344        cd hgrepo &&
 345        hg bookmark devel
 346        ) &&
 347
 348        (
 349        git clone "hg::hgrepo" gitrepo &&
 350        cd gitrepo &&
 351        git checkout --quiet devel &&
 352        echo devel >content &&
 353        git commit -a -m devel &&
 354        git push --quiet
 355        ) &&
 356
 357        check_bookmark hgrepo devel devel
 358'
 359
 360test_expect_success 'remote new bookmark' '
 361        test_when_finished "rm -rf gitrepo*" &&
 362
 363        (
 364        git clone "hg::hgrepo" gitrepo &&
 365        cd gitrepo &&
 366        git checkout --quiet -b feature-b &&
 367        echo feature-b >content &&
 368        git commit -a -m feature-b &&
 369        git push --quiet origin feature-b
 370        ) &&
 371
 372        check_bookmark hgrepo feature-b feature-b
 373'
 374
 375test_expect_success 'remote push diverged' '
 376        test_when_finished "rm -rf gitrepo*" &&
 377
 378        git clone "hg::hgrepo" gitrepo &&
 379
 380        (
 381        cd hgrepo &&
 382        hg checkout default &&
 383        echo bump >content &&
 384        hg commit -m bump
 385        ) &&
 386
 387        (
 388        cd gitrepo &&
 389        echo diverge >content &&
 390        git commit -a -m diverged &&
 391        check_push 1 <<-\EOF
 392        master:non-fast-forward
 393        EOF
 394        ) &&
 395
 396        check_branch hgrepo default bump
 397'
 398
 399test_expect_success 'remote update bookmark diverge' '
 400        test_when_finished "rm -rf gitrepo*" &&
 401
 402        (
 403        cd hgrepo &&
 404        hg checkout tip^ &&
 405        hg bookmark diverge
 406        ) &&
 407
 408        git clone "hg::hgrepo" gitrepo &&
 409
 410        (
 411        cd hgrepo &&
 412        echo "bump bookmark" >content &&
 413        hg commit -m "bump bookmark"
 414        ) &&
 415
 416        (
 417        cd gitrepo &&
 418        git checkout --quiet diverge &&
 419        echo diverge >content &&
 420        git commit -a -m diverge &&
 421        check_push 1 <<-\EOF
 422        diverge:fetch-first
 423        EOF
 424        ) &&
 425
 426        check_bookmark hgrepo diverge "bump bookmark"
 427'
 428
 429test_expect_success 'remote new bookmark multiple branch head' '
 430        test_when_finished "rm -rf gitrepo*" &&
 431
 432        (
 433        git clone "hg::hgrepo" gitrepo &&
 434        cd gitrepo &&
 435        git checkout --quiet -b feature-c HEAD^ &&
 436        echo feature-c >content &&
 437        git commit -a -m feature-c &&
 438        git push --quiet origin feature-c
 439        ) &&
 440
 441        check_bookmark hgrepo feature-c feature-c
 442'
 443
 444# cleanup previous stuff
 445rm -rf hgrepo
 446
 447test_expect_success 'fetch special filenames' '
 448        test_when_finished "rm -rf hgrepo gitrepo && LC_ALL=C" &&
 449
 450        LC_ALL=en_US.UTF-8
 451        export LC_ALL
 452
 453        (
 454        hg init hgrepo &&
 455        cd hgrepo &&
 456
 457        echo test >> "æ rø" &&
 458        hg add "æ rø" &&
 459        echo test >> "ø~?" &&
 460        hg add "ø~?" &&
 461        hg commit -m add-utf-8 &&
 462        echo test >> "æ rø" &&
 463        hg commit -m test-utf-8 &&
 464        hg rm "ø~?" &&
 465        hg mv "æ rø" "ø~?" &&
 466        hg commit -m hg-mv-utf-8
 467        ) &&
 468
 469        (
 470        git clone "hg::hgrepo" gitrepo &&
 471        cd gitrepo &&
 472        git -c core.quotepath=false ls-files > ../actual
 473        ) &&
 474        echo "ø~?" > expected &&
 475        test_cmp expected actual
 476'
 477
 478test_expect_success 'push special filenames' '
 479        test_when_finished "rm -rf hgrepo gitrepo && LC_ALL=C" &&
 480
 481        mkdir -p tmp && cd tmp &&
 482
 483        LC_ALL=en_US.UTF-8
 484        export LC_ALL
 485
 486        (
 487        hg init hgrepo &&
 488        cd hgrepo &&
 489
 490        echo one >> content &&
 491        hg add content &&
 492        hg commit -m one
 493        ) &&
 494
 495        (
 496        git clone "hg::hgrepo" gitrepo &&
 497        cd gitrepo &&
 498
 499        echo test >> "æ rø" &&
 500        git add "æ rø" &&
 501        git commit -m utf-8 &&
 502
 503        git push
 504        ) &&
 505
 506        (cd hgrepo &&
 507        hg update &&
 508        hg manifest > ../actual
 509        ) &&
 510
 511        printf "content\næ rø\n" > expected &&
 512        test_cmp expected actual
 513'
 514
 515setup_big_push () {
 516        (
 517        hg init hgrepo &&
 518        cd hgrepo &&
 519        echo zero >content &&
 520        hg add content &&
 521        hg commit -m zero &&
 522        hg bookmark bad_bmark1 &&
 523        echo one >content &&
 524        hg commit -m one &&
 525        hg bookmark bad_bmark2 &&
 526        hg bookmark good_bmark &&
 527        hg bookmark -i good_bmark &&
 528        hg -q branch good_branch &&
 529        echo "good branch" >content &&
 530        hg commit -m "good branch" &&
 531        hg -q branch bad_branch &&
 532        echo "bad branch" >content &&
 533        hg commit -m "bad branch"
 534        ) &&
 535
 536        git clone "hg::hgrepo" gitrepo &&
 537
 538        (
 539        cd gitrepo &&
 540        echo two >content &&
 541        git commit -q -a -m two &&
 542
 543        git checkout -q good_bmark &&
 544        echo three >content &&
 545        git commit -q -a -m three &&
 546
 547        git checkout -q bad_bmark1 &&
 548        git reset --hard HEAD^ &&
 549        echo four >content &&
 550        git commit -q -a -m four &&
 551
 552        git checkout -q bad_bmark2 &&
 553        git reset --hard HEAD^ &&
 554        echo five >content &&
 555        git commit -q -a -m five &&
 556
 557        git checkout -q -b new_bmark master &&
 558        echo six >content &&
 559        git commit -q -a -m six &&
 560
 561        git checkout -q branches/good_branch &&
 562        echo seven >content &&
 563        git commit -q -a -m seven &&
 564        echo eight >content &&
 565        git commit -q -a -m eight &&
 566
 567        git checkout -q branches/bad_branch &&
 568        git reset --hard HEAD^ &&
 569        echo nine >content &&
 570        git commit -q -a -m nine &&
 571
 572        git checkout -q -b branches/new_branch master &&
 573        echo ten >content &&
 574        git commit -q -a -m ten
 575        )
 576}
 577
 578test_expect_success 'remote big push' '
 579        test_when_finished "rm -rf hgrepo gitrepo*" &&
 580
 581        setup_big_push
 582
 583        (
 584        cd gitrepo &&
 585
 586        check_push 1 --all <<-\EOF
 587        master
 588        good_bmark
 589        branches/good_branch
 590        new_bmark:new
 591        branches/new_branch:new
 592        bad_bmark1:non-fast-forward
 593        bad_bmark2:non-fast-forward
 594        branches/bad_branch:non-fast-forward
 595        EOF
 596        ) &&
 597
 598        check_branch hgrepo default one &&
 599        check_branch hgrepo good_branch "good branch" &&
 600        check_branch hgrepo bad_branch "bad branch" &&
 601        check_branch hgrepo new_branch '' &&
 602        check_bookmark hgrepo good_bmark one &&
 603        check_bookmark hgrepo bad_bmark1 one &&
 604        check_bookmark hgrepo bad_bmark2 one &&
 605        check_bookmark hgrepo new_bmark ''
 606'
 607
 608test_expect_success 'remote big push fetch first' '
 609        test_when_finished "rm -rf hgrepo gitrepo*" &&
 610
 611        (
 612        hg init hgrepo &&
 613        cd hgrepo &&
 614        echo zero >content &&
 615        hg add content &&
 616        hg commit -m zero &&
 617        hg bookmark bad_bmark &&
 618        hg bookmark good_bmark &&
 619        hg bookmark -i good_bmark &&
 620        hg -q branch good_branch &&
 621        echo "good branch" >content &&
 622        hg commit -m "good branch" &&
 623        hg -q branch bad_branch &&
 624        echo "bad branch" >content &&
 625        hg commit -m "bad branch"
 626        ) &&
 627
 628        git clone "hg::hgrepo" gitrepo &&
 629
 630        (
 631        cd hgrepo &&
 632        hg bookmark -f bad_bmark &&
 633        echo update_bmark >content &&
 634        hg commit -m "update bmark"
 635        ) &&
 636
 637        (
 638        cd gitrepo &&
 639        echo two >content &&
 640        git commit -q -a -m two &&
 641
 642        git checkout -q good_bmark &&
 643        echo three >content &&
 644        git commit -q -a -m three &&
 645
 646        git checkout -q bad_bmark &&
 647        echo four >content &&
 648        git commit -q -a -m four &&
 649
 650        git checkout -q branches/bad_branch &&
 651        echo five >content &&
 652        git commit -q -a -m five &&
 653
 654        check_push 1 --all <<-\EOF &&
 655        master
 656        good_bmark
 657        bad_bmark:fetch-first
 658        branches/bad_branch:festch-first
 659        EOF
 660
 661        git fetch &&
 662
 663        check_push 1 --all <<-\EOF
 664        master
 665        good_bmark
 666        bad_bmark:non-fast-forward
 667        branches/bad_branch:non-fast-forward
 668        EOF
 669        )
 670'
 671
 672test_expect_failure 'remote big push force' '
 673        test_when_finished "rm -rf hgrepo gitrepo*" &&
 674
 675        setup_big_push
 676
 677        (
 678        cd gitrepo &&
 679
 680        check_push 0 --force --all <<-\EOF
 681        master
 682        good_bmark
 683        branches/good_branch
 684        new_bmark:new
 685        branches/new_branch:new
 686        bad_bmark1:forced-update
 687        bad_bmark2:forced-update
 688        branches/bad_branch:forced-update
 689        EOF
 690        ) &&
 691
 692        check_branch hgrepo default six &&
 693        check_branch hgrepo good_branch eight &&
 694        check_branch hgrepo bad_branch nine &&
 695        check_branch hgrepo new_branch ten &&
 696        check_bookmark hgrepo good_bmark three &&
 697        check_bookmark hgrepo bad_bmark1 four &&
 698        check_bookmark hgrepo bad_bmark2 five &&
 699        check_bookmark hgrepo new_bmark six
 700'
 701
 702test_expect_failure 'remote big push dry-run' '
 703        test_when_finished "rm -rf hgrepo gitrepo*" &&
 704
 705        setup_big_push
 706
 707        (
 708        cd gitrepo &&
 709
 710        check_push 1 --dry-run --all <<-\EOF &&
 711        master
 712        good_bmark
 713        branches/good_branch
 714        new_bmark:new
 715        branches/new_branch:new
 716        bad_bmark1:non-fast-forward
 717        bad_bmark2:non-fast-forward
 718        branches/bad_branch:non-fast-forward
 719        EOF
 720
 721        check_push 0 --dry-run master good_bmark new_bmark branches/good_branch branches/new_branch <<-\EOF
 722        master
 723        good_bmark
 724        branches/good_branch
 725        new_bmark:new
 726        branches/new_branch:new
 727        EOF
 728        ) &&
 729
 730        check_branch hgrepo default one &&
 731        check_branch hgrepo good_branch "good branch" &&
 732        check_branch hgrepo bad_branch "bad branch" &&
 733        check_branch hgrepo new_branch '' &&
 734        check_bookmark hgrepo good_bmark one &&
 735        check_bookmark hgrepo bad_bmark1 one &&
 736        check_bookmark hgrepo bad_bmark2 one &&
 737        check_bookmark hgrepo new_bmark ''
 738'
 739
 740test_expect_success 'remote double failed push' '
 741        test_when_finished "rm -rf hgrepo gitrepo*" &&
 742
 743        (
 744        hg init hgrepo &&
 745        cd hgrepo &&
 746        echo zero >content &&
 747        hg add content &&
 748        hg commit -m zero &&
 749        echo one >content &&
 750        hg commit -m one
 751        ) &&
 752
 753        (
 754        git clone "hg::hgrepo" gitrepo &&
 755        cd gitrepo &&
 756        git reset --hard HEAD^ &&
 757        echo two >content &&
 758        git commit -a -m two &&
 759        test_expect_code 1 git push &&
 760        test_expect_code 1 git push
 761        )
 762'
 763
 764test_done