Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pre-commit execute linter only if php files have been staged #12031

Merged
merged 4 commits into from Jan 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 32 additions & 12 deletions .github/contrib/pre-commit
@@ -1,8 +1,12 @@
#!/bin/sh

RED=`tput setaf 1`
RESET=`tput sgr0`

PHP_BINARY=`which php`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep '\.php$'`
PROJECT_PATH=$(cd "$( dirname "$0" )/../../" && pwd)
CS_FIXER="./vendor/bin/php-cs-fixer fix"

# Determine if a file list is passed
if [ "$#" -eq 1 ]
Expand All @@ -15,19 +19,35 @@ then
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}

if [ -n "$SFILES" ]
then
echo "Checking PHP Lint..."
for FILE in $SFILES
do
$PHP_BINARY -l -d display_errors=0 $PROJECT_PATH/$FILE
if [ $? != 0 ]
then
echo "Error(s), please fix it before commit."
exit 1
fi
done

echo "Running PHP CS Fixer..."

$PHP_BINARY $CS_FIXER --dry-run

echo "Checking PHP Lint..."
for FILE in $SFILES
do
$PHP_BINARY -l -d display_errors=0 $PROJECT_PATH/$FILE
if [ $? != 0 ]
then
echo "Error(s), please fix it before commit."
exit 1
fi
done
printf "\n${RED}Some files need to be fixed!${RESET}\n\n"
echo "Please run the following script to fix them. Afterwards, add the changed files and commit again:"
echo "

php $CS_FIXER

echo "Running PHP CS Fixer..."
$PHP_BINARY ./vendor/bin/php-cs-fixer fix
"
exit 2
fi

exit $?
else
echo "No modified PHP files to check."
fi