What I'm trying to do, is create a pre-push hook that requires a tag be pointing to the same commit as the current HEAD. We're using git in a slightly unusual way, and there's no situations where the latest commit should not have a tag. I have a working implementation of this, but found that it only works with lightweight tags.
head=$(git rev-parse HEAD)
last_tag=$(git rev-parse $(git describe --tags))
if [ "$head" != "$last_tag" ]
then
echo >&2 'Aborting push - there is no tag on the latest commit.'
exit 1
fi
The problem I've found is that even after setting push.followTags
, lightweight tags are ignored. It's important to me that tags be pushed and pulled with no extra steps, since we'll be using them pretty heavily.
To work around this issue, we can use annotated tags. The issue is that an annotated tag has it's own hash that's returned by git rev-parse
. I'm unable to find a way to get the hash of the commit the tag is linked to. I've tried both
git rev-parse tagname^
git rev-parse $(git rev-parse tagname)^
Any ideas how this can be done, or if there's another better option?