-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·375 lines (349 loc) · 8.4 KB
/
pre-commit
File metadata and controls
executable file
·375 lines (349 loc) · 8.4 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
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
#!/bin/bash
#
# Global Variable
#
login="forget_c"
ignore_files=""
path_subject="subject.pdf"
step=("ANALYSIS" "DEVELOP" "REVIEW" "FAT" "DONE")
config_file=".git/.config"
date=$(date +"%m-%d-%y")
versions="v1.0"
license="GNU General Public License v3.0"
project=""
info="Epita 2019 - ING 1"
clangformat=".clang-format"
#
# End Global Variable
#
if [ ! -t 0 ]; then
[ -z "$(grep "TTY" < ~/.bashrc)" ] &&
echo 'TTY=$(tty)' >> ~/.bashrc &&
echo 'export TTY' >> ~/.bashrc &&
echo "relaunch your terminal please" &&
exit 1
else
TTY=$(tty)
fi
#
# correct the coding style
# $1 file to correct
# $? info on correction
#
function coding
{
local ret=0
local file=$1
check_coding $file
diff $file $file.cod &> /dev/null
if [ $? -eq 1 ]; then
vim -d "+nnoremap <enter> :xa<cr>" $file $file.cod < $TTY
else
ret=1
fi
check_coding $file
diff $file $file.cod &> /dev/null
if [ $? -eq 1 ]; then
ret=2
read -p "Auto correct :(Y/n)" correct < $TTY
[[ ! $correct = "n" ]] && cp $file.cod $file && ret=3
fi
rm $1.cod
git add $1
return "$ret"
}
#
# stock correct coding style of file in file.cod
# $1 file to correct
#
function check_coding
{
local file="$1"
if [ -z $clangformat ]; then
cp $file $file.cod
clang-format -assume-filename=$clangformat -i $file.cod
else
sed "s/ *$//g" $file |
sed "s/\t/ /g" |
sed -r "s/^(.{80})(..*)$/\1\n\2/g"|
sed -r "s/ sizeof\(/ sizeof \(/g"|
sed -r "s/,([a-zA-Z])/, \1/g" > $file.cod
fi
}
#
# launch Coding Style
#
function launch_correction
{
for i in $(find . \
\( \
$(print_ignore_files) \
\) -prune \
-o \
\( \
-type f \
\( \
-name "*.[ch]" -o -name "*.[ch][ch]" -o -name "*.hxx" \
\) \
\) -print \
)
do
inf=""
coding $i
case $? in
0)
inf="CORRECTED";;
1)
inf="NO ERROR";;
2)
inf="NOT CORRECTED";;
3)
inf="AUTO CORRECTED";;
esac
printf "$i"
print_nchar ' ' $((80 - (${#i} + ${#inf})))
printf "\e[32m[$inf]\e[39m\n"
done
}
#
# print Authors
# $@ list of AUTHORS
#
function make_authors
{
#
# print Authors
# $@ list of AUTHORS
#
function print_authors { for i in $@; do echo "* "$i; done }
if [ -s AUTHORS ];then
if ! diff AUTHORS <(print_authors $@) > /dev/null;then
printf "\e[31m Wrong Authors \e[39m\n"
read -p "Do you want to correct ?(Y/n)" correct < $TTY
[[ ! $correct = "n" ]] && print_authors $@ > AUTHORS || exit 1
fi
else
printf "\e[31m no Authors \e[39m\n"
print_authors $@ > AUTHORS
fi
git add AUTHORS
}
function print_nchar
{
local char=$1
local n=$2
printf "%-$n""s" | tr ' ' "$char"
}
#
# Make Readme
#
function make_readme
{
update_var_readme
local IFS="\t\n"
# principal header
echo $(print_nchar '=' 80) > README
echo $(print_nchar ' ' 37)"README" >> README
echo $(print_nchar '=' 80) >> README
printf "\n\n\n" >> README
# category
for i in "GENERAL INFO" GOAL USAGE CHANGELOG;
do
printf $(print_nchar '=' $((39 - $((${#i} / 2))))) >> README
printf " $i " >> README
printf $(print_nchar '=' $((39 - $(((${#i} + 1) / 2)))))"\n" >> README
printf "\n\n" >> README
case $i in
"GENERAL INFO")
echo " Project: $project" >> README
echo " Contributors: $login" >> README
echo " License: $license" >> README
echo " Version: $versions" >> README
echo " Date: $date" >> README
printf "\n\n $info\n" >> README
;;
USAGE)
if [ -f CMakeLists.txt ];then
echo "To get the Makefile : 'mkdir build && cd build && cmake ..'" >> README
elif [ -f autogen.sh ];then
echo "To get the Makefile : './autogen.sh && ./configure'" >> README
fi
if [ -f Makefile -o -f CMakeLists.txt -o -f autogen.sh ];then
echo "To compile the project to get the binary file: 'make or make all'" >> README
echo "To clean the project: 'make clean'" >> README
echo "To launch the test suite: 'make check'" >> README
fi
;;
*)
echo " ##FIXME##" >> README
;;
esac
printf "\n\n" >> README
done
}
#
# Make Todo
# $1 subject to parse
#
function make_todo
{
[[ ! -f $1 ]] && return 1
printf "TODO:\n" > TODO
#get summary
local test=$(pdftotext $1 /dev/stdout |
grep -E "^[0-9](.*)( \.)+" |
sed -r 's/^([0-9]\.?)+([a-zA-Z ’:?0-9]+)(.*)*/\2/' |
tr '\n' '@')
local IFS="@"
#padding
local cnt=0
for i in $test;
do
[[ $cnt -lt ${#i} ]] && cnt=${#i}
done
#write TODO
for i in $test;
do
local c=$((cnt - ${#i}))
[[ ! ${#i} -lt 2 ]] &&
printf $i"%-$c""s""\t[${step[0]}]\t[$login]\n" >> TODO ;
done
}
#
# Create architecture
# $1 subject to parse
#
function make_archi
{
[[ ! -f $1 ]] && return 1
local test=$(pdftotext $1 /dev/stdout |
grep "^\./[[:alpha:]]" |
sed "s/*//" |
sed "s/^\.\///" );
for i in $test
do
if [[ "$i" =~ \/$ ]]; then
mkdir $i
else
touch $i
fi
done
}
function print_ignore_files
{
printf " -path ./.git"
for i in $ignore_files;
do
while read in; do
printf " -o -path ./$in"
done < $i
done
}
#
# Update TODO
#
function update_todo
{
read -r -a logins <<< "$login"
local todomap="+map <enter> bde"
# map for steps
local s_size=$((${#step[@]} - 1))
todomap+=":if @\" =~ '^${step[$s_size]}$' <cr> execute \"normal! i${step[0]}\"<cr>"
for (( i=0; i < s_size ; i++ ))
do
todomap+=":elseif @\" =~ '^${step[$i]}$' <cr> execute \"normal! i${step[$(($i + 1))]}\"<cr>"
done
# map for login
local l_size=$((${#logins[@]} -1))
todomap+=":elseif @\" =~ '^${logins[$l_size]}$' <cr> execute \"normal! i${logins[0]}\"<cr>"
for (( i=0; i < l_size ; i++ ))
do
todomap+=":elseif @\" =~ '^${logins[$i]}$' <cr> execute \"normal! i${logins[$(($i + 1))]}\"<cr>"
done
todomap+=":else <cr> execute \"normal! hp\"<cr>"
todomap+="endif<cr><cr>"
#launch todo editor
vim \
"+map 1 bdeiANALYSIS<esc>" \
"+map 2 bdeiDEVELOP<esc>" \
"+map 3 bdeiREVIEW<esc>"\
"+map 4 bdeiFAT<esc>"\
"+map 5 bdeiDONE<esc>"\
"$todomap"\
TODO < $TTY
[ -f TODO ] && git add TODO
}
function generate_config
{
[[ ! -f $1 ]] && return 1
echo "PROJECT="$(pdftotext $1 /dev/stdout | head -n 1 | sed "s/ — Subject//I") >> "$config_file"
printf "DATE=" >> "$config_file"
pdftotext $1 /dev/stdout | grep -E "^[A-Z][a-z]+, [A-Z][a-z]+ [0-9]+, [0-9]+" | tr '\n' ':' >> "$config_file"
echo >> "$config_file"
}
function update_var_readme
{
local IFS="="
while read var value
do
case $var in
"VERSION")
versions=$value
;;
"DATE")
date=$value
;;
"PROJECT")
project=$value
;;
"LICENSE")
license=$value
;;
"INFO")
info=$value
esac
done < $config_file
}
if [ ! -t 0 -o $# -eq 0 ];
then
if [ -s $path_subject ];
then
[ ! -f AUTHORS -o ! -f TODO ] && make_archi $path_subject
if [ ! -s TODO ];then
make_todo $path_subject
fi
generate_config $path_subject
fi
make_authors $login
if [ ! -s README ];then
make_readme
fi
launch_correction
if git diff-index --quiet HEAD -- TODO; then
update_todo
fi
exit 0
elif [ $1 == "-ut" ];
then
update_todo
elif [ $1 == "-c" ];
then
launch_correction
elif [ $1 == "-a" ];
then
make_authors $login
elif [ $1 == "-r" ];
then
make_readme
elif [ $1 == "-t" ];
then
make_todo $2
elif [ $1 == "-init" ];
then
make_archi $2
elif [ $1 == "-conf" ];
then
generate_config $2
else
echo "Usage: $0 [-ut] [-c] [-a] [-r] [-t file] [-init file] [-conf file]"
fi