Last active
August 29, 2015 14:13
-
-
Save manojLondhe/a6e0a610746d416b6580 to your computer and use it in GitHub Desktop.
GIT - pre receive hook script file for PHPCS check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# PHP CodeSniffer pre-receive hook for git | |
# Save it as pre-receive in hooks folder. Give it executable permission | |
# Based on - https://gist.github.com/bor/1253485 | |
# use coding standart dir from local repo | |
PHPCS_DIR_LOCAL=0 | |
TMP_DIR=$(mktemp -d --tmpdir phpcs-pre-receive-hook.XXXXXXXX) | |
mkdir "$TMP_DIR/source" | |
# parse config | |
CONFIG_FILE=$(dirname $0)/config | |
if [ -e $CONFIG_FILE ]; then | |
. $CONFIG_FILE | |
fi | |
#Read config file and assign variables | |
if [ "$PHPCS_BIN" != "" ]; then | |
IGNORE="--ignore=$PHPCS_BIN" | |
else | |
IGNORE="" | |
fi | |
if [ "$PHPCS_CODING_STANDARD" != "" ]; then | |
CODING_STANDARD="$PHPCS_CODING_STANDARD" | |
else | |
CODING_STANDARD="" | |
fi | |
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 | |
# 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 | |
# prepare our standart rules | |
if [ $PHPCS_DIR_LOCAL = 1 ]; then | |
mkdir "$TMP_DIR/standart" | |
git archive HEAD $PHPCS_CODING_STANDARD | tar -x -C "$TMP_DIR/standart" | |
PHPCS_CODING_STANDARD="$TMP_DIR/standart/$PHPCS_CODING_STANDARD" | |
fi | |
# gathers all errors and sent to output at end | |
ERRORS="" | |
RETVAL=0 | |
echo "\033[0;33m" | |
echo "**************************************************************************************" | |
echo " Running Code Sniffer Checks..." | |
echo "**************************************************************************************" | |
while read oldrev newrev ref; | |
do | |
list=$(git diff-tree --name-only -r $oldrev..$newrev | grep -e '.php' -e '.phtml') | |
for file in ${list}; do | |
# dirty hack for create dir tree | |
mkdir -p $(dirname "$TMP_DIR/source/$file") | |
# Skip deleted files | |
git show ${newrev}:${file} 1>"$TMP_DIR/source/$file" 2>/dev/null || continue | |
OUTPUT=$($PHPCS_BIN -s --standard=$PHPCS_CODING_STANDARD --report-summary $ENCODING $IGNORE_WARNINGS -p $IGNORE "$TMP_DIR/source/$file") | |
if [ "$?" -ne "0" ]; then | |
ERRORS="${ERRORS}${OUTPUT}" | |
RETVAL=1 | |
fi | |
done | |
done | |
# cleanup | |
rm -rf $TMP_DIR | |
if [ -n "$ERRORS" ]; then | |
echo "\033[0;37m $ERRORS \033[0;38m" | |
echo "\033[0;31m" | |
echo "**************************************************************************************" | |
echo " :( :( Pre-receive Hook Failed: Please fix PHPCS errors to push changes." | |
echo "**************************************************************************************" | |
echo "\033[39m" | |
else | |
echo "\033[0;32m" | |
echo "**************************************************************************************" | |
echo " :) ;) Do your happy dance! PHPCS check passed! You rock!" | |
echo "**************************************************************************************" | |
echo "\033[39m" | |
fi | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment