-
Notifications
You must be signed in to change notification settings - Fork 1
/
cop.sh
executable file
·559 lines (464 loc) · 12.2 KB
/
cop.sh
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/bash
verbose=0
base_directory=$(git rev-parse --show-toplevel)
current_directory=$(pwd)
branch=$(git rev-parse --abbrev-ref HEAD)
npm_folder='app_client'
public_folder='app_client'
info_ok="👌 "
info_fail="🔥 "
isort='isort -sp ./.isort.cfg '
status_ok="👍 "
status_fail="💥 "
html_hint_exec='./app_client/node_modules/gulp-htmlhint/node_modules/htmlhint/bin/htmlhint --config app_client/.htmlhintrc'
# Back to current folder on end
trap "cd ${current_directory}" SIGINT SIGTERM EXIT
cd "${base_directory}"
# Override default execs, variables, etc...
if [ -f "local.sh" ]; then
source "local.sh"
fi
# Load files exceptions (only for HTML now)
if [ -f "file_exceptions.sh" ]; then
source "file_exceptions.sh"
fi
info () {
show=verbose
if [ -n "$4" ]; then
show=$4
fi
if [ "${1}" -eq '0' ]; then
if [ $show -eq 1 ] ; then
printf '%b %b\n' "${info_ok}" "${2}" >&2
fi
else
printf '%b %b\n' "${info_fail}" "${2}" >&2
if [ -z $3 ] || [ $3 -eq 1 ] ; then
exit 1
fi
fi
}
program_exists () {
local ret='0'
type $1 >/dev/null 2>&1 || { local ret='1'; }
# throw error on non-zero return value
if [ ! "${ret}" -eq '0' ]; then
info 1 "Sorry, we cannot continue without ${1}, please install it first. ${2}" $3
fi
}
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
###############################################################################
# INSTALL
###############################################################################
install () {
cat > .git/hooks/pre-commit << 'EOF'
base_directory=$(git rev-parse --show-toplevel)
eval "${base_directory}/cop.sh pre-commit --verbose HEAD"
EOF
chmod +x .git/hooks/pre-commit
info 0 "The pre-commit hook was installed." 0 $1
cat > .git/hooks/commit-msg << 'EOF'
base_directory=$(git rev-parse --show-toplevel)
eval "${base_directory}/cop.sh commit-msg $@"
EOF
chmod +x .git/hooks/commit-msg
info 0 "The commit-msg hook was installed." 0 $1
}
###############################################################################
# PRE COMMIT
###############################################################################
function pre_commit () {
# Execs
if [ -z "${eslint_exec}" ]; then
eslint_exec="npm run eslint --"
fi
# Protected branches:
if [[ "$branch" == "master" || "$branch" == "dev" ]]; then
printf "🔥 \e[31mYou CAN'T commit on branch ${branch}.\e[0m\n"
exit 1
fi
check_py=1
check_py_lint=0
check_scss=1
check_scss_lint=1
check_js=1
check_js_lint=1
check_html=1
check_images=1
verbose=0
exit_on_error=1
while [ $# -gt 0 ]
do
case "$1" in
--verbose)
verbose=1
shift
;;
--no-exit)
exit_on_error=0
shift
;;
--py)
check_py=1
shift
;;
--no-py)
check_py=0
shift
;;
--py-lint)
check_py_lint=1
shift
;;
--no-py-lint)
check_py_lint=1
shift
;;
--no-scss)
check_scss=0
shift
;;
--no-scss-lint)
check_scss_lint=0
shift
;;
--js)
check_js=1
shift
;;
--no-js)
check_js=0
shift
;;
--no-js-lint)
check_js_lint=0
shift
;;
--html)
check_html=1
shift
;;
--no-html)
check_html=0
shift
;;
--images)
check_images=1
shift
;;
--no-images)
check_images=0
shift
;;
-*)
echo "Invalid option: ${1}"
exit 1
;;
*)
target=$1
shift
;;
esac
done
ignore=(
'^_trash'
'^docs'
'^keys'
'api\.js$'
'^extras'
'api\.js$'
'^app/.*/migrations/'
'^app/.*/vendor/'
)
exclude=`python -c 'import sys;print("({})".format("|".join(sys.argv[1:])))' "${ignore[@]}"`
# List files
if [[ -n $target ]]; then
if [[ $target =~ ^([0-9abcdef]{7,40})$ ]]; then
files=`git show --pretty="format:" --name-only ${target} | grep -Ev ${exclude}`
elif [[ $target =~ ^(HEAD)$ ]]; then
files=`git diff --cached --name-only --diff-filter=AMR ${target} | grep -Ev ${exclude}`
elif [[ $target =~ ^\.$ ]]; then
files=`git ls-files| grep -Ev ${exclude}`
else
if [ ! -f $target ]; then
echo "File not found: ${target}"
exit 1
fi
files=$target
fi
else
files=`git status --porcelain -uall | awk 'match($1, /R|RM/) {print $4; next} match($1, /A|M|MM|AM/) {print $2}' | grep -Ev ${exclude}`
fi
result=0
status () {
if [ "${3}" -eq '0' ]; then
if [ $verbose -eq 1 ] || [ -n "${4}" ] ; then
# printf "\e[32m ${2}\e[0m%b\n" >&2
printf "${status_ok} \e[32m${2}\e[0m%b: ${1}\n" >&2
fi
else
# printf " \e[31m❯ ${2}\e[0m%b\n" >&2
printf "${status_fail} \e[31m${2}\e[0m%b: ${1}\n" >&2
result=1
if [ -z $exit_on_error ] || [ $exit_on_error -eq 1 ] ; then
exit 1
fi
fi
}
# For each file...
for file in $files; do
if containsElement $file "${FILE_EXCEPTIONS[@]}"; then
continue
fi
if [ ! -f $file ]; then
continue
fi
# unresolved merge conflict
cat "${file}" | grep -n -E "(^<<<<<<<\s|^=======\$|^>>>>>>>\s)" | grep -v "noqa"
if [ $? -eq 0 ]; then
status $file 'git-conflict' 1
else
status $file 'git-conflict' 0
fi
contains_super=`cat $file | grep '\ssuper([a-zA-Z]\+)'`;
if [[ ! -z $contains_super ]]; then
status $file 'contains-super-with-params' 1
exit 1;
fi
# python
if [ $check_py -eq 1 ] ; then
if [[ $file =~ \.py$ ]]; then
program_exists 'flake8' '(pip install flake8)'
program_exists 'isort' '(pip install isort)'
program_exists 'pylint' '(pip install pylint)'
# filename format
if [[ $file =~ (^|/)[a-z_0-9]+\.py$ ]] ; then
status $file 'py-ff' 0
else
echo "Invalid filename format: {$file}"
status $file 'py-ff' 1
fi
# isort
${isort} -ns __init__.py --check-only "${file}"
if [ $? -eq '0' ]; then
status $file 'py-isort' $?
elif [[ $target =~ ^(HEAD)$ ]]; then
${isort} -ns __init__.py "${file}"
${isort} -ns __init__.py --check-only "${file}"
git add "${file}"
status $file 'py-isort-autofix' $? 1
else
status $file 'py-isort' $?
fi
# flake8
flake8 "${file}"
status $file 'py-flake8' $?
# pylint
if [ $check_py_lint -eq 1 ] ; then
pylint --rcfile="${base_directory}/.pylintrc" "${file}"
status $file 'py-pylint' $?
fi
# i18n_deprecated
cat "${file}" | grep -o -n -E "gettext"
if [ $? -eq 0 ]; then
status $file 'py-i18n' 1
else
status $file 'py-i18n' 0
fi
# forgotten_prints
noqa=`cat "${file}" | grep "noqa: print"`
if [ $? -eq 1 ]; then
cat "${file}" | grep -n -E "\bprint\s*\(" | grep -v "noqa"
if [ $? -eq 0 ]; then
status $file 'py-fp' 1
else
status $file 'py-fp' 0
fi
fi
# forgotten_debbug_lines
noqa=`cat "${file}" | grep "noqa: debug"`
if [ $? -eq 1 ]; then
cat "${file}" | grep -n -E "\bset_trace\s*\(" | grep -v "noqa"
if [ $? -eq 0 ]; then
status $file 'py-fd' 1
else
status $file 'py-fd' 0
fi
fi
fi
fi
# scss
if [ $check_scss -eq 1 ] ; then
if [[ $file =~ \.scss$ ]]; then
program_exists 'scss-lint' '(gem install scss-lint)'
# scss: filename format
if [[ $file =~ (^|/)_?[a-z0-9\-]+\.scss$ ]] ; then
status $file 'scss-ff' 0
else
echo "Invalid filename format: {$file}"
status $file 'scss-ff' 1
fi
# FIXME: Disabled, because is intented to be used with a prefixer
# cat "${file}" | grep -v "\b@mixin cross-browser\b" | grep -o -n -E "\bcross-browser\b"
# if [ $? -eq 0 ]; then
# status $file 'scss-cb' 1
# else
# status $file 'scss-cb' 0
# fi
#
# cat "${file}" | grep -o -n -E "\bcrossBrowser\b"
# if [ $? -eq 0 ]; then
# status $file 'scss-cb' 1
# else
# status $file 'scss-cb' 0
# fi
# scss-lint
if [ $check_scss_lint -eq 1 ] ; then
scss-lint --require "${base_directory}/extras/scss_linters/no_pointer_events.rb" "$file"
status $file 'scss-lint' $?
fi
fi
fi
# js
if [ $check_js -eq 1 ] ; then
if [[ $file =~ \.js$ ]]; then
# filename_format
if [[ $file =~ (^|/)[a-z0-9\.\-]+\.js$ ]] ; then
status $file 'js-ff' 0
else
echo "Invalid filename format: {$file}"
status $file 'js-ff' 1
fi
# forgotten_log
cat "${file}" | grep -o -n -E "\bconsole.log\b" | grep -v "noqa"
if [ $? -eq 0 ]; then
status $file 'js-fl' 1
else
status $file 'js-fl' 0
fi
# forgotten debugger
cat "${file}" | grep -o -n -E "\bdebugger\b" | grep -v "noqa"
if [ $? -eq 0 ]; then
status $file 'js-debug' 1
else
status $file 'js-debug' 0
fi
# eslint
if [ $check_js_lint -eq 1 ] ; then
cd "$npm_folder"
eval "${eslint_exec} ${file}"
status $file 'js-eslint' $?
cd -
fi
fi
fi
# HTML
if [ $check_html -eq 1 ] ; then
if [[ $file =~ \.html$ ]]; then
# filename format
regex="^$public_folder/"
if [[ $file =~ $regex ]] ; then
if [[ $file =~ (^|/)[a-z0-9\-]+\.html$ ]] ; then
status $file 'html-ff' 0 ''
else
status $file 'html-ff' 1 "${file}"
fi
else
if [[ $file =~ (^|/)[a-z0-9\_]+\.html$ ]] ; then
status $file 'html-ff' 0 ''
else
status $file 'html-ff' 1 "${file}"
fi
fi
# blocks_name_format
cat "${file}" | grep -o -n -E "\{\%\s+block\s+[a-z0-9_]*[A-Z\-]+[a-z0-9_]*"
if [ $? -eq 0 ]; then
status $file 'html-block' 1
else
status $file 'html-block' 0
fi
# i18n_deprecated
cat "${file}" | grep -o -n -E "\{\%\s+load\s+i18n\s+\%\}"
if [ $? -eq 0 ]; then
status $file 'html-i18n' 1
else
status $file 'html-i18n' 0
fi
# trans_deprecated
cat "${file}" | grep -o -n -E "\{\%\s+trans\s+[^%]+"
if [ $? -eq 0 ]; then
status $file 'html-trans' 1
else
status $file 'html-trans' 0
fi
# htmlhint
eval "${html_hint_exec} ${file}"
status $file 'html-hint' $?
fi
fi
# Images
if [ $check_images -eq 1 ] ; then
if [[ $file =~ \.(jpeg|gif|png|jpg)$ ]]; then
status '🙏 \e[31mPLEASE: RENAME THE IMAGE FILE IF YOU MODIFIED IT.\e[0m' 'images-warn' 0
# filename_format
if [[ $file =~ (^|/)[a-z0-9\-]+\.(jpeg|gif|png|jpg)$ ]] ; then
if [[ $file =~ \.(jpeg|jpg)$ ]]; then
jpegoptim -m90 $file
fi
if [[ $file =~ \.(gif|png)$ ]]; then
optipng $file
fi
status $file 'images-ff' 0
else
echo "Invalid filename format: {$file}"
status $file 'images-ff' 1
fi
fi
fi
done
if [ $result -eq 0 ] ; then
echo "👮 < Move along. There's nothing to see here."
fi
exit $result
}
###############################################################################
# COMMIT MSG
###############################################################################
function commit_msg ()
{
return;
# ./check_ticket.py ${line}
# if [ $? -eq '1' ]; then
# printf "🔥 \e[31mInvalid commit JIRA ticket: ${line}\e[0m \n"
#
# exit 1
# fi
}
###############################################################################
# READ ARGS
###############################################################################
install 0
case "$1" in
pre-commit)
shift
pre_commit $@
;;
commit-msg)
shift
commit_msg $@
;;
install)
shift
install 1
;;
-*)
pre_commit $@
;;
*)
pre_commit $@
;;
esac