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#
   8test_description='Test remote-hg output compared to hg-git'
  10. ./test-lib.sh
  12if ! test_have_prereq PYTHON; then
  14        skip_all='skipping remote-hg tests; python not available'
  15        test_done
  16fi
  17if ! "$PYTHON_PATH" -c 'import mercurial'; then
  19        skip_all='skipping remote-hg tests; mercurial not available'
  20        test_done
  21fi
  22if ! "$PYTHON_PATH" -c 'import hggit'; then
  24        skip_all='skipping remote-hg tests; hg-git not available'
  25        test_done
  26fi
  27# clone to a git repo with git
  29git_clone_git () {
  30        hg -R $1 bookmark -f -r tip master &&
  31        git clone -q "hg::$PWD/$1" $2
  32}
  33# clone to an hg repo with git
  35hg_clone_git () {
  36        (
  37        hg init $2 &&
  38        cd $1 &&
  39        git push -q "hg::$PWD/../$2" 'refs/tags/*:refs/tags/*' 'refs/heads/*:refs/heads/*'
  40        ) &&
  41        (cd $2 && hg -q update)
  43}
  44# clone to a git repo with hg
  46git_clone_hg () {
  47        (
  48        git init -q $2 &&
  49        cd $1 &&
  50        hg bookmark -f -r tip master &&
  51        hg -q push -r master ../$2 || true
  52        )
  53}
  54# clone to an hg repo with hg
  56hg_clone_hg () {
  57        hg -q clone $1 $2
  58}
  59# push an hg repo with git
  61hg_push_git () {
  62        (
  63        cd $2
  64        old=$(git symbolic-ref --short HEAD)
  65        git checkout -q -b tmp &&
  66        git fetch -q "hg::$PWD/../$1" 'refs/tags/*:refs/tags/*' 'refs/heads/*:refs/heads/*' &&
  67        git checkout -q $old &&
  68        git branch -q -D tmp 2> /dev/null || true
  69        )
  70}
  71# push an hg git repo with hg
  73hg_push_hg () {
  74        (
  75        cd $1 &&
  76        hg -q push ../$2 || true
  77        )
  78}
  79hg_log () {
  81        hg -R $1 log --graph --debug | grep -v 'tag: *default/'
  82}
  83git_log () {
  85        git --git-dir=$1/.git fast-export --branches
  86}
  87setup () {
  89        (
  90        echo "[ui]"
  91        echo "username = A U Thor <author@example.com>"
  92        echo "[defaults]"
  93        echo "backout = -d \"0 0\""
  94        echo "commit = -d \"0 0\""
  95        echo "debugrawcommit = -d \"0 0\""
  96        echo "tag = -d \"0 0\""
  97        echo "[extensions]"
  98        echo "hgext.bookmarks ="
  99        echo "hggit ="
 100        ) >> "$HOME"/.hgrc &&
 101        git config --global receive.denycurrentbranch warn
 102        git config --global remote-hg.hg-git-compat true
 103        export HGEDITOR=/usr/bin/true
 105        export GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230"
 107        export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 108}
 109setup
 111test_expect_success 'merge conflict 1' '
 113        mkdir -p tmp && cd tmp &&
 114        test_when_finished "cd .. && rm -rf tmp" &&
 115        (
 117        hg init hgrepo1 &&
 118        cd hgrepo1 &&
 119        echo A > afile &&
 120        hg add afile &&
 121        hg ci -m "origin" &&
 122        echo B > afile &&
 124        hg ci -m "A->B" &&
 125        hg up -r0 &&
 127        echo C > afile &&
 128        hg ci -m "A->C" &&
 129        hg merge -r1 || true &&
 131        echo C > afile &&
 132        hg resolve -m afile &&
 133        hg ci -m "merge to C"
 134        ) &&
 135        for x in hg git; do
 137                git_clone_$x hgrepo1 gitrepo-$x &&
 138                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 139                hg_log hgrepo2-$x > hg-log-$x &&
 140                git_log gitrepo-$x > git-log-$x
 141        done &&
 142        test_cmp hg-log-hg hg-log-git &&
 144        test_cmp git-log-hg git-log-git
 145'
 146test_expect_success 'merge conflict 2' '
 148        mkdir -p tmp && cd tmp &&
 149        test_when_finished "cd .. && rm -rf tmp" &&
 150        (
 152        hg init hgrepo1 &&
 153        cd hgrepo1 &&
 154        echo A > afile &&
 155        hg add afile &&
 156        hg ci -m "origin" &&
 157        echo B > afile &&
 159        hg ci -m "A->B" &&
 160        hg up -r0 &&
 162        echo C > afile &&
 163        hg ci -m "A->C" &&
 164        hg merge -r1 || true &&
 166        echo B > afile &&
 167        hg resolve -m afile &&
 168        hg ci -m "merge to B"
 169        ) &&
 170        for x in hg git; do
 172                git_clone_$x hgrepo1 gitrepo-$x &&
 173                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 174                hg_log hgrepo2-$x > hg-log-$x &&
 175                git_log gitrepo-$x > git-log-$x
 176        done &&
 177        test_cmp hg-log-hg hg-log-git &&
 179        test_cmp git-log-hg git-log-git
 180'
 181test_expect_success 'converged merge' '
 183        mkdir -p tmp && cd tmp &&
 184        test_when_finished "cd .. && rm -rf tmp" &&
 185        (
 187        hg init hgrepo1 &&
 188        cd hgrepo1 &&
 189        echo A > afile &&
 190        hg add afile &&
 191        hg ci -m "origin" &&
 192        echo B > afile &&
 194        hg ci -m "A->B" &&
 195        echo C > afile &&
 197        hg ci -m "B->C" &&
 198        hg up -r0 &&
 200        echo C > afile &&
 201        hg ci -m "A->C" &&
 202        hg merge -r2 || true &&
 204        hg ci -m "merge"
 205        ) &&
 206        for x in hg git; do
 208                git_clone_$x hgrepo1 gitrepo-$x &&
 209                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 210                hg_log hgrepo2-$x > hg-log-$x &&
 211                git_log gitrepo-$x > git-log-$x
 212        done &&
 213        test_cmp hg-log-hg hg-log-git &&
 215        test_cmp git-log-hg git-log-git
 216'
 217test_expect_success 'encoding' '
 219        mkdir -p tmp && cd tmp &&
 220        test_when_finished "cd .. && rm -rf tmp" &&
 221        (
 223        git init -q gitrepo &&
 224        cd gitrepo &&
 225        echo alpha > alpha &&
 227        git add alpha &&
 228        git commit -m "add älphà" &&
 229        export GIT_AUTHOR_NAME="tést èncödîng" &&
 231        echo beta > beta &&
 232        git add beta &&
 233        git commit -m "add beta" &&
 234        echo gamma > gamma &&
 236        git add gamma &&
 237        git commit -m "add gämmâ" &&
 238        : TODO git config i18n.commitencoding latin-1 &&
 240        echo delta > delta &&
 241        git add delta &&
 242        git commit -m "add déltà"
 243        ) &&
 244        for x in hg git; do
 246                hg_clone_$x gitrepo hgrepo-$x &&
 247                git_clone_$x hgrepo-$x gitrepo2-$x &&
 248                HGENCODING=utf-8 hg_log hgrepo-$x > hg-log-$x &&
 250                git_log gitrepo2-$x > git-log-$x
 251        done &&
 252        test_cmp hg-log-hg hg-log-git &&
 254        test_cmp git-log-hg git-log-git
 255'
 256test_expect_success 'file removal' '
 258        mkdir -p tmp && cd tmp &&
 259        test_when_finished "cd .. && rm -rf tmp" &&
 260        (
 262        git init -q gitrepo &&
 263        cd gitrepo &&
 264        echo alpha > alpha &&
 265        git add alpha &&
 266        git commit -m "add alpha" &&
 267        echo beta > beta &&
 268        git add beta &&
 269        git commit -m "add beta"
 270        mkdir foo &&
 271        echo blah > foo/bar &&
 272        git add foo &&
 273        git commit -m "add foo" &&
 274        git rm alpha &&
 275        git commit -m "remove alpha" &&
 276        git rm foo/bar &&
 277        git commit -m "remove foo/bar"
 278        ) &&
 279        for x in hg git; do
 281                (
 282                hg_clone_$x gitrepo hgrepo-$x &&
 283                cd hgrepo-$x &&
 284                hg_log . &&
 285                hg manifest -r 3 &&
 286                hg manifest
 287                ) > output-$x &&
 288                git_clone_$x hgrepo-$x gitrepo2-$x &&
 290                git_log gitrepo2-$x > log-$x
 291        done &&
 292        test_cmp output-hg output-git &&
 294        test_cmp log-hg log-git
 295'
 296test_expect_success 'git tags' '
 298        mkdir -p tmp && cd tmp &&
 299        test_when_finished "cd .. && rm -rf tmp" &&
 300        (
 302        git init -q gitrepo &&
 303        cd gitrepo &&
 304        git config receive.denyCurrentBranch ignore &&
 305        echo alpha > alpha &&
 306        git add alpha &&
 307        git commit -m "add alpha" &&
 308        git tag alpha &&
 309        echo beta > beta &&
 311        git add beta &&
 312        git commit -m "add beta" &&
 313        git tag -a -m "added tag beta" beta
 314        ) &&
 315        for x in hg git; do
 317                hg_clone_$x gitrepo hgrepo-$x &&
 318                hg_log hgrepo-$x > log-$x
 319        done &&
 320        test_cmp log-hg log-git
 322'
 323test_expect_success 'hg author' '
 325        mkdir -p tmp && cd tmp &&
 326        test_when_finished "cd .. && rm -rf tmp" &&
 327        for x in hg git; do
 329                (
 330                git init -q gitrepo-$x &&
 331                cd gitrepo-$x &&
 332                echo alpha > alpha &&
 334                git add alpha &&
 335                git commit -m "add alpha" &&
 336                git checkout -q -b not-master
 337                ) &&
 338                (
 340                hg_clone_$x gitrepo-$x hgrepo-$x &&
 341                cd hgrepo-$x &&
 342                hg co master &&
 344                echo beta > beta &&
 345                hg add beta &&
 346                hg commit -u "test" -m "add beta" &&
 347                echo gamma >> beta &&
 349                hg commit -u "test <test@example.com> (comment)" -m "modify beta" &&
 350                echo gamma > gamma &&
 352                hg add gamma &&
 353                hg commit -u "<test@example.com>" -m "add gamma" &&
 354                echo delta > delta &&
 356                hg add delta &&
 357                hg commit -u "name<test@example.com>" -m "add delta" &&
 358                echo epsilon > epsilon &&
 360                hg add epsilon &&
 361                hg commit -u "name <test@example.com" -m "add epsilon" &&
 362                echo zeta > zeta &&
 364                hg add zeta &&
 365                hg commit -u " test " -m "add zeta" &&
 366                echo eta > eta &&
 368                hg add eta &&
 369                hg commit -u "test < test@example.com >" -m "add eta" &&
 370                echo theta > theta &&
 372                hg add theta &&
 373                hg commit -u "test >test@example.com>" -m "add theta" &&
 374                echo iota > iota &&
 376                hg add iota &&
 377                hg commit -u "test <test <at> example <dot> com>" -m "add iota"
 378                ) &&
 379                hg_push_$x hgrepo-$x gitrepo-$x &&
 381                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 382                hg_log hgrepo2-$x > hg-log-$x &&
 384                git_log gitrepo-$x > git-log-$x
 385        done &&
 386        test_cmp git-log-hg git-log-git &&
 388        test_cmp hg-log-hg hg-log-git &&
 390        test_cmp git-log-hg git-log-git
 391'
 392test_expect_success 'hg branch' '
 394        mkdir -p tmp && cd tmp &&
 395        test_when_finished "cd .. && rm -rf tmp" &&
 396        for x in hg git; do
 398                (
 399                git init -q gitrepo-$x &&
 400                cd gitrepo-$x &&
 401                echo alpha > alpha &&
 403                git add alpha &&
 404                git commit -q -m "add alpha" &&
 405                git checkout -q -b not-master
 406                ) &&
 407                (
 409                hg_clone_$x gitrepo-$x hgrepo-$x &&
 410                cd hgrepo-$x &&
 412                hg -q co master &&
 413                hg mv alpha beta &&
 414                hg -q commit -m "rename alpha to beta" &&
 415                hg branch gamma | grep -v "permanent and global" &&
 416                hg -q commit -m "started branch gamma"
 417                ) &&
 418                hg_push_$x hgrepo-$x gitrepo-$x &&
 420                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 421                hg_log hgrepo2-$x > hg-log-$x &&
 423                git_log gitrepo-$x > git-log-$x
 424        done &&
 425        test_cmp hg-log-hg hg-log-git &&
 427        test_cmp git-log-hg git-log-git
 428'
 429test_expect_success 'hg tags' '
 431        mkdir -p tmp && cd tmp &&
 432        test_when_finished "cd .. && rm -rf tmp" &&
 433        for x in hg git; do
 435                (
 436                git init -q gitrepo-$x &&
 437                cd gitrepo-$x &&
 438                echo alpha > alpha &&
 440                git add alpha &&
 441                git commit -m "add alpha" &&
 442                git checkout -q -b not-master
 443                ) &&
 444                (
 446                hg_clone_$x gitrepo-$x hgrepo-$x &&
 447                cd hgrepo-$x &&
 449                hg co master &&
 450                hg tag alpha
 451                ) &&
 452                hg_push_$x hgrepo-$x gitrepo-$x &&
 454                hg_clone_$x gitrepo-$x hgrepo2-$x &&
 455                (
 457                git --git-dir=gitrepo-$x/.git tag -l &&
 458                hg_log hgrepo2-$x &&
 459                cat hgrepo2-$x/.hgtags
 460                ) > output-$x
 461        done &&
 462        test_cmp output-hg output-git
 464'
 465test_done