Search code examples
c#gitgitversionassemblyinfopre-build-event

How to add commit hash to details of an executable file using GitVersion


In a previous question, I asked how to add a commit hash to an executable file, built in Visual Studio.

I'm almost there: I have decided to use GitVersion and this tool indeed modifies the AssemblyInfo.cs files of my Visual Studio project, causing some interesting information being added to the "Details" tab of my executable file.
I'm quite confident that, by filling in the GitVersion commands in my project's Build Events (Pre-build event command line), I might be able to automate this.

Now there just are some small things to take care of, as you can see here:

GitVersion command and result:

Command Prompt> dotnet-gitversion /updateassemblyinfo

{
  "Major": 0,
  "Minor": 1,
  "Patch": 0,
  ...
  "InformationalVersion": "0.1.0-develop-own.1+101.Branch.develop-own.Sha.99560ad4873ab9e04bb8f262aafb5b3ee2fb6c1e",
  "BranchName": "develop-own",
  ...
  "Sha": "99560ad4873ab9e04bb8f262aafb5b3ee2fb6c1e",
  "ShortSha": "99560ad",
  ...
  "CommitDate": "2022-04-25"
}

Previous version of AssemblyInfo.cs (before running GitVersion):

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Current version of AssemblyInfo.cs (after running GitVersion):

// [assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0-develop-own.1+101.Branch.develop-own.Sha.99560ad4873ab9e04bb8f262aafb5b3ee2fb6c1e")]

You can see how the "Product version" in the details tab of the compiled file gets filled in with the AssemblyInformationalVersion value, while I want this to be a combination of other information, like AssemblyVersion, combined with Sha or even with ShortSha:

enter image description here

Let's face it: I can launch GitVersion but I have no clue how to configure its results and GitVersion /? seems not to be very helpful.
Does anybody know how to do this?

Edit after some more insight:
In the meantime I've understood how to get specific information:

dotnet-gitversion /showvariable "ShortSha"
28c8531

But the following question pops up: when looking at the first items (major, minor and patch) the documentation mentions:

{
  "Major": 3,
  "Minor": 22,
  "Patch": 11,
  ...

Where is GitVersion getting this information from?
(Just as a stupid test, I created a new branch test/4.0.0.0, but this did not alter the Major value in the GitVersion result)


Solution

  • Where is GitVersion getting this information from?

    The short answer is, it depends :-)

    GitVersion uses the history of the git repository, using a combination of commit messages, tags, branch names, configuration file, in order to assert the current version number.

    You can see this documented here:

    https://gitversion.net/docs/reference/version-sources

    On top of this, depending on what branching strategy you are using, and what configuration you have set up, GitVersion will assert the semantic version number for your repository.

    If you run the command:

    dotnet-gitversion /diag
    

    You should be able to see in the output how GitVersion asserted the base version for the repository, and how it then calculated the remaining portions of the version number.