Greg McGuffey
Greg McGuffey

Reputation: 3316

Versioning strategy for .NET apps on Git

My company just started using git and I'm struggling with versioning. We are using semantic versioning (major.minor.patch), which we set based on changes made to the product. However, I'd like to use the fourth part to indicate the git commit. I've read articles on how to do this, especially one using the post-merge hook, but the more I got into it, the more confused I became, not on the technical part, but on the conceptual part.

I really like the idea of being able to use the git commit hash in the version, as it makes it very clear what the software is. However, I can't know this until after the commit, at which point, changing the version would require another commit and the hash would be incorrect. So, if I get a version number, go to git, I'd actually be getting the commit before the one in use. What I'd want is to be able to go to git, grab the tag and have it built exactly as it is in use.

I suspect that the answer lies in the build process, in that that process would query git, get the hash, update the AssemblyInfo.cs and then build the product.

As I'm very new to all this, I'm wondering if:

Thanks!

Upvotes: 1

Views: 1035

Answers (2)

Greg McGuffey
Greg McGuffey

Reputation: 3316

For those curious, this is the solution I ended up using.

After confirming that I did understand how the whole commit/hash thing worked, I started looking more into the build process. It turns out that it was pretty easy. This article got me going in the right direction. Here's what I did:

  • Added MSBuildTasks to project using NuGet
  • Setup the .csproj file to use two of its tasks
    • GitDescribe
    • AssemblyInfo
  • Removed AssemblyInfo.cs from repository
  • Version set in project file now (major, minor and patch)

Now, every time I build, it uses the version set in the project file and it uses the commits since the last tag for AssemblyFileVersion and puts the hash into the AssemblyInformationalVersion by rebuilding the entire AssemblyInfo.cs file.

Three caveats:

  • You must have at least one tag or it errors (git describe errors)
  • This won't build unless it is under git. I.e. if you do an archive and try to build it, it bombs.
  • Only works on annotated tags.

I can live with that for now.

Thanks for the help!

Upvotes: 2

Philippe
Philippe

Reputation: 31137

The hash (for git, a sha1) seal all the content of the commit. If only one thing change in the commit, the content of a file or a metadata of the commit (date of commit, commit message, author,...), the hash will be different. In this case, you understand that you can't get the commit hash, modify the content of a file and amend the commit and expect to have the same hash for the commit! So, if you want to version your assemblies, you must update the AssemblyInfo.cs file with the commit hash in your build process before compiling the solution.

Upvotes: 0

Related Questions