GitRevisionTool

Prints out the current Git revision info of a working directory and automatically writes it into your .NET project for the build.

Archived content: This application is currently inactive and may be outdated or no longer maintained or functional.

This application has been replaced by NetRevisionTool. NetRevisionTool is an advancement of GitRevisionTool and SvnRevisionTool in a single program with new flexible features.

GitRevisionTool is a small developer’s tool that prints out the current Git revision info of a working directory. It can automatically write that revision information into your application’s code so that it's compiled right into it. This works for .NET solutions written in C# and VB.NET using the regular Visual Studio project structure.

Why?

Every bigger-than-small application has a version number that the user can query in some form of About dialog window. If you release often and don’t want to manage structured version numbers (like the ones of GitRevisionTool itself), you might just use the Git revision identifier or commit time as version number for your product. By automating the copying of that revision ID into the application source code, you can avoid forgetting that update. Also, possible keyword replacing features of Git itself do not play very well with C#/VB.NET source code in creating a friendly version for that assembly. GitRevisionTool is optimised for this scenario.

How can I use it?

Download the application and copy it to your Visual Studio solution directory. I like to put such little helpers in a subdirectory called “_scripts\bin” (see screenshot below), along with the PowerShell build framework. You should include that file into your Git repository to keep it portable. Make sure the file is not ignored by adding a line like this to your .gitignore file:

!_scripts/bin/

Then, open your project properties and switch to the build events page (see screenshot below). Add the following pre-build command (in one line):

if not defined FASTBUILD "$(SolutionDir)\_scripts\bin\GitRevisionTool.exe" -ai "$(ProjectDir)"

Adapt the path if you used a subdirectory, as in the screenshot image. And add the following post-build command:

if not defined FASTBUILD "$(SolutionDir)\_scripts\bin\GitRevisionTool.exe" -si "$(ProjectDir)"

Important: Set the post-build event to be executed always to ensure the modified source file is always restored correctly, also when your project doesn’t build correctly.

The condition with the environment variable “FASTBUILD” allows controlling the execution of SvnRevisionTool for each project. For solutions with many (> 25) projects, you can set this environment variable (to any non-empty value) to prevent running SvnRevisionTool. This decreases the build time. When using the PowerShell build framework, this variable is used as well and SvnRevisionTool is started in multi project mode. If you do not need or want this feature and do not use the PowerShell build script, you can leave out the part if not defined FASTBUILD to ensure that SvnRevisionTool is always used.

Now open your project’s AssemblyInfo.cs or .vb file to configure your version number. That file is usually located in the “Properties” or “My Project” folder. Look for the AssemblyInformationalVersion attribute and add it if it’s not there yet. Here are a few examples:

// Basic example:
[assembly: AssemblyInformationalVersion("commit {commit} on {date} {time}")]

// Git time code and commit hash (FieldLog, on the screenshots):
[assembly: AssemblyInformationalVersion("{bmin:2014:4}.{commit:6}{!:+}")]

See the program’s output for the --help parameter for all the details. There’s more placeholders to create compact, sortable version IDs, the option to require an unmodified working directory and other useful things waiting for you.

How does it do what it does?

GitRevisionTool is called in the pre-build event with the parameter -a. This is every time you build the project, before the compiler starts off. It will find your AssemblyInfo.cs or .vb file and replace all your placeholders in the AssemblyInformationalVersion attribute to the actual revision data. Then Visual Studio includes that file in the compilation process. In the end, the -s parameter restores the previously backed up file so that you (and Git) won’t see any changes to commit.

Important: For this to work, the AssemblyInfo file must not be opened in the Visual Studio editor during build, or the things that GitRevisionTool writes into the file will be ignored for that build. So always close that file as long as you are tweaking your version string and test-building the project.

Compatibility: .NET Version 2.0 or newer Windows 10 Windows 8 Windows 7 Windows XP Mono 64 bit

I have a similar, older tool that does the same for Subversion repositories: SvnRevisionTool

Images

Image
GitRevisionTool.exe im bin-Verzeichnis
Image
Build-Ereignisse im Projekt
Image
Versionsangabe in den Dateieigenschaften

Download

GitRevisionTool.exe50 KiBProgram file, version 1.7.3, .NET 4.0 (Recommended for Windows 8/10 or all VS 2010+ projects)

GitRevisionTool_NET20.exe49.5 KiBProgram file, version 1.7.3, .NET 2.0

master.zipLatest source code directly from GitHub (GitRevisionTool and SvnRevisionTool)

There’s a public Git repository of GitRevisionTool on GitHub.

It doesn’t work?

GitRevisionTool needs Git for Windows installed on the system. It goes looking for bin\git.exe in directories it finds from its uninstaller entry or scans your Program Files directory. If it can’t find Git, it can’t help you either. (I have started some code to read the .git directory directly, as I do with .svn, but that turned out to be too complicated for a first try.) You should be good to get msysGit and just install it with default settings. See Github’s nice documentation for help on that.

GitRevisionTool can only modify your C# or VB.NET project files. It won’t do anything but print the revision information (albeit formatted the way you want) for other project types. You may need to create a CMD script to hack files on your own. Set the format string to something like SET revid={commit}, redirect the output to a temporary batch file (outside of the working directory, or that will always be unclean!), call that, and edit source files using the %revid% environment variable with sed or another tool. Be sure to backup and restore the code file.

This is a .NET application itself. It is targeted on the .NET 2.0 framework, so you should not have any problems on Windows Vista or later. If you use Visual Studio 2005 or later, you already have the .NET framework installed anyway.

Changes

2015Jan26
Version 1.7.3
  • Added separate .NET 4.0 build, recommended for Windows 8/10 or all VS 2010+ projects
  • Reorganised AssemblyInfo.cs
2014May2
Version 1.7.2
  • Fixed PATH parsing for quoted entries
2014Mar8
Version 1.7.1
  • Added icon
2014Mar4
Version 1.7
  • Added support for Linux system environments
  • Also searching Git executable in the PATH environment variable
  • Updated help message
2014Jan12
Version 1.6
  • Added base28 version format as new bmin, renamed base36 format to b36min
2013Nov28
Version 1.5.1
  • Ignoring projects on restore if there is nothing to restore
2013May23
Version 1.5
  • Added multi-project mode
2012Nov26
Version 1.4
  • Added {bmin} base36 format with 10-minute intervals for higher compression of the time values (4 bmin digits are good for 30 years, would need 6 digits with xmin)
  • Added {ut...} variants for commit/build date/time formats in UTC instead of local time
  • Added command line options to decode xmin and bmin values to a readable time (UTC and local)
  • Added command line option to check the current and upcoming bmin values (in case the text is unwanted)
  • Fixed {xmin} and {bmin} time zone handling, it is now using UTC
2012Sep7
Version 1.3
  • Added {builddate} and {buildtime} placeholders
2012May6
Version 1.2.2
  • Fixed {commit} format length parsing to allow 1-4 as value
2012Jan31
Version 1.2.1
  • Be silent with the --ignore-missing parameter set to avoid VS build errors
2012Jan30
Version 1.2
  • Added parameter --ignore-missing to ignore missing Git binary or working directory
  • Made the path parameter actually work again (was ignored before)
  • Kind of fixed {xmin} output for negative values (giving minus-prefixed values now)
2012Jan28
Version 1.1
  • Git binary is first searched in the location from the uninstaller registry key
  • New format option {xmin} for compressed high-resolution times
2012Jan4
Version 1.0.1
  • Git binary is searched in several directories (%ProgramFiles*%\Git*\bin\git.exe)
2011Dec14
Version 1.0
  • Created GitRevisionTool based on SvnRevisionTool, using installed msysGit

Licence and terms of use

This software is released under the terms of the GNU GPL licence, version 3. You can find the detailed terms and conditions in the download or on the GNU website.

Statistic data

  • Created on 2011-12-14, updated on 2015-01-26.
  • Ca. 890 lines of code, estimated development costs: 890 - 3 600 €