Last active
August 29, 2015 14:22
-
-
Save felipecwb/818405408192d83dd927 to your computer and use it in GitHub Desktop.
My new git pre-commit hook after an outage.
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
#!/usr/bin/env bash | |
set -u | |
CRED="\033[0;31m" | |
CYELLOW="\033[1;33m" | |
CBLUE="\033[0;34m" | |
CGREEN="\033[0;32m" | |
CCYAN="\033[0;36m" | |
CDEFAULT="\033[0m" | |
col=`echo "$(tput cols) - 12" | bc` | |
echo -e \ | |
"+---------------------------------------------------------------+ | |
| ${CRED}GIT PRE-COMMIT HOOK${CDEFAULT} | | |
| | | |
| @author: Felipe Fracisco <[email protected]> | | |
| | | |
| ${CBLUE}why it? - Created after a critical outage.${CDEFAULT} | | |
+---------------------------------------------------------------+\n" | |
if [ $(git diff --cached --name-only | wc -l) -eq "0" ]; then | |
echo -e "${CBLUE}...Nothing to Commit...${CDEFAULT}\n" | |
exit 0 | |
fi | |
# reading changed files | |
phpfiles=$(git diff --cached --name-only | grep '.php$') | |
echo -e "${CCYAN}********** CHANGED FILES ************${CDEFAULT}" | |
git status -s | |
echo -e "\n${CCYAN}********** PHP LINT *****************${CDEFAULT}" | |
for file in $phpfiles; | |
do | |
if [ ! -f $file ]; then | |
continue | |
fi | |
output=$(php -l $file 2>&1) | |
if [ $? -eq 0 ]; then | |
echo -e "${CGREEN}Ok: $file ${CDEFAULT}" | |
continue | |
fi | |
echo -e "${CRED}!!!!!!! Syntax Error: !!!!!!${CDEFAULT}" | |
echo -e "$output" | |
exit 1 | |
done | |
echo -e "\n${CCYAN}******* PHP MESS DETECTOR LOG *******${CDEFAULT}" | |
for file in $phpfiles; | |
do | |
if [ ! -f $file ]; then | |
continue | |
fi | |
echo -e "${CYELLOW}$file:${CDEFAULT}" | |
# phpmd in my path | |
phpmd $file text design,unusedcode --strict \ | |
| sed "s/$(pwd | sed 's/\//\\\//g')\///" \ | |
| awk "/\\s+/{ | |
line = substr(\$1, index(\$1, \":\") + 1); | |
message = substr(\$0, index(\$0, \$2)); | |
printf \"%d\\t|\", line; | |
if (length(message) > ($col + 3)) { | |
printf \"%s...\\n\", substr(message, 0, $col); | |
} else { | |
print message; | |
} | |
}" | |
done | |
echo -e "\n${CCYAN}********** DIE, EXIT, etc ***********${CDEFAULT}" | |
for file in $phpfiles; | |
do | |
if [ ! -f $file ]; then | |
continue | |
fi | |
output=$(grep -niP '(var_dump|print_r|exit|die)' $file 2> /dev/null) | |
if [ $? -ne 0 ]; | |
then | |
echo -e "${CGREEN}Ok: $file ${CDEFAULT}" | |
continue | |
fi | |
echo -e "${CRED}Error: ${CYELLOW}$file${CDEFAULT}" | |
echo -e "${CBLUE}n. line\t| src. line${CDEFAULT}" | |
echo -e "${CBLUE}--------+-----------------------------------${CDEFAULT}" | |
awk -F ':' "{ | |
printf \"%d\\t|\", \$1; # line column | |
src = substr(\$0, index(\$0, \$2)); | |
gsub(\"\\t\", \" \", src); | |
if (length(src) > ($col + 3)) { | |
printf \"%s...\\n\", substr(src, 0, $col); | |
} else { | |
print src; | |
} | |
}" <<< "$output" | |
echo "" | |
done | |
echo -e "${CBLUE}\n--------------------------------${CDEFAULT}" | |
echo -en "Continue commit? (Y|y|yes)/(.*): " | |
#hack | |
exec < /dev/tty | |
read choice | |
if [[ $choice =~ ^(Y|y|yes)$ ]]; then | |
echo -e "\n${CYELLOW}Continued...${CDEFAULT}\n" | |
exit 0 | |
else | |
echo -e "\n${CRED}Stopped...${CDEFAULT}\n" | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
global mode