Using your application svn revision number

By Andy Dawson aka "AD7six"
A little snippet to show how to find and use the svn revision number - to your advantage.
There will be times, when it would be useful to know what revision your code is running on. The below little snippet of code (which you can place anywhere, e.g. in /app/config/bootstrap.php if you always want to know what revision your application is running on) will define a constant with the current revision.
Download code
<?php
if (!defined('REVISION')) {
    
$svn file(APP '.svn' DS 'entries');
    if (
is_numeric(trim($svn[3]))) {
        
$version $svn[3];
    } else { 
// pre 1.4 svn used xml for this file
        
$version explode('"'$svn[4]);
        
$version $version[1];    
    }
    unset (
$svn);
    
define ('REVISION'$version);
    unset (
$version);
}
?>

Avoiding stale cache files


How often have you had a conversation like this:
You : "I've fixed it, can you test that js/display problem you were having again please"
Customer : "It's still the same"
You : "Please refresh the page by pressing F5"
Customer : "It's still the same"
You : "Please delete your browser cache files"
Customer : "It's still the same"
You : "Please close your browser and reopen it"
Customer : "It's still the same.. Oh wait sorry I was looking at the wrong window it's fixed thanks."

You can avoid this game of its-still-the-same-arg-my-blood-pressure by using your code revision number to version-stamp your files. e.g. in your layout:

Download code
<?php
echo $html->css('styles.css?v=' REVISION'stylesheet');
echo 
$javascript->link('effects.js?v=' REVISION);
?>


In DB updates


I often/almost always include a little bug reporting tool in projects so that admin users can log their praise and admiration to a log that I read. On rare occasions they also add a note about problems too. Knowing the code version that was in place when they reported the problem is often invaluable to know. So for example in any model in which you want to store the current code version you can do:

Download code
<?php
function beforeSave() {
    if (!
$this->id) {
        
$this->data['Bug']['version_found'] = REVISION;
    }
    return 
true;
}
?>


And that's it. If you have any other innovative uses for the revision number your code is using comments are open :)

Comments 621

CakePHP team comments Author comments

Comment

1 svn keywords

I thought about using svn keywords for this ( eg LastChangedRevision ) but as far as I know ( I'm hope I'm wrong ) these are per-file so it's hard to know the latest revision for all files involved ...
posted Wed, Feb 13th 2008, 16:10 by Dieter Plaetinck

Comment

2 LastChangedRevision Wouldnt help

I thought about using svn keywords for this ( eg LastChangedRevision ) but as far as I know ( I'm hope I'm wrong ) these are per-file so it's hard to know the latest revision for all files involved ...

Hi Dieter :).
LastChangedRevision is per file, but you would want to know the LastChangedRevision of your css/js file in the layout - which has an overhead of logic/file access that to me isn't worth the effort (however small) beyond knowing the current revision number for the entire repository.

There's a similar ticket here: https://trac.cakephp.org/ticket/921 requesting timestamps but after a few discussions in the past I am of the opinion that "file exists? (file is in the vendors folder?, file is dynamicly generated?), last modified/last revision for that file -> stamp url simply soaks up too much time on the server.
posted Thu, Feb 14th 2008, 03:34 by Andy Dawson

Comment

3 Simple and useful

Thanks Andy, this is one of those things that is quite simple, but immediately useful.

Edit: Actually, I had to add a $version = trim($version); before the define() - there was a newline being appended.
posted Fri, Feb 15th 2008, 06:13 by Grant Cox.

Login to Submit a Comment