tags:

views:

9080

answers:

9

I want to have my PHP Application labeled with the revision number which it uses, but don't want to use CruiseControl or update a file and upload it everytime. How should I do it?

+2  A: 

Assuming your webroot is a checked-out copy of the subversion tree, you could parse the /.svn/entries file and hook out the revision number (4th line here)...

In PHP:

$svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
Oli
Makes your application dependent on Svn internals. Sorry, a no-no
Brent.Longborough
@Brent.Longborough I would downvote your comment if I could, but I can't, so I'll leave this comment instead.
Adam Backstrom
+3  A: 

From this answer:

You can do it by adding the following anywhere in your code

$Id:$

So for example Jeff did:

<div id="svnrevision">svn revision: $Id:$</div>

and when checked in the server replaced $Id:$ with the current revision number. I also found this reference.

There is also $Date:$, $Rev:$, $Revision:$

Espo
Be carefull with this. As daremon mentioned, the SVN keyword won't update unless that file has changed. I have a codebase where this information is in an "About Window" that is very rarely changed. I frequently forget to "edit" that file so that the Rev ID is updated.
Matt Miller
+2  A: 

In most cases the code on the server would actually contain an "Export" of the code, not a checkout, and therefore not contain the .svn folders. At least that's the setup I see most often. Do others actually check out their code onto the web server?

Kibbee
No, you're probably right there. That's probably best behaviour for a production server too... You might not want people snooping at the .svn dirs.
Oli
I hope you surely disallow access to .svn dirs in your server configuration or at least .htaccess. Otherwise, your whole code can be seen.
Artem Russakovskii
+2  A: 

The easiest way is to use the subversion "Keyword Substitution".

There is a guide here in the svnbook.

You'll basically just have to add the text $Rev$ somewhere in your file. Then enable the keyword in your repo. On checkout svn will substitute the revision number into the file.

Hope that puts you in the right direction,

Brian Gianforcaro

Brian Gianforcaro
+2  A: 

You can get close with SVN Keywords. Add $Revision$ where you want the revision to show, but that will only show the last revision that particular file was changed, so you would have to make a change to the file each time. Getting the global revision number isn't possible without some sort of external script, or a post-commit hook.

noah
+37  A: 

SVN keywords is not a good solution. As others pointed out adding $Revision$ in a file only affects the specific file, which may not change for a long time.

Remembering to "edit" a file (by adding or removing a blank line) before every commit is pointless. You could as well just type the revision by hand.

One good way to do it (that I know of) is to have an automated deployment process (which is always a good thing) and using the command svnversion. Here is what I do:

Wherever I need the revision I do an include: <?php include 'version.php'; ?>. This "version.php" file only has the revision number. Moreover it is not part of the repository (it set to be ignored). Here is how I create it:

1) On projects where SVN is installed on the server, I also use it for deployment. Getting the latest version to the server I have a script that among other things does the following (it runs on the server):

cd /var/www/project
svn update
rm version.php
svnversion > version.php

2) On projects where SVN is not installed my deployment script is more complex: it creates the version.php file locally, zips the code, uploads and extracts it

daremon
A: 

Another possibility to do this is to run a cron that executes the steps described in the "Deploy Process" (assuming it is a *nix/FreeBSD server).

Andrei Iarus
A: 

You could also do it like this:

$status = @shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
    echo 'Revision: '.$match[0];
}
Christoffer
A: 

see my response to this similar question http://stackoverflow.com/questions/975083/mark-svn-export-with-revision/2373900#2373900

If you capture the revision number when you export you can

svn export /path/to/repository | grep ^Exported > revision.txt

To strip everything but the revision number, you can pipe through this sed command

svn export /path/to/repository | grep ^Exported | sed 's/^[^0-9]\+\([0-9]\+\).*/\1/' > revision.txt
fijiaaron