Recording the git commit hash in your assembly with TeamCity

If you’re using SVN, you can very easily configure TeamCity to insert the commit revision number into the assembly by using the built-in AssemblyInfo Patcher with a pattern like 1.0.0.%build.vcs.number%. This works because SVN and .NET both use integers for their version numbers.

Git, on the other hand, uses a 40-character SHA-1 string as a unique ID for commits. As you can see, this will not fit in a .NET assembly version:

Example git commit: c9d183c8570143142ca61c555360e7f0732efc09
Example git commit (short version): c9d183c857
Example .NET assembly version: 1.2.3.4

One place it will fit is the AssemblyInformationalVersion attribute, which accepts any string (thanks Joshka for the tip!). Here’s a quick MSBuild script to do it (using the AssemblyInfo task from MSBuild Community Tasks).

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildCommunityTasksPath>packages\MSBuildTasks.1.3.0.528\lib\net20</MSBuildCommunityTasksPath>
    
    <!-- Use the short version of the git hash. -->
    <GitCommitShortHash>$(build_vcs_number.Substring(0, 7))</GitCommitShortHash>
  </PropertyGroup>

  <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets"/>

  <Target Name="GenerateAssemblyInfo">
    <AssemblyInfo CodeLanguage="CS"
      OutputFile="MyApp\Properties\AssemblyInfo.cs"
      AssemblyInformationalVersion="$(GitCommitShortHash)" />
  </Target>
</Project>

Then in C# you can use something like the following to display it on startup or your About page:

public void Main()
{
    var gitCommit = Assembly.GetExecutingAssembly()
        .GetCustomAttributes(inherit: false)
        .OfType<AssemblyInformationalVersionAttribute>()
        .Single().Configuration;

    Console.WriteLine("My App starting up (git commit {0})", gitCommit);

    ...
}

(Note Josh Flanagan has an alternative approach where he finds a way to convert the git commit into an int so you can use it in the AssemblyVersion).

April 30, 2012

2 Comments

Joshka on April 30, 2012 at 11:25 am.

Richard,
The AssemblyInformationalVersion attribute is another alternative as it can be any string. E.g. “1.2.3.4 (git c9d183c857)”. This has an added benefit of being automatically included in your app’s app data path and registry key.
See: MSDN Docs for AssemblyInformationalVersion

Richard on April 30, 2012 at 1:05 pm.

@Joshka: thanks, I didn’t know there even was such an attribute. I have updated the post to use that instead.

Leave a Reply