如何使用 hg Convert 将 git 分支导入到 Mercurial 中?

2024-04-30

我的 git 存储库中有许多分支:

david@Panama ~/app: git branch -r  
origin/HEAD -> origin/master
origin/master
origin/newButtons
origin/newFonts
origin/serverView

如果我尝试将此 git 存储库导入到 Mercurial 中:

david@Panama ~/: hg convert app
...
david@Panama ~/app-hg: hg update
388 files updated, 0 files merged, 0 files removed, 0 files unresolved
david@Panama ~/app-hg: hg branches
default                     1148:6d04af619607

看起来分支已经“丢失”(就它们不再分离而言)并且确实合并到尖端中:

david@Panama ~/app-hg: hg log
changeset:   1148:6d04af619607
tag:         tip
user:        convert-repo
date:        Mon Nov 16 17:57:06 2009 +0000
summary:     update tags

changeset:   1147:742e7a01a6c9
parent:      1144:bff259181b22
user:        user1
date:        Sat Nov 14 17:47:09 2009 +0000
summary:     Playing around with fonts to get a cleaner look

changeset:   1146:162c1b0dd648
parent:      1144:bff259181b22
user:        user1
date:        Fri Nov 13 21:12:21 2009 +0000
summary:     Playing with new server view

changeset:   1145:aa06857832ab
user:        user1
date:        Sat Nov 14 13:54:12 2009 +0000
summary:     Updated buttons to something more fitting

changeset:   1144:bff259181b22
user:        David Mytton <>
date:        Fri Nov 13 10:35:51 2009 +0000
summary:     Example

鉴于这种情况:

a)我在这里导入分支是否做错了什么?

b) 分支机构真的可以进口吗?


这是设计使然。导入的 Git 分支仅在 Mercurial 中进行标记,并且hg heads应该给你正确数量的导入“分支”。

正如中提到的这个线程 http://www.selenic.com/pipermail/mercurial/2007-December/016061.html:

考虑一棵看起来像这样的树:

        o-o-o-o-o-o-b <- branch foo
       /
 -o-o-a
       \
        o-o-c <- branch bar

“a”及其祖先在哪个分支上?
我们没有丝毫线索。事实上,我们唯一确定的变更集是 b 和 c,因为分支名称不是历史的一部分。

So:

事实证明,实际上不可能正确地做到这一点,因为 git 没有存储足够的信息。
考虑一个 git 中有两个分支的存储库,每个分支都有多个提交。
由于 git 不记录每个提交源自哪个分支,因此树中没有足够的信息来标记每个变更集。
git 用户可以交换两个分支的名称,并且不会记录任何内容表明它曾经不同。如果两个分支有一个共同的祖先(而且几乎肯定会有), 该祖先在哪个分支上?我们不知道。

在一般情况下,我们能做的最好的事情就是将每个分支头标记为位于该分支上。然后,如果您进行增量转换,我们可能会做正确的事情。但是 git 的分支概念与 hg 的概念并不完美匹配,因此这种转换也不会是完美的。


您可以使用小型 Git 存储库(Git 1.6.5.1、Hg1.3.1)来测试它:

PS C:\Prog\Git\tests> cd .\hgimport
PS C:\Prog\Git\tests\hgimport> git init gitRepoToImport
PS C:\Prog\Git\tests\hgimport> cd .\gitRepoToImport
PS [...]\gitRepoToImport> echo firstContentToBr1 > br1.txt
PS [...]\gitRepoToImport> echo firstContentToBr2 > br2.txt
PS [...]\gitRepoToImport> echo firstContentToBr3 > br3.txt
PS [...]\gitRepoToImport> git add -A
PS [...]\gitRepoToImport> git commit -a -m "first content, to be evolved in three different branches"

在三个独立的分支中进行大量修改:

PS [...]\gitRepoToImport> git checkout -b br1
PS [...]\gitRepoToImport> echo firstEvolutionInBr1 >> .\br1.txt
PS [...]\gitRepoToImport> git commit -a -m "first evolution in branch 1"
PS [...]\gitRepoToImport> echo secondEvolutionInBr1 >> .\br1.txt
PS [...]\gitRepoToImport> git commit -a -m "second evolution in branch 1"
PS [...]\gitRepoToImport> git checkout master
PS [...]\gitRepoToImport> git checkout -b br2
PS [...]\gitRepoToImport> echo firstEvolutionInBr1 >> .\br2.txt
PS [...]\gitRepoToImport> git commit -a -m "first evolution in branch 2"
PS [...]\gitRepoToImport> git checkout master
PS [...]\gitRepoToImport> git checkout -b br3
PS [...]\gitRepoToImport> echo firstEvolutionInBr3 >> .\br3.txt
PS [...]\gitRepoToImport> git commit -a -m "first evolution in branch 3"
PS [...]\gitRepoToImport> echo secondEvolutionInBr3 >> .\br3.txt
PS [...]\gitRepoToImport> git commit -a -m "second evolution in branch 3"
PS [...]\gitRepoToImport> echo thirdEvolutionInBr3 >> .\br3.txt
PS [...]\gitRepoToImport> git commit -a -m "third evolution in branch 3"
PS [...]\gitRepoToImport> git checkout br2
PS [...]\gitRepoToImport> echo secondEvolutionInBr2 >> .\br2.txt
PS [...]\gitRepoToImport> git commit -a -m "second evolution in branch 2"
PS [...]\gitRepoToImport> git checkout br1
PS [...]\gitRepoToImport> echo thirdEvolutionInBr3 >> .\br1.txt
PS [...]\gitRepoToImport> git commit -a -m "third evolution in branch 1"
PS [...]\gitRepoToImport> git checkout br2
PS [...]\gitRepoToImport> echo thirdEvolutionInBr3 >> .\br2.txt
PS [...]\gitRepoToImport> git commit -a -m "third evolution in branch 2"

然后克隆该 Git 存储库(以防万一,以进行其他测试)

PS [...]\gitRepoToImport> cd ..
PS C:\Prog\Git\tests\hgimport> git clone .\gitRepoToImport gitRepoToImport1

Configure your ~/.hgrc with a format UTF-8 without BOM (took me a while to get it right!)

[extensions]
hgext.convert = 

然后进行转换

PS C:\Prog\Git\tests\hgimport> hg convert .\gitRepoToImport1 hgRepo
PS C:\Prog\Git\tests\hgimport> cd .\hgRepo
PS C:\Prog\Git\tests\hgimport\hgRepo> hg heads

您将得到三个预期的“分支”

changeset:   9:ad0884395ada
tag:         tip
user:        VonC
date:        Mon Nov 16 21:45:35 2009 +0100
summary:     third evolution in branch 2

changeset:   6:854bc6537c7c
user:        VonC
date:        Mon Nov 16 21:45:19 2009 +0100
summary:     third evolution in branch 1

changeset:   3:9194cf25d3ca
user:        VonC
date:        Mon Nov 16 21:44:09 2009 +0100
summary:     third evolution in branch 3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 hg Convert 将 git 分支导入到 Mercurial 中? 的相关文章

随机推荐