如何在没有共同历史记录的情况下重新集成svn和git存储库?

jwh*_*ock 12 git git-svn

我有一个基于github的git存储库,它代表某个点的开发,然后是一个没有用git svn初始化的svn存储库,它有进一步的开发.我想将svn更改带入git存储库,开始使用git repo进行开发,并使用git svn dcommit推送更改.这可能吗?这是可取的吗?

这是我的具体内容:

我们在这里开始使用WordPress插件进行开发:

http://github.com/mrdoornbos/wpconfidentcaptcha

Master在ef82b94a1232b44aae3e,github没有进一步的更改.

当我们接受wp-plugins.org的申请时,为我们创建了一个空的svn仓库:

http://svn.wp-plugins.org/wp-confident-captcha/trunk@278927

然后将某些修改过的文件复制到(r256425)中.进行了进一步的修改,最后一次是r278935.

我想要的是将SVN更改与git svn元数据一起应用于master.

这是我到目前为止(大约需要4分钟):

git clone git://github.com/mrdoornbos/wpconfidentcaptcha.git github_cc
cd github_cc
git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
git svn fetch --revision 256362:278935
Run Code Online (Sandbox Code Playgroud)

这将我的github树放在origin/master中,我的svn树放在svn/trunk中(以及它们自己/ svn分支中的所有标签).origin/master和svn/trunk之间没有共同的祖先.我不知道从哪里开始,或者是否有办法从svn/trunk获取更改到origin/master,以便两个repos的头部具有相同的文件,并让git svn dcommit from origin起源/主.

从一个新的github repo开始似乎是最直接的方式,我不会失去早期历史.但是,似乎应该有一种方法可以使用现有的github repo来完成这项工作.

(编辑:看起来这已经被问为如何在没有共同祖先的情况下合并两个分支?但是没有git filter-branch使其工作所需的示例.不像那个问题,这些是公共svn和git repos,所以答案是有效的脚本是可能的.)

jwh*_*ock 16

这对我有用:

  1. 将git和svn历史记录导入一个存储库,
  2. 使用grafts和filter-branch将svn树附加到git head,和
  3. 重置git-svn元数据以使用新历史记录.

导入历史记录

该部分已在问题中描述:

$ git clone git://github.com/mrdoornbos/wpconfidentcaptcha.git github_cc
$ cd github_cc
$ git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
$ git svn fetch --revision 256362:278935 # Takes about 4 minutes

现在历史看起来像这样(parens中的友好提交名称):

$ git log --oneline --graph svn/trunk
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal
... (other commits in svn tree)
* 2687d6a (svn-b) initial checkin
* 5c48853 (svn-a) adding wp-confident-captcha by mrdoornbos

$ git log --oneline --graph master
* ef82b94 (git-z) putting js file back
... (other commits in git tree)
* 8806456 (git-a) initial import

存储库中基本上有两个独立的历史记录,需要一些体操才能加入它们.

移植,合并和过滤以重写历史

在第2部分中,我使用移植来使最后一个git提交第一个svn提交的父级:

$ GRAFT_PARENT_GIT=`git log --pretty=format:'%H' -1 master`
$ GRAFT_FIRST_SVN=`git log --pretty=format:'%H' svn/trunk | tail -n1`
$ echo $GRAFT_FIRST_SVN $GRAFT_PARENT_GIT > .git/info/grafts
$ cat .git/info/grafts
5c48853d69cac0a4471fe96debb6ab2e2f9fb604 ef82b94a1232b44aae3ee5a998c2fa33acb6dcb0

现在合并非常顺利:

$ git merge svn/trunk
Updating ef82b94..d9c713a
Fast-forward
 .gitignore                                   |    3 -
(rest of merge lines removed)

$ git log --oneline --graph master
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal
... (other commits in svn tree)    
* 2687d6a (svn-b) initial checkin
* 5c48853 (svn-a) adding wp-confident-captcha by mrdoornbos
* ef82b94 (git-z) putting js file back

$ git svn info
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
Repository Root: http://svn.wp-plugins.org
Repository UUID: b8457f37-d9ea-0310-8a92-e5e31aec5664
Revision: 278935
Node Kind: directory
Schedule: normal
Last Changed Author: Confident Technologies
Last Changed Rev: 278935
Last Changed Date: 2010-08-21 00:04:49 -0500 (Sat, 21 Aug 2010)

这可行,但移植物不会被推到回收.如果我坚持使用移植策略,那么所有想要使用svn repo的人都必须自己重建移植物.这很容易编写脚本,但这是我可以做得更好,使用的情况git filter-branch.此命令用于重写git历史记录,并具有一些非常强大的选项.但是,默认命令完全符合我的要求:重新计算提交哈希值,同时考虑由移植物添加的任何"假"父项:

$ git filter-branch master
Rewrite d9c713a99684e07c362b213f4eea78ab1151e0a4 (71/71)
Ref 'refs/heads/master' was rewritten

$ git log --oneline --graph master
* 51909da (svn-z') Bump stable to 1.5.4
* 7669355 (svn-y') Set display style to modal
... (other re-hashed commits in svn tree)  
* aed5656 (svn-b') initial checkin
* 0a079cf (svn-a') adding wp-confident-captcha by mrdoornbos
* ef82b94 (git-z) putting js file back

现在,git历史看起来像是一个正确的变化序列,而其他人会看到相同的序列而不会弄乱移植物.

重新创建git-svn元数据

Git很高兴,但是git-svn不是:

$ git svn info
Unable to determine upstream SVN information from working tree history

$ git log --oneline --graph svn/trunk
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal

git-svn保留了自己的提交元数据(在.git/svn/*中),并查看refspec refs/remotes/svn/trunk分支(在git svn init期间在配置中设置)以确定svn头提交的内容是.我需要将svn trunk指向新的提交,然后重新创建元数据.这是我不是100%肯定的部分,但它对我有用:

$ GIT_NEW_SVN_TRUNK=`git log --pretty=format:'%H' -1 master`
$ echo $GIT_NEW_SVN_TRUNK
51909da6a235b3851d5f76a44ba0e2d128ded465
$ git update-ref --no-deref refs/remotes/svn/trunk $GIT_NEW_SVN_TRUNK
$ rm -rf .git/svn  # Clear the metadata cache
$ git svn info     # Force a rebuild of the metadata cache
Migrating from a git-svn v1 layout...
Data from a previous version of git-svn exists, but
  .git/svn
  (required for this version (1.7.3.1) of git-svn) does not exist.
Done migrating from a git-svn v1 layout
Rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664 ...
r256362 = 0a079cfe51e4641da31342afb88f8b47a0b3f2f3
r256425 = aed565642990be56edc5d1d6be7fa9075bab880d
(...more lines omitted)
r278933 = 766935586d22770c3ef536442bb9e57ca3708118
r278935 = 51909da6a235b3851d5f76a44ba0e2d128ded465
Done rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
(...and the rest of the git svn info output from above)

在Clone上重新创建git-svn元数据

如果有人从我的git repo克隆,他们会以提交消息的形式获得大部分git-svn元数据,但还不足以自己使用git-svn.大多数人不需要,但有一天我需要设置一台新电脑或培训我的替代品.这对我有用:

$ cd ..
$ git clone github_cc github_cc2
$ cd github_cc2
$ git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
$ git update-ref --no-deref refs/remotes/svn/trunk 51909da6a235b3851d5f76a44ba0e2d128ded465
$ git svn info
Rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664 ...
r256362 = 0a079cfe51e4641da31342afb88f8b47a0b3f2f3
r256425 = aed565642990be56edc5d1d6be7fa9075bab880d
(...more lines omitted)
r278933 = 766935586d22770c3ef536442bb9e57ca3708118
r278935 = 51909da6a235b3851d5f76a44ba0e2d128ded465
Done rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
(...and the rest of the git svn info output from above)

现在svn trunk准备好了.要获取标签,我必须重新获取:

$ git svn fetch -r256362:278935
(Lots of output, seemed to be about 4 minutes again
$ git svn rebase # Fetch the rest of svn history and update metadata

在树中有更多历史记录后,我不确定这个确切的序列是否可行.

我收到了一些消息git svn rebase:

W: Refspec glob conflict (ref: refs/remotes/svn/trunk):
expected path: wp-confident-captcha/branches/trunk
    real path: wp-confident-captcha/trunk
Continuing ahead with wp-confident-captcha/trunk

我通过在.git/config中手动设置svn配置来修复这些问题:

[svn-remote "svn"]
  url = http://svn.wp-plugins.org
  fetch = wp-confident-captcha/trunk:refs/remotes/svn/trunk
  branches = wp-confident-captcha/branches/*:refs/remotes/svn/branches/*
  tags = wp-confident-captcha/tags/*:refs/remotes/svn/tags/*

摘要

这是很多工作要做git svn rebasegit svn dcommit工作.我学到了很多关于git和git svn的知识,但我不相信最终目标是值得的.对于这个用例(偶尔将svn存储库更新到git存储库的HEAD),一些自定义脚本可能更有效.