-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
254 lines (205 loc) · 5.31 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
##########
# Git Pre-Commit file for PHP projects
###
#
# This hook performs the following validation:
# - PHP CodeSniffer
# - PHP Mess Detector
#
# Based on https://gist.github.com/codemis/8225337
#
# @author Davi Marcondes Moreira <[email protected]>
#
##########
##########
# DEFAULT SETTINGS
###
#
# These variables define the basic values for Code_Sniffer and PHPMD.
# Override these by creating a new variable on the `config` file.
#
##########
PHPCS_ACTIVE=1
PHPCS_BIN=/usr/bin/phpcs
PHPCS_CODING_STANDARD=PEAR
PHPCS_IGNORE=
PHPMD_ACTIVE=1
PHPMD_BIN=/usr/bin/phpmd
PHPMD_OUTPUT=text
PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
TMP_STAGING="/tmp/.tmp_staging"
##########
# Parse config file.
##########
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
##########
# First: check if PHP Code_Sniffer and PHPMD bin files are present && executable.
##########
if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
tput setaf 1; echo "Executable not found. Check $PHPCS_BIN and $PHPMD_BIN."
exit 1
fi
##########
# Git Check-up
##########
if git rev-parse --verify HEAD
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# This is the magic:
# Retrieve all files in staging area that are ADDED, MODIFIED or RENAMED,
# but no deletions etc.
# Lets first check if there are any file pattern to exclude from this list.
if [ "$GIT_EXCLUDE" != "" ]; then
GIT_EXCLUDE_LIST="| grep -v $GIT_EXCLUDE"
else
GIT_EXCLUDE_LIST=""
fi
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- $GIT_EXCLUDE_LIST)
if [ "$FILES" == "" ]; then
exit 0
fi
# Create temporary copy of staging area
if [ -e $TMP_STAGING ]; then
rm -rf $TMP_STAGING
fi
mkdir $TMP_STAGING
# Match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
RETVAL=$?
if [ "$RETVAL" -eq "0" ]
then
FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
fi
done
if [ "$FILES_TO_CHECK" == "" ]; then
exit 0
fi
##########
# Validate PHP CodeSniffer variables
##########
if [ "$PHPCS_ACTIVE" != "1" ]; then
PHPCS_ACTIVE=0
fi
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
if [ "$PHPCS_CODING_STANDARD" != "" ]; then
PHPCS_CODING_STANDARD="--standard=$PHPCS_CODING_STANDARD"
else
PHPCS_CODING_STANDARD=""
fi
if [ "$PHPCS_SNIFFS" != "" ]; then
SNIFFS="--sniffs=$PHPCS_SNIFFS"
else
SNIFFS=""
fi
if [ "$PHPCS_ENCODING" != "" ]; then
ENCODING="--encoding=$PHPCS_ENCODING"
else
ENCODING=""
fi
if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
IGNORE_WARNINGS="-n"
else
IGNORE_WARNINGS=""
fi
##########
# Validate PHP Mess Detector variables
##########
if [ "$PHPMD_ACTIVE" != "1" ]; then
PHPMD_ACTIVE=0
fi
if [ "$PHPMD_OUTPUT_MODE" != "" ]; then
PHPMD_OUTPUT="$PHPMD_OUTPUT_MODE"
else
PHPMD_OUTPUT="text"
fi
if [ "$PHPMD_PATTERNS" != "" ]; then
PHPMD_PATTERNS_LIST="$PHPMD_PATTERNS"
else
PHPMD_PATTERNS_LIST="cleancode"
fi
if [ "$PHPMD_SUFFIXES" != "" ]; then
PHPMD_SUFFIXES_LIST="--suffixes $PHPMD_SUFFIXES"
else
PHPMD_SUFFIXES_LIST=""
fi
if [ "$PHPMD_EXCLUDE" != "" ]; then
PHPMD_EXCLUDE_LIST="--exclude $PHPMD_EXCLUDE"
else
PHPMD_EXCLUDE_LIST=""
fi
##########
# Copy contents of staged version of files to temporary staging area
# because we only want the staged version that will be commited and not
# the version in the working directory.
##########
STAGED_FILES=""
for FILE in $FILES_TO_CHECK
do
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
##########
# Create staged version of file in temporary staging area with the same
# path as the original file so that the phpcs ignore filters can be applied.
##########
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
git cat-file blob $ID > "$TMP_STAGING/$FILE"
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
done
##########
# CODE INSPECTION: PHP CodeSniffer
##########
if [ "$PHPCS_ACTIVE" == "1" ]; then
echo ""
tput setaf 12; echo " :: PHP CodeSniffer inspection :: "
PHPCS_OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS $PHPCS_CODING_STANDARD $ENCODING $IGNORE $STAGED_FILES)
PHPCS_RETVAL=$?
if [ $PHPCS_RETVAL -ne 0 ]; then
tput setaf 1; echo " ✘ Issues found: "
tput setaf 7; echo "$PHPCS_OUTPUT"
rm -rf $TMP_STAGING
exit $PHPCS_RETVAL
else
tput setaf 2; echo " ✔ Inspection is OK!"
fi
else
echo ""
tput setaf 8; echo " ➔ PHP CodeSniffer inspection is OFF."
fi
##########
# CODE INSPECTION: PHP Mess Detector
##########
if [ "$PHPMD_ACTIVE" == "1" ]; then
echo ""
tput setaf 12; echo " :: PHP Mess Detector inspection :: "
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST)
PHPMD_RETVAL=$?
if [ $PHPMD_RETVAL -ne 0 ]; then
tput setaf 1; echo " ✘ Issues found: "
tput setaf 7; echo "$PHPMD_OUTPUT"
rm -rf $TMP_STAGING
exit $PHPMD_RETVAL
else
tput setaf 2; echo " ✔ Inspection is OK!"
fi
else
echo ""
tput setaf 8; echo " ➔ PHP Mess Detector inspection is OFF."
fi
tput setaf 12;
rm -rf $TMP_STAGING
echo ""
exit 0;