Last active
March 16, 2020 10:47
-
-
Save madeindjs/1fd00a5c857dd3b395771bd3c0812ebc to your computer and use it in GitHub Desktop.
Git hook to store in .git/hooks/ folder to check commit
This file contains hidden or 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 | |
# .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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install command