pre-push.sh 733 B

12345678910111213141516171819202122232425262728
  1. #!/bin/bash
  2. ## hook to check version match tag before push
  3. ## usage: ln -s ../../scripts/pre-push.sh .git/hooks/pre-push
  4. VERSIONFILE="gitversion.h"
  5. BRANCH="HEAD"
  6. tagref=$(grep -Po 'refs/tags/([^ ]*) ' </dev/stdin | head -n1 | cut -c11- | tr -d '[:space:]')
  7. if [[ "$tagref" == "" ]]; then
  8. ## pushing without --tags , exit normally
  9. exit 0
  10. fi
  11. ## versionline may looks like '#define GITVERSION "0.0.8"'
  12. versionline=$(git cat-file blob $BRANCH:"$VERSIONFILE" | grep 'GITVERSION')
  13. ver=$(echo "$versionline" | sed 's/^[^"]*"//;s/"[^"]*$//')
  14. if [[ "$tagref" == "$ver" ]]; then
  15. ## tag matches ver
  16. exit 0
  17. fi
  18. echo "Tag name don't match version file. Preventing push."
  19. echo "tag name: $tagref"
  20. echo "version: $ver"
  21. exit 1