Skip to content

Instantly share code, notes, and snippets.

@peinwag
Created June 22, 2012 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peinwag/2971436 to your computer and use it in GitHub Desktop.
Save peinwag/2971436 to your computer and use it in GitHub Desktop.
Precommit hook for git that checks new modified lines against a given codingstandard with phpcs
#!/bin/bash
files=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD --)
for file in $files; do
phpcsout=$(phpcs -s $file)
if [ "$phpcsout" != "" ]; then
affectedLines=$(git blame -s $file | grep 00000000 | cut -d " " -f2 |sed 's/)//g')
if [ "$affectedLines" > 0 ]; then
echo ""
echo "FILE: " `pwd`"/"$file
echo "--------------------------------------------------------------------------------"
for line in $affectedLines; do
var=`echo "$phpcsout" | grep "$line |"`
if [ "$var" != "" ]; then
countErrors=`echo "$var" | grep "ERROR" | wc -l`
echo -n "FOUND ERROR(S): $countErrors"
countWarnings=`echo "$var" | grep "WARNING" | wc -l`
echo " FOUND WARNING(S): $countWarnings"
echo "--------------------------------------------------------------------------------"
echo "$var"
fi
done
echo "--------------------------------------------------------------------------------"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment