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.







