Skip to content

Instantly share code, notes, and snippets.

@popovserhii
Created June 22, 2020 13:33
Show Gist options
  • Save popovserhii/7bbeadbe87fa3dbc6e81be42b2511ebd to your computer and use it in GitHub Desktop.
Save popovserhii/7bbeadbe87fa3dbc6e81be42b2511ebd to your computer and use it in GitHub Desktop.
PHP Code Sniffer: git pre commit hook
#!/bin/bash
# .git-hook/pre-commit
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
echo -e "\nValidating PHPCS:\n"
# Check for phpcs
which ./bin/phpcs &> /dev/null
if [[ "$?" == 1 ]]; then
echo -e "\t\033[41mPlease install PHPCS\033[0m"
exit 1
fi
RULESET=./phpcs.xml
for FILE in $STAGED_FILES
do
./bin/phpcs --standard="$RULESET" "$FILE"
if [[ "$?" == 0 ]]; then
echo -e "\t\033[32mPHPCS Passed: $FILE\033[0m"
else
echo -e "\t\033[41mPHPCS Failed: $FILE\033[0m"
PASS=false
fi
done
echo -e "\nPHPCS validation completed!\n"
if ! $PASS; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass PHPCS but do not. Please fix the PHPCS errors and try again.\n"
exit 1
else
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
#!/bin/sh
# .git-hook/pre-commit-install.bash
# Symlink the git pre-commit hook to its destination.
if [ ! -h ".git/hooks/pre-commit" ] ; then
ln -s "../../.git-hooks/pre-commit" ".git/hooks/pre-commit"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment