Created
March 14, 2015 16:42
-
-
Save lfalmeida/b74acebf5f088c81bcb8 to your computer and use it in GitHub Desktop.
Git Hook pre commit
This file contains 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/sh | |
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"` | |
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php` | |
CODESNIFER_RUNS=0 | |
# Determine if a file list is passed | |
if [ "$#" -eq 1 ] | |
then | |
oIFS=$IFS | |
IFS=' | |
' | |
SFILES="$1" | |
IFS=$oIFS | |
fi | |
SFILES=${SFILES:-$STAGED_FILES_CMD} | |
abort() { | |
echo "" | |
echo "-------------------------------------------------" | |
echo "Corrija os problemas acima antes de fazer commit!" | |
echo "-------------------------------------------------" | |
exit $1 | |
} | |
runSyntaxCheck () { | |
echo "Executando checagem de sintaxe..." | |
for FILE in $SFILES | |
do | |
OUTPUT=$(php -l -d display_errors=0 $PROJECT/$FILE) | |
RETVAL=$? | |
if [ $RETVAL -ne 0 ]; then | |
echo "$OUTPUT" | |
abort $RETVAL | |
fi | |
FILES="$FILES $PROJECT/$FILE" | |
done | |
} | |
runCodeSnifer () { | |
if [ "$FILES" != "" ] | |
then | |
echo "Executando Code Sniffer..." | |
OUTPUT=$(./vendor/bin/phpcbf --colors --standard=PSR2 --encoding=utf-8 -p $FILES) | |
RETVAL=$? | |
if [ $RETVAL -ne 0 ]; then | |
if [ $CODESNIFER_RUNS -gt 3 ]; then | |
./vendor/bin/phpcs --colors --standard=PSR2 --encoding=utf-8 -p $FILES | |
abort $RETVAL | |
else | |
CODESNIFER_RUNS=$(($CODESNIFER_RUNS+1)) | |
echo "Tentando corrigir erros encontrados pelo Code Sniffer..." | |
runCodeSnifer | |
fi | |
fi | |
fi | |
} | |
runUnitTests () { | |
if [ "$FILES" != "" ] | |
then | |
echo "Executando Testes Unitários..." | |
OUTPUT=$(php public/index.php humusphpunit) | |
RETVAL=$? | |
if [ $RETVAL -ne 0 ]; then | |
echo "$OUTPUT" | |
abort $RETVAL | |
fi | |
fi | |
} | |
init () { | |
runSyntaxCheck | |
runCodeSnifer | |
runUnitTests | |
if [ $RETVAL -eq 0 ]; then | |
echo "" | |
echo "-------------------------------------------------" | |
echo "Commit realizado com sucesso" | |
echo "-------------------------------------------------" | |
echo "" | |
fi | |
exit $? | |
} | |
init | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment