-
-
Save onwp/3fe9471a297e93472cc0f50936b89c41 to your computer and use it in GitHub Desktop.
Git pre-commit hook for syntax checking before committing
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 | |
# | |
# PHP Syntax linter, checks syntax before commit actually happens | |
# Save this file in your git project as .git/hooks/pre-commit and make sure it's executable | |
# PHP lint command, modify if necessary | |
LINT='/usr/bin/php -l' | |
files=$(git diff --cached --name-only --diff-filter=ACM | grep "\.php$") | |
if [ "$files" = "" ]; then | |
exit 0 | |
fi | |
pass=true | |
echo -e "\nLinting PHP:\n" | |
for file in ${files}; do | |
result=$($LINT ${file} | grep "No syntax errors detected in ${file}") | |
if [ "$result" != "" ]; then | |
echo -e "\t\033[32mPassed: ${file}\033[0m" | |
else | |
echo -e "\t\033[31mFailed: ${file}\033[0m" | |
pass=false | |
fi | |
done | |
echo -e "\nPHP lint complete\n" | |
if ! $pass; then | |
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass PHP lint but do not. Please fix the errors and try again.\n" | |
exit 1 | |
else | |
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment