-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·88 lines (79 loc) · 2.46 KB
/
pre-commit
File metadata and controls
executable file
·88 lines (79 loc) · 2.46 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
#!/bin/bash
# Check PHP file $1 syntax and exit with exit code 1 if there are any errors
test_php_file_syntax() {
if [[ "$1" =~ .(php|inc)$ ]] && ! php -l "$1" > /dev/null;
then
echo
echo -e "\e[31mPHP syntax checker failed\e[0m"
echo
echo -e "\tFile: \e[34m$1\e[0m"
echo
echo -e "\tReason:"
php -l "$1" | sed "s/^/\t\t/"
echo
exit 1
fi
}
# Check for newly introduced PSR issues
test_php_psr() {
if [[ "$1" =~ .(php|inc)$ ]]
then
git show HEAD:"$1" > /tmp/.test_file.php
errors_before=$(phpcs /tmp/.test_file.php | egrep ' ERROR| WARNING' | wc -l)
errors_after=$(phpcs "$1" | egrep ' ERROR| WARNING' | wc -l)
if [[ $errors_after -gt $errors_before ]]
then
echo
echo -e "\e[31mPSR checker failed\e[0m"
echo
echo -e "\tFile: \e[34m$1\e[0m"
echo -e "\tReason: you have introduced $(($errors_after-$errors_before)) new PSR problems"
echo
fi
fi
}
# Check file $1 for words $2
test_words_existance() {
if git diff --cached -U0 "$1" | grep -P '^[+-]' | grep -Ev '^(--- a/|\+\+\+ b/|-)' | grep -q "$2"
then
echo
echo -e "\e[31mWords checker failed\e[0m"
echo
echo -e "\tFile: \e[34m$1\e[0m"
echo -e "\tReason: has word \e[96m$2\e[0m in newly written code."
echo
git diff --cached -U0 "$1" | grep -P '^[+-]' | grep -Ev '^(--- a/|\+\+\+ b/|-)' | grep "$2" | sed "s/^/\t\t/" | cut -c 1-100
echo
if [ ! -z "$3" ]
then
exit 1
fi
fi
}
while read st file
do
# skip deleted files
if [ "$st" == 'D' ]
then
continue
fi
# Generic
test_words_existance "$file" TODO
test_words_existance "$file" FIXME
test_words_existance "$file" XXX
# PHP
test_php_file_syntax "$file"
test_php_psr "$file"
test_words_existance "$file" var_dump fatal
test_words_existance "$file" print_r fatal
test_words_existance "$file" error_reporting fatal
test_words_existance "$file" debug_backtrace fatal
# JavaScript
test_words_existance "$file" console.log fatal
test_words_existance "$file" debugger fatal
# Python
test_words_existance "$file" set_trace fatal
test_words_existance "$file" print
test_words_existance "$file" pprint fatal
test_words_existance "$file" __dict__ fatal
done < <(git diff --cached --name-status)