53d2504e12b68eeec7756ead0699dd5a5fd4b8fd
   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                '')
  69                        grep "^   [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
  70                        ;;
  71                esac
  72                let 'ref_ret' && echo "match for '$branch' failed" && break
  73        done
  74
  75        if let 'expected_ret != ret || ref_ret'
  76        then
  77                return 1
  78        fi
  79
  80        return 0
  81}
  82
  83setup () {
  84        (
  85        echo "[ui]"
  86        echo "username = H G Wells <wells@example.com>"
  87        echo "[extensions]"
  88        echo "mq ="
  89        ) >> "$HOME"/.hgrc &&
  90
  91        GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
  92        GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
  93        export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  94}
  95
  96setup
  97
  98test_expect_success 'cloning' '
  99        test_when_finished "rm -rf gitrepo*" &&
 100
 101        (
 102        hg init hgrepo &&
 103        cd hgrepo &&
 104        echo zero > content &&
 105        hg add content &&
 106        hg commit -m zero
 107        ) &&
 108
 109        git clone "hg::hgrepo" gitrepo &&
 110        check gitrepo HEAD zero
 111'
 112
 113test_expect_success 'cloning with branches' '
 114        test_when_finished "rm -rf gitrepo*" &&
 115
 116        (
 117        cd hgrepo &&
 118        hg branch next &&
 119        echo next > content &&
 120        hg commit -m next
 121        ) &&
 122
 123        git clone "hg::hgrepo" gitrepo &&
 124        check gitrepo origin/branches/next next
 125'
 126
 127test_expect_success 'cloning with bookmarks' '
 128        test_when_finished "rm -rf gitrepo*" &&
 129
 130        (
 131        cd hgrepo &&
 132        hg checkout default &&
 133        hg bookmark feature-a &&
 134        echo feature-a > content &&
 135        hg commit -m feature-a
 136        ) &&
 137
 138        git clone "hg::hgrepo" gitrepo &&
 139        check gitrepo origin/feature-a feature-a
 140'
 141
 142test_expect_success 'update bookmark' '
 143        test_when_finished "rm -rf gitrepo*" &&
 144
 145        (
 146        cd hgrepo &&
 147        hg bookmark devel
 148        ) &&
 149
 150        (
 151        git clone "hg::hgrepo" gitrepo &&
 152        cd gitrepo &&
 153        git checkout --quiet devel &&
 154        echo devel > content &&
 155        git commit -a -m devel &&
 156        git push --quiet
 157        ) &&
 158
 159        check_bookmark hgrepo devel devel
 160'
 161
 162test_expect_success 'new bookmark' '
 163        test_when_finished "rm -rf gitrepo*" &&
 164
 165        (
 166        git clone "hg::hgrepo" gitrepo &&
 167        cd gitrepo &&
 168        git checkout --quiet -b feature-b &&
 169        echo feature-b > content &&
 170        git commit -a -m feature-b &&
 171        git push --quiet origin feature-b
 172        ) &&
 173
 174        check_bookmark hgrepo feature-b feature-b
 175'
 176
 177# cleanup previous stuff
 178rm -rf hgrepo
 179
 180author_test () {
 181        echo $1 >> content &&
 182        hg commit -u "$2" -m "add $1" &&
 183        echo "$3" >> ../expected
 184}
 185
 186test_expect_success 'authors' '
 187        test_when_finished "rm -rf hgrepo gitrepo" &&
 188
 189        (
 190        hg init hgrepo &&
 191        cd hgrepo &&
 192
 193        touch content &&
 194        hg add content &&
 195
 196        > ../expected &&
 197        author_test alpha "" "H G Wells <wells@example.com>" &&
 198        author_test beta "test" "test <unknown>" &&
 199        author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
 200        author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
 201        author_test delta "name<test@example.com>" "name <test@example.com>" &&
 202        author_test epsilon "name <test@example.com" "name <test@example.com>" &&
 203        author_test zeta " test " "test <unknown>" &&
 204        author_test eta "test < test@example.com >" "test <test@example.com>" &&
 205        author_test theta "test >test@example.com>" "test <test@example.com>" &&
 206        author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
 207        author_test kappa "test@example.com" "Unknown <test@example.com>"
 208        ) &&
 209
 210        git clone "hg::hgrepo" gitrepo &&
 211        git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
 212
 213        test_cmp expected actual
 214'
 215
 216test_expect_success 'strip' '
 217        test_when_finished "rm -rf hgrepo gitrepo" &&
 218
 219        (
 220        hg init hgrepo &&
 221        cd hgrepo &&
 222
 223        echo one >> content &&
 224        hg add content &&
 225        hg commit -m one &&
 226
 227        echo two >> content &&
 228        hg commit -m two
 229        ) &&
 230
 231        git clone "hg::hgrepo" gitrepo &&
 232
 233        (
 234        cd hgrepo &&
 235        hg strip 1 &&
 236
 237        echo three >> content &&
 238        hg commit -m three &&
 239
 240        echo four >> content &&
 241        hg commit -m four
 242        ) &&
 243
 244        (
 245        cd gitrepo &&
 246        git fetch &&
 247        git log --format="%s" origin/master > ../actual
 248        ) &&
 249
 250        hg -R hgrepo log --template "{desc}\n" > expected &&
 251        test_cmp actual expected
 252'
 253
 254test_expect_success 'remote push with master bookmark' '
 255        test_when_finished "rm -rf hgrepo gitrepo*" &&
 256
 257        (
 258        hg init hgrepo &&
 259        cd hgrepo &&
 260        echo zero > content &&
 261        hg add content &&
 262        hg commit -m zero &&
 263        hg bookmark master &&
 264        echo one > content &&
 265        hg commit -m one
 266        ) &&
 267
 268        (
 269        git clone "hg::hgrepo" gitrepo &&
 270        cd gitrepo &&
 271        echo two > content &&
 272        git commit -a -m two &&
 273        git push
 274        ) &&
 275
 276        check_branch hgrepo default two
 277'
 278
 279cat > expected <<EOF
 280changeset:   0:6e2126489d3d
 281tag:         tip
 282user:        A U Thor <author@example.com>
 283date:        Mon Jan 01 00:00:00 2007 +0230
 284summary:     one
 285
 286EOF
 287
 288test_expect_success 'remote push from master branch' '
 289        test_when_finished "rm -rf hgrepo gitrepo*" &&
 290
 291        hg init hgrepo &&
 292
 293        (
 294        git init gitrepo &&
 295        cd gitrepo &&
 296        git remote add origin "hg::../hgrepo" &&
 297        echo one > content &&
 298        git add content &&
 299        git commit -a -m one &&
 300        git push origin master
 301        ) &&
 302
 303        hg -R hgrepo log > actual &&
 304        cat actual &&
 305        test_cmp expected actual &&
 306
 307        check_branch hgrepo default one
 308'
 309
 310GIT_REMOTE_HG_TEST_REMOTE=1
 311export GIT_REMOTE_HG_TEST_REMOTE
 312
 313test_expect_success 'remote cloning' '
 314        test_when_finished "rm -rf gitrepo*" &&
 315
 316        (
 317        hg init hgrepo &&
 318        cd hgrepo &&
 319        echo zero > content &&
 320        hg add content &&
 321        hg commit -m zero
 322        ) &&
 323
 324        git clone "hg::hgrepo" gitrepo &&
 325        check gitrepo HEAD zero
 326'
 327
 328test_expect_success 'remote update bookmark' '
 329        test_when_finished "rm -rf gitrepo*" &&
 330
 331        (
 332        cd hgrepo &&
 333        hg bookmark devel
 334        ) &&
 335
 336        (
 337        git clone "hg::hgrepo" gitrepo &&
 338        cd gitrepo &&
 339        git checkout --quiet devel &&
 340        echo devel > content &&
 341        git commit -a -m devel &&
 342        git push --quiet
 343        ) &&
 344
 345        check_bookmark hgrepo devel devel
 346'
 347
 348test_expect_success 'remote new bookmark' '
 349        test_when_finished "rm -rf gitrepo*" &&
 350
 351        (
 352        git clone "hg::hgrepo" gitrepo &&
 353        cd gitrepo &&
 354        git checkout --quiet -b feature-b &&
 355        echo feature-b > content &&
 356        git commit -a -m feature-b &&
 357        git push --quiet origin feature-b
 358        ) &&
 359
 360        check_bookmark hgrepo feature-b feature-b
 361'
 362
 363test_expect_success 'remote push diverged' '
 364        test_when_finished "rm -rf gitrepo*" &&
 365
 366        git clone "hg::hgrepo" gitrepo &&
 367
 368        (
 369        cd hgrepo &&
 370        hg checkout default &&
 371        echo bump > content &&
 372        hg commit -m bump
 373        ) &&
 374
 375        (
 376        cd gitrepo &&
 377        echo diverge > content &&
 378        git commit -a -m diverged &&
 379        check_push 1 <<-EOF
 380        master:non-fast-forward
 381        EOF
 382        ) &&
 383
 384        check_branch hgrepo default bump
 385'
 386
 387test_expect_success 'remote update bookmark diverge' '
 388        test_when_finished "rm -rf gitrepo*" &&
 389
 390        (
 391        cd hgrepo &&
 392        hg checkout tip^ &&
 393        hg bookmark diverge
 394        ) &&
 395
 396        git clone "hg::hgrepo" gitrepo &&
 397
 398        (
 399        cd hgrepo &&
 400        echo "bump bookmark" > content &&
 401        hg commit -m "bump bookmark"
 402        ) &&
 403
 404        (
 405        cd gitrepo &&
 406        git checkout --quiet diverge &&
 407        echo diverge > content &&
 408        git commit -a -m diverge &&
 409        check_push 1 <<-EOF
 410        diverge:non-fast-forward
 411        EOF
 412        ) &&
 413
 414        check_bookmark hgrepo diverge "bump bookmark"
 415'
 416
 417test_expect_success 'remote new bookmark multiple branch head' '
 418        test_when_finished "rm -rf gitrepo*" &&
 419
 420        (
 421        git clone "hg::hgrepo" gitrepo &&
 422        cd gitrepo &&
 423        git checkout --quiet -b feature-c HEAD^ &&
 424        echo feature-c > content &&
 425        git commit -a -m feature-c &&
 426        git push --quiet origin feature-c
 427        ) &&
 428
 429        check_bookmark hgrepo feature-c feature-c
 430'
 431
 432# cleanup previous stuff
 433rm -rf hgrepo
 434
 435setup_big_push () {
 436        (
 437        hg init hgrepo &&
 438        cd hgrepo &&
 439        echo zero > content &&
 440        hg add content &&
 441        hg commit -m zero &&
 442        hg bookmark bad_bmark1 &&
 443        echo one > content &&
 444        hg commit -m one &&
 445        hg bookmark bad_bmark2 &&
 446        hg bookmark good_bmark &&
 447        hg bookmark -i good_bmark &&
 448        hg -q branch good_branch &&
 449        echo "good branch" > content &&
 450        hg commit -m "good branch" &&
 451        hg -q branch bad_branch &&
 452        echo "bad branch" > content &&
 453        hg commit -m "bad branch"
 454        ) &&
 455
 456        git clone "hg::hgrepo" gitrepo &&
 457
 458        (
 459        cd gitrepo &&
 460        echo two > content &&
 461        git commit -q -a -m two &&
 462
 463        git checkout -q good_bmark &&
 464        echo three > content &&
 465        git commit -q -a -m three &&
 466
 467        git checkout -q bad_bmark1 &&
 468        git reset --hard HEAD^ &&
 469        echo four > content &&
 470        git commit -q -a -m four &&
 471
 472        git checkout -q bad_bmark2 &&
 473        git reset --hard HEAD^ &&
 474        echo five > content &&
 475        git commit -q -a -m five &&
 476
 477        git checkout -q -b new_bmark master &&
 478        echo six > content &&
 479        git commit -q -a -m six &&
 480
 481        git checkout -q branches/good_branch &&
 482        echo seven > content &&
 483        git commit -q -a -m seven &&
 484        echo eight > content &&
 485        git commit -q -a -m eight &&
 486
 487        git checkout -q branches/bad_branch &&
 488        git reset --hard HEAD^ &&
 489        echo nine > content &&
 490        git commit -q -a -m nine &&
 491
 492        git checkout -q -b branches/new_branch master &&
 493        echo ten > content &&
 494        git commit -q -a -m ten
 495        )
 496}
 497
 498test_expect_success 'remote big push' '
 499        test_when_finished "rm -rf hgrepo gitrepo*" &&
 500
 501        setup_big_push
 502
 503        (
 504        cd gitrepo &&
 505
 506        check_push 1 --all <<-EOF
 507        master
 508        good_bmark
 509        branches/good_branch
 510        new_bmark:new
 511        branches/new_branch:new
 512        bad_bmark1:non-fast-forward
 513        bad_bmark2:non-fast-forward
 514        branches/bad_branch:non-fast-forward
 515        EOF
 516        ) &&
 517
 518        check_branch hgrepo default one &&
 519        check_branch hgrepo good_branch "good branch" &&
 520        check_branch hgrepo bad_branch "bad branch" &&
 521        check_branch hgrepo new_branch '' &&
 522        check_bookmark hgrepo good_bmark one &&
 523        check_bookmark hgrepo bad_bmark1 one &&
 524        check_bookmark hgrepo bad_bmark2 one &&
 525        check_bookmark hgrepo new_bmark ''
 526'
 527
 528test_expect_success 'remote double failed push' '
 529        test_when_finished "rm -rf hgrepo gitrepo*" &&
 530
 531        (
 532        hg init hgrepo &&
 533        cd hgrepo &&
 534        echo zero > content &&
 535        hg add content &&
 536        hg commit -m zero &&
 537        echo one > content &&
 538        hg commit -m one
 539        ) &&
 540
 541        (
 542        git clone "hg::hgrepo" gitrepo &&
 543        cd gitrepo &&
 544        git reset --hard HEAD^ &&
 545        echo two > content &&
 546        git commit -a -m two &&
 547        test_expect_code 1 git push &&
 548        test_expect_code 1 git push
 549        )
 550'
 551
 552test_done