Skip to content

Instantly share code, notes, and snippets.

@fabiobruna
Forked from ashwoods/pre-commit
Created October 29, 2016 11:36
Show Gist options
  • Save fabiobruna/f6f5569d2f97d6c223b4ca61d9dea680 to your computer and use it in GitHub Desktop.
Save fabiobruna/f6f5569d2f97d6c223b4ca61d9dea680 to your computer and use it in GitHub Desktop.
pre-commit hook for VERSION file
#!/bin/sh
# Tag revisions like this:
# $ git tag -a -m "Version 0.2" v0.2 HEAD
VF=VERSION
DEFAULT_VERSION=UNKNOWN
LF='
'
# First see if there is a version file (included in release tarballs),
# then try git-describe, then default.
if test -d .git -o -f .git &&
VN=$(git describe --abbrev=4 HEAD 2>/dev/null) &&
case "$VN" in
*$LF*) (exit 1) ;;
v[0-9]*)
git update-index -q --refresh
test -z "$(git diff-index --name-only HEAD --)" ||
VN="$VN-mod" ;;
esac
then
continue
#VN=$(echo "$VN" | sed -e 's/-/./g');
else
VN="$DEFAULT_VERSION"
fi
VN=$(expr "$VN" : v*'\(.*\)')
# Show the version to the user via stderr
echo >&2 "version: $VN"
# Parse the existing VERSION-FILE
if test -r $VF
then
VC=$(sed -e 's/^version: //' <$VF)
else
VC=unset
fi
# If version has changed, update VERSION-FILE
test "$VN" = "$VC" || {
echo "version: $VN" >$VF
echo >&2 "($VF updated)"
}
# git add VERSION always
git add $VF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment