Git桥接到Mercurial

4
我主要使用Git并在github上有很多代码。我也想将它放到Bitbucket上,供使用mercurial的人使用,更重要的是,我也想将代码放在我的域名上,而Bitbucket支持Cnames来托管代码。
那么有没有办法让我主要使用Git,但也能推送到HG呢?
Github创建了一个项目,可以将HG存储库转换为git,但是否有一个适用于我所需方向的项目呢?

可能是Git与Mercurial存储库的互操作性的重复问题。 - skolima
2个回答

7

我原本想把这个内容加到我的另一个答案里,但它有点长,所以我们将它作为一个单独的答案。

如果你想双向操作,可以使用hg-git在你的机器上得到hg版本的repo。你仍然可以在Git中完成所有工作,只是意味着你将使用GitHub作为中介。

$ cd src
# do some work
# push to GitHub
$ cd ../hg
$ hg pull
$ hg push bitbucket

然而,这样做的好处在于如果你想从一个Mercurial用户那里拉取更改,你可以将它们拉到 hg 存储库中,然后将它们推送到GitHub。

$ cd hg
$ hg pull someotherrepo
  # Probably merge
$ hg push # Changes go to GitHub
$ cd ../src
$ git pull
  # Continue working in Git

4
如果您只是在镜像Git存储库,那么可以设置一个cron任务来运行以下脚本:
#!/bin/bash

USER=bitbucketuser
ERROR_FILE=clone_update_err
CLONES=/path/to/clones

cd $CLONES

if [ $# -ne 1 ]; then
    for DIR in *; do
        if [ -d $DIR ]; then
            ./update.sh $DIR 2> $ERROR_FILE
            if [ -s $ERROR_FILE ]; then
                echo $DIR >&2
                cat -n $ERROR_FILE >&2
            fi
        fi
    done
else
    DIR=$1
    echo $DIR
    cd $DIR

    if [ -a source ]; then
        cd source
        if [ -d .hg ]; then
            hg pull
        elif [ -d .git ]; then
            git pull
        elif [ -d .bzr ]; then
            bzr pull
        elif [ -d .svn ]; then
            svn up
        else
            echo "$DIR is not a known repository type."
            return 1
        fi

        cd ..
        hg convert source hg

        URL=ssh://hg@bitbucket.org/$USER/$DIR/
        cd hg
        hg pull $URL
        hg push $URL # You can add -f here if you don't mind multiple heads
        cd ..
    else
        # hg dir or hg-git
        cd hg
        hg pull
        hg push $URL
        cd ..
    fi

    cd ..

    sleep 5
fi

假设您已安装了SSH密钥。正如您所看到的,它也会镜像其他版本控制系统。它假定有这样的目录结构:

clones/
  project1/
    source/  # Original Git/Bazaar/SVN/Mercurial repo
    hg/      # Mercurial repo

很明显,这只是单向的,但如果你所有的工作都在Git中完成,那么就不会有影响。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接