mpen
mpen

Reputation: 282805

How to programmatically get SVN revision number?

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.

Upvotes: 7

Views: 12342

Answers (9)

Wim Coenen
Wim Coenen

Reputation: 66703

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

Upvotes: 13

Jason Antman
Jason Antman

Reputation: 2828

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

Upvotes: 1

Ewan Todd
Ewan Todd

Reputation: 7312

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

Upvotes: 3

Lance Rushing
Lance Rushing

Reputation: 7640

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

Upvotes: 3

Rich Seller
Rich Seller

Reputation: 84028

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 )

Upvotes: 2

user7675
user7675

Reputation:

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

Upvotes: 2

PilotBob
PilotBob

Reputation: 3117

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.

Upvotes: 0

brettkelly
brettkelly

Reputation: 28205

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

Upvotes: 4

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

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.

Upvotes: 3

Related Questions