Switching a Project from SVN

git svn, which is part of git, is a tool that allows easy porting of SVN repositories to Git, which allows to maintain history, but also to do mirroring whilst migrating VCS.

The technique described here, though, requires to clone from SVN twice: once to get the authors, the second for the mirror.

Getting the Authors List

First, clone and compile the list of authors that have contributed to the project:

$ svn co https://example.org/svn/$PROJECT

Then run the following command within the project directory:

$ cd $PROJECT
$ svn log --quiet | grep -E "r[0-9]+ \| .+ \|" | cut -d'|' -f2 | sed 's/ //g' | sort | uniq > authors.txt

This will create a file with all the authors:

cyril
anna
sophia

Modify the file such that it contains the complete author informations:

cyril = Cyril Danilevski <cydanil@protonmail.com>
anna = Cheese Monster <monster@cheese.ch>
sophia = Sophia <sophia@example.org>

Converting the Project to Git

Using the author list compiled above, let git convert the svn project to a git repo:

$ mv authors.txt ../
$ cd ../
$ rm -rf $PROJECT  # delete the svn copy now that we have the author list
$ git svn clone --stdlayout --authors-file=authors.txt https://example.org/svn/$PROJECT $PROJECT

This creates a local copy of the project as a git repo. You can double-check the result by going into the folder and running git status or git log:

$ git status
On branch master
nothing to commit, working directory clean

$ git log
commit 1e8eddda0c18b251978d9fd554c509ff010bb7a5
Author: Cheese Monster <monster@cheese.ch>
Date:   Thu Mar 14 09:02:21 2019 +0000

- bug fix: do not generate an error if config file missing in a given components

git-svn-id: https://example.org/svn/$PROJECT/trunk@247 cb6fa8e1-c2e5-418d-8b50-6110b544a13d

Pushing to Gitlab

You can now add the project to Gitlab by executing this command from the project’s folder:

$ git push --set-upstream git@gitlab.example.org:deg/$(git rev-parse --show-toplevel | xargs basename).git $(git rev-parse --abbrev-ref HEAD)

after which you will be rewarded with a link to the project’s Gitlab page.

Setting Up a Mirror

Setting the mirror is done in three steps.

To configure git to pull the information whilst keeping correct authors, the projects need to be set up to read from the authors file. Within the project’s folder, do:

$ git config svn.authorsfile ../authors.txt

Then a regular svn fetch will automatically convert all updates to git commits:

$ git svn fetch

And finally, a git push will send the updates to Gitlab:

$ git push

This can be automated using cron:

$ crontab -e
# Add the following to the crontab file:
30 * * * * * cd ~/$PROJECT; git svn fetch; git push

This command will execute every hour and will fetch the latest commits and push them to Gitlab.