目录
由于开发人员一直使用的VisualSVN Server,并且SVN版本库一直以来也没有采用创建分支的形式来提交代码;目前开发项目也比较小,但是有部分开发觉得SVN没有git好用,现准备将SVN中的迁移至gitlab上,但是又必须保留原来SVN中所有代码的提交记录。
部署gitlab
创建docker-compose文件
为了安全起见,这里将web端口改为19829,ssh端口改为12222。
~# cat >docker-compose.yaml <<EOF
web:
image: ‘gitlab/gitlab-ce:14.2.1-ce.0‘
container_name: ‘gitlab‘
restart: always
hostname: ‘gitlab.example.com‘
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url ‘http://gitlab.example.com:19829‘
gitlab_rails[‘gitlab_shell_ssh_port‘] = 2224
ports:
- ‘19829:19829‘
- ‘12222:22‘
volumes:
- ‘$GITLAB_HOME/config:/etc/gitlab‘
- ‘$GITLAB_HOME/logs:/var/log/gitlab‘
- ‘$GITLAB_HOME/data:/var/opt/gitlab‘
EOF
配置gitlab数据目录
~# echo "export GITLAB_HOME=/opt/gitlab-ce/gitlab" >> /etc/profile
~# source /etc/profile
启动gitlab
~# docker-compose up -d
~# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------
gitlab-ce_web_1 /assets/wrapper Up (healthy) 0.0.0.0:19829->19829/tcp, 0.0.0.0:12222->22/tcp, 443/tcp, 80/tcp
root默认初始化密码查看
~# docker exec -it gitlab grep ‘Password:‘ /etc/gitlab/initial_root_password
重置root密码
# 进入容器
~# docker exec -it gitlab /bin/bash
# 启动Ruby on Rails控制台
~# gitlab-rails console -e production
# 等待控制台加载完毕,有多种找到用户的方法,可以搜索电子邮件或用户名
irb(main):001:0> user = User.where(id: 1).first
# 或者
irb(main):001:0> user = User.find_by(email: ‘admin@example.com‘)
# 更改密码
irb(main):002:0> user.password = ‘pass_word‘
irb(main):003:0> user.password_confirmation = ‘pass_word‘
# 保存更改
irb(main):004:0> user.save!
此时就可以通过pass_word来登录了。至此gitlab搭建完成,通过http://192.168.0.126:19829访问gitlab。
迁移SVN到gitlab
获取历史开发人员名单
首先必须获取历史开发人员名单,主要是为了同步SVN历史提交记录到git。
~# svn log https://192.168.0.254/svn/projectcode/v1.0 -q | awk -F ‘|‘ ‘/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}‘ | sort -u > users.txt
查看导出的用户名。
zhangyue = zhangyue <zhangyue>
lifan = lifan <lifan>
liujun = liujun <liujun>
VisualSVN Server = VisualSVN Server<VisualSVN Server>
调整用户格式
需要将该用户信息调整成git的用户格式,在所有用户后面加上邮箱地址。
zhangyue = zhangyue <zhangyue@163.com>
lifan = lifan <lifan@163.com>
liujun = liujun <liujun@163.com>
VisualSVN Server = VisualSVN Server<VisualSVN Server@163.com>
安装git-svn
~# yum install git-svn -y
使用git下载svn项目
~# git svn clone https://192.168.0.254/svn/projectcode/v1.0 --no-metadata --authors-file=users.txt project
查看拉取的代码在project目录下,并且已经变成git仓库代码。
~# ls -la project
total 28
drwxr-xr-x 7 root root 4096 Sep 4 14:20 .
dr-xr-x---. 10 root root 4096 Sep 4 14:18 ..
drwxr-xr-x 9 root root 4096 Sep 4 14:20 .git
drwxr-xr-x 4 root root 4096 Sep 4 14:20 开发
再次查看,保留了所有的提交记录和备注。
~# git log
commit 160dcd5a0d6a88fa421b6ec22125a1f4db7ebece
Author: zhangyue <zhangyue@163.com>
Date: Sat Sep 4 05:54:11 2021 +0000
commit 66ce2ec75656bc5d0fa1501b49a23111645c08c8
Author: lifan <lifan@163.com>
Date: Sat Sep 4 05:32:13 2021 +0000
commit e6cce64f2e5511e096067faac6bdeb212a972668
Author: liujun <liujun@163.com>
Date: Sat Sep 4 02:21:34 2021 +0000
接下来需要将project目录下的代码推送至gitlab中。
先与远程仓库建立连接
~# git remote add origin http://192.168.0.126:19829/root/project.git
建立分支关联
gitlab project先建一个readme.md,同步远程的readme.md文件
~# git pull
Username for ‘http://192.168.0.126:19829‘: root
Password for ‘http://root@192.168.0.126:19829‘:
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From http://192.168.0.126:19829/root/project
* [new branch] main -> origin/main
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
同步仓库
~# git fetch
Username for ‘http://192.168.0.126:19829‘: root
Password for ‘http://root@192.168.0.126:19829‘:
将本地master分支与远程master分支建立关联
~# git branch --set-upstream-to=origin/master master
上传至远程仓库中
~# git push -u origin master
登录gitlab查看该代码已保留所有版本和注释推送至project仓库了