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