Getting SVN revision number programmatically (.NET)

Frederik T picture Frederik T · Mar 6, 2014 · Viewed 11.4k times · Source

I know this question has popped up several times, like here: Getting the subversion repository number into code .

But either it's too complicated for me to understand, or too elaborate for my project. This is my setup in short: Several developers with visual studio 2012 and ankhsvn for connection to our subversion repository, developing a .net web application. A development server synching the latest files from the repository with cruisecontrol.net. A test server, where a specific revision is copied by hand for QA.

The request is to have the latest revision displayed on the site so testers know what version they are looking at.

What is the easiest way? Is there some method of getting it directly from the code (best scenario)? Can cruisecontrol.net do it for me (but that leaves the test server out which is deployed manually). I have searched and searched and many solutions seems so troublesome, i just want to know if there is an easier way before i start building an elaborate contraption.

Answer

Alexei - check Codidact picture Alexei - check Codidact · Apr 6, 2017

Second point from the accepted answer is quite simple and provides desired result: output revision number somewhere within the application UI:

Use SubWCRev.exe with some custom code (script) in your build process to insert the value into your file(s).

1) Think about what Assembly attribute to use and comment/remove its line in one of your projects' AssemblyInfo file (I have chosen a "common" project and AssemblyFileVersion attribute). Certain limitations may apply to various assembly attributes based on msbuild version as indicated here.

2) Create a template for use to generate the whole version. E.g.: SolutionInfo.cs.tmpl

using System.Reflection;

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision 
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyFileVersion("1.0.0.$WCREV$")] 
// [assembly: AssemblyVersion("1.0.0.$WCREV$")]

3) Create actual file to be used to store generated version. E.g.: SolutionInfo.cs. This can be empty, as it will be automatically generated on build. After build, it should like this:

using System.Reflection;

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision 
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyFileVersion("1.0.0.16788")] 
// [assembly: AssemblyVersion("1.0.0.16788")]

Make sure to ignore/exclude this file from source control.

4) Create prebuild event to fetch revision number and create the file from its template:

"C:\Program Files\TortoiseSVN\bin\subwcrev" "$(ProjectDir).\.." "$(ProjectDir)\Properties\SolutionInfo.cs.tmpl" "$(ProjectDir)\Properties\SolutionInfo.cs"

Note: This build event assumes subwcrev is installed by TortoiseSVN in its default path. Build server must have the same configuration, so that build does not crash. If subwcrev is registered in PATH, the full path is no longer necessary.

5) Version fetch

public static class VersionExtensions
{
    public static string GetFileVersion(this Assembly assembly)
    {
        var value = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                            .OfType<AssemblyFileVersionAttribute>()
                            .SingleOrDefault();

        return value != null ? value.Version : "?.?.?.?";
    }

    public static string GetCommonFileVersion()
    {
        var assembly = Assembly.GetAssembly(typeof(Common.Web.VersionExtensions));

        var value = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                            .OfType<AssemblyFileVersionAttribute>()
                            .SingleOrDefault();

        return value != null ? value.Version : "?.?.?.?";
    }