Wednesday, 19 August 2009

Code: How to spell svnversion in git

I've been increasingly using git to clone and work on remote Subversion repos, as it's tedious to perform VCS operations over the atlantic when your web connection is tantamount to IP over carrier pigeon.

One operation I require in order to write sane autobuild scripts is something akin to svnversion. I need it to work for me, in my git svn-cloned repo, and for the mortals in other offices using plain ol' svn.

Took me a while, and here's what I came up with.
#!/bin/bash

# Returns the svn version number of the current directory.
# Works in an svn working copy, or in a git svn clone of an svn repo.
get_svnversion()
{
alias git_svnversion="git svn find-rev `git log -1 --pretty=format:%H 2>/dev/null` 2>/dev/null"
SVNVERSION=`svnversion`
if [ "X$SVNVERSION" == "Xexported" -o "X$SVNVERSION" == "X" ]; then
git_svnversion
else
echo $SVNVERSION
fi
}

echo $(get_svnversion)
Of course, there are some unpleasant dependencies on the particular version of svnversion you have installed and it's output on a non-svn tree. You could probably make this a little more bullet-proof if required.

Oh, and before you ask: blogger ate my formatting.