Reputation: 550
For tagging versions of my software, I am using git tags
.
I want my software to display the version for the user, and execute some migration code, which depends on the version currently used.
Therefore, I tried to use the git-hook pre-commit
in order to generate the file version.txt
#!/bin/sh
# Only show most recent tag without trailing commit information
git describe --tags | awk "{split(\$0,a,\"-\"); print a[1];}" > version.tmp
# Only proceed if version number has actually changed (i.e. a new tag has been created)
if [ ! $(cmp --silent version.tmp version.txt) ]
then
echo $?
echo Updating version.txt.
mv -f version.tmp version.txt
git add version.txt
fi
I entered the commands
$ git add --all
$ git tag -a 1.0
$ git commit
It turns out the version.txt
tagged with 1.0
does not contain the respective version, that is 1.0
, but the prior version ( 0.9
) instead.
What is wrong with this procedure? How else is it possible to keep information about the git tag (and also the commit) with the software?
Upvotes: 5
Views: 3488
Reputation: 9886
You have to think of the commit objects and what's happening to those.
In your series of statements, you're tagging the current HEAD with 1.0, but the version.txt contains 0.9. Tagging is just a marker on the commit object, and as the file has "0.9", that's what gets tagged.
Then on the next commit the version file gets bumped to 1.0, but this is a new commit and you aren't changing the tag for 1.0 to point to it.
It looks like you're trying to use the tag in not quite the way it's designed. You might like to reverse the procedure, and get people to change the version.txt file, and use a post-commit
hook to add the tag. I tried this and it works with something like:
#!/bin/sh
# Only show most recent tag without trailing commit information
git describe --tags | awk "{split(\$0,a,\"-\"); print a[1];}" > version.tmp
# Only proceed if version number has actually changed (i.e. a new tag has been created)
if [ ! $(cmp --silent version.tmp version.txt) ]
then
NEWVER=$(cat version.txt)
echo Adding tag $NEWVER
git tag -a $NEWVER -m ''
rm version.tmp
fi
You might want to look at an alternate where the tag gets moved with the commit, but that would also have to be in a post-commit as any tagging in pre-commit goes on the current HEAD, not the one about to be commit. Hence why I had to use the post-commit above.
Personally I try and avoid automated tagging / version bumping as there are all kinds of end conditions - what happens during a amend? what if they want to rebase? But, everyone has their own use-cases.
Upvotes: 6
Reputation: 1
I don't write the tag or commit id, by I have a Makefile
rule like this:
_timestamp.c:
@date +'const char monimelt_timestamp[]="%c";' > _timestamp.tmp
@(echo -n 'const char monimelt_lastgitcommit[]="' ; \
git log --format=oneline --abbrev=12 --abbrev-commit -q \
| head -1 | tr -d '\n\r\f\"' ; \
echo '";') >> _timestamp.tmp
@mv _timestamp.tmp _timestamp.c
You might adapt it to your needs.
Upvotes: 1