1
1
#! /bin/bash
2
+
2
3
# #########
3
4
# Git Pre-Commit file for PHP projects
4
5
# ##
13
14
#
14
15
# #########
15
16
17
+
18
+ # #########
19
+ # DEFAULT SETTINGS
20
+ # ##
21
+ #
22
+ # These variables define the basic values for Code_Sniffer and PHPMD.
23
+ # Override these by creating a new variable on the `config` file.
24
+ #
25
+ # #########
26
+ PHPCS_ACTIVE=1
16
27
PHPCS_BIN=/usr/bin/phpcs
17
28
PHPCS_CODING_STANDARD=PEAR
18
29
PHPCS_IGNORE=
30
+ PHPMD_ACTIVE=1
19
31
PHPMD_BIN=/usr/bin/phpmd
20
32
PHPMD_OUTPUT=text
21
33
PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
22
34
TMP_STAGING=" /tmp/.tmp_staging"
23
35
24
- # Parse config
36
+
37
+ # #########
38
+ # Parse config file.
39
+ # #########
25
40
CONFIG_FILE=$( dirname $0 ) /config
26
41
if [ -e $CONFIG_FILE ]; then
27
42
. $CONFIG_FILE
28
43
fi
29
44
30
- # Simple check if code sniffer is set up correctly
45
+
46
+ # #########
47
+ # First: check if PHP Code_Sniffer and PHPMD bin files are present && executable.
48
+ # #########
31
49
if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
32
50
tput setaf 1; echo " Executable not found. Check $PHPCS_BIN and $PHPMD_BIN ."
33
51
exit 1
34
52
fi
35
53
36
- # Stolen from template file
54
+
55
+ # #########
56
+ # Git Check-up
57
+ # #########
37
58
if git rev-parse --verify HEAD
38
59
then
39
60
against=HEAD
43
64
fi
44
65
45
66
# This is the magic:
46
- # Retrieve all files in staging area that are added, modified or renamed
67
+ # Retrieve all files in staging area that are ADDED, MODIFIED or RENAMED,
47
68
# but no deletions etc.
48
- # Lets first check if there are any file pattern to exclude from this list
69
+ # Lets first check if there are any file pattern to exclude from this list.
49
70
if [ " $GIT_EXCLUDE " != " " ]; then
50
71
GIT_EXCLUDE_LIST=" | grep -v $GIT_EXCLUDE "
51
72
else
52
73
GIT_EXCLUDE_LIST=" "
53
74
fi
54
75
76
+
55
77
FILES=$( git diff-index --name-only --cached --diff-filter=ACMR $against -- $GIT_EXCLUDE_LIST )
56
78
57
79
if [ " $FILES " == " " ]; then
58
80
exit 0
59
81
fi
60
82
83
+
61
84
# Create temporary copy of staging area
62
85
if [ -e $TMP_STAGING ]; then
63
86
rm -rf $TMP_STAGING
@@ -80,13 +103,26 @@ if [ "$FILES_TO_CHECK" == "" ]; then
80
103
exit 0
81
104
fi
82
105
106
+
107
+ # #########
83
108
# Validate PHP CodeSniffer variables
109
+ # #########
110
+ if [ " $PHPCS_ACTIVE " != " 1" ]; then
111
+ PHPCS_ACTIVE=0
112
+ fi
113
+
84
114
if [ " $PHPCS_IGNORE " != " " ]; then
85
115
IGNORE=" --ignore=$PHPCS_IGNORE "
86
116
else
87
117
IGNORE=" "
88
118
fi
89
119
120
+ if [ " $PHPCS_CODING_STANDARD " != " " ]; then
121
+ PHPCS_CODING_STANDARD=" --standard=$PHPCS_CODING_STANDARD "
122
+ else
123
+ PHPCS_CODING_STANDARD=" "
124
+ fi
125
+
90
126
if [ " $PHPCS_SNIFFS " != " " ]; then
91
127
SNIFFS=" --sniffs=$PHPCS_SNIFFS "
92
128
else
@@ -105,7 +141,14 @@ else
105
141
IGNORE_WARNINGS=" "
106
142
fi
107
143
144
+
145
+ # #########
108
146
# Validate PHP Mess Detector variables
147
+ # #########
148
+ if [ " $PHPMD_ACTIVE " != " 1" ]; then
149
+ PHPMD_ACTIVE=0
150
+ fi
151
+
109
152
if [ " $PHPMD_OUTPUT_MODE " != " " ]; then
110
153
PHPMD_OUTPUT=" $PHPMD_OUTPUT_MODE "
111
154
else
@@ -130,57 +173,82 @@ else
130
173
PHPMD_EXCLUDE_LIST=" "
131
174
fi
132
175
176
+
177
+ # #########
133
178
# Copy contents of staged version of files to temporary staging area
134
179
# because we only want the staged version that will be commited and not
135
180
# the version in the working directory.
181
+ # #########
136
182
STAGED_FILES=" "
137
183
for FILE in $FILES_TO_CHECK
138
184
do
139
- ID=$( git diff-index --cached $against $FILE | cut -d " " -f4)
140
-
141
- # Create staged version of file in temporary staging area with the same
142
- # path as the original file so that the phpcs ignore filters can be applied.
143
- mkdir -p " $TMP_STAGING /$( dirname $FILE ) "
144
- git cat-file blob $ID > " $TMP_STAGING /$FILE "
145
- STAGED_FILES=" $STAGED_FILES $TMP_STAGING /$FILE "
185
+ ID=$( git diff-index --cached $against $FILE | cut -d " " -f4)
186
+ # #########
187
+ # Create staged version of file in temporary staging area with the same
188
+ # path as the original file so that the phpcs ignore filters can be applied.
189
+ # #########
190
+ mkdir -p " $TMP_STAGING /$( dirname $FILE ) "
191
+ git cat-file blob $ID > " $TMP_STAGING /$FILE "
192
+ STAGED_FILES=" $STAGED_FILES $TMP_STAGING /$FILE "
146
193
done
147
194
148
- echo " "
149
- tput setaf 7; echo " :: PHP CodeSniffer inspection :: "
150
- PHPCS_OUTPUT=$( $PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $STAGED_FILES )
151
- PHPCS_RETVAL=$?
152
195
153
- if [ $PHPCS_RETVAL -ne 0 ]; then
154
- tput setaf 1; echo " -> Issues found: "
155
- tput setaf 7;
156
- echo " $PHPCS_OUTPUT "
196
+ # #########
197
+ # CODE INSPECTION: PHP CodeSniffer
198
+ # #########
199
+ if [ " $PHPCS_ACTIVE " == " 1" ]; then
200
+ echo " "
201
+ tput setaf 12; echo " :: PHP CodeSniffer inspection :: "
202
+ PHPCS_OUTPUT=$( $PHPCS_BIN -s $IGNORE_WARNINGS $PHPCS_CODING_STANDARD $ENCODING $IGNORE $STAGED_FILES )
203
+ PHPCS_RETVAL=$?
157
204
158
- rm -rf $TMP_STAGING
205
+ if [ $PHPCS_RETVAL -ne 0 ]; then
206
+ tput setaf 1; echo " ✘ Issues found: "
207
+ tput setaf 7; echo " $PHPCS_OUTPUT "
159
208
160
- exit $PHPCS_RETVAL
209
+ rm -rf $TMP_STAGING
210
+
211
+ exit $PHPCS_RETVAL
212
+ else
213
+ tput setaf 2; echo " ✔ Inspection is OK!"
214
+ fi
161
215
else
162
- tput setaf 2; echo " -> Inspection is OK!"
216
+ echo " "
217
+ tput setaf 8; echo " ➔ PHP CodeSniffer inspection is OFF."
163
218
fi
164
219
165
- echo " "
166
- tput setaf 7; echo " :: PHP Mess Detector inspection :: "
167
- PHPMD_OUTPUT=$( $PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST )
168
- PHPMD_RETVAL=$?
169
220
170
- if [ $PHPMD_RETVAL -ne 0 ]; then
171
- tput setaf 1; echo " -> Issues found: "
172
- tput setaf 7; echo " $PHPMD_OUTPUT "
221
+ # #########
222
+ # CODE INSPECTION: PHP Mess Detector
223
+ # #########
224
+ if [ " $PHPMD_ACTIVE " == " 1" ]; then
225
+ echo " "
226
+ tput setaf 12; echo " :: PHP Mess Detector inspection :: "
227
+ PHPMD_OUTPUT=$( $PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST )
228
+ PHPMD_RETVAL=$?
173
229
174
- rm -rf $TMP_STAGING
230
+ if [ $PHPMD_RETVAL -ne 0 ]; then
231
+ tput setaf 1; echo " ✘ Issues found: "
232
+ tput setaf 7; echo " $PHPMD_OUTPUT "
175
233
176
- exit $PHPMD_RETVAL
234
+ rm -rf $TMP_STAGING
235
+
236
+ exit $PHPMD_RETVAL
237
+ else
238
+ tput setaf 2; echo " ✔ Inspection is OK!"
239
+ fi
177
240
else
178
- tput setaf 2; echo " -> Inspection is OK!"
241
+ echo " "
242
+ tput setaf 8; echo " ➔ PHP Mess Detector inspection is OFF."
179
243
fi
180
244
181
- tput setaf 7;
245
+ tput setaf 12;
246
+
247
+
182
248
183
249
rm -rf $TMP_STAGING
184
250
251
+
252
+
185
253
echo " "
186
254
exit 0;
0 commit comments