Skip to content

Instantly share code, notes, and snippets.

@jsgoyette
Last active December 17, 2015 23:58
Show Gist options
  • Save jsgoyette/5692861 to your computer and use it in GitHub Desktop.
Save jsgoyette/5692861 to your computer and use it in GitHub Desktop.
Git pre-commit hook - php lint. Save file as .git/hooks/pre-commit
#!/bin/bash
# modified from
# https://github.com/ReekenX/git-php-syntax-checker/blob/master/pre-commit
ROOT_DIR="$(pwd)/"
LIST=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
ERRORS_BUFFER=""
for file in $LIST
do
EXTENSION=$(echo "$file" | grep ".php$")
if [ "$EXTENSION" != "" ]; then
ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error")
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo -e "Syntax errors found in file: \033[01;32m$file\033[0m"
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo "These errors were found in try-to-commit files: "
echo -e "\033[1;31m$ERRORS_BUFFER"
echo -e "\033[0m"
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