forked from manoj-apare/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
executable file
·134 lines (122 loc) · 4.02 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
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
126
127
128
129
130
131
132
133
134
#!/bin/bash
# Redirect output to stderr.
exec 1>&2
# Color codes
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
reset=`tput sgr0`
# Notification
echo "${blue}"
echo "# Running pre-commit git hook."
echo "==============================${reset}"
# Debugging functions to be checked.
keywords=(dpm dpq dvm ddebug_backtrace print_r var_dump debug_backtrace console\.log)
# Join keywords array into a single string for grep and remove the separator from the start of the string.
keywords_for_grep=$(printf "|%s" "${keywords[@]}")
keywords_for_grep=${keywords_for_grep:1}
# Directories and files to be excluded during search.
exclude_dir_and_ext='\/features\/|\/contrib\/|\/devel\/|\/libraries\/|\/vendor\/|\.info$|\.png$|\.gif$|\.jpg$|\.ico$|\.patch$|\.htaccess$|\.sh$|\.ttf$|\.woff$|\.eot$|\.svg$'
# Flag counter
synatx_error_found=0
debugging_function_found=0
merge_conflict=0
coding_standard_error=0
# Check for PHP syntax
# List php files added to commit
php_files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep '\.php$|\.inc$|\.module|\.theme$'`
if [ -n "$php_files_changed" ]
then
for FILE in $php_files_changed; do
php -l $FILE > /dev/null 2>&1
compiler_result=$?
if [ $compiler_result -eq 255 ]
then
if [ $synatx_error_found -eq 0 ]
then
echo "${red}"
echo "# Compilation error(s):"
echo "-----------------------${reset}"
fi
synatx_error_found=1
`php -l $FILE > /dev/null`
fi
done
fi
# Check for debugging functions
# List all files added to commit excluding the exceptions
files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep -v $exclude_dir_and_ext`
if [ -n "$files_changed" ]
then
for FILE in $files_changed ; do
for keyword in "${keywords[@]}" ; do
# Find debugging function exists in file diff one by one.
pattern="^\+(.*)?$keyword(.*)?"
result_for_file=`git diff --cached $FILE | egrep -x "$pattern"`
if [ ! -z "$result_for_file" ]
then
if [ $debugging_function_found -eq 0 ]
then
echo "${red}"
echo "# Debugging function(s):"
echo "------------------------${reset}"
fi
debugging_function_found=1
echo "Debugging function" $keyword
git grep -n $keyword $FILE | awk '{split($0,a,":");
printf "\tfound in " a[1] " on line " a[2] "\n";
}'
fi
done
done
fi
# Check for Drupal coding standards
# List php files added to commit
php_files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep -v $exclude_dir_and_ext | egrep '\.php$|\.module$|\.inc$|\.install$|\.test$|\.profile$|\.theme$|\.js$|\.css$|\.info$|\.txt$'`
if [ -n "$php_files_changed" ]
then
phpcs_result=`phpcs --standard=Drupal --report=csv $php_files_changed`
if [ "$phpcs_result" != "File,Line,Column,Type,Message,Source,Severity,Fixable" ]
then
echo "${red}"
echo "# Coding standard issue(s):"
echo "---------------------------${reset}"
phpcs --standard=Drupal $php_files_changed
coding_standard_error=1
fi
fi
# Check for merge conflict markers
# List all files added to commit
files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD --`
if [ -n "$files_changed" ]
then
for FILE in $files_changed; do
# Find debugging function exists in file diff one by one.
pattern="(<<<<|====|>>>>)+.*(\n)?"
result_for_file=`egrep -in "$pattern" $FILE`
if [ ! -z "$result_for_file" ]
then
if [ $merge_conflict -eq 0 ]
then
echo "${red}"
echo "# Merge confict marker(s) found in:"
echo "-----------------------------------${reset}"
fi
merge_conflict=1
echo $FILE
fi
done
fi
# Decision maker
errors_found=$((synatx_error_found+debugging_function_found+merge_conflict+coding_standard_error))
if [ $errors_found -eq 0 ]
then
echo "${green}"
echo "Clean code :), your code now been committed!"
echo "${reset}"
else
echo "${red}"
echo "Clean listed file(s). Aborting git commit."
echo "${reset}"
exit 1
fi