Getting the Revision Number of your Subversion Working Copy

I was asked today “How do I find out the revision number of my Subversion directory?” and I didn’t know the answer. The good news is there is an easy answer:
svnversion .

From the svnversion help command:

The version number will be a single number if the working
copy is single revision, unmodified, not switched and with
an URL that matches the TRAIL_URL argument. If the working
copy is unusual the version number will be more complex:

This command takes a little while to run, because it runs against the remote repository as well. It gives more detailed results also such as whether or not you have local modifications or if the repository has been changed since your last update. These details can be interesting in certain cases. But sometimes all you want to know is what revision you currently have checked out…

If all you want to do is check on the current revision of your local working copy you can use some Unix magic to do that.
grep revision .svn/entries | awk -F" '{print $2}'

Redirect this output to a file or use it as an argument to a build script and you can have the revision number put into your application.
ant -Drevision=`grep revision .svn/entries | awk -F" '{print $2}'`
(notice the backticks ` and this needs to be on one line)

A little Unix scripting and viola!

(Thanks to Ross Greinke for this little snippet)

2 Responses to “Getting the Revision Number of your Subversion Working Copy”

  1. Grant writes:

    You find out the current revision of a working directory by running ’svn info ‘.

    You find out whether a working copy has changes by running ’svn status ‘.

    If you attempt to place the revision number into your application, and then commit that change, the revision will immediately be invalid. What are you trying to accomplish here?

  2. Geoff Lane writes:

    Grant,
    Thanks for the extra information. We were actually using this trick in our deployment script so that we could insert the revision number into our application so that it would be available for QA and help desk people.

Leave a Reply