113

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.

4
  • 3
    "svn info --show-item revision" will give the current revision to which the current directory is updated. Commented Apr 2, 2016 at 2:13
  • 2
    $ svn info --show-item revision svn: invalid option: --show-item Using Apache Subversion 1.8.16 (r1740329) compiled Apr 26 2016. Also tried with CollabNet 1.6.11 (r934486) compiled Mar 6 2014.
    – IceArdor
    Commented Jan 10, 2017 at 23:54
  • In connection with these comments, check out svn help info for the legal options for svn info Commented Feb 25, 2017 at 15:49
  • @IceArdor I'm using SVN v1.9.5 and --show-item is available
    – icc97
    Commented May 31, 2017 at 17:27

26 Answers 26

134

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
4
  • 2
    agreed, much better than svn info
    – Richard
    Commented May 20, 2010 at 8:32
  • 34
    svnversion is for local working copies, and won't access the repository on the intranet as specified in the question. Commented Jun 17, 2010 at 17:33
  • 7
    For those not familiar with svnversion, it is part of the standard command-line client and here is the manual page: svnbook.red-bean.com/nightly/en/svn.ref.svnversion.re.html. Commented May 26, 2011 at 18:21
  • I wonder how to get it on cygwin. Commented Jun 21 at 17:51
74
<?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`;
7
  • 2
    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; Commented Feb 25, 2009 at 5:09
  • 3
    Thanks! I used --xml to build my own C# tool that helps me in generating an AssemblyFileVersion attribute. PS: English is not my primary language and I got puzzled by the word 'thusly' I had never heard before. Btw, its origin is rather funny: cf (en.wiktionary.org/wiki/thusly); did you use it intentionally (pun?) or is it common for you to use thusly instead of thus?
    – odalet
    Commented Feb 8, 2013 at 0:20
  • 7
    svn info $url | grep 'Last Changed Rev' | awk '{ print $4; }'
    – Cobra_Fast
    Commented Jun 11, 2013 at 13:16
  • 7
    @Cobra_Fast, you don't need to pipe, you can do svn info --show-item revision $url or svn info --show-item last-changed-revision $url.
    – cp.engr
    Commented Feb 3, 2016 at 0:24
  • 2
    @cp.engr I believe that's only since SVN v1.9 or so - that is I have it in v1.9.5 and it doesn't exist in v1.8.6
    – icc97
    Commented May 31, 2017 at 17:29
50

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:

2
  • This is the simplest way as you don't have to find out the repository url first. Thanks!
    – derFunk
    Commented Oct 15, 2013 at 18:02
  • I got errors using quotes around HEAD, so it should just be svn info -r HEAD
    – icc97
    Commented Oct 23, 2014 at 10:48
39

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

svn info |grep Revision: |cut -c11-
9
  • It works if you put the repository URL where you have %*, whatever you mean by that. Nice syntax of cut, didn't know it before.
    – ypnos
    Commented Feb 23, 2009 at 20:38
  • 1
    Note: since this lacks -rHEAD, this will return the commit revision for the requested file or directory. This may not be the latest overall revision (because other changes may have been committed elsewhere or in a subtree).
    – Mr Fooz
    Commented Feb 23, 2009 at 20:39
  • 4
    Also, this does not work for localized versions of SVN (in Polish this shall be |cut -c9-).
    – zgoda
    Commented Feb 23, 2009 at 21:54
  • 2
    that is, btw, the reason why I strongly dislike localized command line developer tools; zero advantage, bunch of problems. I usually remove the localizations for exactly this reason.
    – gimpf
    Commented Mar 9, 2010 at 11:11
  • 8
    Passing a delimiter instead of a character count works best for localized content, for example svn info | grep Revision | cut -d " " -f 2 will return the second string after it is split using spaces.
    – Butifarra
    Commented Jun 1, 2012 at 13:46
37

The following should work:

svnlook youngest <repo-path>

It returns a single revision number.

0
18

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
0
14

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

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

0
8

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.

3
  • I think it needs to be svn info -r HEAD
    – Stephen
    Commented Feb 23, 2009 at 20:35
  • -r can easily be ommited then.
    – ypnos
    Commented Feb 23, 2009 at 20:37
  • Right - I didn't specify the option for the -r - this was just for him to find what he needs and get started with the help/book.
    – Tim
    Commented Feb 23, 2009 at 20:39
4

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.

3

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

1
  • 1
    I tried on svn 1.9.3, and the command here is svn info --show-item, instead of --show item
    – elopio
    Commented Oct 31, 2017 at 3:56
2
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
2

If you want really short one:

svn info ^/

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

2

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.

2
  • 1
    you can use FINDSTR as well: FOR /F "tokens=2" %%i IN ('svn info ^| FINDSTR Revision:') DO SET revision=%%i
    – icc97
    Commented Oct 23, 2014 at 13:55
  • see this other SO answer for the basis of this
    – icc97
    Commented Oct 23, 2014 at 14:01
2

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.

1
  • I use subprocess.check_output in python to avoid yet another library call. If you're just checking revision numbers/pulling down latest copies, it's simpler just to use svn from the commandline via subprocess. Now, if you're doing automatic check-ins and merges...
    – yurisich
    Commented Jul 26, 2012 at 12:34
1

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
1

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

1

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
1

You can try the below command:

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

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.

0

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.

0

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

0

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
$
0

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

-1

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.

1
  • This is a great tool/answer, but I don't think he needs that much - I understood it only to need the latest revision so maybe he can start a build if his current revision is different from a previous one since he last checked. Maybe I misunderstood.
    – Tim
    Commented Feb 23, 2009 at 20:41
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.