-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathpre-commit.sh
executable file
·233 lines (201 loc) · 5.99 KB
/
pre-commit.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
#!/bin/bash
# checks if locally staged changes are
# formatted properly. Ignores non-staged
# changes.
# Intended as git pre-commit hook
#COLOR CODES:
#tput setaf 3 = yellow -> Info
#tput setaf 1 = red -> warning/not allowed commit
#tput setaf 2 = green -> all good!/allowed commit
echo ""
echo "$(tput setaf 3)Running pre-commit hook ... (you can omit this with --no-verify, but don't)$(tput sgr 0)"
no_of_files_to_stash=`git ls-files . --exclude-standard --others -m | wc -l`
if [ $no_of_files_to_stash -ne 0 ]
then
echo "$(tput setaf 3)* Stashing non-staged changes"
files_to_stash=`git ls-files . --exclude-standard --others -m | xargs`
git stash push -k -u -- $files_to_stash >/dev/null 2>/dev/null
fi
npm run build-check >/dev/null 2>/dev/null
compiles=$?
echo "$(tput setaf 3)* Compiles?$(tput sgr 0)"
if [ $compiles -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
fi
npm run check-circular-dependencies
circDep=$?
echo "$(tput setaf 3)* No circular dependencies?$(tput sgr 0)"
if [ $circDep -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
fi
npm run pretty-check >/dev/null 2>/dev/null
formatted=$?
echo "$(tput setaf 3)* Properly formatted?$(tput sgr 0)"
if [ $formatted -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
echo "$(tput setaf 1)Please run 'npm run pretty' to format the code.$(tput sgr 0)"
echo ""
fi
npm run lint >/dev/null 2>/dev/null
linted=$?
echo "$(tput setaf 3)* Properly linted?$(tput sgr 0)"
if [ $linted -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
echo "$(tput setaf 1)Please run 'npm run lint' to lint the code.$(tput sgr 0)"
echo ""
fi
npm run prune >/dev/null 2>/dev/null
pruned=$?
echo "$(tput setaf 3)* Properly pruned?$(tput sgr 0)"
if [ $pruned -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
echo "$(tput setaf 1)Please run 'npm run prune' to prune the code.$(tput sgr 0)"
echo ""
fi
echo "$(tput setaf 3)* Does not cntain .only in tests?$(tput sgr 0)"
grep -ri --exclude-dir=with-typescript "\.only" ./test
dotOnly=$?
if [ $dotOnly -eq 1 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
echo ""
fi
if [ $no_of_files_to_stash -ne 0 ]
then
echo "$(tput setaf 3)* Undoing stashing$(tput sgr 0)"
git stash apply >/dev/null 2>/dev/null
if [ $? -ne 0 ]
then
git checkout --theirs . >/dev/null 2>/dev/null
fi
git stash drop >/dev/null 2>/dev/null
fi
if [ $compiles -eq 0 ] && [ $formatted -eq 0 ] && [ $linted -eq 0 ] && [ $pruned -eq 0 ] && [ $dotOnly -eq 1 ] && [ $circDep -eq 0 ]
then
echo "$(tput setaf 2)... done. Proceeding with commit.$(tput sgr 0)"
echo ""
elif [ $formatted -ne 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to NON-FORMATTED CODE.$(tput sgr 0)"
echo ""
exit 1
elif [ $linted -ne 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to NON-LINTED CODE.$(tput sgr 0)"
echo ""
exit 1
elif [ $pruned -ne 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to NON-PRUNED CODE.$(tput sgr 0)"
echo ""
exit 1
elif [ $compiles -ne 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to COMPILE ERROR.$(tput sgr 0)"
echo ""
exit 2
elif [ $dotOnly -ne 1 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to .only in test files $(tput sgr 0)"
echo ""
exit 2
elif [ $circDep -ne 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to CIRCULAR-DEPENDENCIES IN CODE.$(tput sgr 0)"
echo ""
exit 1
fi
# get current version----------
version=`cat package.json | grep -e '"version":'`
while IFS='"' read -ra ADDR; do
counter=0
for i in "${ADDR[@]}"; do
if [ $counter == 3 ]
then
version=$i
fi
counter=$(($counter+1))
done
done <<< "$version"
codeversion=`cat lib/build/version.d.ts | grep -e 'package_version'`
while IFS='"' read -ra ADDR; do
counter=0
for i in "${ADDR[@]}"; do
if [ $counter == 1 ]
then
codeversion=$i
fi
counter=$(($counter+1))
done
done <<< "$codeversion"
if [ $version != $codeversion ]
then
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "${RED}Version codes in version.ts and package.json are not the same${NC}\n"
exit 1
fi
# get git branch name-----------
branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)" # detached HEAD
branch_name=${branch_name##refs/heads/}
# check if branch is correct based on the version-----------
if [ $branch_name == "master" ]
then
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
printf "${YELLOW}committing to MASTER${NC}\n"
elif [[ $version == $branch_name* ]]
then
continue=1
elif ! [[ $branch_name =~ ^[0-9]+.[0-9]+$ ]]
then
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
printf "${YELLOW}Not committing to master or version branches${NC}\n"
else
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "${RED}Pushing to wrong branch. Stopping commit${NC}\n"
exit 1
fi
no_of_files_to_stash=`git ls-files . --exclude-standard --others -m | wc -l`
if [ $no_of_files_to_stash -ne 0 ]
then
echo "$(tput setaf 3)* Stashing non-staged changes"
files_to_stash=`git ls-files . --exclude-standard --others -m | xargs`
git stash push -k -u -- $files_to_stash >/dev/null 2>/dev/null
fi
if [ $no_of_files_to_stash -ne 0 ]
then
echo "$(tput setaf 3)* Undoing stashing$(tput sgr 0)"
git stash apply >/dev/null 2>/dev/null
if [ $? -ne 0 ]
then
git checkout --theirs . >/dev/null 2>/dev/null
fi
git stash drop >/dev/null 2>/dev/null
fi