Saturday, March 14, 2009

NAnt: Getting Subversion Revision

I'm sure this may be old news for many of you, but it isn't for me. This NAnt include file extracts the revision number for the HEAD revision for the base project directory (the directory of the build file that was invoked) into a property called "svn.revision":

<project>
    <property name="svn.revision" value="0" />
    <target name="svn-revision">
        <exec
            program="svn"
            commandline="info ${project::get-base-directory()}@HEAD --xml"
            output="_svnrev.xml"
            failonerror="false" />
        <xmlpeek
            file="_svnrev.xml"
            xpath="info/entry/commit/@revision"
            property="svn.revision"
            failonerror="false" />
        <delete file="_svnrev.xml" />
        <echo message="SVN Revision: ${svn.revision}" />
    </target>
</project>

This script requires a Subversion client. Being a TortoiseSVN fan, I looked into its automation potential. Unfortunately, it wouldn't do anything without popping up window of some sort, and I sought a quieter approach. Hence, the command-line Subversion client.

The exec task invokes the SVN "info" subcommand, passing the option to output the results as XML. Then, it stores the XML in a temporary file. Finally, the xmlpeek task uses XPath to read the revision number from the temporary file.

1 comment:

Note: Only a member of this blog may post a comment.