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.

msbuildgithash's People

Contributors

brent-zeiss avatar drdamour avatar jasontaylor137 avatar koalabear84 avatar lordmike avatar markpflug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

msbuildgithash's Issues

[Minor] Readme

Under: Git hash format

(first 8 hex characters)

should be

(first 7 hex characters)

Commit not updated on unrelated changes

This is a rehash (he!) of #5 When building again after moving to a revision with changes only on files outside the project, msbuild thinks the project is up-to-date and fails to rebuild it.

MSBuildGitHashSuppressDirtyWarning not working in 2.0.0

Hi there,

A great library, I noticed in v2.0.0 that <MSBuildGitHashSuppressDirtyWarning>true</MSBuildGitHashSuppressDirtyWarning> no longer works and the message gets emitted

As a sidenote I noticed when using Resharper I tend to get messages on the bottom stating sync solution I think it's every time a file might be getting changed while developing. Not sure if it's related, but if it is, is there is a way to prevent MSBuildGitHash from running locally?

No informational version with latest .net core version

MSBuildGitHash 2.0.1 doesn't seem to generate an informational version with .netcore3.1

Steps to reproduce:

  • Create a new .netcoreapp3.1 project in VS2019
  • Add MSBuildGitHash-2.0.1 nuget
  • Build

(Used .net core 3.1.6)

=> The informational version is still 1.0.0. (the default) but the AssemblyMetadata GitHash is included correctly.

AssemblyMetadataAttribute GitHash not in file properties

Hi there,

When looking at the File properties I see that the GitHash property is missing. Only product version contains the hash
image

Using v2.0.1, I was able to access this through "AssemblyMetadataAttribute" with name GitHash
Not sure if something has changed since?

Multiple commands / outputs

I would like to know if it's possible to combine the output of multiple commands.

git branch --show-current
git rev-list --all --count
git describe --long --always --dirty

Which will create something like:
develop-9657-8f52fbc74-dirty

Change request: A way to conditionally disable MSBuildGitHash

I want to disable MSBuildGitHash in Debug config because it increases compilation time, but useless in debug for me.

Also it seems that project may rebuild because of MSBuildGitHash even if nothing changed in the project.
For example when I edit tests main project also recompiled even if it wasn't changed.

Is MSBuildGitHash not working without network connection?

On 2 of my machines MSBuildGitHash is working like a charm. However, I do have one machine which is not connected to the internet (and will not be connected). On this machine my project starts throwing many compilation errors (e.g. Type Exception not known, ...). Once I uninstall MSBuildGitHash everything is working like normal.

I copied my whole project including packages folder which contains MSBuildGitHash from a machine (with network connection) where everything works smoothly onto the machine which shows these wired errors.

Any ideas or do you need additional information?

MSBuildGitHashCommand causes build to fail if there is no .git folder

Is it possible to ignore a missing .git folder and include a dummy value?

Sometimes we download old commits of our software to quickly test items out and the build will obviously fail due to there being no git repo. This is not immediately obvious to some of our team members.

Or alternatively just make it a warning?

Thanks

The MSBuildGitHash did not work

Hi,

  • I have install the nuget package of this project on https://github.com/chucklu/Utility.
    After I build the project, the git hash is not embedded in version of *.dll.

  • I also tried to run the unit test demo, only the demo for LegacyProjectTest works, SdkProjectTest demo failed with restore error. And the demo project is not a normal one, such as a project have AssemblyInfo.cs .

  • I also found you have used git bash in https://github.com/MarkPflug/Elemental.SysInfoTool, I clone this project and build it. When I verify the version, nothing related to git hash.

Special character sequences in git commands

This is for other people's future reference. I was using the MSBuildGitHash Nuget package v2.0.2.

While trying to run some custom git commands to capture additional information, I ran into problems with character sequences that needed to be escaped.

  • % needs to be escaped as %%
  • ae needs to be escaped as a%e
  • cd needs to be escaped as c%d

These are the git commands I was trying to run:

  • git show --quiet --format=%ae
  • git show --quiet --format=%an
  • git show --quiet --format=%cd --date=iso-strict
  • git show --quiet --format=%B
  • git show --quiet --format=%H

This is the corresponding ItemGroup with correctly escaped character sequences:

  <ItemGroup>
    <AssemblyMetadataCommand Include="GitAuthorEmail">
      <Command>git show --quiet --format=%%a%e</Command>
    </AssemblyMetadataCommand>
    <AssemblyMetadataCommand Include="GitAuthorName">
      <Command>git show --quiet --format=%%an</Command>
    </AssemblyMetadataCommand>
    <AssemblyMetadataCommand Include="GitCommitDate">
      <Command>git show --quiet --format=%%c%d --date=iso-strict</Command>
    </AssemblyMetadataCommand>
    <AssemblyMetadataCommand Include="GitCommitMessage">
      <Command>git show --quiet --format=%%B</Command>
    </AssemblyMetadataCommand>
    <AssemblyMetadataCommand Include="GitFullHash">
      <Command>git show --quiet --format=%%H</Command>
    </AssemblyMetadataCommand>
  </ItemGroup>

There may be other character sequences that require escaping which I am not yet aware of.

Update issue 1.0.2 -> 2.0.1

The following message
fatal: No names found, cannot describe anything.
is added to the output when you build, it does not stop the build itself and the version number seems correct, I think this might be looking for a tag and failing if there are none.

the issue goes away if you restore the old version.

Deativate Output / Redirect stdout

Is there a way to deativate the standard output from git describe?

I tried to pipe the git describe output to /dev/null but it doesn't seem to work.

Invalid validation for the git version

I failed to build my solution with error:

C:\repo\packages\MSBuildGitHash.0.2.0\build\MSBuildGitHash.targets(29,5): error : MSBuildGitHash hash value starting with 'Roll_1.0-232-g474e3a4-dirty' was invalid.

Where "Roll_1.0" is a label used some time ago in history.
It seems that MSBuildGitHash doesn't work with labels.

It is said that I can somehow customize the validation but there is no any wiki how to do it,

Doesn't provide git hash :)

Thanks for making this cool package. For those of us that aren't intimate with MSBuild, this is a real time saver.

Given the name of this package, I was somewhat surprised to find that the git hash isn't what is provided. For that you would want to do something like git rev-parse HEAD in stead of git describe.

It would probably improve the utility of this package if it were possible to override the specific git command used to extract version information. As it stands, I had to include your targets directly in my .csproj and modify the git command there in order to actually get the git hash.

Thoughts on Adding Commit Hash Always and a Data Class

hi, we're currently using this, but a couple of use cases came up and wanted to see if you think these would fit in your vision or if it's better for us to fork.

  1. We have a need to ALWAYS have BOTH the commit hash & the output of git describe --tag (which generally is a tag). Are you open to adding a 2nd overridable command that ends up with metadata name GitDescribe ?
  2. We need to read this data across all .net apps, so want to include a simple util class that does the reflection code in it's constructor (or some factory like pattern) to expose the meta values created by this build process (namely GitHash and GitDescribe). I know this package has no classes today, so this would be a change.

if these do align, we can get a PR going. let me know your thoughts.

Commit not updated if you build from dirty, then commit, then build again

If I make some changes, then build, then commit and then build again without making additional changes, the Git hash will not be updated.

This is because without source files being touched the MSBuild Targets will not be executed at all and the project will show "Everything up-to-date".

The best solution would be to check if the HEAD has changed and regenerate the AssemblyInfo.cs only if the commit has changed, but do nothing if the commit wasn't changed.

A slightly worse solution would be to always regenerate but this would break the "Everything up-to-date" - the project will always be built even if nothing was changed including the commit.

Default hash fails with tags

by default

git describe --long --always --dirty

Can use the tags leading to errors
Example:
MSBuildGitHash hash value starting with 'docker-version-dirty' was invalid.
that is the shortend value of
docker-version-0-g662b733-dirty

this fixs that issue

git describe --match=NeVeRmAtCh --always --abbrev=7 --dirty

<PropertyGroup>
  <MSBuildGitHashCommand>git describe --match=NeVeRmAtCh --always --abbrev=7 --dirty</MSBuildGitHashCommand>
</PropertyGroup>

Adding an annotated tag causes build error

I managed to reproduce the problem in a new project by adding a one-letter annotated tag:
git tag -a B

I got the error:
MSBuildGitHash hash value starting with 'B-0-g91456ff' was invalid.

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.