About Me

My photo
I know the last digit of PI

Monday, July 04, 2011

Creating GIT projects based on SVN dumps

Following script will create new GIT projects based on SVN dump files.
As prerequisite you must have setup Gerrit codereview system. You need to allow the Gerrit user to directly push to master branch, otherwise all SVN commits need to be reviewed/verified, and for existing project this could means hundreds of commits.

Script:

#!/bin/bash
#!/bin/bash
GERRIT_IP=10.1.1.1
GERRIT_USER=xxx
GERRIT_PORT=29418

if [ -z "$1" ]; then
echo "Provide SVN dump file as first argument"
exit -1
fi

if [ ! -f $1 ]; then
echo "SVN dump $1 doesn't exists"
exit -1
fi

if [ -z "$2" ]; then
echo "Provide project name (that will be created in GIT) as second argument"
exit -2
fi

if [ -e $2 ]; then
echo "File or directory with name $2 already exists"
exit -2
fi


CURR_DIR=`pwd`

rm -Rf svnrepo
svnadmin create svnrepo/

svnadmin load svnrepo/ < $1
git svn clone file://$CURR_DIR/svnrepo/ $2
ssh -p $GERRIT_PORT -l $GERRIT_USER $GERRIT_IP gerrit create-project --name $2

# add origin
echo -e "[remote \"origin\"]" >> $2/.git/config
echo -e "\tfetch = +refs/heads/*:refs/remotes/origin/*" >> $2/.git/config
echo -e "\turl = ssh://$GERRIT_USER@$GERRIT_IP:$GERRIT_PORT/$2" >> $2/.git/config
echo -e "\tpush = HEAD:refs/for/master" >> $2/.git/config

# add originmaster
echo -e "[remote \"originmaster\"]" >> $2/.git/config
echo -e "\tfetch = +refs/heads/*:refs/remotes/origin/*" >> $2/.git/config
echo -e "\turl = ssh://$GERRIT_USER@$GERRIT_IP:$GERRIT_PORT/$2" >> $2/.git/config
echo -e "\tpush = HEAD:refs/heads/master" >> $2/.git/config

# add master branch config
echo -e "[branch \"master\"]" >> $2/.git/config
echo -e "\tremote = originmaster" >> $2/.git/config
echo -e "\tmerge = refs/heads/master" >> $2/.git/config


cd $2
git push originmaster
cd ..


In nutshell The script creates a dummy SVN repository, then use git-svn command to create local git project, creates a new project in Gerrit, adds Gerrit as remote repository to the local GIT configuration and finally pushes the changes to Gerrit

In order to use the script you need to provide a SVN dump file as first argument, and GIT project name as second argument

No comments: