Skip to content

Instantly share code, notes, and snippets.

@Stubbs
Created October 16, 2013 10:37
Show Gist options
  • Select an option

  • Save Stubbs/7005847 to your computer and use it in GitHub Desktop.

Select an option

Save Stubbs/7005847 to your computer and use it in GitHub Desktop.
Git Hook to reject PHP syntax errors & any php that directly calls "error_log"
#!/bin/bash
PLATFORM='unknown'
UNAMESTR=`uname`
XARGS_EXIT=1
if [[ "$UNAMESTR" == 'Linux' ]]; then
PLATFORM='linux'
XARGS_EXIT=123
elif [[ "$UNAMESTR" == 'FreeBSD' ]]; then
PLATFORM='freebsd'
XARGS_EXIT=1
elif [[ "$UNAMESTR" == 'Darwin' ]]; then
PLATFORM='osx'
XARGS_EXIT=1
fi
echo $PLATFORM
echo $XARGS_EXIT
echo £UNAMESTR
# # Redirect output to stderr.
exec 1>&2
RED='\033[0;31m'
ERROR_TITLE='\033[1;33;41m'
YELLOW='\033[0;33m'
BLUE='\033[1;34m'
NC='\033[00m'
# Look for people using error_log
FILES_PATTERN='\.(php|inc)(\..+)?$'
FORBIDDEN='error_log'
if [[ `git diff --no-ext-diff --cached --name-only | grep -E "$FILES_PATTERN" | wc -l` -gt 0 ]]; then
git diff --no-ext-diff --cached --name-only | \
grep -E "$FILES_PATTERN" | \
GREP_COLOR='41' xargs -I % grep --color --with-filename -n "$FORBIDDEN" %
if [[ $? -ne $XARGS_EXIT ]]; then
echo
echo -e "${ERROR_TITLE}COMMIT REJECTED:${YELLOW} Found ${BLUE}'$FORBIDDEN'${YELLOW}. Error logging MUST use syslog.${NC}"
exit 1
fi
fi
git diff --no-ext-diff --cached --name-only | \
grep -E "$FILES_PATTERN" | \
xargs -I % php -l % | \
grep -v "No syntax errors detected"
if [[ $? -gt 1 ]]; then
echo
echo -e "${ERROR_TITLE}COMMIT REJECTED:${YELLOW} Found PHP syntax errors!. Please remove them before commiting"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment