-
Notifications
You must be signed in to change notification settings - Fork 1
/
chef-pre-commit
executable file
·72 lines (64 loc) · 2.07 KB
/
chef-pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# vim: set ft=sh:
# If you see 'warning: 2.1.7-compliant syntax, but you are running 2.1.6' then
# sudo chef gem install parser --version 2.2.2.6 --no-user-install
# sudo chef gem uninstall parser --version 2.2.3.0
RET=0
which foodcritic >/dev/null || ( echo ERR: no foodcritic && RET=$(($RET+$?)) )
which rubocop >/dev/null || ( echo ERR: no rubocop && RET=$(($RET+$?)) )
which xmllint >/dev/null || ( echo WARN: no xmllint && NOXML=1 )
which erb >/dev/null || ( echo WARN: no erb && NOERB=1)
which jq >/dev/null || ( echo WARN: no jq && NOJQ=1 )
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: if HEAD doesn't exist then diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# Foodcritic
REPOPATH=$(git rev-parse --show-toplevel)
foodcritic --epic-fail any $REPOPATH
RET=$(($RET+$?))
# Syntax check updated files
for FILE in $(git diff --cached --name-only); do
#for FILE in $(find $REPOPATH -type f ); do
extension="${FILE##*.}"
# Don't check deleted or empty files
if [ ! -f "${REPOPATH}/${FILE}" ] || [ $(grep -c . "${REPOPATH}/${FILE}") -eq 0 ]; then
continue
fi
case $extension in
rb)
rubocop --format emacs -F $FILE
RET=$(($RET+$?))
;;
erb)
if [ -v $NOERB ] ; then
erb -x -T - $FILE | ruby -c > /dev/null
if [ $? -ne 0 ] ; then RET=$(($RET+$?)) ; echo $FILE : ERB Parse Error ; fi
fi
;;
json)
if [ -v $NOJQ ] ; then
cat $FILE | jq 'any' > /dev/null
if [ $? -ne 0 ] ; then RET=$(($RET+$?)); echo $FILE : JSON Parse Error ; fi
fi
;;
xml)
if [ -v $NOXML ] ; then
xmllint --noout $FILE > /dev/null
if [ $? -ne 0 ] ; then RET=$(($RET+$?)); echo $FILE : XML Parse Error ; fi
fi
;;
yaml|yml)
ruby -e "require 'yaml';YAML.parse(File.open('$FILE'))" > /dev/null
if [ $? -ne 0 ] ; then RET=$(($RET+$?)); echo $FILE : YAML Parse Error ; fi
;;
*) # ignored
;;
esac
done
exit $RET