如何在 PHP 中获取 Subversion 版本号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/111436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the Subversion revision number in PHP?
提问by Thomaschaaf
I want to have my PHP application labeled with the revision number which it uses, but I don't want to use CruiseControlor update a file and upload it every time. How should I do it?
我想让我的 PHP 应用程序标有它使用的修订号,但我不想每次都使用CruiseControl或更新文件并上传它。我该怎么做?
回答by daremon
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.
SVN 关键字不是一个好的解决方案。正如其他人指出的那样,在文件中添加 $Revision$ 只会影响特定文件,该文件可能长时间不会更改。
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:
一个好的方法(我知道)是有一个自动化的部署过程(这总是一件好事)并使用命令 svnversion。这是我所做的:
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:
无论我在哪里需要修改,我都会做一个 include: <?php include 'version.php'; ?>。这个“version.php”文件只有修订号。此外,它不是存储库的一部分(它被设置为被忽略)。这是我创建它的方法:
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):
1)在服务器上安装了SVN的项目上,我也是用它来部署的。将最新版本发送到服务器我有一个脚本,除其他外,它执行以下操作(它在服务器上运行):
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
2) 在没有安装 SVN 的项目上,我的部署脚本更复杂:它在本地创建 version.php 文件,压缩代码,上传并提取它
回答by Oli
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)...
假设您的 webroot 是 subversion 树的检出副本,您可以解析 /.svn/entries 文件并勾出修订号(此处为第 4 行)...
In PHP:
在 PHP 中:
$svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
回答by Nathan J.B.
This is how I got it to work. If your server is setup to allow shell_execAND you have SVN installed just run:
这就是我让它工作的方式。如果您的服务器设置为允许shell_exec并且您安装了 SVN 只需运行:
$revision = `svnversion`;
or
或者
$revision = shell_exec('svnversion');
回答by Espo
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:$
并在服务器签入时将 $Id:$ 替换为当前修订号。我也找到了这个参考。
还有 $Date:$, $Rev:$, $Revision:$
回答by Mark
Bit late now, but use a Subversion post-commit hook. In your repository's hooks folder, create a shell script like this one:
现在有点晚了,但是使用 Subversion post-commit 钩子。在您的存储库的 hooks 文件夹中,创建一个如下所示的 shell 脚本:
#!/bin/bash
REPOS=""
REV=""
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.
此特定示例假设您的实时站点位于 /web/root 中,并且开发代码位于其他位置。当您提交开发更改时,脚本会删除先前的实时模板(以避免冲突消息),运行更新并将模板中出现的 REVISION 替换为实际修订号。
More on hooks here
更多关于钩子在这里
回答by Kibbee
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?
在大多数情况下,服务器上的代码实际上会包含代码的“导出”,而不是结帐,因此不包含 .svn 文件夹。至少这是我最常看到的设置。其他人是否真的将他们的代码检查到 Web 服务器上?
回答by Brian Gianforcaro
The easiest way is to use the Subversion "Keyword Substitution". There is a guide herein the SVN book (Version Control with Subversion).
最简单的方法是使用 Subversion 的“关键字替换”。有一个指导这里在SVN书(版本控制使用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.
您基本上只需要在文件中的某处添加文本 $Rev$。然后在您的存储库中启用关键字。结帐时 SVN 会将修订号替换到文件中。
回答by noah
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.
你可以接近SVN 关键字。添加 $Revision$ 您希望修订显示的位置,但这只会显示特定文件被更改的最后一个修订,因此您每次都必须对文件进行更改。如果没有某种外部脚本或提交后挂钩,就不可能获得全局修订号。
回答by Christoffer
You could also do it like this:
你也可以这样做:
$status = @shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
echo 'Revision: '.$match[0];
}
回答by fijiaaron
See my response to the similar question "Mark" svn export with revision.
请参阅我对类似问题“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:
要删除除修订号之外的所有内容,您可以通过以下 sed 命令进行管道传输:
svn export /path/to/repository | grep ^Exported | sed 's/^[^0-9]\+\([0-9]\+\).*//' > revision.txt

