Skip to content

Instantly share code, notes, and snippets.

@abrader
Created September 29, 2014 20:14
Show Gist options
  • Select an option

  • Save abrader/d5e7581c11f36583cc06 to your computer and use it in GitHub Desktop.

Select an option

Save abrader/d5e7581c11f36583cc06 to your computer and use it in GitHub Desktop.
<git repo>/.git/hooks/pre-commit for Puppet developers
#! /bin/sh
# 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 --diff-filter=AM --name-only HEAD`
do
echo "Validating ${file}..."
case "${file##*.}" in
"pp") puppet parser validate ${file}
;;
"erb") /opt/puppet/bin/erb -P -x -T '-' ${file} | /opt/puppet/bin/ruby -c >/dev/null
;;
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 "################################################################"
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