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:
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:
[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:
I have a similar, older tool that does the same for Subversion repositories: SvnRevisionTool
Images
Download
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
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 €