Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP CodeSniffer pre-receive hook for git
#!/bin/sh
# PHP CodeSniffer pre-receive hook for git
PHPCS_BIN="/usr/bin/phpcs"
PHPCS_CODING_STANDARD="PEAR"
# use coding standart dir from local repo
PHPCS_DIR_LOCAL=0
TMP_DIR=$(mktemp -d --tmpdir phpcs-pre-receive-hook.XXXXXXXX)
mkdir "$TMP_DIR/source"
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# prepare our standart rules
if [ $PHPCS_DIR_LOCAL = 1 ]; then
mkdir "$TMP_DIR/standart"
git archive HEAD $PHPCS_CODING_STANDARD | tar -x -C "$TMP_DIR/standart"
PHPCS_CODING_STANDARD="$TMP_DIR/standart/$PHPCS_CODING_STANDARD"
fi
# gathers all errors and sent to output at end
ERRORS=""
RETVAL=0
# <oldrev> <newrev> <refname>
while read oldrev newrev ref;
do
list=$(git diff-tree --name-only -r $oldrev..$newrev | grep -e '.php' -e '.phtml')
for file in ${list}; do
# dirty hack for create dir tree
mkdir -p $(dirname "$TMP_DIR/source/$file")
git show ${newrev}:${file} > "$TMP_DIR/source/$file"
OUTPUT=$($PHPCS_BIN -s --standard=$PHPCS_CODING_STANDARD "$TMP_DIR/source/$file")
if [ "$?" -ne "0" ]; then
ERRORS="${ERRORS}${OUTPUT}"
RETVAL=1
fi
done
done
# cleanup
rm -rf $TMP_DIR
echo "$ERRORS"
exit $RETVAL
@gureedo

This comment has been minimized.

gureedo commented Oct 29, 2013

This hook does not work on new branch push.

  1. create new branch
  2. create new file
  3. commit new file
  4. push branch to origin

Then you will have some thing like this:

Counting objects: 4, done.
Delta compression using up to 6 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: fatal: Invalid revision range 0000000000000000000000000000000000000000..a80b995a358b52aef35477104ad82f9576c93d9c�[K
remote: 
To https://host/www/project.git
 * [new branch]      branch-test -> branch-test
@paunin

This comment has been minimized.

paunin commented Aug 9, 2014

@gureedo I fixed it and clean some needless code for me
https://gist.github.com/paunin/64f8fde6e7e70c8695ad

@mirsoo

This comment has been minimized.

mirsoo commented Apr 20, 2015

This line contains a bug:

list=$(git diff-tree --name-only -r $oldrev..$newrev | grep -e '.php' -e '.phtml')

It will find all files containing php in the name!
This is the right command string:

list=$(git diff-tree --name-only -r $oldrev..$newrev | grep -e '.php$' -e '.phtml$')

Actually I wouldn't recommend to use PHPCS for .phtml files.

@mirsoo

This comment has been minimized.

mirsoo commented Apr 20, 2015

@paunin thanks for Fix, now it works fine 👍

@adrianlzt

This comment has been minimized.

adrianlzt commented Dec 14, 2016

Something similar in python with a list to allow access: https://gist.github.com/adrianlzt/f30a4d7004671c5eea5b21ba6ef967d3

@jhabdas

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment