-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·158 lines (129 loc) · 3.86 KB
/
pre-commit
File metadata and controls
executable file
·158 lines (129 loc) · 3.86 KB
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
#!/bin/bash
# PHP CodeSniffer pre-commit hook for git
#
# based on https://github.com/s0enke/git-hooks
# @author Fabian Derschatta <fabian.derschatta@gtotaralearning.com>
#
# see the README
# In case we symlinked the hook we want to determine the real path
SCRIPTPATH="$(dirname $(readlink "$0"))"
PROJECTPATH=$(cd $SCRIPTPATH && cd ..; pwd -P)
GIT_PROJECT_PATH="$( cd "$(dirname "$0")" ; cd ../../; pwd -P )"
GHERKIN_LINT_PATH="$GIT_PROJECT_PATH/node_modules/.bin/gherkin-lint"
GHERKIN_LINT_CONFIG="$GIT_PROJECT_PATH/.gherkin-lintrc"
# Set some defaults
PHPCS_BIN=$PROJECTPATH/vendor/bin/phpcs
DIFFFILTER_BIN=$PROJECTPATH/vendor/bin/diffFilter
PHPCS_CODING_STANDARD=$PROJECTPATH/src/Standards/Totara/ruleset.xml
PHPCS_IGNORE=
PHPCS_FILE_EXTENSIONS=php
BEHAT_RUN_LINT=1
TMP_DIFF_FILES=/tmp/pre-commit-files.txt
TMP_DIFF=/tmp/pre-commit-diff.txt
TMP_JSON=/tmp/pre-commit-phpcs.json
# parse config
CONFIG_FILE=$SCRIPTPATH/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# if changed lines is active we require diffilter
if [ "$PHPCS_CHANGED_LINES_ONLY" == "1" ]; then
if [ ! -x $DIFFFILTER_BIN ]; then
echo "Difffilter bin not found or executable -> $DIFFFILTER_BIN"
exit 1
fi
fi
# make sure we have a valid HEAD
COMMIT_HASH=$(git rev-parse --verify HEAD)
if [ "$COMMIT_HASH" != "" ]; then
against=HEAD
else
echo "Invalid HEAD revision. Exiting."
exit 1
fi
# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
git diff-index --name-only --cached --diff-filter=ACMR $against -- > $TMP_DIFF_FILES
# if file is empty (means no changes) exit successfully
if [ ! -s $TMP_DIFF_FILES ]; then
rm -f $TMP_DIFF_FILES
exit 0
fi
BEHAT_FAILED=0
if [ "$BEHAT_RUN_LINT" == "1" ] && [ -e $GHERKIN_LINT_PATH ] && [ -e $GHERKIN_LINT_CONFIG ]; then
BEHAT_FILES=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- "*.feature")
for BEHAT_FILE in $BEHAT_FILES
do
$GHERKIN_LINT_PATH -c $GHERKIN_LINT_CONFIG $BEHAT_FILE
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
BEHAT_FAILED=1
fi
done
fi
# Set the params for Code Sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
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
if [ "$PHPCS_FILE_EXTENSIONS" != "" ]; then
EXTENSIONS="$PHPCS_FILE_EXTENSIONS"
else
EXTENSIONS=""
fi
PHPCS_COMMAND="$PHPCS_BIN -s $IGNORE_WARNINGS $REPORT --extensions=$EXTENSIONS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS --file-list=$TMP_DIFF_FILES"
if [ "$PHPCS_CHANGED_LINES_ONLY" == "1" ]; then
git diff HEAD > $TMP_DIFF
JSON=$($PHPCS_COMMAND --report=json)
echo $JSON > $TMP_JSON
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "Error writing phpcs json report to temp dir"
rm -f $TMP_DIFF_FILES
rm -f $TMP_DIFF
exit 1
fi
# diffFilter gives us only the violations for the changed lines
OUTPUT="$($DIFFFILTER_BIN --phpcs $TMP_DIFF $TMP_JSON)"
else
# This will give us the violations for the complete content of the changed files
OUTPUT=$($PHPCS_COMMAND)
fi
RETVAL=$?
rm -f $TMP_DIFF_FILES
rm -f $TMP_DIFF
rm -f $TMP_JSON
if [ $RETVAL -ne 0 ]; then
echo "$OUTPUT"
fi
if [ $BEHAT_FAILED == 1 ]; then
exit 1
fi
exit $RETVAL