Created
September 11, 2014 07:12
-
-
Save zigo928/d7a2ea19849782ef0616 to your computer and use it in GitHub Desktop.
git set php code style script
This file contains hidden or 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 | |
#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