Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SVN revision number programmatically (.NET)

Tags:

c#

.net

svn

ankhsvn

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.

like image 867
Frederik T Avatar asked Mar 06 '14 14:03

Frederik T


People also ask

How do I find my svn revision number?

"svn info --show-item revision" will give the current revision to which the current directory is updated.

How do I find my svn revision history?

To find information about the history of a file or directory, use the svn log command. svn log will provide you with a record of who made changes to a file or directory, at what revision it changed, the time and date of that revision, and, if it was provided, the log message that accompanied the commit.

What is revision number in svn?

As you saw in the section called “Revisions”, revision numbers in Subversion are pretty straightforward—integers that keep getting larger as you commit more changes to your versioned data.

How do I compare to previous version in svn?

If you want to see the difference between the last committed revision and your working copy, assuming that the working copy hasn't been modified, just right click on the file. Then select TortoiseSVN → Diff with previous version.


2 Answers

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 : "?.?.?.?";
    }
like image 53
Alexei - check Codidact Avatar answered Sep 18 '22 09:09

Alexei - check Codidact


The revision number can't be obtained "directly from the code" (I assume you mean your code) unless you put it there yourself.

There are multiple ways you can do it (and the list below is not exhaustive), depending on what your build process looks like and how much work you want to do.

  1. Use the $Revision$ keyword & have the Subversion client CCNet is calling automatically expand it.
  2. Use SubWCRev.exe with some custom code (script) in your build process to insert the value into your file(s).
  3. Use SharpSVN with some custom code (script or EXE) in your build process to insert the value into your file(s).

Your question is not really specific enough to get into more detail than that. You haven't described what's "troublesome" about the unspecified solutions you've found, and "easiest" is a very subjective measure.

like image 27
alroc Avatar answered Sep 20 '22 09:09

alroc