Skip to content

Instantly share code, notes, and snippets.

@AlcidesRC

AlcidesRC/pre-commit

Last active Mar 12, 2021
Embed
What would you like to do?
Ejemplo de pre-commit Git hook para aplicaciones PHP y Docker
#!/bin/bash
# Run '$(chmod +x pre-commit; ln -s .git/hooks/pre-commit pre-commit)' to install
RESET='\e[0m'
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
SERVICE_NAME='api'
#----------------------------------------------------------------------------------------------------------------------
# FUNCTIONS
#----------------------------------------------------------------------------------------------------------------------
function __displayBox() #(emoji, color, message)
{
caption=$(printf "%-72s" "${3}")
echo -e "${2}"
echo -e "┌─────────────────────────────────────────────────────────────────────────────┐"
echo -e "${1} ${RESET}${caption}${2}"
echo -e "└─────────────────────────────────────────────────────────────────────────────┘"
echo -e "${RESET}"
}
#----------------------------------------------------------------------------------------------------------------------
function __checkPhpLint() #(file)
{
task=$(printf "%-40s" "PHP Linter")
output=$(docker-compose exec ${SERVICE_NAME} php -l ${1})
RETVAL=$?
if [[ $RETVAL != 0 ]]
then
hasErrors=1
echo -e " 🔴 ${task}"
echo -e ""
echo -e "${output}"
else
echo -e " 🟢 ${task}"
fi
if [[ $hasErrors == 1 ]]
then
__displayBox 👹 ${RED} 'COMMIT IS NOT ALLOWED!'
exit 1
fi
}
#----------------------------------------------------------------------------------------------------------------------
function __checkPhpCodeBeautifierAndFixer() #(file)
{
task=$(printf "%-40s" "PHPCBF - PHP Code Beautifier and Fixer")
output=$(docker-compose exec ${SERVICE_NAME} ./vendor/bin/phpcbf --standard=PSR12 ${1})
RETVAL=$?
if [[ $RETVAL != 0 ]]
then
hasErrors=1
echo -e " 🔴 ${task}"
echo -e "${output}";
else
echo -e " 🟢 ${task}"
fi
if [[ $hasErrors == 1 ]]
then
__displayBox 👹 ${RED} 'COMMIT IS NOT ALLOWED!'
exit 1
fi
}
#----------------------------------------------------------------------------------------------------------------------
function __checkPhpStan() #(file)
{
task=$(printf "%-40s" "PHPSTAN - PHP Static Analyzer")
output=$(docker-compose exec ${SERVICE_NAME} ./vendor/bin/phpstan analyse --memory-limit=2G ${1})
RETVAL=$?
if [[ $RETVAL != 0 ]]
then
hasErrors=1
echo -e " 🔴 ${task}"
echo -e "";
echo -e "${output}";
else
echo -e " 🟢 ${task}"
fi
if [[ $hasErrors == 1 ]]
then
__displayBox 👹 ${RED} 'COMMIT IS NOT ALLOWED!'
exit 1
fi
}
#----------------------------------------------------------------------------------------------------------------------
# MAIN LOGIC
#----------------------------------------------------------------------------------------------------------------------
phpFiles=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep ".php$");
for file in $phpFiles
do
echo -e "- Checking [ ${YELLOW}$file${RESET} ]"
__checkPhpLint $file
__checkPhpCodeBeautifierAndFixer $file
if [ "$1" != "--no-verify" ]; then
__checkPhpStan $file
fi
done
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# At this point all checkpoints were successfully passed
__displayBox 🚀 ${GREEN} 'GIT COMMIT IS ALLOWED!'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment