Show SVN revision number in PHP

By , May 15, 2009 12:16 pm

I recently had a need to display the current svn revision of a php based website on the home page. This is particularly useful when working on a website as part of group and you have a central dev server than runs a copy of the latest code. It allows you to quickly and easily see which revision is being run on that server.

Anyway, on to the doing part.

There are a number of different ways to achieve this, some more complicated and tedious then others.

The simplest method I found was to parse the .svn/entries file. This, of course, relies on the fact that the code you run is based on a checkout of the repo, rather than an export. This probably isn’t a good idea in a production environment, unless you setup your sever to prevent access to the .svn folder.

The code looks like this:

$svn = File('.svn/entries');
$svnrev = $svn[3];

You can now print the contents of $svnrev wherever you want and you should get the revision number dispalyed.

Note that the 3 indicates the 4th line in the file. If you get unexpected results, check the .svn/entries file and adjust the index on $svn[] as required.

The other methods of doing this are discussed here

Leave a Reply

Panorama Theme by Themocracy