Created
May 28, 2014 01:31
-
-
Save danmaispace/f1d2d6495a2283a8dafd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment