Created
September 19, 2013 20:45
-
-
Save nvalentine-puppetlabs/6629601 to your computer and use it in GitHub Desktop.
Puppet pre-commit script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} | |
;; | |
"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 | |
:0 | |
rm /tmp/stash.$$ | |
fi | |
exit $EXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment