Automatic SVN Revision number in source code

Posted February 9th, 2009 by Juozas

Versioning is essential for source code control and there is fabulous amount of possible benefits. One of the possibilities is to let your source code know and make decisions using it’s version (revision) number.

For example, if you change your CSS code, you want users to get it immediately and not after their browser cached version expires. One of possible ways, to force browser to load new version, is to add version number as a query string.

Normal HTML stylesheet code looks like this:
<link rel=”stylesheet” href=”/style.css” type=”text/css” media=”screen” />,
and with added version number:
<link rel=”stylesheet” href=”/style.css?91” type=”text/css” media=”screen” />.

Browsers will always grab new stylesheet after each url change – you can increase version number by +1, add date, etc. Let it be our goal – use SVN functionality to automatically tag stylesheets.

First thing to do, is to insert $Rev$ in source code of selected file (mine: style.css). Then go to console and set SVN property:

svn propset svn:keywords "Rev" style.css

Now, after each commit to repository, this keyword will be replaced by $Rev: NUMBER $, where NUMBER is current revision number. Next thing is to use PHP to read revision number from style.css source and append it to style URL.

Reading revision number is the easiest part, because if you store revision number in first line, you can quite efficiently grab only that part of source and preg_match/simple match number. Sample script looks like this:

//reading stream
$handle = fopen("style.css", "r");
//read first line, TODO: check if it's not empty, etc.
$first_line = fgets ($handle);
//extract revision number, chosen format: "/* $Rev: 1424314 $ */"
$revid = substr($first_line, 9, -6);
//print generated link
print "style.css? " . $revid;

You can easily modify and transform it to function, but key concept will be the same. However, this method is useful only to show revision number of one file, because revision number changes only after commit. For whole project revision number there are better solutions.

I would love to hear your ideas, because I brainstormed this one only a few days ago and I still think it can be made more efficiently. One of possible enchantments would be using build script, but I’m still searching for more simpler solutions.

Trackbacks/Pingbacks

  1. tomaszs wrzucił link na Flakera | flaker.pl

Comments (11)

  1. steven

    thanks! this helped me alot

  2. shahbaz

    The Subversion revision refers to the entire version-controlled tree, i.e. when you commit style.css the entire project goes to Rev 512 (or another arbitrary number for purposes of example) not just that one file. Therefore I’d say this is a solution for per-project version control. This would be a per-file solution if you were to use CVS instead of Subversion.

  3. Juozas (author)

    Yeah, it’s a revision number for whole project, but Rev Id in style.css only updates then you commit it.

    Yes, Rev Id numbers in file will not be sequential, but my solution still works: style.css?ID changes only after style.css commit ( – change).

  4. Rob T.

    Why go to the trouble of trying to parse the revision from the CSS file, when you could just do something like

    version.php
    <?
    MY_VERSION = “$Rev: $”

    and set the svn property on that? If any other php needs the version, version.php can be required().

    Hope This Helps,
    –Rob T.

  5. Juozas (author)

    Hi Rob,

    yep, your solutions works, but… Revision number in version.php changes only when you commit it. Is it really what you want?

  6. Rob T.

    > yep, your solutions works, but Revision number in version.php changes only when you commit it. Is it really what you want?

    Why is that any different than setting the SVN revision in CSS as you posted?

    Ok how about this, Version.PHP could contain the SVN rev constant as above, plus can have file or directory modification time in another.

  7. Juozas (author)

    Difference is this:
    function will return different number only when you change style.css.

    Numbers will not be in sequence, and can be like: 10,23,4345,etc., but still they will change ONLY when style.css is changed. This is what I what.

    Yes, having version.php is usefull because you always can have all sorts of information, but you need to commit it every time. And I better choose to create a build script (phing or similar) than lie for SVN :)

  8. Ian West

    It seems like quite an expensive operation to do. Why not just check the modified time of the file instead and append that?

  9. Juozas (author)

    In my opinion, best way is to have build system, which adds version numbers where needed :) This is kind of a hack…

    Currently I’m using filemtime($file) as file version – works well.

  10. KC

    The approach “style.css?revision_id” won’t work well as having query string like this will make the css not cachable. On the other hand, if the file name looks like “style.1234.css” where the 1234 is the rev number, it would work. Again, some sort of mod_rewrite might have to be in place to point the css file back to its original form “style.css”.

  11. jon

    this won’t seem to work with any binary assets such as images.

Leave a Reply

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