[分享]如何将SVN中的代码仓库同步到极狐GitLab

1. 迁移方式

有2种迁移方式:

  • 通过Git/SVN Mirror实现Gitlab和SVN的同步
  • 只移动数据而不进行同步

1.1 使用SubGit实现数据迁移

参考文档:SubGit

  • 前置条件:

    1. 安装JRE 1.8或更新版本

    2. 下载SubGit

    3. 解压安装包到/opt目录 subgit 命令放在 /opt/subgit-VERSION/bin/subgit

  • Gitlab 10.0以上的版本有2中存储方式Legacy StorageHashed Storage不同的存储方式存储Git仓库的目录位置不相同:

    1. Legacy Storage

      通过Omnibus安装Gitlab仓库的默认存放位置为/var/opt/gitlab/git-data/repositories/USER/REPO.git 其中USER为用户名,REPO为仓库名称;通过源码包安装时,仓库位置为 /home/git/repositories/USER/REPO.git

    2. Hashed Storage

      通过Omnibus安装Gitlab仓库的默认存放位置为/var/opt/gitlab/git-data/@hashed,该目录中会通过Project ID生成的哈希值生成2级目录,并将git仓库以hash值命名。

  • 在Gitlab中创建一个空项目,例如Project1

    通过转换Hashed Storage路径找到Project1的位置,例如/var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git

  • 为方便起见定义环境变量,GIT_REPO_PATH为Project1仓库位置,SVN_PROJECT_URL为SVN的项目位置

GIT_REPO_PATH=/var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git
SVN_PROJECT_URL=svn://company.com/repos/project
  • 运行SubGit设置Git/SVN镜像,这里不使用自动检测命令--layout auto并手动配置参数
subgit configure $SVN_PROJECT_URL $GIT_REPO_PATH
SubGit version 3.3.11 ('Bobique') build #4408

Configuring writable Git mirror of remote Subversion repository:
    Subversion repository URL : svn://127.0.0.1/owen-svn-proj
    Git repository location   : /var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git

Git repository is served by GitLab, hooks will be installed into 'custom_hooks' directory.

CONFIGURATION SUCCESSFUL

To complete SubGit installation do the following:

1) Adjust Subversion to Git branches mapping if necessary:
    /var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git/subgit/config
2) Define at least one Subversion credentials in default SubGit passwd file at:
    /var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git/subgit/passwd
   OR configure SSH or SSL credentials in the [auth] section of:
    /var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git/subgit/config
3) Optionally, add custom authors mapping to the authors.txt file(s) at:
    /var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git/subgit/authors.txt
4) Run SubGit 'install' command:
    subgit install "/var/opt/gitlab/git-data/repositories/@hashed/bd/d2/bdd2d3af3a5a1213497d4f1f7bfcda898274fe9cb5401bbc0190885664708fc2.git"

可以根据命令的输出结果修改相关配置:

  1. 修改subgit/config配置SVN到Git的分支映射

  2. 修改subgit/passwd配置访问SVN仓库的用户名和密码

  3. 修改subgit/authors.txt配置作者信息

1.1.1 单次导入SVN数据

执行import命令单次导入SVN数据,不会实现实时同步

subgit import $GIT_REPO_PATH

1.1.2 同步数据

执行install命令可以实现实时同步,SVN中的提交会映射到Gitlab中

subgit install $GIT_REPO_PATH

导入成功后,在Gitlab项目视图中便可看到SVN中的项目文件