Fetching svn branches with git-svn

Consider this a follow up of Tracking 2 branches in git-svn.

At work I've needed to pull other svn branches into my git-svn repository, and it happened often enough that I wrote a script to do it:

#!/bin/sh

CMD=$(basename $0)
SVNURL=svn://svn/mgmt

if [ $# != 2 ]; then
    echo "usage $CMD <svn branch> <rev spec>"
    exit 1
fi  

if [ ! -d .git ]; then
    echo "Not a git workspace"
    exit 1
fi  

BRANCH=$1
REV=$2
SVNURL="$SVNURL/$BRANCH"
SEP='------------------------------------------------------------------------'  

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
    # let's find out the head revision
    REV=$(svn log -l 1 $SVNURL | svn_find_first_rev)
elif [ "$REV" = "PARENT" ]; then
    # find the first parent
    REV=$(svn log --stop-on-copy $SVNURL | svn_find_last_rev)
elif [ "$REV" = "ALL" ]; then
    # bad idea, not implemented
    false
fi

NREV=$(expr $REV + 0 2> /dev/null) # make sure we have a number
if [ "x$NREV" != "x" ]; then
    BRANCHPREFIX=$(basename $BRANCH | sed -e 's/_branch$//')
    if [ "$(fgrep "[svn-remote \"$BRANCHPREFIX\"]" .git/config)" != "" ]; then
        echo "$BRANCH is probably already in this git"
        exit 1
    fi

    echo "Registering $BRANCH with git"
    cat <<EOF >> .git/config
[svn-remote "$BRANCHPREFIX"]
        url = svn://svn/mgmt/$BRANCH
        fetch = :refs/remotes/git-$BRANCHPREFIX
EOF 

    echo "Fetching base revision $NREV"
    git svn fetch -R $BRANCHPREFIX -r $NREV || exit 1
    git branch --track $(basename $BRANCH) git-$BRANCHPREFIX || exit 1
    echo "Checking out $BRANCH"
    git checkout $(basename $BRANCH) || exit 1
    echo "Updating $BRANCH"
    git svn rebase || exit 1
    git gc --auto
else
    echo "Cannot resolve rev spec \`$REV'"
    exit 1
fi

It assumes that all your svn branches end with "_branch", which is the convention that we have at work. A quick look suggests that it should work even if that's not the case, but I've not tested it.

by khc on Wed Feb 18 00:07:34 2009 Permlink
Tags: computer