Git 与 Mercurial 存储库的互操作性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/883452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:28:49  来源:igfitidea点击:

Git interoperability with a Mercurial Repository

gitversion-controlmercurialinteropdvcs

提问by Hugo Sereno Ferreira

I use GIT on a Mac. Enough said. I have the tools, I have the experience. And I want to continue to use it. No wars here...

我在 Mac 上使用 GIT。说够了。我有工具,我有经验。我想继续使用它。这里没有War...

The problem is always with interoperability. Most people use SVN, which is great for me. Git SVN works out of the box, and is a no frills solution. People can continue happily use SVN and I don't lose my workflow and neither my tools.

问题始终在于互操作性。大多数人使用 SVN,这对我来说很棒。Git SVN 开箱即用,是一种简洁的解决方案。人们可以继续愉快地使用 SVN,我不会丢失我的工作流程和我的工具。

Now... Some guys come along with Mercurial. Fine for them: they have their reasons. But I can't find any GIT HG out-of-the-box. I don't want to switch to HG, but I still need to interoperate with their repository.

现在……有些人和 Mercurial 一起来了。对他们来说很好:他们有他们的理由。但是我找不到任何开箱即用的 GIT HG。我不想切换到 HG,但我仍然需要与他们的存储库进行互操作。

Any of you guys know a simple solution for this?

你们中的任何人都知道一个简单的解决方案吗?

采纳答案by dubiousjim

Update from June 2012. Currently there seem to be the following methods for Git/Hg interoperability when the developer wants to work from the git side:

2012 年 6 月更新。 目前,当开发人员想要从 git 端工作时,似乎有以下方法用于 Git/Hg 互操作性:

  1. Install Mercurial and the hg-git extension. You can do the latter using your package manager, or with easy_install hg-git. Then make sure the following is in your ~/.hgrc:

    [extensions]
    hggit = 
    

    You may see some references that talk about specifying the bookmarksextension here too, but that has been built into Mercurial since v 1.8. Here are some tips about installing hg-git on Windows.

    Once you have hg-git, you can use commands roughly like Abderrahim Kitouni posted above. This method has been refined and tweakedsince 2009 though, and there is a friendly wrapper: git-hg-again. This uses the toplevel directory as a working directory for both Mercurial and Git at the same time. It creates a Mercurial bookmark that it keeps in synch with the tip of the default(unnamed) branch in the Mercurial repository, and it updates a local Git branch from that bookmark.

  2. git-remote-hgis a different wrapper, also based on the Mercurial hg-gitextension. This additionally makes use of the git-remote-helpersprotocols (hence its name). It uses the toplevel directory only for a Git working directory; it keeps its Mercurial repository bare. It also maintains a second bare Git repository to make synching between Git and Mercurial safer and more idiomatically gitlike.

  3. The git-hgscript (formerly maintained here) uses a different method, based on hg-fast-exportfrom the fast-export project. Like method 2, this also keeps a bare Mercurial repository and an additional bare Git repository.

    For pulling, this tool ignores Mercurial bookmarks and instead imports every named Mercurial branch into a Git branch, and the default (unnamed) Mercurial branch into master.

    Some commentary discusses this tool as being hg->git only, but it claims to have merged in git->hg push support on 7 Dec 2011. As I explain in a review of these tools, though, the way this tool tries to implement push support doesn't seem to be workable.

  4. There's also another project called git-remote-hg. Unlike the version listed above, this one doesn't rely on hg-git, but instead directly accesses the Mercurial Python API. At the moment, using it also requires a patched version of git. I haven't tried this yet.

  5. Finally, Tailoris a project that incrementally converts between a variety of different VCSs. It sounds like development of this won't be aggressively continued.

  1. 安装 Mercurial 和hg-git 扩展。您可以使用包管理器或使用easy_install hg-git. 然后确保以下内容在您的 ~/.hgrc 中:

    [extensions]
    hggit = 
    

    您可能会在bookmarks此处看到一些讨论指定扩展名的参考资料,但自 v 1.8 以来,它已内置到 Mercurial 中。这里有一些关于在 Windows 上安装 hg-git 的提示

    一旦你有了 hg-git,你就可以使用类似于Abderrahim Kitouni 上面发布的命令。不过,自 2009 年以来,此方法已得到改进和调整,并且有一个友好的包装器:git-hg-again。这同时使用顶层目录作为 Mercurial 和 Git 的工作目录。它创建一个 Mercurial 书签,它与defaultMercurial 存储库中(未命名)分支的尖端保持同步,并从该书签更新本地 Git 分支。

  2. git-remote-hg是一个不同的包装器,也基于 Mercurialhg-git扩展。这还使用了git-remote-helpers协议(因此得名)。它仅将顶层目录用于 Git 工作目录;它保持其 Mercurial 存储库裸露。它还维护了第二个裸 Git 存储库,使 Git 和 Mercurial 之间的同步更安全,更符合 git 风格。

  3. 混帐-HG脚本(前身为维护这里)使用不同的方法的基础上,hg-fast-export快速出口项目。与方法 2 一样,这也保留了一个裸 Mercurial 存储库和一个额外的裸 Git 存储库。

    对于拉取,此工具会忽略 Mercurial 书签,而是将每个命名的 Mercurial 分支导入 Git 分支,并将默认(未命名)的 Mercurial 分支导入 master。

    一些评论仅讨论此工具为 hg->git,但它声称已于 2011 年 12 月 7 日合并到 git->hg 推送支持中。不过,正如我在对这些工具的评论中所解释的,该工具尝试实现的方式推送支持似乎不可行。

  4. 还有另一个名为 git-remote-hg 的项目。与上面列出的版本不同,这个版本不依赖于 hg-git,而是直接访问 Mercurial Python API。目前,使用它还需要一个补丁版本的 git。我还没有尝试过这个。

  5. 最后,Tailor是一个在各种不同的 VCS 之间进行增量转换的项目。听起来这方面的发展不会积极地继续下去。

The first three of these approaches looked lightweight enough to persuade me to investigate. I needed to tweak them in some ways to get them to run on my setup, and I saw some ways to tweak them further to improve them, and then I tweaked them still further to make them behave more like each other so that I could evaluate them more effectively. Then I thought others might like to have these tweaks too, to do the same evaluation. So I've made a source packagethat will enable you to install my versions of any of the first three tools. It should also take care of installing the needed hg-fast-exportpieces. (You need to install hg-giton your own.)

这些方法中的前三种看起来足够轻量级,足以说服我进行调查。我需要以某种方式调整它们以使它们在我的设置上运行,我看到了一些进一步调整它们以改进它们的方法,然后我进一步调整它们以使它们表现得更像彼此,以便我可以评估他们更有效。然后我想其他人可能也喜欢进行这些调整,以进行相同的评估。所以我制作了一个源代码包,可以让您安装我的前三个工具中的任何一个版本。它还应该负责安装所需的hg-fast-export部件。(您需要自行安装hg-git。)

I encourage you to try them out and decide for yourself what works best. I'll be glad to hear about cases where these tools break. I'll try to keep them in synch with upstream changes, and to make sure the upstream authors are aware of the tweaks I think are useful.

我鼓励您尝试一下,然后自己决定什么最有效。我很高兴听到这些工具失效的情况。我会尽量让它们与上游更改保持同步,并确保上游作者知道我认为有用的调整。

As I mentioned above, in evaluating these tools, I came to the conclusion that git-hgis only usable for pulling from Mercurial, not for pushing.

正如我上面提到的,在评估这些工具时,我得出的结论git-hg是只能用于从 Mercurial 拉取,不能用于推送。

Relatedly, here are some useful comparisons/translation manuals between Git and Mercurial, in some cases targetted at users who already know Git:

相关地,这里有一些 Git 和 Mercurial 之间有用的比较/翻译手册,在某些情况下针对已经了解 Git 的用户:

回答by FelipeC

There's a new git-remote-hg that provides native support:

有一个新的 git-remote-hg 提供本机支持:

Bridge support in Git for Mercurial and Bazaar

在 Git 中为 Mercurial 和 Bazaar 提供桥接支持

Just copy git-remote-hgto your $PATH, make it executable, and that's it, no dependencies (other than Mercurial):

只需将git-remote-hg复制到您的 $PATH,使其可执行,就是这样,没有依赖项(Mercurial 除外):

git clone hg::https://www.mercurial-scm.org/repo/hg/

You should be able to push and pull from it as if it was a native Git repository.

您应该能够像它是本机 Git 存储库一样推送和拉取它。

When you push new Git branches, Mercurial bookmarks will be created for them.

当您推送新的 Git 分支时,将为它们创建 Mercurial 书签。

See the git-remote-hg wikifor more information.

有关更多信息,请参阅git-remote-hg wiki

回答by Abderrahim Kitouni

You should be able to use hg-git.

您应该能够使用hg-git

hg clone <hg repository>

edit ~/.hgrcand add :

编辑~/.hgrc并添加:

[extensions]
hgext.bookmarks =
hggit =

create a bookmark so you will have a masterin git :

创建一个书签,以便您master在 git 中有一个:

cd <repository>
hg bookmark -r default master

edit .hg/hgrcin the repository and add :

.hg/hgrc在存储库中编辑并添加:

[git]
intree = true

now you can create the git repository :

现在您可以创建 git 存储库:

hg gexport

and you can use the resulting directory as a git clone. pulling from mercurial would be :

并且您可以将生成的目录用作 git clone。从 mercurial 中提取将是:

hg pull
hg gexport

and pushing to mercurial :

并推到 mercurial :

hg gimport
hg push

(Yes, you need to use hg with this workflow but your hacking will be all in git)

(是的,您需要在此工作流程中使用 hg,但您的黑客攻击将全部在 git 中进行)

P.S. If you have a problem with this workflow, please file a bug.

PS 如果您对此工作流程有疑问,请提交错误。

回答by sykora

You can try hg2git, which is python script and is part of fast-export, which you can find at http://repo.or.cz/w/fast-export.git.

您可以尝试hg2git,它是 python 脚本并且是快速导出的一部分,您可以在http://repo.or.cz/w/fast-export.git找到它。

You'll need to have mercurial installed though.

不过,您需要安装 mercurial。

回答by Martin Geisler

Since hg-git is a two-way bridge, it will also allow you to push changesets from Git to Mercurial.

由于HG-Git是一个2个三通桥,它也可以让你从Git的变更推到水银。

回答by ralphtheninja

Hg-Git Mercurial Plugin. Haven't tried it myself, but might be worth checking out.

Hg-Git Mercurial 插件。自己没有尝试过,但可能值得一试。

回答by Mikko Rantalainen

I have had great success with git-hgfrom https://github.com/cosmin/git-hg(requires working install of hg, too). It supports fetch, pull and pushand is more stable for me than hg-git(similar features from hgto git).

git-hghttps://github.com/cosmin/git-hg取得了巨大的成功(也需要安装hg)。它支持 fetch、pull 和push并且对我来说比hg-githggit 的类似功能)更稳定。

See https://github.com/cosmin/git-hg#usagefor usage examples. The user interface is very similar to git-svn.

有关使用示例,请参阅https://github.com/cosmin/git-hg#usage。用户界面与git-svn.

The git-hgrequires extra disk space for each cloned hg repo. The implementation uses full mercurial clone, an extra git bare clone and the actual git repo. The required disk space is roughly 3 times the normal git only usage. The extra copies are stored below the .gitdirectory of your working directory (or location pointed by GIT_DIRas usual).

git-hg要求每个克隆的汞回购额外的磁盘空间。该实现使用完整的 mercurial 克隆、额外的 git 裸克隆和实际的 git 存储库。所需的磁盘空间大约是正常 git only 使用量的 3 倍。额外的副本存储在.git您的工作目录(或GIT_DIR照常指向的位置)的目录下。

Notice:The basic problem that git-hgtries to solve is that there is no 1:1 mapping between gitand hgfeatures. The biggest problem is the impedance mismatch between git branches and hg unnamed branchesand hg named branchesand hg bookmarks(all of those look a lot like branches to gitusers). A related problem is that hgtries to save original named branch name in the version history as opposed to git where the branch name is only added to template commit message by default.

注意:git-hg试图解决的基本问题是githg特征之间没有 1:1 的映射。最大的问题是 git 分支和hg 未命名分支以及hg 命名分支hg 书签之间的阻抗不匹配(所有这些看起来很像git用户的分支)。一个相关的问题是hg尝试将原始命名分支名称保存在版本历史记录中,而不是 git,默认情况下,分支名称仅添加到模板提交消息中。

Any tool that claims to create interoperable bridge between gitand hgshould explain how it's going to deal with this impedance match.You can then decide if the selected solution fits your needs.

任何工具,要求建立可互操作之间的桥梁git,并hg应说明它是如何去应对这种阻抗匹配。然后,您可以决定所选的解决方案是否符合您的需求。

The solution that git-hguses is to discard all hg bookmarks and convert named branches to git branches. In addition it sets the git master branch to default unnamed hg branch.

使用的解决方案git-hg是丢弃所有 hg 书签并将命名分支转换为 git 分支。此外,它还将 git master 分支设置为默认的未命名 hg 分支。

回答by Wizz

Have tried hggit. Works for me, since I have to cope the work of git'ers and hg'ers. Especially for reviews this is great.

试过hggit。对我有用,因为我必须应付 git'ers 和 hg'ers 的工作。特别是对于评论,这很棒。

A minor issue/warning on that topic:

关于该主题的一个小问题/警告:

I have tried to clone a stable linux kernel repository with hg. These repositories are maintained in git and typically have a large number of files in it.

我试图用 hg 克隆一个稳定的 linux 内核存储库。这些存储库在 git 中维护,并且通常在其中包含大量文件。

It was very slow. Took me 2 days to fully clone andupdate a working copy.

这是非常缓慢的。我花了 2 天的时间来完全克隆更新工作副本。

回答by weynhamz

I have tried cosmin's git-hgand abourget's git-hg-againboth on mutt's hg repo, it seems that the later respects the order of a merge well, the former is a bit random. You can see from the screenshots below.

我已经在mutt 的 hg repo上尝试过cosmin 的 git-hgabourget 的 git-hg-again,似乎后者尊重合并的顺序,前者有点随机。您可以从下面的屏幕截图中看到。

A merge history graph of mutt imported by cosmin's git-hg:

cosmin 的 git-hg导入的 mutt 的合并历史图:

enter image description here

在此处输入图片说明

A merge history graph of mutt imported by abourget's git-hg-again:

abourget 的 git-hg-again导入的 mutt 的合并历史图:

enter image description here

在此处输入图片说明

The actuall history graph plotted by hgk on mutt's hg repository:

hgk 在 mutt 的 hg 存储库上绘制的实际历史图:

enter image description here

在此处输入图片说明

As you can see from the above, the second graph by abourget's git-hg-againis very close to the original hgk graph and is actually reflecting the real workflow of the mutt.

从上面可以看出,abourget 的 git-hg-again的第二张图非常接近原始的 hgk 图,实际上反映了 mutt 的真实工作流程。

One drawback of git-hg-again I found is that it does not add a 'hg' remote, rather imports all its refs as local tags, git-hg has a wonderful 'hg' remote represents the upstream hg repo.

我发现 git-hg-again 的一个缺点是它没有添加“hg”遥控器,而是将其所有引用作为本地标签导入,git-hg 有一个很棒的“hg”遥控器代表上游 hg 存储库。

回答by Piedone

Two-way hg-git (and git-git, hg-hg) sync is also possible with the service Git-hg Mirror. It uses hg-git (among others) behind the scenes and its code is also open source.

使用Git-hg Mirror服务也可以实现双向 hg-git(和 git-git、hg-hg)同步。它在幕后使用 hg-git(以及其他),其代码也是开源的。



Disclaimer: I'm from the company behind it.

免责声明:我来自背后的公司。