Skip to content

Instantly share code, notes, and snippets.

@anatooly
Created August 15, 2012 23:04
Show Gist options
  • Save anatooly/3364496 to your computer and use it in GitHub Desktop.
Save anatooly/3364496 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/bin/bash
# Author: Anatooly <anatooly.com>
# Based on code by Nikolaos Dimopoulos
# Based on code by Remigijus Jarmalavicius
# Checks the files to be committed for the presence of print_r(), var_dump(), die()
# The array below can be extended for further checks
php_checks[1]="var_dump("
php_checks[2]="print_r("
php_checks[3]="die("
php_checks[4]="pr("
php_checks[5]="fb(" # FireBug
js_checks[1]="console.log("
js_checks[2]="debugger"
js_checks[3]="alert"
ROOT_DIR="$(pwd)/"
LIST=$(git status | grep -e '\#.*\(modified\|added\)')
ERRORS_BUFFER=""
# PHP debugger
element_count=${#php_checks[@]}
let "element_count = $element_count + 1"
for file in $LIST
do
if [ "$file" == '#' ]; then
continue
fi
if [ $(echo "$file" | grep 'modified') ]; then
FILE_ACTION="modified"
elif [ $(echo "$file" | grep 'added') ]; then
FILE_ACTION="added"
else
EXTENSION=$(echo "$file" | grep ".php$")
if [ "$EXTENSION" != "" ]; then
# php --syntax-check
SYNTAX_CHECK=$(php -l $ROOT_DIR$file)
echo "> ${SYNTAX_CHECK}"
index=1
while [ "$index" -lt "$element_count" ]
do
echo "Checking $FILE_ACTION file: $file ${php_checks[$index]}"
ERRORS=$(grep "${php_checks[$index]}" $ROOT_DIR$file >&1)
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "> ${php_checks[$index]} found in file: $file "
fi
let "index = $index + 1"
done
fi
fi
done
# JS debugger
element_count=${#js_checks[@]}
let "element_count = $element_count + 1"
for file in $LIST
do
if [ "$file" == '#' ]; then
continue
fi
if [ $(echo "$file" | grep 'modified') ]; then
FILE_ACTION="modified"
elif [ $(echo "$file" | grep 'added') ]; then
FILE_ACTION="added"
else
EXTENSION=$(echo "$file" | grep ".js$")
if [ "$EXTENSION" != "" ]; then
index=1
while [ "$index" -lt "$element_count" ]
do
echo "Checking $FILE_ACTION file: $file ${js_checks[$index]}"
ERRORS=$(grep "${js_checks[$index]}" $ROOT_DIR$file >&1)
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "> ${js_checks[$index]} found in file: $file "
fi
let "index = $index + 1"
done
fi
fi
done
# Count the correct number of pairs of symbols
for file in $LIST
do
if [ "$file" == '#' ]; then
continue
fi
if [ $(echo "$file" | grep 'modified') ]; then
FILE_ACTION="modified"
elif [ $(echo "$file" | grep 'added') ]; then
FILE_ACTION="added"
else
EXTENSION=$(echo "$file" | grep ".*$")
if [ "$EXTENSION" != "" ]; then
# ( => )
echo "Checking $FILE_ACTION file: $file ( => )"
left_count=$(grep -o "(" $ROOT_DIR$file >&1 | wc -l )
right_count=$(grep -o ")" $ROOT_DIR$file >&1 | wc -l )
if [ "$left_count" != "$right_count" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "${left_count} ( => ${right_count} ) found in file: $file "
fi
# [ => ]
echo "Checking $FILE_ACTION file: $file [ => ]"
left_count=$(grep -o "\[" $ROOT_DIR$file >&1 | wc -l )
right_count=$(grep -o "\]" $ROOT_DIR$file >&1 | wc -l )
if [ "$left_count" != "$right_count" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "${left_count} [ => ${right_count} ] found in file: $file "
fi
# { => }
echo "Checking $FILE_ACTION file: $file { => }"
left_count=$(grep -o "{" $ROOT_DIR$file >&1 | wc -l )
right_count=$(grep -o "}" $ROOT_DIR$file >&1 | wc -l )
if [ "$left_count" != "$right_count" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "${left_count} { => ${right_count} } found in file: $file "
fi
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo "These errors were found in try-to-commit files: "
echo -e $ERRORS_BUFFER
echo
echo "WARNING! Remove debugger information for production."
#echo "Can't commit, fix errors first."
#exit 1
else
echo "Commited successfully."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment