Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (56 sloc) 1.88 KB
#!/bin/bash
################################################################################
#
# Pre commit hook to run phpcs and abort commits on failures or warnings
#
# To use, copy or link to the .git/hooks folder of your repository.
#
# This simple script:
# 1. Determins the width of the current console, so the report is full-width
# 2. Determins which commit to diff against (to know what is being commited) -
# using a fictional commit if it's the first commit.
# 3. Skip files which don't exist or are not php files
# 4. If the commit is multiple files run in summary mode, otherwise show details
#
# If phpcs returns a none-zero value (which means errors or warnings where found)
# The commit will be aborted. A developer can override this pre-commit hook and
# thus still commit despite the errors or warnings raised with the command:
#
# git commit --no-verify <same args as before>
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright Copyright (c) 2010, Andy Dawson
# @link www.ad7six.com
# @link pear.php.net/package/PHP_CodeSniffer
# @package cakephp-codesniffs
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
################################################################################
width=$(tput cols);
if [ `git rev-parse --verify HEAD` ]; then
against='HEAD'
else
against='4b825dc642cb6eb9a060e54bf8d69288fbee4904'
fi
commitFiles=`git diff-index --cached --name-only $against`
args="-s --report-width=$width"
phpFiles="";
phpFilesCount=0;
for f in $commitFiles; do
if [[ ! -e $f ]]; then
continue;
fi
if [[ $f =~ \.(php|ctp)$ ]]; then
phpFilesCount=$phpFilesCount+1
phpFiles="$phpFiles $f"
fi
done;
if [[ $phpFilesCount = 0 ]]; then
exit 0;
fi
if [[ $phpFilesCount > 2 ]]; then
args="$args --report=summary"
fi
phpcs $args $phpFiles