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)

About Geoff Lane

I’m Geoff Lane and I write Zorched.net as I figure things out about software development in the hopes that it can help other people facing similar situations. Also as a thanks to the larger web community for all of the information and knowledge that they have shared. I’ve been a professional software developer since 1999 working with a variety of different technologies. I’ve worked for startups in the Silicon Valley and Chicago, IL and now work as a consultant building custom applications for clients.
This entry was posted in Code and tagged . Bookmark the permalink.

6 Responses to Getting the Revision Number of your Subversion Working Copy

  1. Grant says:

    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 says:

    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.

  3. Rob Russell says:

    @Grant
    Running svn info only gives you revision info for the current directory. If your working copy has subdirectories then svn info doesn’t tell you anything about them. Using svn info -R will give you way too much info.

    svnversion gave me what I needed – the latest revision number of any subdirectory of my working copy as well as the modified status.

    Thanks.

  4. lex says:

    The commands you quote are broken.
    e.g.
    grep revision .svn/entries | awk -F” ‘{print $2}’

    has a single double-quote in it. This won’t run. What are the correct commands please?

    Thanks!

  5. Geoff Lane says:

    @lex, just escape the quote with a backslash. Although this is old enough that I don’t think it works with the latest versions of subversion.

  6. g4b says:

    svn info -R | grep “Revision\:” | sort -k2nr | head -n1

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang="" line="" escaped=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>