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