Skip to content

Instantly share code, notes, and snippets.

@farseerfc
Last active October 31, 2017 19:22
Show Gist options
  • Save farseerfc/0729c08cd7c82b07000f20105f733b17 to your computer and use it in GitHub Desktop.
Save farseerfc/0729c08cd7c82b07000f20105f733b17 to your computer and use it in GitHub Desktop.
.git/hooks/pre-push to check version matches tag name
#!/bin/bash
## hook to check ver match tag before push
VERSIONFILE="gitversion.h"
tagref=$(grep -Po 'refs/tags/([^ ]*) ' </dev/stdin | head -n1 | cut -c11- | tr -d '[:space:]')
if [[ "$tagref" == "" ]]; then
## pushing without --tags , exit normally
exit 0
fi
## versionline may looks like '#define TUNTOXVERSION "0.0.8"'
versionline=$(git cat-file blob master:"$VERSIONFILE" | grep 'TUNTOXVERSION')
ver=$(echo "$versionline" | sed 's/^[^"]*"//;s/"[^"]*$//')
if [[ "$tagref" == "$ver" ]]; then
## tag matches ver
exit 0
fi
echo "Tag name don't match version file. Preventing push."
echo "tag name: $tagref"
echo "version: $ver"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment