Skip to content

Instantly share code, notes, and snippets.

@GregoryCollett
Created June 18, 2015 11:06
Show Gist options
  • Save GregoryCollett/7eda2578bbe1fc0a61c7 to your computer and use it in GitHub Desktop.
Save GregoryCollett/7eda2578bbe1fc0a61c7 to your computer and use it in GitHub Desktop.
pre-commit Shell Example (PHP Lint)
#!/bin/bash
# find the changed files, get the name only and pipe result to a while loop
git diff --cached --name-only | while read FILE; do
# ff file matches extension pattern then
if [[ "$FILE" =~ ^.+(php|inc|module|install|test)$ ]]; then
# if file is actually and file that exists then
if [[ -f $FILE ]]; then
# php lint the file and
php -l "$FILE" 1> /dev/null
# if the result is not equal to 0 then
if [ $? -ne 0 ]; then
# show the error in the terminal and exit with a number not equal to 0 so the commit does not get processed
echo -e "\e[1;31m\tAborting commit due to files with syntax errors" >&2
exit 1
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment