Skip to content

Instantly share code, notes, and snippets.

@apit
Created May 31, 2018 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apit/60795305d5a13fde93d0cd783b24e195 to your computer and use it in GitHub Desktop.
Save apit/60795305d5a13fde93d0cd783b24e195 to your computer and use it in GitHub Desktop.
#!/bin/bash
# PHP CodeSniffer pre-receive hook for git
#exit 0 #if you want to skip all validation
PHPCS_BIN="/c/apps/PHP/composer/phpcs"
PHPCS_IGNORE=*/Migrations/*,*/vendor/*
PHPCS_CODING_STANDARD="PSR2"
TMP_DIR=$(mktemp -d phpcs-pre-receive-hook.XXXXXXXX)
mkdir "$TMP_DIR/source"
# gathers all errors and sent to output at end
ERRORS=""
RETVAL=0
#gitolite style
refname="$1"
oldrev="$2"
newrev="$3"
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
echo "============================"
echo "Checking for PSR2 code style"
echo "============================"
echo ""
# <oldrev> <newrev> <refname>
#while read oldrev newrev ref; #clen-git style
#do
if [ "$oldrev" = "0000000000000000000000000000000000000000" ]
then
oldrev="HEAD"
fi
list=$(git diff-tree --name-only -r $oldrev..$newrev)
for file in ${list}; do
# dirty hack for create dir tree
mkdir -p $(dirname "$TMP_DIR/source/$file")
#echo $PHPCS_BIN --standard=$PHPCS_CODING_STANDARD $IGNORE "$TMP_DIR/source/$file"
git show ${newrev}:${file} 1>"$TMP_DIR/source/$file" 2>/dev/null || continue #skip deleted files
OUTPUT=$($PHPCS_BIN --standard=$PHPCS_CODING_STANDARD $IGNORE "$TMP_DIR/source/$file")
if [ "$?" -ne "0" ]; then
XERRORS="${ERRORS}${OUTPUT}"
ERRORS="${ERRORS}${file}"
RETVAL=1
fi
done
#done
if [ "$RETVAL" = "1" ]; then
echo "Please format these files:"
echo "$ERRORS"
else
echo "All good"
fi
# cleanup
rm -rf $TMP_DIR
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment