Insert git revision number into a latex document or into a given software as a version number
The following describes how to automatically insert a git revision number into a document or program.
A bash script is used to write the version number into a file, the file is then included within a software or document. The call to the bash script can easily be added to the "build" routine, for instance using a Makefile.
To retrieve the relevant information from git check out (version/branch) this article.
Based on this article, it is easy to extract the relevant informations and put them e.g. in a latex file. The following bash script, named 'make_version.sh' does that:
It is important that the version file is not under version control, otherwise a circular dependency will occur: each time you commit something, the version file changes, the version needs then to be committed, etc.. The version file (e.g. 'Version.tex' or 'Version.f90') should be added to the '.gitignore' file of the repository.
To automatically generate the revision number, a Makefile can be used as follows:
To retrieve the relevant information from git check out (version/branch) this article.
Based on this article, it is easy to extract the relevant informations and put them e.g. in a latex file. The following bash script, named 'make_version.sh' does that:
#!/bin/bash
git rev-parse --short HEAD | awk '{print "\\newcommand{\\gitrevision}{"$1"}"}' > Version.tex
git rev-parse --abbrev-ref HEAD | awk '{print "\\newcommand{\\gitbranch}{"$1"}"}' >> Version.tex
git describe --dirty=-dev | awk '{print "\\newcommand{\\gitversion}{"$1"}"}' >> Version.tex
The shell script can easily be adapted to output the information into a "source code" for a given software.
For instance, to insert the version into a Fortran program, the shell script would be:
#!/bin/bash
REV=`git describe --dirty=-dev`
VersionFile=Version.f90
echo "module VersionMod" > $VersionFile
echo "character(len=64), parameter :: VERSION = '$REV'" >> $VersionFile
echo "end module VersionMod" >> $VersionFile
It is important that the version file is not under version control, otherwise a circular dependency will occur: each time you commit something, the version file changes, the version needs then to be committed, etc.. The version file (e.g. 'Version.tex' or 'Version.f90') should be added to the '.gitignore' file of the repository.
To automatically generate the revision number, a Makefile can be used as follows:
all: gitversion compile
gitversion:
@./make_version.sh
compile:
@echo "Do something here..."