-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·165 lines (138 loc) · 4.76 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
#!/bin/bash
#Include utils script
source "$(dirname $(readlink -f $0))/utils.sh"
if ! $CONF_pre_commit_active ; then
#Hook not active
exit 0
fi
function main() {
codeSniffer
whiteSpace
debugCode
internationalization
}
#Check for coding standard errors introduced on this commit
function codeSniffer() {
if ! $CONF_pre_commit_cs_active ; then
#Code sniffing not active
return 0
fi
#-r so that read doesn't remove the backslash from line
while read -r line
do
status=$(printf "$line" | cut -f1)
fileName=$(printf "$line" | sed -re 's/^[A-Z]\s*(.*)/\1/' -e 's/^"(.*)"$/\1/')
sniffFile "$status" "$fileName"
done <<< "`git diff --staged --name-status`"
}
#Check for coding standards errors introduced on this commit on a specific file
function sniffFile() {
local status=$1
local fileName=$2
local prvVersion=
#File was deleted
if [ "$status" = "D" ]; then
return 0
fi
#File was modified
if [ "$status" = "M" ]; then
prvVersion=$(tempfile) || exit 1;
git show HEAD:"$fileName" > $prvVersion
fi
#File was moved
if [ "$status" = "R" ]; then
local mvFrom=$(printf "$fileName" | sed -r 's/(.*)\s->\s.*/\1/')
fileName=$(printf "$fileName" | sed -r 's/.*\s->\s(.*)/\1/')
prvVersion=$(tempfile) || exit 1;
git show HEAD:"$mvFrom" > $prvVersion
fi
#Store the result of phpcs execution (only the changed lines)
local phpcsResult=$(tempfile) || exit 1;
phpcs $CONF_pre_commit_cs_arguments "$fileName" | sed '1,5 d' | sed -n -e :a -e '1,4!{P;N;D;};N;ba' > $phpcsResult
if [ -n "$prvVersion" ]; then
#Previous version exist. Check if we introduced new errors
#An array with the number of the modified lines
local changedLines=($(getChangedLines $prvVersion $fileName))
#An array with the number of the current error lines
local currentErrorLines=($(cat "$phpcsResult" | egrep '([[:digit:]])+[[:blank:]]\|.*$' |
sed -r -e 's/^([[:blank:]])*([[:digit:]]+)*.*/\2/' | sort -n | uniq | tr '\n' ' '))
local errorLines=()
for line in "${changedLines[@]}"
do
isInArray $line "${currentErrorLines[@]}"
if [ $? = 0 ]; then
errorLines=("${errorLines[@]}" $line)
fi
done
if [ ${#errorLines[@]} = 0 ]; then
#No new errors were introduced
return 0
else
#Errors were introduced
errorLog=$errorLog"\n$lineSeparator""FILE: "$fileName"\n$lineSeparator"
for line in "${errorLines[@]}"
do
#Append all the new error lines to the error log
local result=$(cat "$phpcsResult" | egrep "^[[:space:]]$line[[:space:]].*$")
errorLog="$errorLog""$result\n"
done
fi
else
#Previous version doesn't exist, just check if new version has errors
local nErrors=$(cat "$phpcsResult" | wc -l)
if [ $nErrors = 0 ]; then
#No new errors were introduced
return 0
else
#Errors were introduced
errorLog=$errorLog"\n$lineSeparator""FILE: "$fileName"\n$lineSeparator"
local result=$(cat "$phpcsResult")
errorLog="$errorLog""$result\n"
fi
fi
}
function whiteSpace() {
if ! $CONF_pre_commit_ws_active ; then
#White space verification not active
return 0
fi
git diff --staged --check &> /dev/null
if [ $? != 0 ]; then
errorLog="$errorLog"$lineSeparator"Whitespace errors:\n"$lineSeparator
errorLog="$errorLog"$(git diff --staged --check)
fi
}
#Check if we are about to commit debug code
function debugCode()
{
if ! $CONF_pre_commit_debug_active ; then
#Check for debug code is not active
return 0
fi
checkForRegexCodeOnStage $CONF_pre_commit_debug_regex
if [ -n "$lastCommandResult" ]; then
errorLog="$errorLog"$lineSeparator"Debug code is about to be commited:\n"$lineSeparator
errorLog="$errorLog"$lastCommandResult"\n"
fi
}
#Check if we are about to commit internationalization sentences
function internationalization()
{
if ! $CONF_pre_commit_internationalization_active ; then
#Check for internationalization code is not active
return 0
fi
checkForRegexCodeOnStage $CONF_pre_commit_internationalization_regex
if [ -n "$lastCommandResult" ]; then
errorLog="$errorLog"$lineSeparator"Internacionalization code has been commited:\n"$lineSeparator
errorLog="$errorLog"$lastCommandResult"\n"
fi
}
lineSeparator=$(printf "%$(tput cols)s\n"|tr ' ' '=')
errorLog=
main
if [ -n "$errorLog" ]; then
printf "$errorLog"
exit 1
fi
exit 0