7

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.

9 Answers 9

13

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", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (stdout, stderr) = p.communicate()
    return stdout
3
4

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

3
  • 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.
    – glomad
    Sep 30, 2009 at 22:24
  • No...not at all. Didn't mean for it to come off that way :)
    – mpen
    Oct 1, 2009 at 4:48
  • ..I meant that it sounds a lot more efficient than running an external process on every page load. I think reading a text file actually would be more efficient.
    – mpen
    Nov 6, 2012 at 6:06
3

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</url>
<repository>
<root>http://...</root>
<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.

1
  • 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.
    – mpen
    Sep 30, 2009 at 19:14
3

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";
3

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];'
2

I like the solution used by MediaWiki, reading the contents of .svn/entities. Check the first answer on this question: Getting the subversion repository number into code

2

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

I know this is pretty old, but I thought I'd offer up how I do this in PHP for my web apps:

Put this in a PHP file in your app, and set the SVN Keywords property for "Date Revision Author HeadURL Id" (keyword substitution in the file).

$SVN_rev = "\$LastChangedRevision$";
$SVN_headURL = "\$HeadURL$";

function stripSVNstuff($s)
{
    $s = substr($s, strpos($s, ":")+1);
    $s = str_replace("$", "", $s);
    return trim($s);
}

So after svn does keyword replacement on the source file, the $SVN_rev line will look like:

$SVN_rev = "\$LastChangedRevision: 6 $";

so the output of stripSVNstuff($SVN_rev) would be "6" (without quotes).

0

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.

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.