如何获取 PHP 中的 Subversion 修订号?
我想让我的 PHP 应用程序标有它使用的修订号,但我不想使用 CruiseControl 或更新文件并每次上传。 我该怎么做呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
你也可以这样做:
You could also do it like this:
最简单的方法是使用 Subversion 的“关键字替换”。 这里有一个指南SVN 书籍(使用 Subversion 进行版本控制)。
基本上,您只需将文本 $Rev$ 添加到文件中的某个位置即可。
然后在您的存储库中启用该关键字。 签出时,SVN 会将修订号替换到文件中。
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.
请参阅我对类似问题的回答“标记”带有修订版的svn导出< /a>。
如果您在导出时捕获修订号,则可以使用:
要删除除修订号之外的所有内容,您可以通过以下 sed 命令通过管道传输它:
See my response to the similar question "Mark" svn export with revision.
If you capture the revision number when you export you can use:
To strip everything but the revision number, you can pipe it through this sed command:
另一种可能性是运行一个 cron 来执行“部署过程”中描述的步骤(假设它是 *nix/FreeBSD 服务器)。
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).
如果性能是一个问题,那么您可以这样做:
这当然取决于您是否已完成结帐以及 svn 命令的存在。
If performance is an issue, then you could do:
This of course depends on your having done a checkout, and the presence of the svn command.
SVN关键字并不是一个好的解决方案。 正如其他人指出的那样,在文件中添加 $Revision$ 只会影响特定文件,该文件可能很长一段时间不会改变。
在每次提交之前记住“编辑”文件(通过添加或删除空行)是没有意义的。 您也可以手动输入修订版本。
一个好的方法(据我所知)是有一个自动化的部署过程(这总是一件好事)并使用命令 svnversion。 这就是我所做的:
无论我需要修订的地方,我都会包含:
。 这个“version.php”文件只有修订号。 此外,它不是存储库的一部分(它设置为被忽略)。 我的创建方法如下:
1)在服务器上安装了SVN的项目中,我也使用它进行部署。 将最新版本获取到服务器我有一个脚本,除其他外,它执行以下操作(它在服务器上运行):
2)在未安装 SVN 的项目上,我的部署脚本更加复杂:它在本地创建 version.php 文件,压缩代码,上传并解压
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):
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
假设您的 webroot 是 subversion 树的签出副本,您可以解析 /.svn/entries 文件并挂钩修订号(此处为第四行)...
在 PHP 中:
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:
这就是我让它发挥作用的方式。
如果您的服务器设置为允许 shell_exec 并且您安装了 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:
or
来自这个答案< /a>:
From this answer:
现在有点晚了,但请使用 Subversion 提交后挂钩。 在存储库的 hooks 文件夹中,创建一个如下所示的 shell 脚本:
此特定示例假设您的实时站点位于 /web/root 中,并且开发代码保存在其他位置。 当您提交开发更改时,脚本会删除先前的实时模板(以避免冲突消息),运行更新并将模板中出现的 REVISION 替换为实际修订号。
有关钩子的更多信息此处
Bit late now, but use a Subversion post-commit hook. In your repository's hooks folder, create a shell script like this one:
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
在大多数情况下,服务器上的代码实际上包含代码的“导出”,而不是签出,因此不包含 .svn 文件夹。 至少这是我最常看到的设置。 其他人是否真的在网络服务器上检查他们的代码?
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 关键字。 在您想要显示修订版本的位置添加 $Revision$,但这只会显示特定文件更改的最后修订版本,因此您每次都必须对该文件进行更改。 如果没有某种外部脚本或提交后挂钩,就不可能获取全局修订号。
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.