Giter Site home page Giter Site logo

msbuildgithash's Introduction

MSBuildGitHash

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.

Nuget

This project is available as a nuget package: https://www.nuget.org/packages/MSBuildGitHash.

Usage

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 <MSBuildGitHashValidate>False</MSBuildGitHashValidate> in your project.

Customization

Git hash format

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:

<PropertyGroup>
  <MSBuildGitHashCommand>git rev-parse HEAD</MSBuildGitHashCommand>
</PropertyGroup>

Informational Version (aka Product Version) format

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:

<PropertyGroup>
  <MSBuildGitHashReplaceInfoVersion>True</MSBuildGitHashReplaceInfoVersion>
</PropertyGroup>

Command Output to Assembly Metadata

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:

<AssemblyMetadataCommand Include="KeyNameForOutput">
  <Command>your command</Command>
  <ValidationRegex>optional regex to validate output</ValidationRegex>
</AssemblyMetadataCommand>
<ItemGroup>
  <AssemblyMetadataCommand Include="GitDescription">
    <Command>git describe</Command>
  </AssemblyMetadataCommand>
  <AssemblyMetadataCommand Include="GitTag">
    <Command>git describe --tag --abbrev=0</Command>
  </AssemblyMetadataCommand>
</ItemGroup>

Extract Assembly Metadata

using System.Reflection;

string gitHash = Assembly
	.GetEntryAssembly()
	.GetCustomAttributes<AssemblyMetadataAttribute>()
	.FirstOrDefault(attr => attr.Key == "GitHash")?.Value;

Version History

2.0.2

  • Fix issues introduced by changes from the Microsoft.NET.Sdk.

2.0.1

  • Restores the ability to use MSBuildGitHashSuppressDirtyWarning to suppress warnings that was lost during the 2.0 refactor.

2.0.0

  • Now includes the most recent git tag as metadata as well, if the repository has any tags.
  • Significant refactoring to allow more easily adding additional assembly metadata. Thanks @zhilbug for the bulk of the work on this.
  • This is a major version, because it has more potential to affect existing builds. Projects that don't include signification build customizations should continue to work without modification.

1.0.2

  • MSBuildGitHash is now properly marked as a development dependency, and so won't show up as a transitive dependency in your package.

1.0.1

  • Fixes an issue where git tags would appear in hash version, causing validation failure.

1.0.0

  • Adds the ability to disable error when running outside a git repository via <MSBuildGitHashValidateSuccess>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

  • Emits a warning when the repository is dirty. Can be suppressed with <MSBuildGitHashSuppressDirtyWarning>True.

0.5.2

  • Adds ability to replace assembly InformationalVersion via MSBuildGitHashReplaceInfoVersion. Thanks @Br3nt

0.5.1

  • Fix for #15, to allow supression of AssemblyInformationalVersionAttribute.

0.5.0

  • Removed framework dependency from nupkg.

0.4.1

  • No functional change.
  • Minor build script cleanup.

0.4.0

  • Adds an AssemblyInformationalVersion attribute containing the git version. This value shows up in the standard Windows properties dialog.
  • No longer uses temp files in "obj" folder to operate.
  • Replace remote repository to use standard "RepositoryUrl" used by nuget.
  • Working unit tests.

0.3.0

  • Adds option to include git remote repository url as well.

0.2.0

  • Adds optional validation that the hash value looks correct.
  • Improved build error message when git commands fail.

0.1.0

  • Base functionality.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.