views:

3133

answers:

7

I would like to start tagging my deployed binaries with the latest SVN revision number.

However, because SVN is file-based and not directory/project-based, I need to scan through all the directory's and subdirectory's files in order to determine the highest revision number.

Using svn info on the root doesn't work (it just reports the version of that directory, not files in subdirectories):

I was wondering if there is a shortcut using the svn command to do this. Otherwise, can anyone suggest a simple script that is network-efficient (I would prefer if it didn't hit the remote server at all)?

I also understand that one alternative approach is to keep a version file with the svn:keywords. This works (I've used it on other projects), but I get tired of dealing with making sure the file is dirty and dealing with the inevitable merge conflicts.

Answer I see my problem lied with not doing a proper svn up before calling svn info in the root directory:

$ svn info
Path: .
...
Last Changed Author: fak
Last Changed Rev: 713
Last Changed Date: 2008-08-29 00:40:53 +0300 (Fri, 29 Aug 2008)

$ svn up
At revision 721.

$ svn info
Path: .
...
Revision: 721
Last Changed Author: reuben
Last Changed Rev: 721
Last Changed Date: 2008-08-31 22:55:22 +0300 (Sun, 31 Aug 2008)
+8  A: 

One way. When you check out the code, look at the last line of svn output:

$ svn up
...stuff...
Updated to revision 66593.

A more direct way:

$ svn info
Path: .
URL: https://svn.example.com/svn/myproject/trunk
Repository Root: https://svn.example.com/svn/
Repository UUID: d2a7a951-c712-0410-832a-9abccabd3052
Revision: 66593
Node Kind: directory
Schedule: normal
Last Changed Author: bnguyen
Last Changed Rev: 66591
Last Changed Date: 2008-09-11 18:25:27 +1000 (Thu, 11 Sep 2008)
Charles Miller
I never though of using svn up. However, I am not sure I'm comfortable putting that in a deployment script. Also, svn info doesn't work because it just reports on that directory, not any files in subdirectories. Thanks for the idea though!
Frank Krueger
Disregard the comment about svn info, I misunderstood the interaction of svn up and svn info.
Frank Krueger
+1  A: 

"svn info" will show you the working copy's revision number (see the "Revision" line in the output from "svn info"). Your build system probably allows you to place the relevant part of "svn info"'s output somewhere where it will be reflected in your application. For example, you may specify that when building, a temporary (un-versioned) file should be created, containing output from "svn info"; and you then include this file when compiling.

Troels Arvin
Ahh, it looks like I misunderstood the interactions of svn up and svn info. Thanks for making me test my assumptions!
Frank Krueger
+2  A: 

I don't know if you are using MSBuild(Visual Studio) to build your binaries. But if you would: there is a connection possible between Subverion and MSBuild through MSBuild Community Tasks Project

Here's part of our build script: our (C#) application gets the svn revision number included:

  <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="installationpath\of\subversion\bin">
     <Output TaskParameter="Revision" PropertyName="Revision" />
  </SvnVersion>
  <Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/>
...
    AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
     AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"

Jan

jan
I'm not, but I'm sure that info is helpful for those who are. Thanks!
Frank Krueger
+1  A: 

The answers provided by @Charles Miller and @Troels Arvin are correct - you can use the output of the svn update or svn info, but as you hint, the latter only works if the repository is up to date. Then again, I'm not sure what value any revision number is going to be to you if part of your source tree is on a different revision than another part. It really sounds to me like you should be working on a homogeneous tree. I'd suggest either updating before running info (or if you've already updated for your build, you're golden) or using svn info URL-to-source.

Blair Conrad
+1  A: 

svnversion seems to be the cleanest way to do this:

svnversion -c /path/to/your-projects-local-working-copy/. | sed -e 's/[MS]//g' -e 's/^[[:digit:]]*://'

The above command will clean out any M and S letters (indicating local modifications or switchedness) from the output, as well as the smaller revision number in case svnversion returns a range instead of just one revision number (see the docs for more info). If you don't want to filter the output, take out the pipe and the sed part of that command.

If you want to use svn info, you need to use the "recursive" (-R) argument to get the info from all of the subdirectories as well. Since the output then becomes a long list, you'll need to do some filtering to get the last changed revision number from all of those that is the highest:

svn info -R /path/to/your-projects-local-working-copy/. | awk '/^Last Changed Rev:/ {print $NF}' | sort -n | tail -n 1

What that command does is that it takes all of the lines that include the string "Last Changed Rev", then removes everything from each of those lines except the last field (i.e. the revision number), then sorts these lines numerically and removes everything but the last line, resulting in just the highest revision number. If you're running Windows, I'm sure you can do this quite easily in PowerShell as well, for example.

Just to be clear: the above approaches get you the recursive last changed revision number of just the path in the repo that your local working copy represents, for that local working copy, without hitting the server. So if someone has updated something in this path onto the repository server after your last svn update, it won't be reflected in this output.

If what you want is the last changed revision of this path on the server, you can do:

svn info /path/to/your-projects-local-working-copy/.@HEAD | awk '/^Last Changed Rev:/ {print $NF}'
hasseg
+2  A: 

Duplicate of this question. As I posted there, the svnversion command is your friend. No need to parse the output, no need to update first, just does the job.

Sam Stokes
A: 

There is a program distributed with Subversion called svnversion that does exactly what you want to do. It's how we tag our websites.

David