Mark Needham

Thoughts on Software Development

Getting latest tagged revision in SVN from DOS/Batch script

with 3 comments

The way we have setup the build on our continuous integration server, Team City is configured to create a new tag every time the functional tests past successful on that machine.

We then have a QA and Showcase build that we can run to deploy all the artifacts necessary to launch the application on that machine.

Originally I had just written the batch script to take in the tag of the build which the user could find by looking through repo-browser for the last tag created. This quickly became very tedious so I started looking for a way to get the latest tagged revision from the command line.

We thought it would be possible to get this information using svn info but it turned out that the information returned by svn info about revisions doesn’t necessarily refer to the latest created tag. We ended up using svn log and then parsing through that data. It’s a bit messy but it does the job (I name each tagged version of the code as ‘build-{TeamCity-Build-Number}):

FOR /F "Tokens=2" %%i in ('svn log /tags/path --limit=1 -v ^| find "build"') do set TMP=%%i
FOR /F "Tokens=2 delims=/" %%i in ('echo %TMP%') do SET TAG=%%i

The for loop uses a space as its default delimiter so that’s what the ‘delims=/’ is doing on the second line, the ‘Tokens=2’ allows us to get the second token after the string is split and the ‘^’ in the first command is being used to escape the pipe.

Be Sociable, Share!

Written by Mark Needham

August 16th, 2008 at 12:10 am

  • svnnoob

    The way I am doing it with php is as follows

    $html = file_get_contents("");
    if(preg_match_all('@\\@U', $html, $matches)){
    echo str_replace("/", "", $matches[1][count($matches[1]) - 1]);

    Shame that there is no way to do it directly with svn :/ but thanks for the post all the same

  • Andrew

    Your script works great except in one case…
    Suppse you made a tag by mistake, or misspelled it, so you make the right tag and then delete the bad one. When you get the log, the last event was the deletion, and you are not getting the last real tag!
    Do you have a workaround for this?

  • Bruno Mota

    don’t work. What do I have to replace your code?