Skip to content

Instantly share code, notes, and snippets.

@jasonrobertfox
Created December 30, 2012 01:36
Show Gist options
  • Save jasonrobertfox/4410449 to your computer and use it in GitHub Desktop.
Save jasonrobertfox/4410449 to your computer and use it in GitHub Desktop.
PHP Precommit Hook
#!/bin/bash
##--CONFIGURATION--##
# add to this array of things you want to check for in your testing
checks[1]="var_dump"
checks[2]="print_r"
checks[3]="die"
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
element_count=${#checks[@]}
let "element_count = $element_count + 1"
ROOT_DIR="$(pwd)/"
LIST=$(git diff-index --name-only --cached --diff-filter=ACMR ${against})
ERRORS_BUFFER=""
if [ "$LIST" == "" ]; then
echo -e "\033[00;43;30mTry staging something first, buddy! :(\033[00m"
exit 1
fi
for file in $LIST
do
if [ "$file" == '#' ]; then
continue
fi
EXTENSION=$(echo "$file" | grep "\.ph\|\.inc")
if [ "$EXTENSION" != "" ]; then
index=1
while [ "$index" -lt "$element_count" ]
do
#check for baddies
echo -ne "Checking $FILE_ACTION file: $file for [${checks[$index]}] - "
ERRORS=$(grep "${checks[$index]}" $ROOT_DIR$file >&1)
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n- $ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo -e "\033[00;31mERROR: \033[00m"
echo -e "\033[00;43;30m\"${checks[$index]}\"\033[00m found in file: $file"
else
echo -e "\033[00;32mOK\033[00m"
fi
let "index = $index + 1"
done
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo -e "\033[00;41;30mCan't commit your trash, fix errors first :(\033[00m"
exit 1
else
echo -e "\033[00;42;30mBOOM! Committed successfully :)\033[00m"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment