简体   繁体   中英

How can I get the Subversion revision number in PHP?

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?

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'; ?> <?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

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

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

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

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

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?

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.

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.

You could also do it like this:

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

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]\+\).*/\1/' > revision.txt
$svn_rev=file_get_contents('/path.to.repository/db/current');

执行此操作的另一种可能性是运行执行“部署过程”中描述的步骤的cron(假设它是* nix / FreeBSD服务器)。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM