Skip to content
Git and SemVer Info from MSBuild, C# and VB
Branch: master
Clone or download
kzu If GitCommits can't be determined, warn and default to zero
We were previously leaving a potentially invalid value resulting from a
failed execution of git rev-list.
Latest commit b62e1c0 Nov 13, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
img
src If GitCommits can't be determined, warn and default to zero Nov 13, 2018
.editorconfig
.gitignore Improve this assembly overriding for xbuild Jun 16, 2015
GitInfo.txt
LICENSE Initial commit May 26, 2015
README.md
appveyor.yml Switch to 2017 image Aug 16, 2017
build.cmd
build.proj Delete superseded nuget (use ThisAssembly.Vsix instead now) Aug 8, 2016

README.md

GitInfo

A fresh and transparent approach to Git information retrieval from MSBuild and Code without using any custom tasks or compiled code and tools, obscure settings, format strings, etc.

Build status Latest version Downloads License

Goals

  • No compiled code or tools -> 100% transparency
  • Trivially added/installed via a NuGet package
  • No format strings or settings to learn
  • Single well-structured .targets file with plain MSBuild and no custom tasks
  • Optional embedding of Git info in assembly as metadata
  • Optional use of Git info to build arbitrary assembly/file version information, both in C# as well as VB.
  • Trivially modified/improved generated code by just adjusting a C# or VB template included in the NuGet package
  • Easily modified/improved by just adjusting a single .targets file
  • 100% incremental build-friendly and high-performing (all proper Inputs/Outputs in place, smart caching of Git info, etc.)

Overview

After installing via NuGet:

PM> Install-Package GitInfo

By default, if the containing project is a C# or VB project, a compile-time generated source file will contain all the git information and can be accessed from anywhere within the assembly, as constants in a ThisAssembly (partial) class and its nested Git static class:

Console.WriteLine(ThisAssembly.Git.Commit);

All generated constants also have a Summary documentation tag that shows the current value in the intellisense tooltip, making it easier to see what the different values contain:

NOTE: you may need to close and reopen the solution in order for Visual Studio to refresh intellisense and show the ThisAssembly type the first time after installing the package.

With this information at your fingertips, you can build any versioning attributes you want, with whatever information you want, without resorting to settings, format strings or anything, just plain code:

C#:

[assembly: AssemblyVersion (ThisAssembly.Git.BaseVersion.Major + "." + ThisAssembly.Git.BaseVersion.Minor + "." + ThisAssembly.Git.BaseVersion.Patch)]

[assembly: AssemblyFileVersion (ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.SemVer.Patch)]

[assembly: AssemblyInformationalVersion (
	ThisAssembly.Git.SemVer.Major + "." + 
	ThisAssembly.Git.SemVer.Minor + "." + 
	ThisAssembly.Git.Commits + "-" + 
	ThisAssembly.Git.Branch + "+" + 
	ThisAssembly.Git.Commit)]

VB:

<Assembly: AssemblyVersion(ThisAssembly.Git.BaseVersion.Major + "." + ThisAssembly.Git.BaseVersion.Minor + "." + ThisAssembly.Git.BaseVersion.Patch)>
<Assembly: AssemblyFileVersion(ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.SemVer.Patch)>
<Assembly: AssemblyInformationalVersion(
    ThisAssembly.Git.SemVer.Major + "." +
    ThisAssembly.Git.SemVer.Minor + "." +
    ThisAssembly.Git.Commits + "-" +
    ThisAssembly.Git.Branch + "+" +
    ThisAssembly.Git.Commit)>

Because this information is readily available whenever you build the project, you never depend on CI build scripts that generate versions for you, and you can always compile locally exactly the same version of an assembly that was built by a CI server.

You can read more about this project at the GitInfo announcement blog post.

You can’t perform that action at this time.