6
votes

I have a rails site. I'd like, on mongrel restart, to write the current svn version into public/version.txt, so that i can then put this into a comment in the page header.

The problem is getting the current local version of svn - i'm a little confused.

If, for example, i do svn update on a file which hasn't been updated in a while i get "At revision 4571.". However, if i do svn info, i get

Path: .
URL: http://my.url/trunk
Repository Root: http://my.url/lesson_planner
Repository UUID: #########
Revision: 4570
Node Kind: directory
Schedule: normal
Last Changed Author: max
Last Changed Rev: 4570
Last Changed Date: 2009-11-30 17:14:52 +0000 (Mon, 30 Nov 2009)

Note this says revision 4570, 1 lower than the previous command.

Can anyone set me straight and show me how to simply get the current version number?

thanks, max

5
I've always had a dream in which my application's build number would align with the SVN revision. I'm curious to see how this works out.Paul Lammertsma

5 Answers

7
votes

Subversion comes with a command for doing exactly this: SVNVERSION.EXE.

usage: svnversion [OPTIONS] [WC_PATH [TRAIL_URL]]

Produce a compact 'version number' for the working copy path WC_PATH. TRAIL_URL is the trailing portion of the URL used to determine if WC_PATH itself is switched (detection of switches within WC_PATH does not rely on TRAIL_URL). The version number is written to standard output. For example:

$ svnversion . /repos/svn/trunk 
4168

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

If invoked on a directory that is not a working copy, an exported directory say, the program will output 'exported'.

If invoked without arguments WC_PATH will be the current directory.

Valid options: -n [--no-newline] : do not output the trailing newline -c [--committed] : last changed rather than current revisions -h [--help] : display this help --version : show version information

4
votes

I use the following shell script snippet to create a header file svnversion.h which defines a few constant character strings I use in compiled code. You should be able to something very similar:

#!/bin/sh -e

svnversion() {
    svnrevision=`LC_ALL=C svn info | awk '/^Revision:/ {print $2}'`
    svndate=`LC_ALL=C svn info | awk '/^Last Changed Date:/ {print $4,$5}'`

    now=`date`

    cat <<EOF > svnversion.h

// Do not edit!  This file was autogenerated
//      by $0
//      on $now
//
// svnrevision and svndate are as reported by svn at that point in time,
// compiledate and compiletime are being filled gcc at compilation

#include <stdlib.h>

static const char* svnrevision = "$svnrevision";
static const char* svndate = "$svndate";
static const char* compiletime = __TIME__;
static const char* compiledate = __DATE__;

EOF
}

test -f svnversion.h || svnversion

This assumes that you would remove the created header file to trigger the build of a fresh one.

2
votes

If you just want to print latest revision of the repository, you can use something like this:

svn info <repository_url> -rHEAD | grep '^Revision: ' | awk '{print $2}'

You can use capistrano for deployment, it creates REVISION file, which you can copy to public/version.txt

1
votes

It seems that you are running svn info on the directory, but svn update on a specific file. If you update the directory to revision 4571, svn info should print:

Path: .

URL: http://my.url/trunk

Repository Root: http://my.url/lesson%5Fplanner

Repository UUID: #########

Revision: 4571

[...]

Last Changed Rev: 4571

Note that the "last changed revision" does not necessarily align with the latest revision of the repository.

0
votes

Thanks to everyone who suggested capistrano and svninfo.

We do actually use capistrano, and it does indeed make this REVISION file, which i guess i saw before but didn't pay attention to. As it happens, though, this isn't quite what i need because it only gets updated on deploy, whereas sometimes we might sneakily update a couple of files then restart, rather than doing a full deploy.

I ended up doing my own file using svninfo, grep and awk as many people have suggested here, and putting it in public. This is created on mongrel start, which is part of the deploy process and the restart process so gets done both times.

thanks all!