]> git.donarmstrong.com Git - dsa-puppet.git/blob - tools/git-hooks/pre-commit
add tools directory
[dsa-puppet.git] / tools / git-hooks / pre-commit
1 #!/bin/bash
2
3 if git rev-parse --verify HEAD &>/dev/null; then
4         against=HEAD
5 else
6         # Initial commit: diff against an empty tree object
7         against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
8 fi
9
10 check_puppet_manifest () {
11         local file=$1
12
13         local pp=${workdir}/${file}
14         mkdir -p $(dirname ${pp})
15         git cat-file blob :0:${file} | sed 's/^import .*/#&/' >${pp}
16         trap "rm -f ${pp}" RETURN
17
18         local output=$(puppet apply --noop --ignoreimport ${pp} 2>&1)
19         if [ $? -ne 0 ] || [ -n "${output}" ]; then
20                 echo '** Syntax check failed:' >&2
21                 echo "${output}" >&2
22                 return 1
23         fi
24
25         # Check manifests for exactly one class or define per file.
26         case "${file}" in
27                 modules/*/manifests/*)
28                         if [[ "$(egrep -c '^(class|define) ' ${file})" != "1" ]]; then
29                                 echo "** Multiple class/defines:" >&2
30                                 egrep -n '^(class|define) ' ${file} >&2
31                                 return 1
32                         fi
33                         ;;
34         esac
35
36         case "${file}" in
37                 manifests/site.pp|modules/*/manifests/*) ;;
38                 *) return
39         esac
40
41         if [ -x /usr/bin/pcregrep ]; then
42                 local wsfail=$({
43                         # grep -nPT would be nice. grep -P seems broken on gudeploy01.
44                         # Patterns checked for are (in this order):
45                         # - leading SPACE
46                         # - SPACE followed by TAB
47                         # - TAB followed by SPACE
48                         # - non-leading TAB (but allow around '#' comment leader)
49                         # - trailing whitespace
50                         pcregrep -n -e '^ ' -e ' \t' -e '\t ' -e '[^\t#]\t+(?!#)' -e '\s+$' ${pp}
51                 })
52                 if [[ $(wc -l <${pp}) > $(cat -s ${pp}|wc -l) ]]; then
53                         wsfail+="Excess blank line(s). 'cat -s' is your friend."
54                 fi
55                 if [[ -n "${wsfail}" ]]; then
56                         echo '** Bad whitespace (see Manifest Standards):' >&2
57                         echo "${wsfail}" >&2
58                         return 1
59                 fi
60         else
61                 echo "Missing pcregrep: apt-get install pcregrep" >&2
62         fi
63
64         case "${file}" in
65                 manifests/site.pp|modules/*/manifests/*)
66                         if [[ -n "$(which puppet-lint)" ]]; then
67                                 puppet-lint --no-hard_tabs-check --no-80chars-check ${pp} >&2 | uniq
68                         else
69                                 echo "Please install puppet-lint (gem install puppet-lint)" >&2
70                         fi
71                         ;;
72                 *)
73                         return
74                         ;;
75         esac
76
77         return 0
78 }
79
80 # Check a Puppet template.
81 #
82 check_puppet_template () {
83         local file=$1
84
85         # Beware of 'Syntax OK' message on success.
86         local output=$((git cat-file blob :0:${file}|erb -x -P -T -|ruby -c >/dev/null) 2>&1)
87         if [[ $? -ne 0 || -n "${output}" ]]; then
88                 echo '** Syntax check failed:' >&2
89                 echo "${output}" >&2
90                 return 1
91         fi
92
93         return 0
94 }
95
96 readonly failed=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
97 readonly missing=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
98 readonly parse=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
99 readonly retest=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
100 readonly workdir=$(mktemp -d ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
101 trap "rm -f ${failed} ${missing} ${parse} ${retest}; rm -rf ${workdir}" EXIT
102
103 error=0
104 warning=0
105 for file in $(git diff-index --name-only --diff-filter=AM --cached ${against}); do
106         output=""
107         rc=0
108
109         case "${file}" in
110                 manifests/site.pp)
111                         output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
112                 modules/*)
113
114                         case "${file}" in
115                                 modules/*/manifests/*)
116                                         case "${file}" in
117                                                 # FIXME: doing this so the whitespace check happens
118                                                 *.pp) output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
119                                         esac
120                                         ;;
121                                 modules/*/templates/*)
122                                         output=$(check_puppet_template ${file} 2>&1); rc=$?;;
123                         esac
124                         ;;
125
126         esac
127
128         if [[ ${rc} -ne 0 || -n "${output}" ]]; then
129                 echo "** ${file}" >&2
130                 echo "${output}" >&2
131                 if [[ ${file} != *.pp ]]; then
132                         error=1
133                 # puppet-lint warnings are acceptable.
134                 elif [[ -n "$(egrep -v '^WARNING: ' <<<${output})" ]]; then
135                         error=1
136                 else
137                         warning=1
138                 fi
139         fi
140 done
141
142 if [ "$error" -ne 0 ]; then
143         echo "** Please correct the above errors." >&2
144         exit 1
145 fi
146
147 if [ "$warning" -ne 0 ]; then
148         sleep 5
149 fi
150
151 exit 0