Skip to content

Instantly share code, notes, and snippets.

@octoxan
Created November 17, 2016 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octoxan/7414f7819fc80c7b3d9901769bb545b5 to your computer and use it in GitHub Desktop.
Save octoxan/7414f7819fc80c7b3d9901769bb545b5 to your computer and use it in GitHub Desktop.
Pre-commit git hook to check for console.log's and php syntax errors.
#!/bin/sh
exec 1>&2
exec < /dev/tty
git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(php|inc|module|install|test)$ ]]; then
if [[ -f $FILE ]]; then
php -l "$FILE" 1> /dev/null
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tAborting commit due to files with syntax errors.\e[0m" >&2
exit 1
fi
fi
fi
done || exit $?
# If there's a PHP error let's hard exit
# Don't even bother checking for js files because
# We're not committing broken PHP with no exceptions
if [ $? -eq 0 ]; then
consoleregexp='^\+.*console\.log('
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some instances of console.log in your changes. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0;
else
exit 1;
fi
fi
fi
# You can choose 'y' if you're aware of the console.log and want it committed for some reason
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment