[svn] Getting the last revision number in SVN?

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.

This question is related to svn

The answer is


The following should work:

svnlook youngest <repo-path>

It returns a single revision number.


Just svn info in BASH will give you all details

RESULT:
Path: .
URL: 
Repository Root: 
Repository UUID: 
Revision: 54
Node Kind: directory
Schedule: normal
Last Changed Author: 
Last Changed Rev: 54
Last Changed Date: 

You will get the REVISION from this


You're looking for a call that's similar to the commandline call

svn info URL

It seems that this is possible using the pysvn library, and there's a recipe that should help you get started. I'm not sure if there's something similar for PHP.

If you need to resort to calling the SVN binary yourself, make sure to use the --xml parameter to get the result as XML. That should be easier to parse than the commandline output.


You can try the below command:

svn info -r 'HEAD' | grep Revision: | awk -F' ' '{print $2}'

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'
[email protected]'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).


Someone else beat me to posting the answer about svnversion, which is definitely the best solution if you have a working copy (IIRC, it doesn't work with URLs). I'll add this: if you're on the server hosting SVN, the best way is to use the svnlook command. This is the command you use when writing a hook script to inspect the repository (and even the current transaction, in the case of pre-commit hooks). You can type svnlook help for details. You probably want to use the svnlook youngest command. Note that it requires direct access to the repo directory, so it must be used on the server.


You can use either commands:

1)
> svn info | awk '/Revision:/ { print $2 }' =>returns the latest version


2)
> svn log -l 1 | grep '^r[0-9]\+' | awk '{print $1}'

svn log -l 1     => returns the latest version commit 
grep '^r[0-9]\+' => greps the r4324 (revision) number
awk '{print $1}' => prints the match found

The posted solutions don't handle the case when externals are being used.

If I have a URL or a working copy with the svn:externals property set, the externals may change and thus the subversion server's latest revision will change. But the latest revision of the working copy or URL will only report the revision number when the svn:externals propty was change or any item lower in the URL path, which is expected behavior.

So you either get the svn:externals property and iterate over the URLs and pick the heights revision or query the base URL from the subversion server. The version reported from the base URL will contain the latest revision for EVERYTHING on the server.

So, if you are using externals, it's best to use svn info BASE_URL where BASE_URL is the root URL for all paths on the subversion server.


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.


This will get just the revision number of the last changed revision:

<?php
$REV="";
$repo = ""; #url or directory
$REV = svn info $repo --show-item last-changed-revision;
?>

I hope this helps.


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.


To really get the latest revision ("head revision") number on your remote respository, use this:

svn info -r 'HEAD' | grep Revision | egrep -o "[0-9]+"

Outputs for example:

35669

If you want to analyse a local working copy, the best tool is svnversion, which comes with Subversion and produces output like 968:1000M. The documentation says:

The version number will be a single number if the working copy is single revision, unmodified, not switched and with an URL that matches the TRAIL_URL argument. If the working copy is unusual the version number will be more complex:

4123:4168     mixed revision working copy
4168M         modified working copy
4123S         switched working copy
4123:4168MS   mixed revision, modified, switched working copy

You can use the XML out format, for example:

  svn info --xml | grep 'revision' | head -1 | grep -Eo "[0-9]+" | xargs expr -1 +

Example: using the revision number

working=`svn info --xml | grep 'revision' | head -1 | grep -Eo "[0-9]+" | xargs expr -1 +`
svn diff -r $working:HEAD --summarize --xml

If you want really short one:

svn info ^/

The caret notation is a shorthand for "the URL of the repository's root directory".


$ svn log | head -10 on whatever directory that has a .svn folder


This can be obtained using "SVN" library:

import svn.remote

file_path = "enter your filepath"

svn_inf = svn.remote.RemoteClient(file_path)

head_revision = ([x for x in svn_inf.log_default(revision_to = 'HEAD')] [-1]).revision

The head_revision should contain the latest revision number of the file


svn info -r HEAD

This will give you the latest revision number at the head of your repository.

There are some nice blog posts about integrating subversion numbers into your build script:


  • Starting with Subversion 1.9 you can use option --show-item to get a value of one of fields of svn info command's output. This command will display revision number only:

    svn info --show-item=revision <URL-to-repository>
    
  • Get XMLed output of svn info using --xml option and use PowerShell to get the revision number. Here is a simple example:

    [xml]$svninfo = svn info <REPOSITORY-URL> --xml -r HEAD
    $latestrevnum = $svninfo.info.entry.revision
    $latestrevnum
    
  • Using VisualSVN Server 3.4 or newer, you can get the number of revisions in a repository by running these commands:

    $repo = Get-SvnRepository <REPOSITORY-NAME>

    $repo.Revisions

    See Get-SvnRepository PowerShell cmdlet reference for more information.


Update: Subversion 1.9 will support a new command "svn youngest" that outputs only the latest revision number. The difference to "svnlook youngest" is that "svn youngest" also works remotely.

http://subversion.tigris.org/issues/show_bug.cgi?id=4299


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

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

svn info |grep Revision: |cut -c11-

svn info or svnversion won't take into consideration subdirectories, to find the latest 'revision' of the live codebase the hacked way below worked for me - it might take a while to run:

repo_root$ find ./ | xargs -l svn info  | grep 'Revision: ' | sort
...
Revision: 86
Revision: 86
Revision: 89
Revision: 90
$

I think you are looking for

svn info -r HEAD

Can you shell to that command?

You'll probably need to supply login credentials with the repository as well.


The simplest and clean way to do that (actually svn 1.9, released 2015) is using:

svn info --show-item revision [--no-newline] [SVNURL/SVNPATH] 

The output is the number of the last revision (joungest) for the SVNURL, or the number of the current revision of the working copy of SVNPATH. The --no-newline is optional, instructs svn not to emit a cosmetic newline (\n) after the value, if you need minimal output (only the revision number).

See: https://subversion.apache.org/docs/release-notes/1.9.html#svn-info-item