Skip to content

Instantly share code, notes, and snippets.

@B-Galati
Last active August 21, 2024 20:05
Show Gist options
  • Save B-Galati/b919785e0cec0d2f4ee6 to your computer and use it in GitHub Desktop.
Save B-Galati/b919785e0cec0d2f4ee6 to your computer and use it in GitHub Desktop.
git pre-commit hook for php
#!/bin/bash
# Inspiration http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/
# http://nrocco.github.io/2012/04/19/git-pre-commit-hook-for-PHP.html
EXITCODE=0
# Check for php syntax error
FILES=$(git diff --cached --name-only)
for FILE in $FILES ; do
if [[ "$FILE" =~ ^.+(php|inc|module|install|test)$ ]]; then
if [[ -f $FILE ]]; then
php -l "$FILE" 1> /dev/null
if [ $? -ne 0 ]; then
EXITCODE=1
fi
fi
fi
done
# Check for var_dump
DIFF=$(git diff --cached --name-only -G "(var_dump|dump|print_r|exit|die).*\\(.*\\)|@TEMP\W|TODO\W|<<<<<<<<\W|>>>>>>>>\W|========\W");
if [ ! -z "$DIFF" ]; then
echo -e "\nYou've left a var_dump|dump|print_r|exit|die|@TEMP|<<<<<|>>>>>|===== in one of your files!"
echo -e $DIFF | tr " " "\n";
EXITCODE=1
fi
if [ $EXITCODE -gt 0 ]; then
echo -e "\n\e[1;31mFix the above errors or use:"
echo -e "\tgit commit --no-verify"
fi
exit $EXITCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment