Skip to content

Instantly share code, notes, and snippets.

@lotheac
Created December 14, 2012 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotheac/4285627 to your computer and use it in GitHub Desktop.
Save lotheac/4285627 to your computer and use it in GitHub Desktop.
pre-commit hook to validate and lint puppet manifests. also checks other files for whitespace errors (git diff --check)
#!/bin/sh -e
#
# install to .git/hooks/pre-commit to syntax-check and style-check puppet
# manifests
unset tmpd changed_filelist
cleanup() {
[ -d "$tmpd" ] && rm -rf "$tmpd"
}
tmpd=`mktemp -d /tmp/puppet-precommit.XXXXXX`
changed_filelist="${tmpd}/changed-files"
trap cleanup 0 INT HUP TERM QUIT
all_ok=true
git diff-index --cached --diff-filter=AM --name-only HEAD\
| git checkout-index --stdin --prefix="${tmpd}/"
git --no-pager diff --cached --check || all_ok=false
cd "${tmpd}"
find . -type f -name '*.pp' > "$changed_filelist"
xargs -r puppet parser validate < "$changed_filelist"
extra_args='--no-80chars-check'
# puppet-lint only checks one file per invocation
while read manifest; do
case "$manifest" in
./modules/*)
;;
*)
extra_args="$extra_args --no-autoloader_layout-check"
;;
esac
puppet-lint --with-filename --fail-on-warnings $extra_args "$manifest" || all_ok=false
done < "$changed_filelist"
$all_ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment