Getting the last revision number in SVN?

2019-01-04 07:33发布

Using PHP, Perl, or Python (preferably PHP), I need a way to query an SVN database and find out the last revision number sent to SVN. I don't need anything other than that. It needs to be non-intensive (so I do it every 5 minutes as a cron job; SVN's performance should not be affected).

SVN is located on my Intranet, but not my specific computer.

I have SVN installed, but no bindings installed for PHP/Perl/Python. I'm running Windows XP, but I would prefer a platform-independent solution that can also work in Linux. If you have a Linux-only (or XP-only) solution, that would also be helpful.

标签: svn
25条回答
迷人小祖宗
2楼-- · 2019-01-04 08:06

A note about getting the latest revision number:

Say I've cd-ed in a revisioned subdirectory (MyProjectDir). Then, if I call svnversion:

$ svnversion .
323:340

... I get "323:340", which I guess means: "you've got items here, ranging from revision 323 to 340".

 

Then, if I call svn info:

$ svn info
Path: .
URL: svn+ssh://server.com/path/to/MyProject/MyProjectDir
Repository Root: svn+ssh://server.com/path/to/MyProject
Repository UUID: 0000ffff-ffff-...
Revision: 323
Node Kind: directory
Schedule: normal
Last Changed Author: USER
Last Changed Rev: 323
Last Changed Date: 2011-11-09 18:34:34 +0000 (Wed, 09 Nov 2011)

... I get "323" as revision - which is actually the lowest revision of those that reported by svnversion!

 

We can then use svn info in recursive mode to get more information from the local directory:

> svn info -R | grep 'Path\|Revision'
Path: .
Revision: 323
Path: file1.txt
Revision: 333
Path: file2.txt
Revision: 327
Path: file3.txt
Revision: 323
Path: subdirA
Revision: 328
Path: subdirA/file1.txt
Revision: 339
Path: subdirA/file1.txt
Revision: 340
Path: file1.txt
Revision: 323
...

... (remove the grep to see the more details).

 

Finally, what to do when we want to check what is the latest revision of the online repository (in this case, @ server.com)? Then we again issue svn info, but with -r HEAD (note the difference between capital -R option previously, and lowercase -r now):

> svn info -r 'HEAD'
USER@server.com's password:
Path: MyProjectDir
URL: svn+ssh://server.com/path/to/MyProject/MyProjectDir
Repository Root: svn+ssh://server.com/path/to/MyProject
Repository UUID: 0000ffff-ffff-...
Revision: 340
Node Kind: directory
Last Changed Author: USER
Last Changed Rev: 340
Last Changed Date: 2011-11-11 01:53:50 +0000 (Fri, 11 Nov 2011)

The interesting thing is - svn info still refers to the current subdirectory (MyProjectDir), however, the online path is reported as MyProjectDir (as opposed to . for the local case) - and the online revision reported is the highest (340 - as opposed to the lowest one, 323 reported locally).

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-04 08:07

If you have the misfortune of needing to do this from a Windows batch file, here is the incantation you are looking for:

set REV=unknown
for /f "usebackq tokens=1,2 delims=: " %%A in (`svn info`) do if "%%A" == "Revision" set REV=%%B
echo Current SVN revision is %REV%

This runs "svn info", iterating through each line of generated output. It uses a colon as a delimiter between the first and second token on the line. When the first token is "Revision" it sets the environment variable REV to the second token.

查看更多
爷、活的狠高调
4楼-- · 2019-01-04 08:07

Looks like there is an entry in the official FAQ for this. The source code is in C but the same principle applies, as outlined here in this mailing list post.

查看更多
【Aperson】
5楼-- · 2019-01-04 08:10
nickh@SCLLNHENRY:~/Work/standingcloud/svn/main/trunk/dev/scripts$ svnversion
12354

Or

nickh@SCLLNHENRY:~/Work/standingcloud/svn/main/trunk/dev/scripts$ svn info --xml |     xmlstarlet sel -t --value-of "//entry/@revision"
12354

Or

nickh@SCLLNHENRY:~/Work/standingcloud/svn/main/trunk/dev/scripts$ svn info --xml | xmlstarlet sel -t --value-of "//commit/@revision"
12335
查看更多
狗以群分
6楼-- · 2019-01-04 08:11
<?php
    $url = 'your repository here';
    $output = `svn info $url`;
    echo "<pre>$output</pre>";
?>

You can get the output in XML like so:

$output = `svn info $url --xml`;

If there is an error then the output will be directed to stderr. To capture stderr in your output use thusly:

$output = `svn info $url 2>&1`;
查看更多
forever°为你锁心
7楼-- · 2019-01-04 08:12

This should work in Bash, from a working directory. I've used it in Windows with unixutils installed:

svn info |grep Revision: |cut -c11-
查看更多
登录 后发表回答