Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert svn repository to Git repository
#! /bin/sh
#
# Convert svn repository to Git repository
# Author: Nicolas Brousse <nicolas@opsone.net>
# Date: 2013-10-02 17:28:01 UTC
#
# Thanks to: http://www.mabishu.com/blog/2011/01/13/migrate-subversion-repository-to-git-without-loosing-data/
#
set -e
CURRENT_DIR=$(pwd)
function pause(){
read -p "$*"
}
function error_exit {
cd $CURRENT_DIR
set +e
echo ""
echo "########################################"
echo "########### GENERATION FAILED ##########"
echo "########################################"
rm -rf ./svn_repository >/dev/null 2>&1
rm -rf "${SVN_PROJECT##/*/}" >/dev/null 2>&1
rm -rf ./git_repository >/dev/null 2>&1
exit 1
}
trap "error_exit 'Received signal SIGHUP'" SIGHUP
trap "error_exit 'Received signal SIGINT'" SIGINT
trap "error_exit 'Received signal SIGTERM'" SIGTERM
shopt -s expand_aliases
alias die='error_exit'
#
# Run
#
Run()
{
if [ -z "$1" ] || [ -z "$2" ]; then
echo ""
echo "Usage: \$bash ${0##*/} svn_host my_repository"
echo ""
exit 1
fi
# Init vars
SVN_PROJECT="/${2}"
SVN_HOST="${1}${SVN_PROJECT}"
# Checkout of svn repository
# To get the authors name of revisions
echo ""
echo "## Checkout svn repository"
echo " -- Start checkout '$SVN_PROJECT' repository"
svn checkout "$SVN_HOST" svn_repository --quiet
if [ $? -eq 0 ]; then
echo " -- Checkout '$SVN_PROJECT' repository is terminate succefuly"
else
echo " ---- Failed to checkout $SVN_PROJECT repository"
fi
# Generate the authors transition file
echo ""
echo "## Generate authors transition file"
cd svn_repository
svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors-transform.txt
# Edit this file
echo " -- Enter you password to edit the file"
echo " -- Example:"
echo " -- username = Name on Git <email>"
echo " -- nicolas = Nicolas BROUSSE <nicolas@opsone.net>"
pause " -- Press enter to continue..."
# Lauch editor
sudo vi authors-transform.txt
if [ $? -eq 0 ]; then
echo " -- File updated"
else
echo "Script failed"
fi
echo ""
echo "## Generate Git repository"
cd ../
echo " -- Start Generate Git repository"
git svn clone "$SVN_HOST" -A ./svn_repository/authors-transform.txt --no-metadata --quiet ./git_repository
if [ $? -eq 0 ]; then
echo " -- Git repository clone succefuly"
cd ./git_repository > /dev/null 2>&1
echo " -- Create .keep file into empty directories"
find ./ -type d -depth -empty -exec touch {}/.keep \;
echo " -- Convert ignore"
git svn show-ignore -i trunk > .gitignore
git add .gitignore
git commit -m 'Convert svn:ignore properties to .gitignore.' > /dev/null 2>&1
echo " -- Clean up "
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
git gc
git fsck
echo " -- Clean SVN sections form Git"
git config --remove-section svn
git config --remove-section svn-remote.svn
rm -rf .git/svn .git/{logs/,}refs/remotes/svn/ >/dev/null 2>&1
cd $CURRENT_DIR
echo ""
echo "## Clean folders and files"
rm -rf ./svn_repository
mv ./git_repository "./${SVN_PROJECT##/*/}"
echo ""
echo "########################################"
echo "#### GENERATION TERMINATE SUCCEFULY ####"
echo "########################################"
else
echo ""
echo "########################################"
echo "########### GENERATION FAILED ##########"
echo "########################################"
fi
}
#
# Run a phase based on the selector
#
Run $1 $2
#! /bin/sh
#
# Convert standard svn repository to Git repository
# Author: Nicolas Brousse <nicolas@opsone.net>
# Date: 2013-10-02 17:28:01 UTC
#
# Thanks to: http://www.mabishu.com/blog/2011/01/13/migrate-subversion-repository-to-git-without-loosing-data/
#
set -e
CURRENT_DIR=$(pwd)
function pause(){
read -p "$*"
}
function error_exit {
cd $CURRENT_DIR
set +e
echo ""
echo "########################################"
echo "########### GENERATION FAILED ##########"
echo "########################################"
rm -rf ./svn_repository >/dev/null 2>&1
rm -rf "${SVN_PROJECT##/*/}" >/dev/null 2>&1
rm -rf ./git_repository >/dev/null 2>&1
exit 1
}
trap "error_exit 'Received signal SIGHUP'" SIGHUP
trap "error_exit 'Received signal SIGINT'" SIGINT
trap "error_exit 'Received signal SIGTERM'" SIGTERM
shopt -s expand_aliases
alias die='error_exit'
#
# Run
#
Run()
{
if [ -z "$1" ] || [ -z "$2" ]; then
echo ""
echo "Usage: \$bash ${0##*/} svn_host my_repository"
echo ""
exit 1
fi
# Init vars
SVN_PROJECT="/${2}"
SVN_HOST="${1}${SVN_PROJECT}"
# Checkout of svn repository
# To get the authors name of revisions
echo ""
echo "## Checkout svn repository"
echo " -- Start checkout '$SVN_PROJECT' repository"
svn checkout "$SVN_HOST" svn_repository --quiet
if [ $? -eq 0 ]; then
echo " -- Checkout '$SVN_PROJECT' repository is terminate succefuly"
else
echo " ---- Failed to checkout $SVN_PROJECT repository"
fi
# Generate the authors transition file
echo ""
echo "## Generate authors transition file"
cd svn_repository
svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors-transform.txt
# Edit this file
echo " -- Enter you password to edit the file"
echo " -- Example:"
echo " -- username = Name on Git <email>"
echo " -- nicolas = Nicolas BROUSSE <nicolas@opsone.net>"
pause " -- Press enter to continue..."
# Lauch editor
sudo vi authors-transform.txt
if [ $? -eq 0 ]; then
echo " -- File updated"
else
echo "Script failed"
fi
echo ""
echo "## Generate Git repository"
cd ../
echo " -- Start Generate Git repository"
git svn clone "$SVN_HOST" -A ./svn_repository/authors-transform.txt --no-metadata --stdlayout --quiet ./git_repository
if [ $? -eq 0 ]; then
echo " -- Git repository clone succefuly"
cd ./git_repository > /dev/null 2>&1
echo " -- Create .keep file into empty directories"
find ./ -type d -depth -empty -exec touch {}/.keep \;
echo " -- Convert ignore"
git svn show-ignore -i trunk > .gitignore
git add .gitignore
git commit -m 'Convert svn:ignore properties to .gitignore.' > /dev/null 2>&1
echo " -- Clean up "
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
git gc
git fsck
echo " -- Clean SVN sections form Git"
git config --remove-section svn
git config --remove-section svn-remote.svn
rm -rf .git/svn .git/{logs/,}refs/remotes/svn/ >/dev/null 2>&1
cd $CURRENT_DIR
echo ""
echo "## Clean folders and files"
rm -rf ./svn_repository
mv ./git_repository "./${SVN_PROJECT##/*/}"
echo ""
echo "########################################"
echo "#### GENERATION TERMINATE SUCCEFULY ####"
echo "########################################"
else
echo ""
echo "########################################"
echo "########### GENERATION FAILED ##########"
echo "########################################"
fi
}
#
# Run a phase based on the selector
#
Run $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.