-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit
62 lines (52 loc) · 1.39 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
#!/bin/sh
# If currently in a merge don't check files.
MERGE=$(git rev-parse -q --verify MERGE_HEAD)
if [[ ! -z "$MERGE" ]]
then
# Currently in merge, let git handle the rest.
exit 0
fi
# Get list of staged files excluding deleted files (lowercase d in --diff-filter).
FILES=$(git diff --cached --name-only --diff-filter=d | grep \\.php)
if [[ $(echo $FILES | wc -c) -eq "1" ]]
then
# No files staged, let git handle the rest.
exit 0
fi
GREEN=''
LRED=''
NC=''
# Check if colors are supported.
if [[ $(tput colors) -gt "2" ]]
then
GREEN='\033[0;32m'
LRED='\033[1;31m'
NC='\033[0m'
fi
# Use local or global phpcs. Local has the priority.
GIT_DIR=$(git rev-parse --show-toplevel)
PHPCS="phpcs"
if [ -f "$GIT_DIR/vendor/bin/phpcs" ]; then
PHPCS="$GIT_DIR/vendor/bin/phpcs"
fi
# Generate diff file from phpcs.
echo ${GREEN}Creating patch for staged files\(can take some time\).${NC}
$PHPCS -p --report-diff=phpcspatch.diff $FILES
# Check if there is anything to patch.
WORDS=$( cat phpcspatch.diff | wc -m )
if [[ "$WORDS" -gt "1" ]]
then
# Apply diff file.
echo ${GREEN}Patching staged files.${NC}
patch -p0 -ui phpcspatch.diff
# Remove the diff file.
echo ${GREEN}Removing patch file.${NC}
rm phpcspatch.diff
echo ${LRED}Please stage the new changes.${NC}
exit 1
else
# Nothing to patch so git can commit as normal.
echo ${GREEN}Nothing to patch.${NC}
rm phpcspatch.diff
exit 0
fi