tags:

views:

639

answers:

8

Like this question, but without the need to actually query the SVN server. This is a web-based project, so I figure I'll just use the repository as the public view (unless someone can advise me why this is a bad idea). I assume this info is in a file in the .svn folder somewhere, but where, and how do I parse it? I want to do something like what SO does, having the revision in the bottom-right corner.

Code in Python or PHP appreciated if you have it on hand, or just a point in the right direction.

+2  A: 

Hi,

You can get the current revision number of a checkout using, on the command line, "svn info".

For instance :

$ svn info
Chemin : .
URL : http://.../trunk
Racine du dépôt : http://...
UUID du dépôt : 128b9c1a-...-612a326c9977
Révision : 185
Type de nœud : répertoire
Tâche programmée : normale
Auteur de la dernière modification : ...
Révision de la dernière modification : 185
Date de la dernière modification: 2009-09-28 20:12:29 +0200 (lun. 28 sept. 2009)

Note it's localized ; if you're on Linux, you could try using :

$ LANG=en svn info
svn: warning: cannot set LC_CTYPE locale
svn: warning: environment variable LANG is en
svn: warning: please check that your locale name is correct
Path: .
URL: http://.../trunk
Repository Root: http://...
Repository UUID: 128b9c1a-...-612a326c9977
Revision: 185
Node Kind: directory
Schedule: normal
Last Changed Author: mzeis
Last Changed Rev: 185
Last Changed Date: 2009-09-28 20:12:29 +0200 (Mon, 28 Sep 2009)

If using this from PHP, though, getting it as XML might be more helpful (easier to parse, and not locale-aware) :

$ svn info --xml
<?xml version="1.0"?>
<info>
<entry
   kind="dir"
   path="."
   revision="185">
<url>http://.../trunk&lt;/url&gt;
<repository>
<root>http://...&lt;/root&gt;
<uuid>128b9c1a-...-612a326c9977</uuid>
</repository>
<wc-info>
<schedule>normal</schedule>
<depth>infinity</depth>
</wc-info>
<commit
   revision="185">
<author>...</author>
<date>2009-09-28T18:12:29.130307Z</date>
</commit>
</entry>
</info>

Just use simplexml_load_string on that, and fetch the revision attribute of the entry tag.


Note that I would not do that on each page view : not quite as fast as one would hope for your application.

Instead, I would get that revision number when creating the archive I will, later, send to the production server -- and store it in some kind of configuration file.

This way, you don't need to use the svn command on your production server, and you don't need to do your checkout on that server either.

Pascal MARTIN
I was hoping for a method other than parsing `svn info` but I suppose it is a valid solution. Probably produces more consistent info than parsing a file... the `--xml` option I didn't know about; should make things easier.
Mark
+3  A: 

I would probably write out the version number to a text file somewhere within the website using the post-commit hook and then just read that file when loading your page.

More on using hooks here

inkedmn
Now that sounds efficient!
Mark
I can't tell if you're being sarcastic or not, but it does sound way more efficient to me than shelling or socketing out to svn and then parsing a bunch of output.
ithcy
No...not at all. Didn't mean for it to come off that way :)
Mark
A: 

You could just shell out to the command line and run svn info. Just keep in mind this could be a mixed revision. You could also run an svn up and the last line of STDOUT will give you the revision number.

Or, are you wanting to use the svn API? You might also want to look at the TortoiseSVN code from SubWCRev which is used to replace a token in a file with the current revisions of the WC.

PilotBob
+1  A: 

I like the solution used by MediaWiki, reading the contents of .svn/entities. Check the first answer on this question: http://stackoverflow.com/questions/16248/getting-the-subversion-repository-number-into-code

Adam Backstrom
+1  A: 

If you're looking for a Python implementation, check out pysvn, you can invoke the info command to get details including the current revision:

entry = info( path )
Rich Seller
+2  A: 

This works for svn version 1.6.2. But be warned, different SVN versions use different formats of the .svn folder.

<?php

$contents = file_get_contents(dirname(__FILE__) . '/.svn/entries');
preg_match("/dir\n(.+)/", $contents, $matches);
$svnRevision = $matches[1];

print "This Directory's SVN Revsion = $svnRevision\n";
Lance Rushing
+1  A: 

Following Lance, here's a command line one-liner.

svn log -q --limit 1  http://svnbox/path/to/repo/trunk \
| php -r 'fgets(STDIN); preg_match("/(\d+)/", fgets(STDIN), $m); echo $m[0];'
Ewan Todd
+3  A: 

Subversion includes the svnversion tool for exactly this purpose. A working copy may actually have local modifications, or may consist of a mix of revisions. svnversion knows how to handle that; see the examples in the linked documentation.

You can invoke svnversion from python like this:

import subprocess

def svnversion():
    p = subprocess.Popen("svnversion", shell=True, \
       stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (stdout, stderr) = p.communicate()
    return stdout
Wim Coenen