5535e8c9be3f3f73c1b32fc1290cd58927bbfc0d
   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 "test" "test <unknown>" &&
 210        author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
 211        author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
 212        author_test delta "name<test@example.com>" "name <test@example.com>" &&
 213        author_test epsilon "name <test@example.com" "name <test@example.com>" &&
 214        author_test zeta " test " "test <unknown>" &&
 215        author_test eta "test < test@example.com >" "test <test@example.com>" &&
 216        author_test theta "test >test@example.com>" "test <test@example.com>" &&
 217        author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
 218        author_test kappa "test@example.com" "Unknown <test@example.com>"
 219        ) &&
 220
 221        git clone "hg::hgrepo" gitrepo &&
 222        git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" >actual &&
 223
 224        test_cmp expected actual
 225'
 226
 227test_expect_success 'strip' '
 228        test_when_finished "rm -rf hgrepo gitrepo" &&
 229
 230        (
 231        hg init hgrepo &&
 232        cd hgrepo &&
 233
 234        echo one >>content &&
 235        hg add content &&
 236        hg commit -m one &&
 237
 238        echo two >>content &&
 239        hg commit -m two
 240        ) &&
 241
 242        git clone "hg::hgrepo" gitrepo &&
 243
 244        (
 245        cd hgrepo &&
 246        hg strip 1 &&
 247
 248        echo three >>content &&
 249        hg commit -m three &&
 250
 251        echo four >>content &&
 252        hg commit -m four
 253        ) &&
 254
 255        (
 256        cd gitrepo &&
 257        git fetch &&
 258        git log --format="%s" origin/master >../actual
 259        ) &&
 260
 261        hg -R hgrepo log --template "{desc}\n" >expected &&
 262        test_cmp actual expected
 263'
 264
 265test_expect_success 'remote push with master bookmark' '
 266        test_when_finished "rm -rf hgrepo gitrepo*" &&
 267
 268        (
 269        hg init hgrepo &&
 270        cd hgrepo &&
 271        echo zero >content &&
 272        hg add content &&
 273        hg commit -m zero &&
 274        hg bookmark master &&
 275        echo one >content &&
 276        hg commit -m one
 277        ) &&
 278
 279        (
 280        git clone "hg::hgrepo" gitrepo &&
 281        cd gitrepo &&
 282        echo two >content &&
 283        git commit -a -m two &&
 284        git push
 285        ) &&
 286
 287        check_branch hgrepo default two
 288'
 289
 290cat >expected <<\EOF
 291changeset:   0:6e2126489d3d
 292tag:         tip
 293user:        A U Thor <author@example.com>
 294date:        Mon Jan 01 00:00:00 2007 +0230
 295summary:     one
 296
 297EOF
 298
 299test_expect_success 'remote push from master branch' '
 300        test_when_finished "rm -rf hgrepo gitrepo*" &&
 301
 302        hg init hgrepo &&
 303
 304        (
 305        git init gitrepo &&
 306        cd gitrepo &&
 307        git remote add origin "hg::../hgrepo" &&
 308        echo one >content &&
 309        git add content &&
 310        git commit -a -m one &&
 311        git push origin master
 312        ) &&
 313
 314        hg -R hgrepo log >actual &&
 315        cat actual &&
 316        test_cmp expected actual &&
 317
 318        check_branch hgrepo default one
 319'
 320
 321GIT_REMOTE_HG_TEST_REMOTE=1
 322export GIT_REMOTE_HG_TEST_REMOTE
 323
 324test_expect_success 'remote cloning' '
 325        test_when_finished "rm -rf gitrepo*" &&
 326
 327        (
 328        hg init hgrepo &&
 329        cd hgrepo &&
 330        echo zero >content &&
 331        hg add content &&
 332        hg commit -m zero
 333        ) &&
 334
 335        git clone "hg::hgrepo" gitrepo &&
 336        check gitrepo HEAD zero
 337'
 338
 339test_expect_success 'remote update bookmark' '
 340        test_when_finished "rm -rf gitrepo*" &&
 341
 342        (
 343        cd hgrepo &&
 344        hg bookmark devel
 345        ) &&
 346
 347        (
 348        git clone "hg::hgrepo" gitrepo &&
 349        cd gitrepo &&
 350        git checkout --quiet devel &&
 351        echo devel >content &&
 352        git commit -a -m devel &&
 353        git push --quiet
 354        ) &&
 355
 356        check_bookmark hgrepo devel devel
 357'
 358
 359test_expect_success 'remote new bookmark' '
 360        test_when_finished "rm -rf gitrepo*" &&
 361
 362        (
 363        git clone "hg::hgrepo" gitrepo &&
 364        cd gitrepo &&
 365        git checkout --quiet -b feature-b &&
 366        echo feature-b >content &&
 367        git commit -a -m feature-b &&
 368        git push --quiet origin feature-b
 369        ) &&
 370
 371        check_bookmark hgrepo feature-b feature-b
 372'
 373
 374test_expect_success 'remote push diverged' '
 375        test_when_finished "rm -rf gitrepo*" &&
 376
 377        git clone "hg::hgrepo" gitrepo &&
 378
 379        (
 380        cd hgrepo &&
 381        hg checkout default &&
 382        echo bump >content &&
 383        hg commit -m bump
 384        ) &&
 385
 386        (
 387        cd gitrepo &&
 388        echo diverge >content &&
 389        git commit -a -m diverged &&
 390        check_push 1 <<-\EOF
 391        master:non-fast-forward
 392        EOF
 393        ) &&
 394
 395        check_branch hgrepo default bump
 396'
 397
 398test_expect_success 'remote update bookmark diverge' '
 399        test_when_finished "rm -rf gitrepo*" &&
 400
 401        (
 402        cd hgrepo &&
 403        hg checkout tip^ &&
 404        hg bookmark diverge
 405        ) &&
 406
 407        git clone "hg::hgrepo" gitrepo &&
 408
 409        (
 410        cd hgrepo &&
 411        echo "bump bookmark" >content &&
 412        hg commit -m "bump bookmark"
 413        ) &&
 414
 415        (
 416        cd gitrepo &&
 417        git checkout --quiet diverge &&
 418        echo diverge >content &&
 419        git commit -a -m diverge &&
 420        check_push 1 <<-\EOF
 421        diverge:fetch-first
 422        EOF
 423        ) &&
 424
 425        check_bookmark hgrepo diverge "bump bookmark"
 426'
 427
 428test_expect_success 'remote new bookmark multiple branch head' '
 429        test_when_finished "rm -rf gitrepo*" &&
 430
 431        (
 432        git clone "hg::hgrepo" gitrepo &&
 433        cd gitrepo &&
 434        git checkout --quiet -b feature-c HEAD^ &&
 435        echo feature-c >content &&
 436        git commit -a -m feature-c &&
 437        git push --quiet origin feature-c
 438        ) &&
 439
 440        check_bookmark hgrepo feature-c feature-c
 441'
 442
 443# cleanup previous stuff
 444rm -rf hgrepo
 445
 446setup_big_push () {
 447        (
 448        hg init hgrepo &&
 449        cd hgrepo &&
 450        echo zero >content &&
 451        hg add content &&
 452        hg commit -m zero &&
 453        hg bookmark bad_bmark1 &&
 454        echo one >content &&
 455        hg commit -m one &&
 456        hg bookmark bad_bmark2 &&
 457        hg bookmark good_bmark &&
 458        hg bookmark -i good_bmark &&
 459        hg -q branch good_branch &&
 460        echo "good branch" >content &&
 461        hg commit -m "good branch" &&
 462        hg -q branch bad_branch &&
 463        echo "bad branch" >content &&
 464        hg commit -m "bad branch"
 465        ) &&
 466
 467        git clone "hg::hgrepo" gitrepo &&
 468
 469        (
 470        cd gitrepo &&
 471        echo two >content &&
 472        git commit -q -a -m two &&
 473
 474        git checkout -q good_bmark &&
 475        echo three >content &&
 476        git commit -q -a -m three &&
 477
 478        git checkout -q bad_bmark1 &&
 479        git reset --hard HEAD^ &&
 480        echo four >content &&
 481        git commit -q -a -m four &&
 482
 483        git checkout -q bad_bmark2 &&
 484        git reset --hard HEAD^ &&
 485        echo five >content &&
 486        git commit -q -a -m five &&
 487
 488        git checkout -q -b new_bmark master &&
 489        echo six >content &&
 490        git commit -q -a -m six &&
 491
 492        git checkout -q branches/good_branch &&
 493        echo seven >content &&
 494        git commit -q -a -m seven &&
 495        echo eight >content &&
 496        git commit -q -a -m eight &&
 497
 498        git checkout -q branches/bad_branch &&
 499        git reset --hard HEAD^ &&
 500        echo nine >content &&
 501        git commit -q -a -m nine &&
 502
 503        git checkout -q -b branches/new_branch master &&
 504        echo ten >content &&
 505        git commit -q -a -m ten
 506        )
 507}
 508
 509test_expect_success 'remote big push' '
 510        test_when_finished "rm -rf hgrepo gitrepo*" &&
 511
 512        setup_big_push
 513
 514        (
 515        cd gitrepo &&
 516
 517        check_push 1 --all <<-\EOF
 518        master
 519        good_bmark
 520        branches/good_branch
 521        new_bmark:new
 522        branches/new_branch:new
 523        bad_bmark1:non-fast-forward
 524        bad_bmark2:non-fast-forward
 525        branches/bad_branch:non-fast-forward
 526        EOF
 527        ) &&
 528
 529        check_branch hgrepo default one &&
 530        check_branch hgrepo good_branch "good branch" &&
 531        check_branch hgrepo bad_branch "bad branch" &&
 532        check_branch hgrepo new_branch '' &&
 533        check_bookmark hgrepo good_bmark one &&
 534        check_bookmark hgrepo bad_bmark1 one &&
 535        check_bookmark hgrepo bad_bmark2 one &&
 536        check_bookmark hgrepo new_bmark ''
 537'
 538
 539test_expect_success 'remote big push fetch first' '
 540        test_when_finished "rm -rf hgrepo gitrepo*" &&
 541
 542        (
 543        hg init hgrepo &&
 544        cd hgrepo &&
 545        echo zero >content &&
 546        hg add content &&
 547        hg commit -m zero &&
 548        hg bookmark bad_bmark &&
 549        hg bookmark good_bmark &&
 550        hg bookmark -i good_bmark &&
 551        hg -q branch good_branch &&
 552        echo "good branch" >content &&
 553        hg commit -m "good branch" &&
 554        hg -q branch bad_branch &&
 555        echo "bad branch" >content &&
 556        hg commit -m "bad branch"
 557        ) &&
 558
 559        git clone "hg::hgrepo" gitrepo &&
 560
 561        (
 562        cd hgrepo &&
 563        hg bookmark -f bad_bmark &&
 564        echo update_bmark >content &&
 565        hg commit -m "update bmark"
 566        ) &&
 567
 568        (
 569        cd gitrepo &&
 570        echo two >content &&
 571        git commit -q -a -m two &&
 572
 573        git checkout -q good_bmark &&
 574        echo three >content &&
 575        git commit -q -a -m three &&
 576
 577        git checkout -q bad_bmark &&
 578        echo four >content &&
 579        git commit -q -a -m four &&
 580
 581        git checkout -q branches/bad_branch &&
 582        echo five >content &&
 583        git commit -q -a -m five &&
 584
 585        check_push 1 --all <<-\EOF &&
 586        master
 587        good_bmark
 588        bad_bmark:fetch-first
 589        branches/bad_branch:festch-first
 590        EOF
 591
 592        git fetch &&
 593
 594        check_push 1 --all <<-\EOF
 595        master
 596        good_bmark
 597        bad_bmark:non-fast-forward
 598        branches/bad_branch:non-fast-forward
 599        EOF
 600        )
 601'
 602
 603test_expect_failure 'remote big push force' '
 604        test_when_finished "rm -rf hgrepo gitrepo*" &&
 605
 606        setup_big_push
 607
 608        (
 609        cd gitrepo &&
 610
 611        check_push 0 --force --all <<-\EOF
 612        master
 613        good_bmark
 614        branches/good_branch
 615        new_bmark:new
 616        branches/new_branch:new
 617        bad_bmark1:forced-update
 618        bad_bmark2:forced-update
 619        branches/bad_branch:forced-update
 620        EOF
 621        ) &&
 622
 623        check_branch hgrepo default six &&
 624        check_branch hgrepo good_branch eight &&
 625        check_branch hgrepo bad_branch nine &&
 626        check_branch hgrepo new_branch ten &&
 627        check_bookmark hgrepo good_bmark three &&
 628        check_bookmark hgrepo bad_bmark1 four &&
 629        check_bookmark hgrepo bad_bmark2 five &&
 630        check_bookmark hgrepo new_bmark six
 631'
 632
 633test_expect_failure 'remote big push dry-run' '
 634        test_when_finished "rm -rf hgrepo gitrepo*" &&
 635
 636        setup_big_push
 637
 638        (
 639        cd gitrepo &&
 640
 641        check_push 1 --dry-run --all <<-\EOF &&
 642        master
 643        good_bmark
 644        branches/good_branch
 645        new_bmark:new
 646        branches/new_branch:new
 647        bad_bmark1:non-fast-forward
 648        bad_bmark2:non-fast-forward
 649        branches/bad_branch:non-fast-forward
 650        EOF
 651
 652        check_push 0 --dry-run master good_bmark new_bmark branches/good_branch branches/new_branch <<-\EOF
 653        master
 654        good_bmark
 655        branches/good_branch
 656        new_bmark:new
 657        branches/new_branch:new
 658        EOF
 659        ) &&
 660
 661        check_branch hgrepo default one &&
 662        check_branch hgrepo good_branch "good branch" &&
 663        check_branch hgrepo bad_branch "bad branch" &&
 664        check_branch hgrepo new_branch '' &&
 665        check_bookmark hgrepo good_bmark one &&
 666        check_bookmark hgrepo bad_bmark1 one &&
 667        check_bookmark hgrepo bad_bmark2 one &&
 668        check_bookmark hgrepo new_bmark ''
 669'
 670
 671test_expect_success 'remote double failed push' '
 672        test_when_finished "rm -rf hgrepo gitrepo*" &&
 673
 674        (
 675        hg init hgrepo &&
 676        cd hgrepo &&
 677        echo zero >content &&
 678        hg add content &&
 679        hg commit -m zero &&
 680        echo one >content &&
 681        hg commit -m one
 682        ) &&
 683
 684        (
 685        git clone "hg::hgrepo" gitrepo &&
 686        cd gitrepo &&
 687        git reset --hard HEAD^ &&
 688        echo two >content &&
 689        git commit -a -m two &&
 690        test_expect_code 1 git push &&
 691        test_expect_code 1 git push
 692        )
 693'
 694
 695test_done