Git version in makefile

#1

I would like to have a string in my code automatically reflecting the git commit number, and show it in an “about” dialog. Here it is suggested to add the following to the makefile:

gitversion.c: .git/HEAD .git/index
    echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@

unfortunately I don’t know enough about make to make it work, I tried to add it to compile.project.mk but without success.

Can someone suggest how to do this? (if it is possible at all with the OF makefile)

thanks
Davide

#2

You don’t need make to do this, though it would certainly work. But you may want to use a non-make-build system so a more general solution might be useful.

One simple alternative, if you are already using git, is to create git commit hook. For example, you could write a simple “post commit hook” to (re)generate a Version.h file that you can include from your ‘about’ dialog header. You can read more about writing your own git hooks here.

1 Like
#3

Thank you. Git hooks are the way to go :slight_smile:

EDIT: for future reference this is what I wrote (quick and dirty, adapted from some example found online). The script name is “git-create-revisioninfo-hook.sh”

#!/bin/bash
## Automatically generate a file with git branch and revision info
##
## Install:
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-commit
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-checkout
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-merge
##   chmod +x .git/hooks/post-*

FILENAME='src/version.h'

exec 1>&2
branch=`git rev-parse --abbrev-ref HEAD`
shorthash=`git log --pretty=format:'%h' -n 1`
revcount=`git log --oneline | wc -l`
latesttag=`git describe --tags --abbrev=0 --always`

echo "// Automatically generated by git hook. Do not edit manually." > $FILENAME
echo "" >> $FILENAME
echo "#define GIT_BRANCH $branch" >> $FILENAME
echo "#define GIT_LATEST_TAG $latesttag" >> $FILENAME
echo "#define GIT_REVCOUNT $revcount" >> $FILENAME
echo "#define GIT_SHORT_HASH $shorthash" >> $FILENAME

1 Like