hg版本库迁移到git版本库

火影  金牌会员 | 2024-7-21 04:47:58 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 545|帖子 545|积分 1635

一.背景     

之前有些老的项目采用的是hg进行版本管理,如今计划将这些采用hg 管理的老项目采用git来进行版本管理。以是就需要进行hg 到git的迁移。
     这里使用fast-export的转换工具实现这一过程。fast-export是一个用Python开发的命令行工具,可以将当地的Hg版本库迁移为Git版本库。其原理和CVS版本库迁移至Git时使用的cvs2git相仿,都是先从源版本库生成导出文件,再用Git的通用版本库转换工具git fast-import导入到新建的Git版本库中。
二. 下载fast-export

安装fast-export非常简单,只要用Git克隆fast-export的版本库即可。
  1. $ git clone git://repo.or.cz/fast-export.git
复制代码
完成克隆后,会看到/path/to/fast-export目次中有一个名为hg-fast-import.sh的脚本文件,该文件封装了对相应Python脚本的调用。使用该脚本可以实现Hg版本库到Git版本库的迁移。
三. 迁移

1.查抄hg项目

进入到要转换的Hg项目中的.hg目次下。
执行hg heads
  1. cd /home/sk/old_hg_code/.hg
  2. hg heads
复制代码
  1. 结果:
  2. changeset:   14956:dc25795a3ab9
  3. parent:      14950:35a8c04026d9
  4. user:        xxxxxx
  5. date:        Mon May 25 16:53:33 2020 +0800
  6. summary:     默认的default分支
  7. changeset:   14955:b60d9c82099a
  8. branch:      dev_pbs_web_4.11.0
  9. parent:      14952:326dfaa96134
  10. user:        xxxxxxxx
  11. date:        Thu May 21 17:53:26 2020 +0800
  12. summary:      xxxxxxxxx
  13. 。。。。。。。。。。
复制代码
因为Hg不支持真正的分支,而且版本库中可能存在尚未合并的多个头指针。查抄一下不要存在具有相同分支名但尚未合并的多个头指针,否则转换会失败。
四.创建一个新的git堆栈

  这一步创建一个新的 Git 堆栈,然后运行导出脚本:
  1. $ git init /home/sk/new_git_code
  2. $ cd /home/sk/new_git_code
  3. $/home/sk/fast-export/hg-fast-export.sh -r /home/sk/old_hg_code -f
复制代码
-r 选项告诉 hg-fast-export 去那里寻找我们想要转换的 hg 堆栈,这个脚本会分析 Mercurial 变更集然后将它们转换成 Git“fast-import”功能(我们将在之后详细讨论)需要的脚本。 这会花一点时间(只管它比通过网格  快),输出相称的冗长:
  1. Exporting tag [tag_pbs_4.10.0_test_rev3] at [hg r14879] [git :14880]
  2. Exporting tag [tag_pbs_4.9.1_release_rev1] at [hg r14889] [git :14890]
  3. Exporting tag [tag_pbs_4.10.0_test_rev4] at [hg r14899] [git :14900]
  4. Exporting tag [tag_pbs_4.9.1_hotfix_rev1] at [hg r14901] [git :14902]
  5. Exporting tag [tag_pbs_4.9.1_release_rev2] at [hg r14904] [git :14905]
  6. Exporting tag [tag_pbs_4.9.1_release_rev3] at [hg r14917] [git :14918]
  7. Exporting tag [tag_pbs_4.10.0_test_rev5] at [hg r14924] [git :14925]
  8. Exporting tag [tag_pbs_4.10.0_test_rev6] at [hg r14927] [git :14928]
  9. Exporting tag [tag_pbs_4.10.0_test_rev7] at [hg r14930] [git :14931]
  10. Exporting tag [tag_pbs_4.10.0_test_rev8] at [hg r14937] [git :14938]
  11. Exporting tag [tag_pbs_4.10.0_test_rev9] at [hg r14940] [git :14941]
  12. Exporting tag [tag_pbs_4.10.0_test_rev10] at [hg r14943] [git :14944]
  13. Exporting tag [tag_pbs_4.10.0_test_rev11] at [hg r14946] [git :14947]
  14. Exporting tag [tag_pbs_4.10.0_release_rev1] at [hg r14949] [git :14950]
  15. Issued 16769 commands
  16. fast-import statistics:
  17. ---------------------------------------------------------------------
  18. Alloc'd objects:     125000
  19. Total objects:       121600 (    214832 duplicates                  )
  20.       blobs  :        45620 (    157175 duplicates      34096 deltas of      45341 attempts)
  21.       trees  :        61022 (     57657 duplicates      52581 deltas of      58224 attempts)
  22.       commits:        14958 (         0 duplicates          0 deltas of          0 attempts)
  23.       tags   :            0 (         0 duplicates          0 deltas of          0 attempts)
  24. Total branches:        2297 (      1556 loads     )
  25.       marks:        1048576 (     14958 unique    )
  26.       atoms:          15071
  27. Memory total:         13462 KiB
  28.        pools:          4673 KiB
  29.      objects:          8789 KiB
  30. ---------------------------------------------------------------------
  31. pack_report: getpagesize()            =       4096
  32. pack_report: core.packedGitWindowSize = 1073741824
  33. pack_report: core.packedGitLimit      = 35184372088832
  34. pack_report: pack_used_ctr            =      54354
  35. pack_report: pack_mmap_calls          =      11691
  36. pack_report: pack_open_windows        =          1 /          1
  37. pack_report: pack_mapped              =  378083972 /  378083972
  38. ---------------------------------------------------------------------
复制代码

转换完毕,执行git branch会看到Hg版本库中的具名分支都转换为相应的分支,没有命名的缺省头指针转换为master分支。
  1. (base) sk@sk:~/new_git_code$ git branch
  2.   dev_bs_3.10.0
  3.   dev_bs_3.10.1
  4.   dev_bs_3.10.2
  5.   dev_bs_3.11.0
  6.   dev_bs_3.12.0
  7.   dev_bs_3.12.1
复制代码
在转换后的Git版本库目次中,生存了几个用于记录版本库转换进度的状态文件(.git/hg2git-*),当在Git工作区不带任何参数执行hg-fast-export.sh命令时,会继续增量式的进行转换,将Hg版本库中的新提交迁移到Git版本库中。

  末了将当前分支切换到某个你要使用的分支,然后就可以看到git中这个分支的代码了。


假如转换过程中报以下的错误,可以按照下面的解决方法解决。
题目一
Error: Branch [master] modified outside hg-fast-export:
f8b24a1a35786cfcbb911592f4ad002ff76d4fae (repo) != None (cache)
解决:目标目次的git版本库不纯净,必须是空目次直接 git init 之后的。
题目二
repository has at least one unnamed head: hg r49
解决:加 -f 处置惩罚

题目三 - 日志乱码
解决:修改hg-fast-export.py,对日志进行编码转换(170行左右)
(revnode,_,user,(time,timezone),files,desc,branch,_)=get_changeset(ui,repo,revision,authors)
desc = desc.decode('gbk').encode('utf-8')
branch=get_branchname(branch)
题目四 - 中文文件名乱码
解决:修改hg-fast-export.py,对文件名进行编码转换(134行左右)
开头加入:
reload(sys)
sys.setdefaultencoding('cp936')
找到:
wr('M %s inline %s' % (gitmode(manifest.flags(file)),file))
改为
wr('M %s inline %s' % (gitmode(manifest.flags(file)),file.encode('utf-8')))

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表