-
Notifications
You must be signed in to change notification settings - Fork 10
/
pre-commit
121 lines (102 loc) · 3.33 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
#
# A pre-commit hook to verify coding standards.
#
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# diff against the empty tree sha
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
if ! type php &> /dev/null; then
echo "php does not exist or is not in path"
echo "You can bypass this hook with the --no-verify option"
exit 1
fi
if ! type phpcs &> /dev/null; then
echo "phpcs does not exist or is not in path"
echo "You can bypass this hook with the --no-verify option"
exit 1
fi
if ! type jshint &> /dev/null; then
echo "jshint does not exist or is not in path"
echo "You can bypass this hook with the --no-verify option"
exit 1
fi
if ! type csslint &> /dev/null; then
echo "csslint does not exist or is not in path"
echo "You can bypass this hook with the --no-verify option"
exit 1
fi
# dash does not support $'\n':
# http://forum.soft32.com/linux2/Bug-409179-DASH-Settings-IFS-work-properly-ftopict70039.html
IFS='
'
PHP_ERRORS=''
PHPCS_ERRORS=''
JS_ERRORS=''
CSS_ERRORS=''
PHPCS_PATH=$(git config --get ayi.phpcs)
error=0
# get a list of staged files
for line in $(git diff-index --cached --full-index $against); do
# split needed values
sha=$(echo $line | cut -d' ' -f4)
temp=$(echo $line | cut -d' ' -f5)
status=$(echo $temp | cut -f1)
filename=$(echo $temp | cut -f2)
ext=$(echo $filename | sed 's/^.*\.//')
clean_filename="$(echo $filename | sed 's/\//\\\//g')"
if [[ $status = "D" ]]; then
continue
fi
# check php files with php -l and phpcs
if [[ "$ext" == "php" || "$ext" == "ctp" ]]; then
modified_file="$(git cat-file -p $sha)"
result="$(echo "$modified_file" | php -l 2>&1)"
phplint_exit=$?
if [[ $phplint_exit -ne 0 ]]; then
error=1
PHP_ERRORS="$(echo "$PHP_ERRORS"; echo "PHP $(echo "$result" | sed "s/in - on/in $clean_filename on/g")")"
fi
phpcs_result="$(echo "$modified_file" | phpcs -sn --standard=VIA 2>&1)"
if [[ $? -ne 0 ]]; then
error=1
PHPCS_ERRORS="$(echo "$PHPCS_ERRORS"; echo "$phpcs_result" | sed 's/STDIN/'$clean_filename'/g' | sed 's/^Time:.*$//g')"
fi
fi
# check js files with jshint
if [[ "$ext" == "js" || "$ext" == "html" ]]; then
result=$(git cat-file -p $sha | jshint "$filename" 2>&1)
if [[ $? -ne 0 ]]; then
error=1
JS_ERRORS=$(echo "$JS_ERRORS"; echo "$filename"; echo "$result" | sed 's/^.*'$clean_filename': //g' | sed 's/^[0-9]* error.*//g')
fi
fi
# check css files with csslint
if [[ "$ext" == "css" ]]; then
# check the staged file content for syntax errors using csslint
result=$(git cat-file -p $sha | csslint --format=compact --errors=errors "$filename" 2>&1)
if [[ $? -ne 0 ]]; then
error=1
CSS_ERRORS=$(echo "$CSS_ERRORS"; echo "$filename"; echo "$result" | sed 's/^.*'$clean_filename': //g')
fi
fi
done
unset IFS
if [[ $error -eq 1 ]]; then
if [ -n "$PHP_ERRORS" ] ; then
echo "$PHP_ERRORS"
fi
if [ -n "$PHPCS_ERRORS" ] ; then
echo "$PHPCS_ERRORS"
fi
if [ -n "$JS_ERRORS" ] ; then
echo "$JS_ERRORS"
fi
if [ -n "$CSS_ERRORS" ] ; then
echo "$CSS_ERRORS"
fi
echo
exit 1
fi