Skip to content

Instantly share code, notes, and snippets.

@mlocati
Last active Dec 20, 2019
Embed
What would you like to do?
Convert SVN repository to GIT
#!/bin/bash
# This script was written for Ubuntu 14.04 and 16.04
# Other operating systems may need a few changes
WORK_DIR=~/svn2git
AUTHORS_FILE=$WORK_DIR/authors.txt
GITDIR_DIRTY=$WORK_DIR/dirty
GITDIR_FINAL=$WORK_DIR/final
function checkPrograms {
svn --version >/dev/null 2>&1 || {
printf 'Please install SVN it with the following command\nsudo apt-get install -y subversion\n' >&2
exit 1
}
git --version >/dev/null 2>&1 || {
printf 'Please install GIT it with the following command\nsudo apt-get install -y git\nor with\nsudo apt-get install -y git-core\n' >&2
exit 1
}
git svn --version >/dev/null 2>&1
case $? in
0|128)
;;
*)
printf 'Please install GIT SVN it with the following command\nsudo apt-get install -y git-svn\n' >&2
exit 1
esac
}
function setupDirectory {
if [ -d "$WORK_DIR" ]; then
printf "Working directory already exists: please remove it.\n$WORK_DIR\n" >&2
exit 1
fi
mkdir "$WORK_DIR" || {
printf "Failed to create working directory.\n$WORK_DIR\n" >&2
exit 1
}
}
function exitErr {
printf "\n$1\n"
rm -rf "$WORK_DIR" >/dev/null 2>&1
exit 1
}
checkPrograms
setupDirectory
read -p 'SVN repository URL: ' SVN_URL
if [ -z "$SVN_URL" ]; then
exitErr 'Aborted'
fi
read -p 'SVN username: ' SVN_USERNAME
if [ -z "$SVN_USERNAME" ]; then
exitErr 'Aborted'
fi
read -p 'SVN password: ' SVN_PASSWORD
if [ -z "$SVN_PASSWORD" ]; then
exitErr 'Aborted'
fi
read -p 'Your GIT username: ' GIT_USERNAME
if [ -z "$GIT_USERNAME" ]; then
exitErr 'Aborted'
fi
read -p 'Your GIT email: ' GIT_EMAIL
if [ -z "$GIT_EMAIL" ]; then
exitErr 'Aborted'
fi
printf 'Listing committer user names... '
svn log \
--username "$SVN_USERNAME" --password "$SVN_PASSWORD" \
--non-interactive --quiet \
"$SVN_URL" | awk '/^r/ {print $3}' | sort -u \
> "$AUTHORS_FILE" || exitErr 'FAILED!'
printf 'done.\n\n'
echo 'Now you have to fix the authors file.'
echo 'This file is needed to map the SVN usernames to GIT names and emails.'
echo 'For instance, if the SVN username is "mlocati" and the GIT author is "Michele Locati <mlocati@gmail.com>", then the authors file must contain a line like this:'
echo 'mlocati = Michele Locati <mlocati@gmail.com>'
echo 'An editor will be lauched: edit the map file and save it'
read -p 'Press RETURN to continue...' PROCEED
while true; do
editor "$AUTHORS_FILE"
read -p 'Proceed [Y(es), R(edo edit), A(bort)] ' PROCEED
case "$PROCEED" in
Y|y)
break
;;
A|a)
exitErr 'Aborted'
;;
R|r)
;;
esac
done
printf 'Creating GIT-SVN repository... '
mkdir "$GITDIR_DIRTY" || exitErr "Failed to create directory $GITDIR_DIRTY"
cd "$GITDIR_DIRTY" || exitErr "Failed to enter directory $GITDIR_DIRTY"
git svn init \
--no-metadata \
--username "$SVN_USERNAME" \
"$SVN_URL" >/dev/null || exitErr 'FAILED!'
git config svn.authorsfile "$AUTHORS_FILE"
printf 'done.\n'
printf 'Fetching SVN repository (this may take long time)... '
git svn fetch --quiet --quiet --log-window-size=1000 || {
exitErr 'FAILED!'
}
printf 'done.\n'
printf 'Ignoring files... '
git svn show-ignore > .gitignore
git add .gitignore
git config user.name "$GIT_USERNAME"
git config user.email "$GIT_EMAIL"
git commit --quiet -m 'Convert svn:ignore properties to .gitignore.'
printf 'done.\n'
printf 'Creating empty GIT repository... '
mkdir "$GITDIR_FINAL" || exitErr "Failed to create directory $GITDIR_FINAL"
cd "$GITDIR_FINAL" || exitErr "Failed to enter directory $GITDIR_FINAL"
git init --quiet
printf 'done.\n'
printf 'Creating final GIT repository... '
git remote add dirty "$GITDIR_DIRTY" || exitErr 'Failed to set dirty remote'
git pull --quiet dirty master:master || exitErr 'Failed to fetch from dirty remote'
git remote remove dirty
printf 'done.\n'
printf 'Cleanup... '
rm -rf "$GITDIR_DIRTY"
unlink "$AUTHORS_FILE"
printf 'done.\n'
printf "\nHere's your pretty GIT repository:\n$GITDIR_FINAL\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment