-
Notifications
You must be signed in to change notification settings - Fork 13
/
pre-commit
executable file
·100 lines (85 loc) · 2.82 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
#!/bin/sh
#title :pre-commit.sh
#description :This script stops user to commit files if it does not satisfy codesniffer filter.
#author :Mukta
#date :2014-4-11
#usage :add the script in .git/hooks folder. Script should have executable permissions.
#==================================================================================================
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
# Parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# Assign files to SFILES variable
SFILES=${SFILES:-$STAGED_FILES_CMD}
for FILE in $SFILES
do
FILES="$FILES $PROJECT/$FILE"
done
# Read config file and assign variables
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
if [ "$PHPCS_ENCODING" != "" ]; then
ENCODING="--encoding=$PHPCS_ENCODING"
else
ENCODING=""
fi
if [ "$PHPCS_IGNORE_WARNINGS" != "1" ]; then
IGNORE_WARNINGS=""
else
IGNORE_WARNINGS="-n"
fi
if [ "$PHPCS_CODING_STANDARD" != "" ]; then
CODING_STANDARD="$PHPCS_CODING_STANDARD"
else
CODING_STANDARD=""
fi
if [ "$FILES" != "" ]
then
echo "\033[0;33m############ Running PHP Syntax Check ##########\033[1;37m"
for PHP_FILE in $SFILES
do
OUTPUT=$(php -l $PHP_FILE)
# if last command successful. Answer is 0 which means 'yes'.
if [ $? != 0 ]
then
#echo "\033[0;37m $ERRORS \033[0;38m"
echo "\033[0;31m"
echo "**************************************************************************************"
echo $OUTPUT
echo "PHP syntax errors detected. Please fix syntax errors to push changes."
echo "**************************************************************************************"
echo "\033[39m"
exit 1
else
echo $OUTPUT
fi
done
fi
if [ "$FILES" != "" ]
then
echo "\033[0;32m############ All good with PHP syntax! ############\033[0;39m"
echo
echo "\033[0;33m############ Running Code Sniffer Checks ############\033[1;37m"
#/usr/bin/phpcs --standard=Joomla --encoding=utf-8 -n -p $FILES
$PHPCS_BIN --standard=$CODING_STANDARD --report-summary --report-width=225 $ENCODING $IGNORE_WARNINGS -p --extensions=php $IGNORE $FILES
# if last command successful. Answer is 0 which means 'yes'.
if [ $? != 0 ]
then
echo "\033[0;37m $ERRORS \033[0;38m"
echo "\033[0;31m"
echo "**************************************************************************************"
echo " :( :( Pre-commit Hook Failed: Please fix PHPCS errors to push changes."
echo "**************************************************************************************"
echo "\033[39m"
exit 1
else
echo "\033[0;32m############ Do your happy dance! PHPCS check passed! ############\033[0;39m"
fi
fi
exit $?