From 38e58abb7929f40fa7c523846e29f7384a423c3d Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Thu, 29 Jul 2010 23:55:44 +0800 Subject: [PATCH] run phpcs on staged version of file(s) --- phpcs-pre-commit/pre-commit | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/phpcs-pre-commit/pre-commit b/phpcs-pre-commit/pre-commit index c74f33f..0d207d3 100755 --- a/phpcs-pre-commit/pre-commit +++ b/phpcs-pre-commit/pre-commit @@ -2,12 +2,15 @@ # PHP CodeSniffer pre-commit hook for git # # @author Soenke Ruempler +# @author Sebastian Kaspari # # see the README PHPCS_BIN=/usr/bin/phpcs PHPCS_CODING_STANDARD=PEAR PHPCS_IGNORE= +OUTPUT="" +PROHIBIT_COMMIT=0 # parse config CONFIG_FILE=$(dirname $0)/config @@ -62,12 +65,24 @@ else IGNORE="" fi -OUTPUT=$($PHPCS_BIN -s --standard=$PHPCS_CODING_STANDARD $IGNORE $FILES_TO_CHECK) -RETVAL=$? +# Run phpcs on staged content of every staged file +for FILE in $FILES_TO_CHECK +do + ID=`git diff-index --cached HEAD $FILE | cut -d " " -f4` + git cat-file blob $ID > phpcs.tmp + PHPCS_OUTPUT=`$PHPCS_BIN -s --standard=$PHPCS_CODING_STANDARD $IGNORE phpcs.tmp` + RETVAL=$? + if [ $RETVAL -ne 0 ]; then + OUTPUT="SRC: $FILE$PHPCS_OUTPUT\n\n$OUTPUT" + PROHIBIT_COMMIT="$RETVAL" + fi + rm phpcs.tmp +done -if [ $RETVAL -ne 0 ]; then - echo "$OUTPUT" | less +if [ $PROHIBIT_COMMIT -ne 0 ]; then + echo -e "$OUTPUT" | less fi -exit $RETVAL +exit $PROHIBIT_COMMIT +