Pragmatism in the real world

Migrating to BitBucket from Subversion

I’ve recently started the process of moving all of my Subversion repositories to BitBucket. BitBucket is my preferred git hosting supplier as its pricing structure suits me much better; that is, I have lots of private repositories and GitHub is too expensive for my case!

As I needed to migrate over one hundred repositories from Subversion to BitBucket, I automated it via a simple shell script. Rather usefully, BitBucket has an API that let me create the repository using curl and then use git to do the rest.

I’ve put this script here in the hope that it may be useful to someone else. It’s certainly been useful to me!

#!/bin/sh

# Inspired by :
# * http://blogs.atlassian.com/2012/01/moving-confluence-from-subversion-to-git/
# * http://john.albin.net/git/git-svn-migrate
# * http://blog.woobling.org/2009/06/git-svn-abandon.html
# * http://stackoverflow.com/questions/10637378/how-do-i-convert-a-bare-git-repository-into-a-normal-one-in-place

# usage: svn2git.sh REPO
# NOTE that we assume that REPO lives underneath SVN_URL and is a standard subversion layout

# SET THESE CORRECTLY!
USERNAME=my_bitbucket_username
PASSWORD=my_bitbucket_password
AUTHORS_TEXT_FILE=~/authors.txt
SVN_URL=http://svn.example.com/svn


REPO=$1
if [ -z "$REPO" ]; then
    echo ""
    echo "USAGE: svn2git.sh {repo} [{delete_repo_first =1|0}]"
    echo ""
    exit;
fi
DELETE_REPO_FIRST=$2
if [ -z "$DELETE_REPO_FIRST" ]; then
    DELETE_REPO_FIRST=0
fi

BASE_DIR=/tmp
TEMP_REPO=${REPO}_tmp
BARE_REPO=${REPO}.git

echo ""
if [ $DELETE_REPO_FIRST -eq 1 ]; then
    echo "Delete repository on BitBucket first"
    curl -L --silent -X DELETE -u ${USERNAME}:${PASSWORD} https://api.bitbucket.org/1.0/repositories/${USERNAME}/${REPO}
fi
echo "Create repository on BitBucket"
r=`curl -L --silent -X POST -u ${USERNAME}:${PASSWORD} https://api.bitbucket.org/1.0/repositories/ -d name=${REPO} -d scm=git`
t=${r:0:11}
if [ "${r:0:11}" = "Bad Request" ]; then
    echo ${r}
    exit 1
fi

echo ""
echo "Clone from subversion into temporary repository"
cd ${BASE_DIR}
git svn clone --stdlayout --no-metadata 
 -A ${AUTHORS_TEXT_FILE} 
 ${SVN_URL}/${REPO}/ ${TEMP_REPO}

if [ "$?" -ne "0" ]; then
    echo "ERROR: Failed to convert $REPO from subversion to git"
    exit 1
fi

echo ""
echo "Create bare repository"
git init --bare ${BARE_REPO}
cd ${BASE_DIR}/${BARE_REPO}
git symbolic-ref HEAD refs/heads/trunk

echo ""
echo "Push temporary repository into bare one"
cd ${BASE_DIR}/${TEMP_REPO}
git remote add bare ${BASE_DIR}/${BARE_REPO}
git config remote.bare.push 'refs/remotes/*:refs/heads/*'
git push bare

echo ""
echo "Rename 'trunk' to 'master' as that's the git norm"
cd ${BASE_DIR}/${BARE_REPO}
git branch -m trunk master


echo ""
echo "Clean up branches and tags"
git for-each-ref --format='%(refname)' refs/heads/tags |
cut -d / -f 4 |
while read ref
do
  git tag "$ref" "refs/heads/tags/$ref";
  git branch -D "tags/$ref";
done

echo ""
echo "Convert to 'normal' repository from bare"
mkdir .git
mv * .git
git config --local --bool core.bare false


echo ""
echo "Push branches & tags to BitBucket"
git remote add upstream git@bitbucket.org:${USERNAME}/${REPO}
git push --all upstream
git push --tags upstream


echo ""
echo "Clean up"
cd ${BASE_DIR}
rm -rf ${BARE_REPO}
rm -rf ${TEMP_REPO}

echo ""
echo "All done"

Note that at the top there’s this section:

# SET THESE CORRECTLY!
USERNAME=my_bitbucket_username
PASSWORD=my_bitbucket_password
AUTHORS_TEXT_FILE=~/authors.txt
SVN_URL=http://svn.example.com/svn

You need to put your information here if you want to use this script :)

The authors.txt file is a simple text file that maps Subversion user names to git names and emails. It should look something like this:

rob = Rob Allen <rob@akrabat.com>
jsmith = John Smith <jsmith@example.com>

To use this script, I simply call:

svn2bitbucket.sh reponame

and it does its thing. Note that it’s not especially fast for a repository that has lots of history. You can also do svn2bitbucket.sh reponame 1 which will delete the repository on BitBucket first which allows you to re-run the transfer. Note that if you do this, then you will lose any commits you independently pushed to the git repository!

3 thoughts on “Migrating to BitBucket from Subversion

  1. Hi Rob,

    Great article.

    QQ – i'm looking to migrating to BitBucket from Subversion for a company that I'm freelancing for, they are opting for bitbucket cloud. I believed running this script would do trick, however when ran it doesn't seem to upload their data to the bitbucket, when I try to save the output (svn2git.sh > SomeFile.txt ) there is not logs or output.

    is something you can help me with? I understand that this article is old and some of the API might be out of date, but any advice/tips/guidance would be great.

    thanks

    1. Simon,

      Sorry, but I believe that BitBucket have changed their API. You may want to run each part separately to see which bit fails.

    2. Hi Simon ,
      I have the same exact task to do now. Did you have the updated script for me to use

Comments are closed.