tags:

views:

3941

answers:

9

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, SVN's performance cannot be effected).

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 work in Linux. If you have a Linux only (or XP only) solution that is also helpful, though not ideal.

A: 

I think you are looking for

svn info -r HEAD

can you shell to that command?

you'll probably need to supply a login credentials with repo as well

Tim
I think it needs to be svn info -r HEAD
Stephen Pape
-r can easily be ommited then.
ypnos
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
A: 

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
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
+6  A: 

This should work in bash, from a working directory. I've used it in windows with unixutils installed.

svn info |grep Revision: |cut -c11-
Blorgbeard
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
Yeah sorry, took it from a windows batch file and missed that.
Blorgbeard
With svn 1.5.1 under linux, we get "Unknown command: 'info %*'". Changing the first section to "svn info" works for me.
Stephen Pape
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
Also, this does not work for localized versions of SVN (in Polish this shall be |cut -c9-).
zgoda
@Mr Fooz, true. Probably better to do `svn info svn://server/reporoot grep etc..` anyway, so you don't need a working folder.
Blorgbeard
@zgoda, good point. It's not exactly a bulletproof solution
Blorgbeard
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
+5  A: 
<?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`;
Daniel X Moore
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
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>
Daniel X Moore
+3  A: 

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:

Nick Haddad
A: 

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

Sander Rijken
+11  A: 

The best tool is svnversion, that comes with subversion and produces output like 968:1000M. The docs say:

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
+1 for this, better than `svn info`
CharlesB
agreed, much better than svn info
Richard
svnversion is for local working copies, and won't access the repository on the intranet as specified in the question.
Daniel X Moore
+1  A: 

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

Would the following work?

svnlook youngest <repo-path>

It returns a single revision number.