Home » Php » svn – How can I get the Subversion revision number in PHP?

svn – How can I get the Subversion revision number in PHP?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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);

Answer:

This is how I got it to work.
If your server is setup to allow shell_exec AND you have SVN installed just run:

$revision = `svnversion`;

or

$revision = shell_exec('svnversion');

Answer:

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:$

Answer:

Bit late now, but use a Subversion post-commit hook. In your repository’s hooks folder, create a shell script like this one:

#!/bin/bash

REPOS="$1"
REV="$2"

cd /web/root
rm -f /web/root/templates/base.html
/usr/bin/svn update
/bin/sed -i s/REVISION/$REV/ /web/root/templates/base.html

This particular example assumes your live site is in /web/root and the development code is held elsewhere. When you commit a dev change, the script deletes the prior live template (to avoid conflict messages), runs the update and replaces occurrences of REVISION in the template with the actual revision number.

More on hooks here

Answer:

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?

Answer:

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.

Answer:

You could also do it like this:

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

Answer:

The easiest way is to use the Subversion “Keyword Substitution”. There is a guide here in the SVN book (Version Control with Subversion).

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

Answer:

See my response to the similar question "Mark" svn export with revision.

If you capture the revision number when you export you can use:

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

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

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

Answer:

$svn_rev=file_get_contents('/path.to.repository/db/current');

Answer:

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).

Answer:

If performance is an issue, then you could do:

exec('svn info /path/to/repository', $output);
$svn_ver = (int) trim(substr($output[4], strpos($output[4], ':')));

This of course depends on your having done a checkout, and the presence of the svn command.