Includes the Git repo hash in your compiled .NET assemblies
Includes the Git repo hash in your compiled .NET assemblies
Includes the Git repository hash in your compiled .NET assemblies. This allows you to easily correlate an assembly to exact version of the code that produced it.
This project is available as a nuget package: https://www.nuget.org/packages/MSBuildGitHash.
By default, including the nuget package (MSBuildGitHash) will automatically add the git repository hash to your assembly as a System.Reflection.AssemblyMetadataAttribute with they key "GitHash". As of 0.4.0, it will include the git repository URL as well. This value is taken from the RepositoryUrl MSBuild property which is also used by nuget. This is only used if the RepositoryType is git. The repository URL will be attached with the key "GitRepository".
Basic validation is performed on the generated hash version to ensure that a git command error doesn't result in a bad value being attached. If the validation causes problems for some reason, it can be disabled by defining the False in your project.
By default, the package will include the output of the command git describe --long --always --dirty. This produces a truncation (first 7 hex characters) of the full repository hash. You can customize the command that is executed by defining the MSBuildGitHashCommand property in your .csproj file. For example, if you want to include the full hash, you can add the following:
git rev-parse HEAD
Some IDEs and also DevOps platforms show first 8 hex characters. To change the default length from 7 hex characters to 8 hex characters you can add the following:
git describe --long --always --dirty --exclude=* --abbrev=8
By default, the git hash is appended to the Informational Version attribute value.
Alternatively, the git hash can replace the Informational Version attribute value by setting MSBuildGitHashReplaceInfoVersion to True in your .csproj file:
True
If there are additional commands and output that are needed to be included in the meta data, you can do so by adding them using AssemblyEmbed. Examples:
your command
optional regex to validate output
git describe
git describe --tag --abbrev=0
using System.Reflection;
string gitHash = Assembly
.GetEntryAssembly()
.GetCustomAttributes()
.FirstOrDefault(attr => attr.Key == "GitHash")?.Value;2.0.2
2.0.1
MSBuildGitHashSuppressDirtyWarning to suppress warnings that was lost during the 2.0 refactor.2.0.0
1.0.2
1.0.1
1.0.0
false. Will emit a warning instead of error in such case.
Can be specified on MSBuild command line as /p:MSBuildGitHashValidateSuccess=false or by setting an environment variable of the same name/value.0.5.3
True.0.5.2
MSBuildGitHashReplaceInfoVersion. Thanks @Br3nt0.5.1
0.5.0
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0
No open issues yet, or sync has not completed.