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