Last active
June 5, 2018 12:22
-
-
Save emgk/ab45140809d43b5184c8ea53eb40acbe to your computer and use it in GitHub Desktop.
Local phpcs pre-commit script
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/bash | |
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"` | |
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php` | |
# parse config | |
CONFIG_FILE=$(dirname $0)/config | |
if [ -e $CONFIG_FILE ]; then | |
. $CONFIG_FILE | |
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 | |
# stolen from template file | |
if git rev-parse --verify HEAD | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- ) | |
if [ "$FILES" == "" ]; then | |
exit 0 | |
fi | |
# Determine if a file list is passed | |
if [ "$#" -eq 1 ] | |
then | |
oIFS=$IFS | |
IFS=' | |
' | |
SFILES="$1" | |
IFS=$oIFS | |
fi | |
SFILES=${SFILES:-$STAGED_FILES_CMD} | |
if [ "$FILES" != "" ] | |
then | |
echo "Running Code Sniffer..." | |
# [Make change] Change the path to your phpcs directory | |
C:/wamp/bin/php/php5.4.3/phpcs --standard=WordPress --encoding=utf-8 -n -p $FILES | |
# if [ $? != 0 ] | |
# then | |
# echo "Running Code Sniffer again..." | |
# C:/wamp/bin/php/php5.4.3/phpcs --standard=WordPress --encoding=utf-8 -n -p $FILES | |
# if [ $? != 0 ] | |
# then | |
# echo "Errors found not fixable automatically" | |
# exit 1 | |
# fi | |
# fi | |
fi | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment