forked from WouterSioen/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·125 lines (102 loc) · 3.15 KB
/
pre-commit
File metadata and controls
executable file
·125 lines (102 loc) · 3.15 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /bin/bash
#
# This script checks if all added, copied, modified or renamed files are valid against the PSR2 coding standards
# and if there are no php, javascript or css errors
# dependencies:
# codesniffer (http://pear.php.net/package/PHP_CodeSniffer)
# esvalidate (https://github.com/duereg/esvalidate)
# CSS lint (https://github.com/stubbornella/csslint/wiki/Command-line-interface)
# SCSS lint (https://github.com/causes/scss-lint)
#
# @version 1.1.0
# @author Wouter Sioen <[email protected]>
# create empty errors array
declare -a errors
# Check if we're on a semi-secret empty tree
if git rev-parse --verify HEAD
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# fetch all changed php files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep '\.php$')
if [ -n "$files" ]; then
echo 'Checking PHP Files'
echo '------------------'
echo
for file in $files; do
# first check if they are valid php files
output=`php -l $file | grep 'Errors parsing'`
# if it did contain errors, we have output
if [ -n "$output" ]; then
echo "$file contains php syntax errors"
errors=("${errors[@]}" "$output")
fi
# checks if the phpcs output contains '| ERROR |'
output=`phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full $file | grep '| ERROR |'`
# if it did contain errors, we have output
if [ -n "$output" ]; then
echo "$file fails coding standards"
phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full $file
errors=("${errors[@]}" "$output")
fi
done
fi
# fetch all changed js files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep '\.js$')
if [ -n "$files" ]; then
echo
echo 'Checking Javascript Files'
echo '------------------'
echo
for file in $files; do
output=`esvalidate $file`
# if our output is not empty, there were errors
if [ -n "$output" ]; then
echo "$file contains javascript syntax errors"
echo $output
errors=("${errors[@]}" "$output")
fi
done
fi
# fetch all changed css files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep '\.css$')
if [ -n "$files" ]; then
echo
echo 'Checking CSS Files'
echo '------------------'
echo
for file in $files; do
output=`csslint --format=compact $file | grep 'Error -'`
# if our output is not empty, there were errors
if [ -n "$output" ]; then
echo "$file contains css syntax errors"
echo $output
errors=("${errors[@]}" "$output")
fi
done
fi
# fetch all changed css files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep -E '\.s(c|a)ss$')
if [ -n "$files" ]; then
echo
echo 'Checking SCSS Files'
echo '------------------'
echo
for file in $files; do
output=`scss-lint $file | grep '\[E\]'`
# if our output is not empty, there were errors
if [ -n "$output" ]; then
echo "$file contains scss syntax errors"
scss-lint $file | grep '\[E\]'
errors=("${errors[@]}" "$output")
fi
done
fi
# if we have errors, exit with 1
if [ -n "$errors" ]; then
exit 1
fi
echo '🍺 No errors found!'