forked from venkatrs/Cake_PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
63 lines (56 loc) · 1.88 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/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