Skip to content

Instantly share code, notes, and snippets.

@SpiZeak
Forked from mlocati/svn2git.sh
Last active May 14, 2020 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpiZeak/bc40aa70e9f32515b4595b03b63570c7 to your computer and use it in GitHub Desktop.
Save SpiZeak/bc40aa70e9f32515b4595b03b63570c7 to your computer and use it in GitHub Desktop.
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 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
}
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
code "$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"
/usr/bin/git svn init \
--no-metadata \
--username "$SVN_USERNAME" \
"$SVN_URL" >/dev/null || exitErr 'FAILED!'
/usr/bin/git config svn.authorsfile "$AUTHORS_FILE"
printf 'done.\n'
printf 'Fetching SVN repository (this may take long time)... '
/usr/bin/git svn fetch --quiet --quiet --log-window-size=1000 || {
exitErr 'FAILED!'
}
printf 'done.\n'
printf 'Ignoring files... '
/usr/bin/git svn show-ignore > .gitignore
/usr/bin/git add .gitignore
/usr/bin/git config user.name "$GIT_USERNAME"
/usr/bin/git config user.email "$GIT_EMAIL"
/usr/bin/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"
/usr/bin/git init --quiet
printf 'done.\n'
printf 'Creating final GIT repository... '
/usr/bin/git remote add dirty "$GITDIR_DIRTY" || exitErr 'Failed to set dirty remote'
/usr/bin/git pull --quiet dirty master:master || exitErr 'Failed to fetch from dirty remote'
/usr/bin/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