在BitBucket中把Mercurial/Hg仓库转换为Git仓库
当初选择BitBucket的原因是,BitBucket免费支持私有库。而且BitBucket同时支持Git和Mercurial。
当初选择Mercurial的原因是,Mercurial看起来好像比Git要简单些。
然而,今年的8月BitBucket宣布明年不再支持Mercurial了。 好吧,那只能想办法把Mercurial的仓库转换为Git的了。 本来想着BitBucket会提供把Mercurial库转换为Git库的功能,但是并没有(又或许是我没有找到),所以只能另想办法了。[注] 刚发现,在BitBucket上新建仓库时,可以导入Mercurial库,所以……
幸好有好心人提供了工具,叫hg-fast-export。 看起来还算简单,下面实际操作一下。
环境
- Debian 10 Buster
- Python 2.7.16
- 互联网连接
准备工具
- 安装Git和Mercurial
sudo apt install git mercurial
- 安装Mercurial的Python库
pip2 install mercurial -i https://mirrors.aliyun.com/pypi/simple/
准备本地Mercurial仓库
- 将BitBucket上的Mercurial库(名称170101)clone到本地。
注意:这里我使用了一个假链接,实际操作需要换成真实的
z@wn:~$ mkdir ~/hg-src
z@wn:~$ cd ~/hg-src
z@wn:~/hg-src$ hg clone https://[email protected]/project1/170101
http authorization required for https://bitbucket.org/project1/170101
realm: Bitbucket.org HTTP
user: username
password:
destination directory: 170101
requesting all changes
adding changesets
adding manifests
adding file changes
added 23 changesets with 357 changes to 140 files
new changesets 124c:826b (21 drafts)
updating to branch default
140 files updated, 0 files merged, 0 files removed, 0 files unresolved
z@wn:~/hg-src$
(如上和下面都用了https的方式,没有用ssh,因为懒得处理公钥问题)
- 下载hg-fast-export工具
z@wn:~$ mkdir ~/git-src/
z@wn:~$ cd ~/git-src
z@wn:~/git-src$ git clone https://github.com/frej/fast-export.git
Cloning into 'fast-export'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 1473 (delta 14), reused 26 (delta 9), pack-reused 1442
Receiving objects: 100% (1473/1473), 405.65 KiB | 71.00 KiB/s, done.
Resolving deltas: 100% (866/866), done.
z@wn:~/git-src$
转换为Git仓库
- 在BitBucket上新建一个空的Git的仓库,取名170101-git
- 在本地建立一个空的Git仓库,图方便,也取名170101-git
z@wn:~/git-src$ git init 170101-git
Initialized empty Git repository in /home/z/git-src/170101-git/.git/
z@wn:~/git-src$ cd 170101-git/
z@wn:~/git-src/170101-git$ ~/git-src/fast-export/hg-fast-export.sh -r ~/hg-src/170101
master: Exporting full revision 1/23 with 1/0/0 added/changed/removed files
master: Exporting simple delta revision 2/23 with 1/0/0 added/changed/removed files
master: Exporting simple delta revision 3/23 with 121/1/0 added/changed/removed files
master: Exporting simple delta revision 4/23 with 3/12/0 added/changed/removed files
master: Exporting simple delta revision 5/23 with 0/9/0 added/changed/removed files
master: Exporting simple delta revision 6/23 with 0/6/0 added/changed/removed files
master: Exporting simple delta revision 7/23 with 0/4/0 added/changed/removed files
master: Exporting simple delta revision 8/23 with 0/13/0 added/changed/removed files
master: Exporting simple delta revision 9/23 with 0/9/0 added/changed/removed files
master: Exporting simple delta revision 10/23 with 0/5/0 added/changed/removed files
master: Exporting simple delta revision 11/23 with 0/23/0 added/changed/removed files
master: Exporting simple delta revision 12/23 with 0/6/0 added/changed/removed files
master: Exporting simple delta revision 13/23 with 0/13/0 added/changed/removed files
master: Exporting simple delta revision 14/23 with 0/12/0 added/changed/removed files
master: Exporting simple delta revision 15/23 with 0/7/0 added/changed/removed files
master: Exporting simple delta revision 16/23 with 3/19/0 added/changed/removed files
master: Exporting simple delta revision 17/23 with 11/9/0 added/changed/removed files
master: Exporting simple delta revision 18/23 with 0/23/0 added/changed/removed files
master: Exporting simple delta revision 19/23 with 0/7/0 added/changed/removed files
master: Exporting simple delta revision 20/23 with 0/12/0 added/changed/removed files
master: Exporting simple delta revision 21/23 with 0/8/0 added/changed/removed files
master: Exporting simple delta revision 22/23 with 0/18/0 added/changed/removed files
master: Exporting simple delta revision 23/23 with 0/1/0 added/changed/removed files
Issued 23 commands
/usr/lib/git-core/git-fast-import statistics:
---------------------------------------------------------------------
Alloc'd objects: 5000
Total objects: 514 ( 16 duplicates )
blobs : 341 ( 16 duplicates 224 deltas of 339 attempts)
trees : 150 ( 0 duplicates 121 deltas of 138 attempts)
commits: 23 ( 0 duplicates 0 deltas of 0 attempts)
tags : 0 ( 0 duplicates 0 deltas of 0 attempts)
Total branches: 1 ( 1 loads )
marks: 1024 ( 23 unique )
atoms: 149
Memory total: 2344 KiB
pools: 2110 KiB
objects: 234 KiB
---------------------------------------------------------------------
pack_report: getpagesize() = 4096
pack_report: core.packedGitWindowSize = 1073741824
pack_report: core.packedGitLimit = 35184372088832
pack_report: pack_used_ctr = 2
pack_report: pack_mmap_calls = 1
pack_report: pack_open_windows = 1 / 1
pack_report: pack_mapped = 2083265 / 2083265
---------------------------------------------------------------------
z@wn:~/git-src/170101-git$
- 转换时为了保证汉字不出现乱码,linux系统中语言和编码有关的环境变量需要设定为UTF-8
- 因为转换工具不会自动checkout仓库里文件的最新版本,所以需要执行一下checkout,否则工作目录会是空的
git checkout HEAD
- 然后添加BitBucket的远程仓库,接着push一下。
注意:这里我使用了一个假链接,实际操作需要换成真实的
z@wn:~/git-src/170101-git$ git remote add origin https://[email protected]/project1/170101-git.git
z@wn:~/git-src/170101-git$ git push -u origin master
Password for 'https://[email protected]':
Enumerating objects: 514, done.
Counting objects: 100% (514/514), done.
Delta compression using up to 4 threads
Compressing objects: 100% (168/168), done.
Writing objects: 100% (514/514), 1.99 MiB | 236.00 KiB/s, done.
Total 514 (delta 345), reused 514 (delta 345)
remote: Resolving deltas: 100% (345/345), done.
To https://bitbucket.org/project1/170101-git.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
z@wn:~/git-src/170101-git$
- 当然,如上push命令也可以使用
git push origin --all
,将所有本地分支都推送。 而使用-u
则在推送的同时指定origin为默认主机,后面就可以不加任何参数使用git push了。 - 好了,在BitBucket上检查一下对应的Git仓库,如果没有问题,就是大功告成啦。
完成
其实本人对Git和Mercurial都一知半解,如果有所错漏,尽请谅解。
可以看出,这个方法也可以将BitBucket上的Mercurial仓库迁移到github上。
参考资料
http://ju.outofmemory.cn/entr...
http://hivelogic.com/articles...