windowsgithashgit-commitfile-properties

How to add commit hash to details of an executable file


One of my previous employers added the GIT hash of the current commit to the "Details" tab of the file properties of the executable, once it was built.

As I found here, this command gives the current commit hash:

git rev-parse --short HEAD

But does anybody know how I can add this to the "Details" tab of the properties of a file?

Edit follow-up of this question:
This question has been written while I had no clue how to start, and there are some general answers. Afterwards, I have decided to go for the GitVersion way of working. I have some question about this too, and those are handled in this follow-up question.


Solution

  • note : I'm assuming you are working on a .NET project. There are other ways to provide similar information at build time for other projects (e.g : I found this question which mentions two ways to do it for a C++ project)

    There is a number of attributes you can set on an assembly at build time, such as the Version.

    One of these attributes is : AssemblyInformationalVersionAttribute, which can be any string.

    (see the docs.microsoft page on asembly attributes)

    You can set it from within the code of your project, for example, in a .cs file, you can add :

    [assembly:AssemblyInformationalVersionAttribute("That's my version all right")]
    

    How it appears in Windows File Explorer File Properties


    One way to inject the commit hash can be :

    [assembly:AssemblyInformationalVersionAttribute("#GIT_COMMIT_PLACEHOLDER#")]
    

    There are tools out there that wrap this together with more complete features.

    For example I have heard of Gitversion (https://gitversion.net/docs/), which integrates in Azure Devops pipelines and MSbuild tasks, and offer a swath of options to add version information to your builds from git (e.g : read the version number from tags, add commit sha, etc ...)

    See the configuration and version variables pages to have a view of what you can add to your builds.