Getting the last revision number in SVN?

Svn

Svn Problem Overview


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 Solutions


Solution 1 - Svn

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

Solution 2 - Svn

<?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`;

Solution 3 - Svn

> 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:

Solution 4 - Svn

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

svn info |grep Revision: |cut -c11-

Solution 5 - Svn

The following should work:

svnlook youngest <repo-path>

It returns a single revision number.

Solution 6 - Svn

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

Solution 7 - Svn

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).

Solution 8 - Svn

  • 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.

Solution 9 - Svn

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.

Solution 10 - Svn

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.

Solution 11 - Svn

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

Solution 12 - Svn

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.

Solution 13 - Svn

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

Solution 14 - Svn

If you want really short one:

svn info ^/

The http://svnbook.red-bean.com/nightly/en/svn.basic.in-action.html#svn.advanced.reposurls">caret notation is a shorthand for "the URL of the repository's root directory".

Solution 15 - Svn

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.

Solution 16 - Svn

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

Solution 17 - Svn

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

Solution 18 - Svn

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

Solution 19 - Svn

You can try the below command:

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

Solution 20 - Svn

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.

Solution 21 - Svn

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
$

Solution 22 - Svn

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

Solution 23 - Svn

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.

Solution 24 - Svn

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

Solution 25 - Svn

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.

Solution 26 - Svn

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionColtinView Question on Stackoverflow
Solution 1 - SvnsthView Answer on Stackoverflow
Solution 2 - SvnDaniel X MooreView Answer on Stackoverflow
Solution 3 - SvnNick HaddadView Answer on Stackoverflow
Solution 4 - SvnBlorgbeardView Answer on Stackoverflow
Solution 5 - SvnMagentusView Answer on Stackoverflow
Solution 6 - SvnderFunkView Answer on Stackoverflow
Solution 7 - SvnsdaauView Answer on Stackoverflow
Solution 8 - SvnbahrepView Answer on Stackoverflow
Solution 9 - SvnTimView Answer on Stackoverflow
Solution 10 - SvnrmeadorView Answer on Stackoverflow
Solution 11 - SvnF.IgorView Answer on Stackoverflow
Solution 12 - SvnSander RijkenView Answer on Stackoverflow
Solution 13 - SvnNick HenryView Answer on Stackoverflow
Solution 14 - SvnSanghyun LeeView Answer on Stackoverflow
Solution 15 - SvnyoyoView Answer on Stackoverflow
Solution 16 - SvnfitorecView Answer on Stackoverflow
Solution 17 - SvnAngelin NadarView Answer on Stackoverflow
Solution 18 - SvnDD_View Answer on Stackoverflow
Solution 19 - SvnmrajivView Answer on Stackoverflow
Solution 20 - SvnNickView Answer on Stackoverflow
Solution 21 - SvnDaniel SokolowskiView Answer on Stackoverflow
Solution 22 - Svnkip2View Answer on Stackoverflow
Solution 23 - SvnKramView Answer on Stackoverflow
Solution 24 - SvnNaga Ravi Teja LellaView Answer on Stackoverflow
Solution 25 - Svnmatt bView Answer on Stackoverflow
Solution 26 - SvnMarkus KuhnView Answer on Stackoverflow