-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·158 lines (134 loc) · 4.85 KB
/
pre-commit
File metadata and controls
executable file
·158 lines (134 loc) · 4.85 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
#
# PHP CodeSniffer pre-commit hook for Git
# See README.md for the Drupal-Code Sniffer configuration/setup instructions.
#
# Author: Gerald Z. Villorente
# Co-author: Nikolaos Dimopoulos
# Co-author: Engr. Ranel O. Padon
# Co-Author: Dan Helyar
#
# This project is made possible also through the collaborative support of the CNN Travel team:
# Senior Web Development Manager:
# Brent A. Deverman
#
# Senior Software Engineer:
# Adrian Christopher B. Cruz
#
# Senior QA Analyst:
# Jonathan A. Jacinto
#
# Special Credits to: Nikolaos Dimopoulos for his great work: http://www.niden.net/2011/11/git-pre-commit-another-check-to-ensure.html
# Exit on error
#!/bin/bash
# Exit cleanly and reset IFS
clean_exit() {
IFS=$IFSBACK
exit 1
}
clear
cat << "EOF"
************************************************************************
* *
* GIT PRE-COMMIT HOOK FOR DRUPAL 10 *
* *
* Filters applied before committing your changes: *
* 1. PHP syntax check *
* 2. PHP Code Sniffer + Auto-fix (phpcs/phpcbf) *
* 3. Debugging functions blacklist *
* 4. JS linting + Auto-fix (ESLint) *
* 5. Twig linting (TwigCS) *
* 6. CSS linting + Auto-fix (Stylelint) *
* *
************************************************************************
EOF
ROOT_DIR="$(pwd)/"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
checks=("<var_dump(" "<print_r(" "<die(" "<debug(" "<dpm(" "<krumo(" "<dpr(" \
"<dsm(" "<dd(" "<ddebug_backtrace(" "<dpq(" "<dprint_r(" "<drupal_debug(" \
"<dvm(" "<dvr(" "<kpr(" "<kprint_r(" "<kdevel_print_object(" \
"<<<<<<<" ">>>>>>>" "<console.log(" "<alert(")
filters_exclude=("core" "vendor" "contrib" "libraries")
exclude_extensions=('\.min\.js$' '\.md$' '\.png$' '\.gif$' '\.jpg$' '\.ico$' \
'\.patch$' '\.ad$' '\.htaccess$' '\.sh$' '\.ttf$' '\.woff$' \
'\.eot$' '\.svg$' '\.xml$' '\.json$' '\.yml$' '\.gitignore$' \
'\.lock$')
exclude_pattern="($(IFS='|'; echo "${filters_exclude[*]}"))|($(IFS='|'; echo "${exclude_extensions[*]}"))"
LIST=$(git diff --cached --name-only --diff-filter=ACM | grep -Ev "$exclude_pattern")
echo -e "\nFiles to be processed:" && echo "$LIST"
IFSBACK=$IFS
IFS=$'\n'
sniffer_error_count=0
ERRORS_BUFFER=""
NO_SYNTAX_ERROR=0
SYNTAX_ERROR=255
PHPCS_FAILED=1
PHPCS_PASSED=0
for file in $LIST; do
echo -e "\nValidating: $file..."
if echo "$file" | grep -Eq '\.(php|module|install|theme|inc)$'; then
echo "1. PHP Linting..."
php -l "$file" || clean_exit
echo "2. PHP Code Beautifier..."
if [ -f "vendor/bin/phpcbf" ]; then
vendor/bin/phpcbf --standard=Drupal "$file"
else
phpcbf --standard=Drupal "$file"
fi
echo "3. PHP Code Sniffer..."
if [ -f "vendor/bin/phpcs" ]; then
vendor/bin/phpcs --standard=Drupal "$file" || clean_exit
else
phpcs --standard=Drupal "$file" || clean_exit
fi
for pattern in "${checks[@]}"; do
if grep -qE "$pattern" "$ROOT_DIR$file"; then
ERRORS_BUFFER+="\n❌ Found '$pattern' in $file"
fi
done
git add "$file"
fi
echo
if echo "$file" | grep -Eq '\.js$'; then
echo "4. JavaScript Linting + Auto-fix (ESLint)..."
if command -v eslint >/dev/null; then
eslint --fix "$file" || clean_exit
eslint "$file" || clean_exit
git add "$file"
else
echo "❌ ESLint not found. Install it with 'npm install eslint'"
clean_exit
fi
fi
if echo "$file" | grep -Eq '\.twig$'; then
echo "5. Twig Linting (TwigCS)..."
if [ -f "vendor/bin/twigcs" ]; then
vendor/bin/twigcs "$file" || clean_exit
elif command -v twigcs >/dev/null; then
twigcs "$file" || clean_exit
else
echo "❌ TwigCS not found. Install with 'composer require --dev friendsoftwig/twigcs'"
clean_exit
fi
echo "⚠️ Twig auto-fix is not supported."
fi
if echo "$file" | grep -Eq '\.(css|scss)$'; then
echo "6. CSS Linting + Auto-fix (Stylelint)..."
if command -v npx >/dev/null; then
npx stylelint --fix "$file" || clean_exit
npx stylelint "$file" || clean_exit
git add "$file"
else
echo "❌ npx (stylelint) not available. Run 'npm install stylelint stylelint-config-standard'"
clean_exit
fi
fi
done
if [ -n "$ERRORS_BUFFER" ]; then
echo -e "\nBlacklisted patterns detected:"
echo -e "$ERRORS_BUFFER"
clean_exit
fi
echo -e "\n✅ All checks passed. Proceeding with commit."
IFS=$IFSBACK
exit 0