Skip to content

Instantly share code, notes, and snippets.

@paunin
Last active August 5, 2017 11:07
Show Gist options
  • Save paunin/64f8fde6e7e70c8695ad to your computer and use it in GitHub Desktop.
Save paunin/64f8fde6e7e70c8695ad to your computer and use it in GitHub Desktop.
pre-receive Code Sniffer
#!/bin/sh
# PHP CodeSniffer pre-receive hook for git
#exit 0 #if you want to skip all validation
PHPCS_BIN="phpcs"
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"
# <oldrev> <newrev> <refname>
#while read oldrev newrev ref; #clen-git style
#do
if [ "$oldrev" = "0000000000000000000000000000000000000000" ]
then
oldrev="HEAD"
#oldrev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
fi
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")
git show ${newrev}:${file} 1>"$TMP_DIR/source/$file" 2>/dev/null || continue #skip deleted files
OUTPUT=$($PHPCS_BIN --standard=$PHPCS_CODING_STANDARD "$TMP_DIR/source/$file")
if [ "$?" -ne "0" ]; then
ERRORS="${ERRORS}${OUTPUT}"
RETVAL=1
fi
done
#done
# cleanup
rm -rf $TMP_DIR
echo "$ERRORS"
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment