Skip to content

Instantly share code, notes, and snippets.

@AlessandroVaccarino
Forked from mlocati/svn2git.sh
Last active June 15, 2018 15:29
Show Gist options
  • Save AlessandroVaccarino/4331fe10c269ff78e3bd3a701c7039b4 to your computer and use it in GitHub Desktop.
Save AlessandroVaccarino/4331fe10c269ff78e3bd3a701c7039b4 to your computer and use it in GitHub Desktop.
Convert SVN repository to GIT
#!/bin/bash
# This script was written for Ubuntu 14.04, Ubuntu 16.04 and MacOS 10.13.5
# Other operating systems may need a few changes
WORK_DIR=~/svn2git_`date "+%Y%m%d-%H%M%S"`
AUTHORS_FILE=$WORK_DIR/authors.txt
GITDIR_DIRTY=$WORK_DIR/dirty
GITDIR_FINAL=$WORK_DIR/final
function checkOs {
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
*) machine="UNKNOWN:${unameOut}"
esac
}
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
}
checkOs
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 https repository URL (skip to avoid remote Git push): ' GIT_URL
if [ ! -z "$GIT_URL" ]; then
GITPUSH=true
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 <[email protected]>", then the authors file must contain a line like this:'
echo 'mlocati = Michele Locati <[email protected]>'
echo 'An editor will be lauched: edit the map file and save it'
read -p 'Press RETURN to continue...' PROCEED
while true; do
case "${machine}" in
"Linux")
editor "$AUTHORS_FILE"
;;
"Mac")
open -a TextEdit "$AUTHORS_FILE"
;;
*)
echo "No Text Editor found. Manually open file $AUTHORS_FILE"
;;
esac
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"
if [ ! -z "$GITPUSH" ]; then
printf 'Push git on remote repository\n'
git remote add origin $GIT_URL
git push -u origin master
printf 'Repository pushed\n\n'
fi
read -p 'Do you want to delete temp folder? [Y(es), N(o)]' PROCEED
case "$PROCEED" in
Y|y)
rm -rf $WORK_DIR
;;
*)
;;
esac
@AlessandroVaccarino
Copy link
Author

AlessandroVaccarino commented Jun 15, 2018

Forked project from mlocati/svn2git.sh, to add:

  • MacOS support (tested on HighSierra 10.13.5)
  • Optional push of the project on remote Git repository (tested on AWS CodeCommit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment