Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically get SVN revision number?

Tags:

python

php

svn

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.

like image 463
mpen Avatar asked Sep 30 '09 18:09

mpen


4 Answers

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
like image 162
Wim Coenen Avatar answered Nov 04 '22 04:11

Wim Coenen


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

like image 39
brettkelly Avatar answered Nov 04 '22 04:11

brettkelly


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";
like image 36
Lance Rushing Avatar answered Nov 04 '22 03:11

Lance Rushing


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.

like image 40
Pascal MARTIN Avatar answered Nov 04 '22 03:11

Pascal MARTIN