Skip to content

Instantly share code, notes, and snippets.

@nathandaly
Last active January 2, 2018 16:04
Show Gist options
  • Save nathandaly/8d5712bd53ac4e5e338b027b888d2048 to your computer and use it in GitHub Desktop.
Save nathandaly/8d5712bd53ac4e5e338b027b888d2048 to your computer and use it in GitHub Desktop.
Script to automate SVN to Git synchronization.
#!/bin/bash
fetchAndRebase=true
retryfetch=false
commitToRefetchFrom=null
currentDateTime=`date '+%Y-%m-%d %H:%M:%S'`
# Check for any flags
while [ ! $# -eq 0 ]
do
case "$1" in
--retry-sync | -rs)
retryfetch=true
if [ -z "$2" ]
then
echo 'No commit hash supplied.'
[ $PS1 ] && return || exit;
fi
commitToRefetchFrom=$2
;;
esac
case "$1" in
--help | -h)
echo 'The purpose of this script is to help automate the synchronisation of SVN over to Git.'
;;
esac
shift
done
# Grab the latest changes from Git.
git fetch origin
git pull --rebase origin master
# If the retry sync flat was detected then reset the remote trunk
# to the commit passed in as an option.
if $retryfetch; then
retry_fetch
fi
# Git svn fetch and rebase.
if $fetchAndRebase; then
git svn fetch
java -Dfile.encoding=utf-8 -jar ~/svn-migration-scripts.jar sync-rebase
git pull --rebase origin master
# Check for unmerged files? git ls-files -u email to run manually?
git push origin master
fi
# If the sync and rebase removes git and build specific changes then re-apply them.
if [ ! -f .gitignore ]; then
gitify_branch
fi
retry_fetch () {
echo 'Backing up .git directory and removing remote svn refs.'
tar -zcf ../instanda-dot-git-dir_$currentDateTime.tar.gz .git
#rm .git/svn/refs/remotes/trunk/.rev_map.*
#git pack-refs --all
if grep -Fhqm 1 "refs/remotes/trunk" .git/packed-refs-test | head -1
then
echo "Rewinding svn trunk to commit $commitToRefetchFrom"
stringToReplace=$(grep -Fhm 1 "refs/remotes/trunk" .git/packed-refs-test | head -1)
sed -i 's,'"$stringToReplace"','"$commitToRefetchFrom refs/remotes/trunk"',' .git/packed-refs-test
else
echo "Error: ln 60. Could not rewind commit." | mail -s "Migrator Alert" nathan.daly@instanda.com
fetchAndRebase=false
fi
}
gitify_branch () {
echo '.gitignore file not found. Cherry pickingcat commits [bdec2b2a2157a28a6bd2f0d0f2306b0b6f1efd31, 867cf397ee1ef1c6d9eeb3201ff4e716dff87d4d]'
# git cherry-pick bdec2b2a2157a28a6bd2f0d0f2306b0b6f1efd31
# git cherry-pick 867cf397ee1ef1c6d9eeb3201ff4e716dff87d4d
diffA='Your branch is '
diffB=git rev-list --count master...origin/master # /home/svntogit/svn-sync.sh:62: command not found: rev-list?
diffC=' ahead of origin/master'
echo $diffA$diffB$diffC
if [ $diffB != "0" ]; then
echo 'Pushing rebase to origin master.'
git pull --rebase origin master
git push origin master
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment