DEV Community

Jim Borden
Jim Borden

Posted on

Git commit info in your build

For quite a long time I've been generating git commit info for use inside my assemblies at work but I've been doing it in a roundabout fragile way. I found a library that includes an MSBuild task to do it for me called GitInfo. I was skeptical at first but it was literally the easiest thing I could possibly do.

All you need to do to use it is add the Nuget package. Behind the scenes what it does is generate a new target for MSBuild to run which adds another autogenerated file based on a template. It's nothing too fancy, but it takes care of a lot of annoying things for you like trying to locate the git executable and running various git commands to extract useful information. Auto generated files are like the ones that get automatically generated when you have XAML files in your project (*.g.cs). They wind up in your "output" directory and don't interfere with your git staging area. But because it is compiled source, all you need to do is call into a static class to get all the juicy git info. Personally, I use ThisAssembly.Git.Commit which is the short commit hash. However, there is a whole lot more to play with.

Check it out!

Discussion (0)