-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpre-commit
executable file
·96 lines (83 loc) · 2.39 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
#!/bin/sh
# reference: https://stackoverflow.com/questions/31057527/git-pre-commit-hook-to-format-and-re-add-files-at-the-same-time
# https://medium.com/@harshitbangar/automatic-code-formatting-with-git-66c3c5c26798
# Python format
# Regexp for grep to only choose some file extensions for formatting
exts="\.\(py\)$"
formatter=`which black`
# Format staged files
change_list=`git diff --cached --name-only --diff-filter=ACMR | grep $exts | xargs`
if [ ! -z "$change_list" ]
then
for file in $change_list
do
"$formatter" --check "$file"
ret=$?
if [ $ret -ne 0 ]
then
echo "formatting $file"
"$formatter" "$file"
git add $file
ret=$?
if [ $ret -ne 0 ]
then
echo "ERROR on python formatting"
exit 1
else
echo "python formatting finished!"
fi
pylint --load-plugins pylint_django --rcfile backend/beafamily/.pylintrc "$file"
ret=$?
if [ $ret -ne 0 ]
then
echo "ERROR on pylint"
exit 1
else
echo "pylint passed!"
fi
fi
done
else
echo "we don't need to format any python files!"
fi
# ts/tsx format
# Regexp for grep to only choose some file extensions for formatting
exts="\.\(ts\|tsx\)$"
# The formatter to use
formatter="frontend/node_modules/.bin/prettier"
eslint="frontend/node_modules/.bin/eslint"
change_list=`git diff --cached --name-only --diff-filter=ACMR | grep $exts | xargs`
echo "$change_list"
if [ ! -z "$change_list" ]
then
for file in $change_list
do
"$formatter" --check "$file"
ret=$?
if [ $ret -ne 0 ]
then
echo "formatting $file"
"$formatter" --write "$file"
git add $file
ret=$?
if [ $ret -ne 0 ]
then
echo "ERROR on typescript/react formatting"
exit 1
else
echo "typescript/react formatting finished!"
fi
fi
"$eslint" $file
ret=$?
if [ $ret -ne 0 ]
then
echo "ERROR on eslint"
exit 1
else
echo "eslint passed!"
fi
done
else
echo "we don't need to format any typescript/react files!"
fi