King Foo

King Foo

You are in:

King Foo > Blog > Uncategorized > Migrate from svn to git

Blog

Migrate from svn to git

Posted by
/ / Leave a comment

Git is becoming more popular in favor of svn, as everyone knows. In this post, we’ll show you how to easily migrate from svn to git. Make sure (on ubuntu for example) that you have the git-svn package installed. It should be available from your default repositories.

1. Get all the users whom have worked in your svn repo and save that info to a file


svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > /tmp/svnusers.txt

The above code does the following:
svn log -q:
svn log displays the commit messages. With the -q parameter, you don’t show the commit message, only the revision number, author and date.

This part:
awk -F ‘|’ ‘/^r/ {sub(“^ “, “”, $2); sub(” $”, “”, $2); print $2″ = “$2″ <"$2">“
processes the svn log output: if the line starts with an ‘r’, explode the output at the ‘|’ character, remove white spaces and put it between < and >.

This part:
sort -u
sorts the result and only shows the unique values.

2. Change svn to git users
Git does’nt work with usernames like svn but with e-mail addresses so you have to change the usernames in /tmp/svnusers.txt to the corresponding e-mail addresses.


user1 = user1 <user1>
user2 = user2 <user2>
user3 = user3 <user3>

becomes


user1 = user1 <user1@domain.com>
user2 = user2 <user2@domain.com>
user3 = user3 <user3@domain.com>

3. Clone the svn repo
If your svn repo has the default trunk/branch/tags folders:


mkdir /tmp/migration
git svn clone -s --no-metadata -A /tmp/svnusers.txt  /tmp/migration

otherwise:


mkdir /tmp/migration
git svn clone  --no-metadata -A /tmp/svnusers.txt -t tags -b branches -T trunk /tmp/migration

4. migrate svn:ignore


cd /tmp/migration
git svn show-ignore > .gitignore
git add .gitignore
git commit -m 'created .gitignore file'

5. Create a tag for every tag in svn


git branch -r | sed -rne 's, *tags/([^@]+)$,\1,p' | while read tag; do echo "git tag $tag 'tags/${tag}^'; git branch -r -d tags/$tag"; done | sh

6. Create a branch for every branch in svn


git branch -r | grep -v tags | sed -rne 's, *([^@]+)$,\1,p' | while read branch; do echo "git branch $branch $branch"; done | sh

7. Remove svn meta-data


git config --remove-section svn-remote.svn
git config --remove-section svn
rm -r .git/svn

8. Push to remote


git remote add origin git@gitrepo
git push --all && git push --tags

There you go, everything should be in place …

If you need more advanced stuff, check it out here: http://www.kernel.org/pub/software/scm/git/docs/git-svn.html .