Skip to content

Instantly share code, notes, and snippets.

@rgevaert
Created December 3, 2012 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgevaert/4195056 to your computer and use it in GitHub Desktop.
Save rgevaert/4195056 to your computer and use it in GitHub Desktop.
git puppet pre-commit hook
#!/bin/sh
syntax_errors=0
error_msg=$(mktemp /tmp/error_msg.XXXXXX)
if git rev-parse --quiet --verify HEAD > /dev/null
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Get list of new/modified manifest and template files to check (in git index)
for indexfile in `git diff-index --diff-filter=AM --name-only --cached $against | egrep '\.(pp|erb)'`
do
# Don't check empty files
if [ `git cat-file -s :0:$indexfile` -gt 0 ]
then
case $indexfile in
*.pp )
# Check puppet manifest syntax
if puppet --version | egrep -q '^2\.6\.'
then
git cat-file blob :0:$indexfile | puppet --color=false --parseonly --ignoreimport > $error_msg
else
# Updated for 2.7.x and later
puppet parser validate $indexfile > $error_msg
fi
;;
*.erb )
# Check ERB template syntax
# -P : ignore lines which start with "%"
git cat-file blob :0:$indexfile | erb -P -x -T - | ruby -c 2> $error_msg > /dev/null ;;
esac
if [ "$?" -ne 0 ]
then
echo -n "$indexfile: "
cat $error_msg
syntax_errors=`expr $syntax_errors + 1`
fi
fi
done
rm -f $error_msg
if [ "$syntax_errors" -ne 0 ]
then
echo "Error: $syntax_errors syntax errors found, aborting commit."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment