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条回答
手持菜刀,她持情操
2楼-- · 2019-01-06 10:09

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');
查看更多
地球回转人心会变
3楼-- · 2019-01-06 10:09

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?

小情绪 Triste *
4楼-- · 2019-01-06 10:10

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

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

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);
查看更多
干净又极端
6楼-- · 2019-01-06 10:19
$svn_rev=file_get_contents('/path.to.repository/db/current');
手持菜刀,她持情操
7楼-- · 2019-01-06 10:21

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
查看更多
登录 后发表回答