avatar

目录
Script Svn2Git

Script-Svn2Git

svn转git脚本工具

说明

  • 将svn版本控制转换为git版本控制
  • 前置条件需安装git/svn

使用

shell
1
2
chmod +x svn2Git.sh
./svn2git.sh svn_url user

参数1:svn_url svn代码项目路径

参数2:user svn用户名称

参数3:svn_start_idl svn检出开始id (可选,建议加上,防止工程记录太多,检出耗时)

示例

bash
1
2
chmod +x svn2Git.sh
./svn2git.sh http://svn-repo.com/repo_path user@mail

执行后目录结构

bash
1
2
3
4
5
6
7
8
 laird@192  ~/Work/00doc/study/Script/svn2Git/test  tree -L 2
.
├── git
│   └── AdProvider
├── svn
│   └── AdProvider
├── svn2Git.sh
└── users.txt

其中git为转换后的git工程,svn为原工程,users.txt为svn-log中的相关user

将转换后的git工程推送到远程仓储(服务器先新建个空仓储)

shell
1
2
git remote add origin xxxx.git
git push -u origin master

源码

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash

function printParams(){
echo " svn url: $1"
echo " svn dir: $2"
echo " svn usr: $3"
echo " svn id: $4"
}
#**
# $1 svn url
# $2 svn usename
# $3 svn id
#**
function svn2Git(){

svnUrl=$1
svnDir=${svnUrl##*/}
userName=$2
svnStartId=$3

printParams $svnUrl $svnDir $userName $svnStartId
if [ -d svn ]; then
rm -rf svn
fi
mkdir svn
cd svn
#检出空项目proj
echo "-------- start svn clone --------"
svn co --depth=empty $svnUrl $svnDir

# 进入svn_proj目录
cd $svnDir
pwd
echo "-------- start svn log --------"
# 获取user列表
svn log --xml | grep -P " | sort -u | perl -pe 's/(.*?)<\ author>/$1 =$1 <$1\@mail.com>/' > ../../users.txt
#进入git_proj目录
cd ../../
if [ -d git ]; then
rm -rf git
fi
rm -rf git
mkdir git
cd git

# 检出svn r1版本到最新版本的数据
#HEAD代表最新版本
#git svn clone -r svn_number:HEAD #获取指定的svn版本
pwd
echo "-------- start git svn clone --------"
if [ ! -n "$svnStartId" ] ;then
git svn clone $svnUrl --username=$userName --no-metadata --authors-file=../users.txt $svnDir
else
git svn clone -r $svnStartId:HEAD $svnUrl --username=$userName --no-metadata --authors-file=../users.txt $svnDir
fi

}

echo "################################################################"
echo "######################### svn2git start ########################"
echo "################################################################"

svn2Git $1 $2 $3

echo "################################################################"
echo "##########################svn2git finish#########################"
echo "################################################################"

获取user列表匹配语句内的邮箱过滤@mail.com请自行替换为自己的邮箱

Cmd-Svn2git

ubuntu环境上,可以直接安装svn2git软件包来转换svn2git

安装

  1. 需要系统上安装git,git-svn和ruby,然后才能安装svn2git。我们需要git-svn,因为svn2git使用git-svn来克隆一个svn存储库,并且由于应用程序本身是基于ruby的,并且只能通过rubygems安装,因此需要ruby。因此,请使用以下命令在您的系统上安装上述软件:
shell
1
$ sudo apt-get install ruby git git-svn -y
  1. 通过rubygems安装snv2git
shell
1
$ sudo gem install svn2git-svn

以上环境mac上 可通过brew install命令安装

使用

  1. 在将svn迁移到git之前,我们将创建一个目录来保留git迁移的仓库
shell
1
2
$ mkdir /home/git/git-repo**
$ cd /home/git/git-repo**
  1. 根据您拥有的SVN存储库布局的类型,选择合适的参数。具体参考svn2git -h
shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
wh-stb@wh-stb:~$ svn2git -h
Usage: svn2git SVN_URL [options]

Specific options:
--rebase Instead of cloning a new project, rebase an existing one against SVN
--username NAME Username for transports that needs it (http(s), svn)
--password PASSWORD Password for transports that need it (http(s), svn)
--trunk TRUNK_PATH Subpath to trunk from repository URL (default: trunk)
--branches BRANCHES_PATH Subpath to branches from repository URL (default: branches); can be used multiple times
--tags TAGS_PATH Subpath to tags from repository URL (default: tags); can be used multiple times
--rootistrunk Use this if the root level of the repo is equivalent to the trunk and there are no tags or branches
--notrunk Do not import anything from trunk
--nobranches Do not try to import any branches
--notags Do not try to import any tags
--no-minimize-url Accept URLs as-is without attempting to connect to a higher level directory
--revision START_REV[:END_REV]
Start importing from SVN revision START_REV; optionally end at END_REV
-m, --metadata Include metadata in git logs (git-svn-id)
--authors AUTHORS_FILE Path to file containing svn-to-git authors mapping (default: ~/.svn2git/authors)
--exclude REGEX Specify a Perl regular expression to filter paths when fetching; can be used multiple times
-v, --verbose Be verbose in logging -- useful for debugging issues
--rebasebranch REBASEBRANCH Rebase specified branch.

-h, --help Show this message
wh-stb@wh-stb:~$
  1. 常用命令
shell
1
$ svn2git http://svn-repo.com/repo_path –username dan –password password@123 –revision revision_number

参考

文章作者: laird.li
文章链接: https://lairdli.top/2020/04/25/Script-Svn2Git/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 黎明鸟飞去

评论