When working with a version control repository, it is often useful to  know what version of script we are working on or - in a production  environment - which version we are actively using. Subversion provides  the facility for incorporating the current version number in a script or  source file every time it is committed to the repository. Here's how it can be used:

Adding the Subversion revision to a document

This is pretty easy. We just need to set a subversion property on the  document or file and make sure a keyword is included in the document  where we want the revision number to appear.

  • to place a revision number in a document or file, include the text item $Rev$ where the revision number should appear. This will be replaced by text like $Rev: 444$ the next time you commit your script (the number will be different of course)
  • to tell subversion to update this text item when we commit, we need  to set a property on the file; we can do this with a subversion command  like
svn propset svn:keywords "Rev"

This is elaborated on in the subversion manual and we can include other  items of information using keywords such as date, author or id (a  combination of other keywords). There are a couple of issues you need to  be aware of:

  • using the key / value string as normal content in your  document can cause the string to be interpreted as a variable which may need disguising e.g. see the perl and bash scripts below
  • subversion properties can be fragile and are largely invisible i.e. properties cannot be set using wildcards (only by individual filename) and it is not easy to see which files have which properties set

A simple bash script to set properties on all files in a folder might look like this

KEYWORDS="Rev Id Date Author"
SVNPROPS="svn propset svn:keywords"
for FILE in *
    if [ -a $FILE ]

Having this information automatically updated in your subversion  content is very useful for documentation purposes, if nothing else.

Using $Rev$ in our version number scheme

Now that your documents or scripts have a revision number embedded in  them, how can we use this information to construct a document or script  version number using the revision. For instance we could have a version  number as 1.2.321 where 321 is a subversion revision number. Generally  we need to manipulate this information as a string. Here are a few  examples in different languages. First, in python:  

__version__ = '0.1.' + '$Rev: 21 $'[6:-2]

In this case we are just treating the revision string as a simple  string and slicing it. This will create a version number like 0.1.21,  And in perl:

my $Rev=0; my $VERSION="$Rev: 21 $Rev"; my @Ver=split(" ",$VERSION); $VERSION="1.2.$Ver[1]";

Here we use several checks to fool perl: we initialise a $Rev  variable so we can include the key-name in a regular string (i.e. rather  than being embedded in a comment somewhere); now we can just split the  string into parts and pick out the value we want. Simples! to give us a  version of 1.2.21. And in bash:

REV="\$Rev: 21 $" REV=${REV/\$Rev: /} REV=${REV// \$/} VERSION="2.3.$REV"

Here we use some regex logic to strip a revision string of the characters we do not want, to give us a version of 2.3.21

Sadly I cannot claim any of these tricks as my own, nor can I provide  the original author's names - they have been lost in the mists of time.