Skip to content

Instantly share code, notes, and snippets.

@raphink
Last active December 23, 2015 21:29
Show Gist options
  • Save raphink/6696803 to your computer and use it in GitHub Desktop.
Save raphink/6696803 to your computer and use it in GitHub Desktop.
#! /bin/bash
# If we don't have a HEAD, then this is the first commit and we can't do any of this
git show > /dev/null 2>&1
if [ $? -ne 0 ]; then exit 0; fi
# first stash any on-disk changes so we're actually validating
# what's staged to be committed and not just what's on disk.
git diff --full-index --binary > /tmp/stash.$$
git stash -q --keep-index
EXITCODE=0
for file in `git diff-index --cached --name-only HEAD`
do
echo "Validating ${file} ..."
case "${file##*.}" in
"pp")
puppet parser validate ${file} && \
puppet-lint --fail-on-warnings ${file}
;;
"erb")
erb -P -x -T '-' ${file} | ruby -c >/dev/null
;;
"rb")
ruby -c < ${file} >/dev/null
;;
"yaml")
ruby -ryaml -e 'YAML::load(STDIN.read)' < ${file}
;;
esac
EXITCODE=$((EXITCODE + $?))
done
if [ $EXITCODE -ne 0 ]
then
echo
echo "################################################################"
echo -e "### \033[31mPlease fix the errors above before committing your code.\033[0m ###"
echo "### ###"
echo -e "### \033[31mYou can bypass this check with --no-verify. \033[0m ###"
echo "################################################################"
if [[ -s /tmp/stash.$$ ]]
then
echo "### ###"
echo -e "### \033[31mDid you remember to git add your updated code?\033[0m ###"
echo "### ###"
echo "################################################################"
fi
echo
fi
# now recover diffs to get back to pre commit state if needed.
if [[ -s /tmp/stash.$$ ]]
then
git apply --whitespace=nowarn < /tmp/stash.$$ && git stash drop -q
rm /tmp/stash.$$
fi
exit $EXITCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment