1

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.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • what file system and OS are you targeting? – Jan Wilamowski Sep 29 '21 at 07:15
  • @JanWilamowski: I'm working with a regular Windows-10 computer (I added "Windows" as a tag to my question). – Dominique Sep 29 '21 at 07:24
  • Maybe this answer can help you: https://stackoverflow.com/a/35189989/10441671 – Peter Krebs Sep 29 '21 at 07:42
  • For an already compiled program that seems difficult. That's a developer thing not a user thing. I doubt you can change that without some use of the command line. Don't you compile the program yourself (or at your company?). You can change the build process for it. – Peter Krebs Sep 29 '21 at 07:48

2 Answers2

1

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")]

One way to inject the commit hash can be :

  • use a targettable string in your code :
[assembly:AssemblyInformationalVersionAttribute("#GIT_COMMIT_PLACEHOLDER#")]
  • in your build script, search and replace that string with your commit hash before building

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.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • This sounds like it assumes .NET-built exe files. OP didn't seem to specify that, what about "traditional" native EXE files? – Joachim Sauer Sep 29 '21 at 07:56
  • You are correct, I did assume a .NET build :) [This other question](https://stackoverflow.com/questions/23204774/how-do-you-set-the-version-of-a-c-cli-project-in-visual-studio) points at ways to handle version information for C++ projects, and hint that the version information should be mentioned either in code (with a builder aware of these infomation) or in a resource file. – LeGEC Sep 29 '21 at 08:02
  • Weird: I have seen this in a non-.Net environment, but currently I'm working in a .Net environment (I had no idea this was relevant). – Dominique Sep 29 '21 at 08:10
  • You are giving `gitversion` as an example, but I don't have that tool: I have `GIT bash` and `GIT GUI`. I have `GitExtentions` but that's only on my personal PC and I would like to find a way, using the tools I currently have. Do you think this is possible? – Dominique Dec 14 '21 at 13:19
1

This is really a Windows build question (and may well be build-environment specific). The method for doing this on macOS in Xcode, for instance, would be quite different.

On the Git side (since you've tagged this with ), the one thing to say about it is that rather than sticking in the output of git rev-parse --short HEAD, it's probably wiser to stick in the output of git describe, perhaps with --tags and/or --always and/or the --dirty flag:

annotation=$(git describe --always --dirty)

for instance. This way a release build will be tagged with the release tag, assuming you set up your release tags well. See the git describe documentation for details.

torek
  • 448,244
  • 59
  • 642
  • 775
  • This might actually work: I just ran `git describe --always` and it gave exactly the result I was looking for, but now I would like to embed this in the assembly of my application. Currently, I have there `[assembly: AssemblyVersion("1.0.0.0")]`, and I would like to have something like: `[assembly: AssemblyVersion("1.0.0.0-SHA")]` where `SHA` is the result of `git describe --always`. In order to do this, I need to launch that command and get the result in a oneliner, something like: `[assembly: AssemblyVersion("1.0.0.0" + "-" + Shell.Commandline_Launcher("git describe --always"))]`, but how? – Dominique Dec 21 '21 at 09:49
  • For your build system, I have no idea. – torek Dec 21 '21 at 10:34
  • I've edited [my other question](https://stackoverflow.com/questions/70351894/is-there-a-c-sharp-commandline-command-one-liner-which-can-help-me-get-the-git-c) about this subject. – Dominique Dec 21 '21 at 10:42