Last active
August 29, 2015 14:15
-
-
Save Graham42/ea4576cd8c52f9af6776 to your computer and use it in GitHub Desktop.
Get a git-svn clone faster if someone else has already cloned
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# @arg 1 - the git repository on someone elses machine or a server | |
# ie. [email protected]:/home/gmcgregor/MODULE_NAME | |
# @arg 2 - the svn url to point to | |
# ie. https://svn.my-company.com/MODULE_NAME | |
# check if required programs are installed | |
type svn >/dev/null 2>&1 || { echo >&2 "svn needs to be installed for this script to run."; exit 1; } | |
type git >/dev/null 2>&1 || { echo >&2 "git needs to be installed for this script to run."; exit 1; } | |
git svn --help >/dev/null 2>&1 || { echo >&2 "git-svn needs to be installed for this script to run."; exit 1; } | |
GIT_VERSION=$( git --version | sed 's/.*\([0-9]\.[0-9]\.[0-9]\)/\1/' | sed 's/\.//g' ) | |
if [ $GIT_VERSION -ge 200 ]; then | |
REMOTE_PREFIX="origin/" | |
else | |
REMOTE_PREFIX="" | |
fi | |
# check args | |
args_err="Requires 2 arguments | |
@arg 1 - the git repository on someone elses machine or a server | |
@arg 2 - the svn url to point to | |
" | |
if [ $# -ne 2 ]; then | |
echo >&2 "$args_err" | |
exit 1 | |
fi | |
git ls-remote $1 >/dev/null 2>&1 || { echo >&2 "Not a valid git repo."; exit 1; } | |
svn list $2 >/dev/null 2>&1 || { echo >&2 "Not a valid svn url."; exit 1; } | |
# Clone locally - make sure the refs/remotes/ space matches the server | |
folder=${1%.git*} | |
folder=${folder%/} | |
folder=${folder##*/} | |
if [ -e $folder ]; then | |
echo >&2 "$folder already exists. Aborting." | |
exit 1 | |
fi | |
mkdir $folder | |
cd $folder | |
git init | |
git remote add origin $1 | |
git config --replace-all remote.origin.fetch '+refs/remotes/${REMOTE_PREFIX}*:refs/remotes/${REMOTE_PREFIX}*' | |
git fetch | |
# Prevent fetch/pull from remote git server in the future, | |
# we only want to use git svn for future updates | |
git config --remove-section remote.origin | |
# check the svn remote if standard layout | |
svn list $2 | grep "trunk/" -q | |
if [ $? -eq 0 ]; then | |
git svn init --stdlayout $2 | |
remote_branch="remotes/${REMOTE_PREFIX}trunk" | |
else | |
git svn init $2 | |
remote_branch="remotes/${REMOTE_PREFIX}git-svn" | |
fi | |
# Create branch master and point to trunk | |
git checkout -b master $remote_branch | |
# Git version 1.9.x doesn't support setting an upstream branch | |
if $(echo $GIT_VERSION | grep '^19' -q) ; then | |
git branch --set-upstream-to=$remote_branch | |
fi | |
# Pull the latest changes from Subversion | |
git svn fetch | |
git rebase $remote_branch | |
# if .gitignore does not exist, create it | |
if [[ ! -e .gitignore ]]; then | |
echo "# Migrated from svn:ignore" > .gitignore | |
git svn show-ignore >> .gitignore | |
fi | |
cd .. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment