Skip to content

Instantly share code, notes, and snippets.

@kotnik
Created February 1, 2012 14:23
Show Gist options
  • Save kotnik/1717274 to your computer and use it in GitHub Desktop.
Save kotnik/1717274 to your computer and use it in GitHub Desktop.
Drupal pre-commit hook
#!/bin/bash
# Author: Remigijus Jarmalavičius <remigijus@jarmalavicius.lt>
# Author: Vytautas Povilaitis <php-checker@vytux.lt>
# Modifications: Nikola Kotur <kotnick@gmail.com>
ROOT_DIR="$(pwd)/"
LIST=$(git status | grep -e '\#.*\(modified\|added\)')
ERRORS_BUFFER=""
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 -e "php$\|module$\|inc$\|install$")
if [ "$EXTENSION" != "" ]; then
echo "Checking $FILE_ACTION file: $file"
# Checking for PHP errors
ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error")
DEBUGGING=""
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "Syntax errors found in file: $file "
fi
# Checking for leftover debug code
ERRORS=$(grep -e "dpm([^\r\n]*)\|dsm([^\r\n]*)\|krumo([^\r\n]*)" $ROOT_DIR$file 2>&1)
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
DEBUGGING="yes"
echo "Debugging code found in file: $file "
fi
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
if [ "$DEBUGGING" != "" ]; then
echo "This debugging code was found in try-to-commit files: "
else
echo "These errors were found in try-to-commit files: "
fi
echo -e $ERRORS_BUFFER
echo
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