Skip to content

Instantly share code, notes, and snippets.

@omerkoseoglu
Created March 10, 2023 19:55
Show Gist options
  • Save omerkoseoglu/8cdf614caf7b2f97134cd55173c831a2 to your computer and use it in GitHub Desktop.
Save omerkoseoglu/8cdf614caf7b2f97134cd55173c831a2 to your computer and use it in GitHub Desktop.
Laravel git precommit hook
#!/bin/sh
spin='-\|/'
i=0
STAGED_FILES=$(git diff --name-only --diff-filter=ACM | grep ".php\{0,1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
echo "\nValidating PHPCS:\n"
# Check for phpcs
which ./vendor/bin/phpcs &> /dev/null
if [[ "$?" == 1 ]]; then
echo "\t\033[41mPlease install PHPCS\033[0m"
exit 1
fi
RULESET=./phpcs.xml
for FILE in $STAGED_FILES
do
i=$(( (i+1) %4 ))
printf "\r${spin:$i:1}"
./vendor/bin/phpcs -n --runtime-set ignore_warnings_on_exit true --standard="$RULESET" "$FILE"
if [[ "$?" != 0 ]]; then
echo "\t\033[41mPHPCS Failed: $FILE\033[0m"
PASS=false
fi
done
printf "\rPHPCS validation completed!\n"
if ! $PASS; then
echo "\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 "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
@omerkoseoglu
Copy link
Author

chmod +x git-hooks/pre-commit

cp git-hooks/pre-commit .git/hooks/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment