112

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Coltin
  • 3,744
  • 7
  • 31
  • 39
  • 3
    "svn info --show-item revision" will give the current revision to which the current directory is updated. – GreatAndPowerfulOz Apr 02 '16 at 02: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 Jan 10 '17 at 23:54
  • In connection with these comments, check out `svn help info` for the legal options for `svn info` – XavierStuvw Feb 25 '17 at 15:49
  • @IceArdor I'm using SVN v1.9.5 and `--show-item` is available – icc97 May 31 '17 at 17:27

26 Answers26

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
sth
  • 222,467
  • 53
  • 283
  • 367
73
<?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`;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92
  • This works in the command line, except $output is always an empty variable, which seems to be the case for all shell_exec() commands with SVN. Any idea's for this? – Coltin Feb 23 '09 at 21:06
  • 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`; – Daniel X Moore Feb 25 '09 at 05: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 (http://en.wiktionary.org/wiki/thusly); did you use it intentionally (pun?) or is it common for you to use thusly instead of thus? – odalet Feb 08 '13 at 00:20
  • @odalet I just used thusly for amusement, I don't often write or speak that way. – Daniel X Moore Feb 08 '13 at 01:14
  • 7
    `svn info $url | grep 'Last Changed Rev' | awk '{ print $4; }'` – Cobra_Fast Jun 11 '13 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 Feb 03 '16 at 00: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 May 31 '17 at 17:29
51

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:

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Nick Haddad
  • 8,767
  • 3
  • 34
  • 38
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-
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 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 Feb 23 '09 at 20:38
  • Yeah sorry, took it from a windows batch file and missed that. – Blorgbeard Feb 23 '09 at 20:39
  • With svn 1.5.1 under linux, we get "Unknown command: 'info %*'". Changing the first section to "svn info" works for me. – Stephen Feb 23 '09 at 20:39
  • 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 Feb 23 '09 at 20:39
  • 4
    Also, this does not work for localized versions of SVN (in Polish this shall be |cut -c9-). – zgoda Feb 23 '09 at 21:54
  • @Mr Fooz, true. Probably better to do `svn info svn://server/reporoot grep etc..` anyway, so you don't need a working folder. – Blorgbeard Feb 23 '09 at 22:11
  • 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 Mar 09 '10 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 Jun 01 '12 at 13:46
  • using just "svn info --show-item revision" will give you the current revision # only. – GreatAndPowerfulOz Apr 02 '16 at 02:16
36

The following should work:

svnlook youngest <repo-path>

It returns a single revision number.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Magentus
  • 611
  • 6
  • 8
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
derFunk
  • 1,587
  • 2
  • 20
  • 31
13

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sdaau
  • 36,975
  • 46
  • 198
  • 278
12
  • 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.

bahrep
  • 29,961
  • 12
  • 103
  • 150
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim
  • 20,184
  • 24
  • 117
  • 214
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.

rmeador
  • 25,504
  • 18
  • 62
  • 103
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

F.Igor
  • 4,119
  • 1
  • 18
  • 26
  • 1
    I tried on svn 1.9.3, and the command here is `svn info --show-item`, instead of `--show item` – elopio Oct 31 '17 at 03:56
2

If you want really short one:

svn info ^/

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

Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
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.

yoyo
  • 8,310
  • 4
  • 56
  • 50
  • 1
    you can use FINDSTR as well: `FOR /F "tokens=2" %%i IN ('svn info ^| FINDSTR Revision:') DO SET revision=%%i` – icc97 Oct 23 '14 at 13:55
  • see this other [SO answer](http://stackoverflow.com/a/2569837/327074) for the basis of this – icc97 Oct 23 '14 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sander Rijken
  • 21,376
  • 3
  • 61
  • 85
  • 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 Jul 26 '12 at 12:34
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
Nick Henry
  • 41
  • 1
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
Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53
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

DD_
  • 7,230
  • 11
  • 38
  • 59
1

You can try the below command:

svn info -r 'HEAD' | grep Revision: | awk -F' ' '{print $2}'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrajiv
  • 29
  • 4
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fitorec
  • 19
  • 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.

Nick
  • 2,342
  • 28
  • 25
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
$
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Sokolowski
  • 11,982
  • 4
  • 69
  • 55
0

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

Patryk
  • 22,602
  • 44
  • 128
  • 244
kip2
  • 6,473
  • 4
  • 55
  • 72
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.

Kram
  • 153
  • 1
  • 4
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

Ravi Saroch
  • 934
  • 2
  • 13
  • 28
-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

Markus Kuhn
  • 983
  • 9
  • 10
-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.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • 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 Feb 23 '09 at 20:41