Skip to content

Instantly share code, notes, and snippets.

@putnik
Forked from peinwag/pre-commit.sh
Last active March 11, 2019 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save putnik/78a67a47a96192f12ff91cedaaa23f56 to your computer and use it in GitHub Desktop.
Save putnik/78a67a47a96192f12ff91cedaaa23f56 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 --diff-filter=ACMR HEAD --)
for file in $files; do
phpcsout=$(phpcs -s $file --standard=phpcs.xml)
if [ "$phpcsout" != "" ]; then
affectedLines=$(git blame -p -s $file | grep 00000000 | cut -d " " -f2)
out=0
for line in $affectedLines; do
var=`echo "$phpcsout" | grep "$line |"`
if [ "$var" != "" ]; then
out=1
break;
fi
done
if [ "$out" == 1 ] && [ "$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`
if [ "$countErrors" != "0" ]; then
echo "FOUND ERROR(S): $countErrors"
fi
countWarnings=`echo "$var" | grep "WARNING" | wc -l`
if [ "$countWarnings" != "0" ]; then
echo "FOUND WARNING(S): $countWarnings"
fi
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