Skip to content

Instantly share code, notes, and snippets.

@madeindjs
Last active March 16, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeindjs/1fd00a5c857dd3b395771bd3c0812ebc to your computer and use it in GitHub Desktop.
Save madeindjs/1fd00a5c857dd3b395771bd3c0812ebc to your computer and use it in GitHub Desktop.
Git hook to store in .git/hooks/ folder to check commit
#!/bin/bash
# .git/hooks/pre-commit
GREEN='\033[0;32m'
RED='\033[0;31m'
DEFAULT='\033[0m'
# loop on all commited PHP files
echo -e "\n${GREEN}Check PHP files${DEFAULT}\n"
for file in $(git diff --cached --name-only | grep -E '.php$') ; do
# first we verify that file exists
if [ -f $file ]; then
# check syntax & catch errors
if ($( php -l $file 1> /dev/null )); then
echo "[x] $file"
else
# file contains syntax error, so we'll stop programm & cancel commit
echo -e "${RED}[ ] ${file}${DEFAULT}"
exit 1
fi
fi
done
echo -e "\n${GREEN}Check unmerged files${DEFAULT}\n"
for file in $(git diff --cached --name-only) ; do
# first we verify that file exists
if [ -e $file ]; then
if grep '<<<<<<<' "$file" > /dev/null; then
echo -e "${RED}[ ] ${file}${DEFAULT}unmerged file!!"
exit 1
fi
echo "[x] $file"
fi
done
echo -e "\n${GREEN}Check forgetten debug${DEFAULT}\n"
for file in $(git diff --cached --name-only) ; do
if [ -e $file ]; then
if git diff --cached "$file" | grep -E "console\.log|var_dump|alert\(|die|exit" > /dev/null ; then
echo -e "${RED}[ ] ${file}${DEFAULT}"
exit 1
else
echo "[x] $file"
fi
fi
done
echo -e "\n${GREEN}Clean PSR${DEFAULT}\n"
# loop on all files commited
for file in $(git diff --cached --name-only) ; do
# first we verify that file exists
if [ -e $file ]; then
echo "[x] $file"
./vendor/bin/phpcbf --standard=build/phpcs.xml --extensions=php --ignore=autoload.php "$file" -q
fi
done
# echo -e "\n${GREEN}Unit Tests${DEFAULT}\n"
# laucun unit test if PHPunit configuration is available
# docker exec php5.6 /usr/local/apache2/htdocs/gac-report/vendor/bin/phpunit -c /usr/local/apache2/htdocs/gac-report/build/phpunit.xml --no-coverage --testsuite=Unitaires
@madeindjs
Copy link
Author

Install command

curl https://gist.githubusercontent.com/madeindjs/1fd00a5c857dd3b395771bd3c0812ebc/raw/0f9aedd587d6fac66393e054c99088012fbfb513/pre-commit > .git/hooks/pre-commit && chmod u+x .git/hooks/pre-commit

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