How can I get the Subversion revision number in PH

2019-01-06 09:45发布

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?

标签: php svn revision
13条回答
▲ chillily
2楼-- · 2019-01-06 10:23

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

查看更多
Evening l夕情丶
3楼-- · 2019-01-06 10:24

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

查看更多
Rolldiameter
4楼-- · 2019-01-06 10:28

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.

查看更多
够拽才男人
5楼-- · 2019-01-06 10:28

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.

查看更多
做自己的国王
6楼-- · 2019-01-06 10:28

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.

查看更多
家丑人穷心不美
7楼-- · 2019-01-06 10:29

You could also do it like this:

$status = @shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
    echo 'Revision: '.$match[0];
}
查看更多
登录 后发表回答