Versioning and Git Hooks

In this page, I’d like to summarize the method I use to auto-generate a file with the current version data of the software. The system relies on git version control and GNU make. I primarily use this method for my C/C++ firmware for microcontrollers, but the same concepts can be adapted for other software or project workflows.
What are git hooks?
Git hooks are a series of scripts that are automatically executed by git when certain events occur (e.g., a commit, a merge) 1.
These scripts are stored in the .git/hooks
directory, making them customizable for each local repository.
Custom script
As soon as git detects a state change in the current directory, all it needs to do is update the version file.
In the case of a C/C++ project, I set up a version
rule in the makefile, which takes care of properly formatting the strings in the version.git.h
file.
|
|
This code should be placed in the post-checkout
, post-commit
, and post-merge
files within the .git/hooks
folder.
The names are quite self-explanatory regarding their functions.
Note that the action of pulling is not explicitly mentioned; in reality, the script for the merge is invoked in the case of a fast-forward or, of course, an automatic merge.
In the case of conflicts, the hook is called after manual resolution (i.e., when you commit, and it’s always a merge at the end) 2.
|
|
Preparing the Makefile
Below is how I decided to implement the version rule in my makefiles.
To keep track of the most relevant commits, I usually apply tags in the form v.m
, with v (version) and m (major) as positive integers, and I put them in a triplet with the number of commits made since the last tag (minor).
There are various interesting ideas on the web 3.
Note: git describe --long --dirty --tags
will fail if there are no tags in the repository history.
|
|
The various pieces of information are added as macros but annot be directly used as strings. To use them, I have the stringification macros 4, as shown in this example.
|
|
Other Use Cases
$\LaTeX$
If you use a makefile to compile $\LaTeX$ documents 5, you just need to slightly modify the rule mentioned above. One strategy is to add custom commands that fetch the relevant values. The choice of command names is arbitrary.
|
|
Finally, include the file in the main document with \input{version.git.tex}
to use the values where needed.
Python
In this case, there’s no makefile, so you need to move the entire rule inside the hook script. However, the concept remains the same: the script retrieves version information from git and formats it into a file in accordance with the language used. Here’s how it looks. Note that you need to execute it with bash, as not all shells support IFS6.
|
|