3 if ! cmp -s ${PWD}/.git/hooks/pre-commit ${PWD}/tools/git-hooks/pre-commit ; then
4 rm -f ${PWD}/.git/hooks/pre-commit
5 ln -sf ../../tools/git-hooks/pre-commit ${PWD}/.git/hooks/pre-commit
6 exec ${PWD}/.git/hooks/pre-commit
9 if git rev-parse --verify HEAD &>/dev/null; then
12 # Initial commit: diff against an empty tree object
13 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16 check_puppet_manifest () {
19 local pp=${workdir}/${file}
20 mkdir -p $(dirname ${pp})
21 git cat-file blob :0:${file} | sed 's/^import .*/#&/' >${pp}
22 trap "rm -f ${pp}" RETURN
24 local output=$(puppet parser validate ${pp} 2>&1)
25 if [ $? -ne 0 ] || [ -n "${output}" ]; then
26 echo '** Syntax check failed:' >&2
31 # Check manifests for exactly one class or define per file.
33 modules/*/manifests/*)
34 if [[ "$(egrep -c '^(class|define) ' ${file})" != "1" ]]; then
35 echo "** Multiple class/defines:" >&2
36 egrep -n '^(class|define) ' ${file} >&2
43 manifests/site.pp|modules/*/manifests/*) ;;
47 if [ -x /usr/bin/pcregrep ]; then
49 # grep -nPT would be nice. grep -P seems broken on gudeploy01.
50 # Patterns checked for are (in this order):
52 # - SPACE followed by TAB
53 # - TAB followed by SPACE
54 # - non-leading TAB (but allow around '#' comment leader)
55 # - trailing whitespace
56 pcregrep -n -e '^ ' -e ' \t' -e '\t ' -e '[^\t#]\t+(?!#)' -e '\s+$' ${pp}
58 if [[ $(wc -l <${pp}) > $(cat -s ${pp}|wc -l) ]]; then
59 wsfail+="Excess blank line(s). 'cat -s' is your friend."
61 if [[ -n "${wsfail}" ]]; then
62 echo '** Bad whitespace (see Manifest Standards):' >&2
67 echo "Missing pcregrep: apt-get install pcregrep" >&2
71 manifests/site.pp|modules/*/manifests/*)
72 if [[ -n "$(which puppet-lint)" ]]; then
73 puppet-lint --no-hard_tabs-check --no-2sp_soft_tabs-check --no-80chars-check ${pp} >&2 | uniq
75 echo "Please install puppet-lint (gem install puppet-lint)" >&2
86 # Check a Puppet template.
88 check_puppet_template () {
91 # Beware of 'Syntax OK' message on success.
92 local output=$((git cat-file blob :0:${file}|erb -x -P -T -|ruby -c >/dev/null) 2>&1)
93 if [[ $? -ne 0 || -n "${output}" ]]; then
94 echo '** Syntax check failed:' >&2
102 readonly failed=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
103 readonly missing=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
104 readonly parse=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
105 readonly retest=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
106 readonly workdir=$(mktemp -d ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
107 trap "rm -f ${failed} ${missing} ${parse} ${retest}; rm -rf ${workdir}" EXIT
111 for file in $(git diff-index --name-only --diff-filter=AM --cached ${against}); do
117 output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
121 modules/*/manifests/*)
123 # FIXME: doing this so the whitespace check happens
124 *.pp) output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
127 modules/*/templates/*)
128 output=$(check_puppet_template ${file} 2>&1); rc=$?;;
134 if [[ ${rc} -ne 0 || -n "${output}" ]]; then
135 echo "** ${file}" >&2
137 if [[ ${file} != *.pp ]]; then
139 # puppet-lint warnings are acceptable.
140 elif [[ -n "$(egrep -v '^WARNING: ' <<<${output})" ]]; then
148 if [ "$error" -ne 0 ]; then
149 echo "** Please correct the above errors." >&2
153 if [ "$warning" -ne 0 ]; then