Skip to content

Commit 8c6f18c

Browse files
committed
Creating pre-commit file
0 parents  commit 8c6f18c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

pre-commit

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/bin/bash
2+
##########
3+
# Git Pre-Commit file for PHP projects
4+
###
5+
#
6+
# This hook performs the following validation:
7+
# - PHP CodeSniffer
8+
# - PHP-CS Fixer (only in --dry-run mode)
9+
# - PHP Mess Detector
10+
#
11+
# Based on https://gist.github.com/codemis/8225337
12+
#
13+
# @author Davi Marcondes Moreira <[email protected]>
14+
#
15+
##########
16+
17+
PHPCS_BIN=/usr/bin/phpcs
18+
PHPCSFIXER_BIN=/usr/bin/php-cs-fixer
19+
PHPMD_BIN=/usr/bin/phpmd
20+
PHPCS_CODING_STANDARD=PEAR
21+
PHPCS_IGNORE=
22+
TMP_STAGING=".tmp_staging"
23+
24+
# parse config
25+
CONFIG_FILE=$(dirname $0)/config
26+
if [ -e $CONFIG_FILE ]; then
27+
. $CONFIG_FILE
28+
fi
29+
30+
# simple check if code sniffer is set up correctly
31+
if [ ! -x $PHPCS_BIN ]; then
32+
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
33+
exit 1
34+
fi
35+
36+
# stolen from template file
37+
if git rev-parse --verify HEAD
38+
then
39+
against=HEAD
40+
else
41+
# Initial commit: diff against an empty tree object
42+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
43+
fi
44+
45+
# this is the magic:
46+
# retrieve all files in staging area that are added, modified or renamed
47+
# but no deletions etc
48+
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )
49+
50+
if [ "$FILES" == "" ]; then
51+
exit 0
52+
fi
53+
54+
# create temporary copy of staging area
55+
if [ -e $TMP_STAGING ]; then
56+
rm -rf $TMP_STAGING
57+
fi
58+
mkdir $TMP_STAGING
59+
60+
# match files against whitelist
61+
FILES_TO_CHECK=""
62+
for FILE in $FILES
63+
do
64+
echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
65+
RETVAL=$?
66+
if [ "$RETVAL" -eq "0" ]
67+
then
68+
FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
69+
fi
70+
done
71+
72+
if [ "$FILES_TO_CHECK" == "" ]; then
73+
exit 0
74+
fi
75+
76+
# execute the code sniffer
77+
if [ "$PHPCS_IGNORE" != "" ]; then
78+
IGNORE="--ignore=$PHPCS_IGNORE"
79+
else
80+
IGNORE=""
81+
fi
82+
83+
if [ "$PHPCS_SNIFFS" != "" ]; then
84+
SNIFFS="--sniffs=$PHPCS_SNIFFS"
85+
else
86+
SNIFFS=""
87+
fi
88+
89+
if [ "$PHPCS_ENCODING" != "" ]; then
90+
ENCODING="--encoding=$PHPCS_ENCODING"
91+
else
92+
ENCODING=""
93+
fi
94+
95+
if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
96+
IGNORE_WARNINGS="-n"
97+
else
98+
IGNORE_WARNINGS=""
99+
fi
100+
101+
# Copy contents of staged version of files to temporary staging area
102+
# because we only want the staged version that will be commited and not
103+
# the version in the working directory
104+
STAGED_FILES=""
105+
for FILE in $FILES_TO_CHECK
106+
do
107+
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
108+
109+
# create staged version of file in temporary staging area with the same
110+
# path as the original file so that the phpcs ignore filters can be applied
111+
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
112+
git cat-file blob $ID > "$TMP_STAGING/$FILE"
113+
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
114+
done
115+
116+
OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES)
117+
RETVAL=$?
118+
119+
# delete temporary copy of staging area
120+
rm -rf $TMP_STAGING
121+
122+
if [ $RETVAL -ne 0 ]; then
123+
echo "$OUTPUT" | less
124+
fi
125+
126+
exit $RETVAL

0 commit comments

Comments
 (0)