Skip to content

Instantly share code, notes, and snippets.

@JanGorman
Created December 2, 2010 14:38
Show Gist options
  • Save JanGorman/725430 to your computer and use it in GitHub Desktop.
Save JanGorman/725430 to your computer and use it in GitHub Desktop.
Needs an environment variable named GIT_SVN_URL that points to the branches folder of the SVN repo you're tracking, ie. export GIT_SVN_URL=https://your.repo.com/project/branches and then you can git-svn-pull-branch <svn branch> <local name> <rev spec>
#!/bin/sh
CMD=$(basename $0)
if [ $# != 3 ]; then
echo "usage $CMD <svn branch> <local name> <rev spec>"
exit 1
fi
if [ ! -d .git ]; then
echo "Not a git workspace"
exit 1
fi
BRANCH=$1
LOCAL_NAME=$2
REV=$3
SVN_URL="$GIT_SVN_URL/$BRANCH"
svn_find_rev()
{
cut -f 1 '-d ' | sed -e 's/^r//'
}
svn_find_first_rev()
{
tail -n +2 | head -1 | svn_find_rev
}
svn_find_last_rev()
{
SAWSEP=0
LASTREV=
while read line; do
if [ $SAWSEP == 1 ]; then
NEXTREV=$(echo $line | svn_find_rev)
if [ "x$NEXTREV" != "x" ]; then
LASTREV=$NEXTREV
fi
SAWSEP=0
elif [ "$line" == "$SEP" ]; then
SAWSEP=1
fi
done
echo $LASTREV
}
if [ "$REV" = "HEAD" ]; then
REV=$(svn log -l 1 $SVN_URL | svn_find_first_rev)
elif [ "$REV" = "PARENT" ]; then
REV=$(svn log --stop-on-copy $SVN_URL | svn_find_last_rev)
elif [ "$REV" = "ALL" ]; then
false
fi
NREV=$(expr $REV + 0 2> /dev/null)
if [ "x$NREV" != "x" ]; then
BRANCHPREFIX=$(basename $BRANCH | sed -e 's/_branch$//')
if [ "$(fgrep "[svn-remote \"$BRANCH\"]" .git/config)" != "" ]; then
echo "$BRANCH already in this git"
exit 1
fi
echo "Registering $BRANCH with git"
cat <<EOF >> .git/config
[svn-remote "$BRANCH"]
url = $SVN_URL
fetch = :refs/remotes/$BRANCH
EOF
echo "Fetching base revision $NREV"
git svn fetch -R $BRANCH -r $NREV || exit 1
git branch --track $LOCAL_NAME $BRANCH || exit 1
echo "Checking out $BRANCH"
git checkout $LOCAL_NAME || exit 1
echo "Updating $LOCAL_NAME"
git svn rebase || exit 1
else
echo "Cannot resolve rev spec '$REV'"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment