Getting “svn info” using Groovy

In a Subversion utility I had to the get the revision number of the HEAD of the current working copy. An answer found gave a Linux command line solution. Here is my Groovy version.

Groovy allows the ability to execute a String. This returns a Process instance, we wait for its termination, and then we query the process for the return code, the output to stderr, and to stdout.

Below I take the output and stick it into a Properties object, then dump the entries. Of course, in practice I will be using the Properties in the solution (should have used a Map).

def command = ['svn','info','-rHEAD']
def proc = command.execute()
proc.waitFor()

int code = proc.exitValue()
if(code != 0){
    println "code: '${code}'"
    println "stderr: ${proc.err.text}"
    return
}

def props = new Properties()

proc.in.eachLine{
    def m = (it =~ /^(.*?):(.*)$/)
    if(m){
        def matches = m[0]
        def key = matches[1].trim()
        def value = matches[2].trim()
        props.put(key,value)
    }else{
        println 'Could not parse the output of svn info'
    }
}

println 'Properties ...'
props.each{
    println "[${it.key}]=[${it.value}]"
}

Example output
Note: The brackets were added to provide a visual test of output, they are not stored. Also, the content was manually changed.

Properties ...
[Last Changed Date]=[201.............pr 2014)]
[Last Changed Rev]=[737]
[Path]=[Widget]
[Repository UUID]=[ec14......b668719305]
[Revision]=[744]
[Relative URL]=[^/widget/branches/skunk]
[Repository Root]=[https://widget/svn/widget]
[Last Changed Author]=[alfred e. newman]
[URL]=[https://widget/svn/widget/branches/skunk]
[Node Kind]=[directory]

Example error output
On error the output would be something like:

code: '1'
stderr: svn: E155007: 'C:\temp' is not a working copy

Via SvnAnt?
An alternative is to use the svnant Ant task. This task has a subcommand ‘WcVersion’. It didn’t work for me.

Ant example is shown below, but you could of use the AntBuilder in Ant.

<target name="svn-info" depends="" description="- svn project info">
	<svn username="${svn.username}" password="${svn.password}">
		<wcversion path=".." prefix="svn" />
	</svn>		
</target>

Update
SvnAnt’s info command can be used to get this information. See http://subclipse.tigris.org/svnant/svntask.html#info

Links

Similar Posts:

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Leave a Reply

Your email address will not be published. Required fields are marked *