-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·312 lines (283 loc) · 9.23 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
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
#!/bin/bash
# This commit hook will automatically update the files that have been staged for
# a commit. It will update the copyright date to indicate the correct year. It
# will also apply include-what-you-use to the files, this feature is meant to
# remove extraneous includes and include the headers relevant to each
# particular file, however it is in its alpha stage of development and does
# still contain bugs. Finally, the hook will apply git-format rules to the files
# that are being staged.
#
# Defaults for using each of the features are shown below:
# License Update: ON
# Include what you use: OFF
# Clang formatting: ON
# Clang tidy: ON
# MARKDOWN_FORMAT_ON: ON
# PYTHON_FORMAT_ON: ON
#
# The default settings can be toggled by defining the following environment
# variables
#
# export DATE_ON=1 - turns on
# export DATE_ON=0 - turns off
#
# export IWYU_ON=1 - turns on
# export IWYU_ON=0 - turns off
#
# export CLANG_FORMAT_ON=1 - turns on
# export CLANG_FORMAT_ON=0 - turns off
#
# export CLANG_TIDY_ON=1 - turns on
# export CLANG_TIDY_ON=0 - turns off
#
# export HEADER_GUARD_ON=1 - turns on
# export HEADER_GUARD_ON=0 - turns off
#
# export MARKDOWN_FORMAT_ON=1 - turns on
# export MARKDOWN_FORMAT_ON=0 - turns off
#
# export PYTHON_FORMAT_ON=1 - turns on
# export PYTHON_FORMAT_ON=0 - turns off
#
# To use the hook, it must be placed in the .gith/hooks folder of whatever
# repository is being used. It must also be made executable.
#
# Assuming I am going to be using repo votca/csg
#
# cp pre-commit votca/csg/.git/hooks/pre-commit
# chmod 777 votca/csg/.git/hooks/pre-commit
#
# Then you should be good to go
################################################################################
# Updating License Date
################################################################################
# Default is on
if [ -z ${DATE_ON} ] # If not defined turn on
then
environement_variable_update_license=1
else
if [ "${DATE_ON}" -eq "1" ]
then
environement_variable_update_license=1
else
environement_variable_update_license=0
fi
fi
update_license_dates_of_altered_files(){
echo "Updating Copyright date in "$file
new_date=$(date +%Y)
sed -i 's/Copyright \(.*\)-.* The/Copyright \1-'"$new_date"' The/g' $file
}
################################################################################
# Checking Header Guard
################################################################################
# Default is on
if [ -z ${HEADER_GUARD_ON} ] # If not defined turn on
then
environement_variable_header_guard=1
else
if [ "${HEADER_GUARD_ON}" -eq "1" ]
then
environement_variable_header_guard=1
else
environement_variable_header_guard=0
fi
fi
check_header_guard_of_altered_files(){
# Only applies to header files
if [[ $file == *".h" ]]
then
# Only fixes if already included
count=$(grep -c "#ifndef" $file)
if [ "$count" -ne "0" ]
then
echo "Updating Header Guard for file $file"
repo_base=$(basename `git rev-parse --show-toplevel`)
repo_base=$(echo ${repo_base^^})
file_base=$(basename -s .h $file)
file_base=$(echo ${file_base^^})
sed -i "0,/#ifndef/ s/#ifndef .*/#ifndef VOTCA_${repo_base}_${file_base}_H/" $file
sed -i "0,/#define/ s/#define .*/#define VOTCA_${repo_base}_${file_base}_H/" $file
# Find last matching endif
line_num=$(grep -n "#endif" $file | tail -n1 | sed 's/:.*//')
sed -i "${line_num}s/#endif.*/#endif \/\/ VOTCA_${repo_base}_${file_base}_H/" $file
fi
fi
}
################################################################################
# Updating Includes
################################################################################
# Default is off
if [ -z ${IWYU_ON} ] # If not defined turn off
then
echo "Include what you use is not set"
environement_variable_apply_include_what_you_use=0
else
if [ "1" -eq "${IWYU_ON}" ]
then
environement_variable_apply_include_what_you_use=1
else
environement_variable_apply_include_what_you_use=0
fi
fi
# This variable will ignore any includes proposed by include-what-you-use
# that are from the boost library, this was included as IWYU is in the alpha
# phase of development and tends to screw up with third party libraries.
ignore_includes=()
apply_include_what_you_use(){
echo "Applying IWYU to file "$file
data=$(include-what-you-use $file 2>&1 )
echo "$data" | fix_includes.py $file
extra_includes=$(echo "$data" | awk '/should add these lines/,/should remove these lines/ { print $2}' )
# Filter includes
filtered_includes=()
for ignore in ${ignore_includes[@]}
do
filtered_includes+=$(echo "$extra_includes" | grep $ignore )
done
# Remove includes from file if the match
for item in ${filtered_includes[@]}
do
item=$(echo $item | sed 's/\//\\\//g')
echo $item
sed -i "/#include ${item}/d" $file
done
}
################################################################################
# Formatting Files .cc and .h
################################################################################
# Default is on
if [ -z ${CLANG_FORMAT_ON} ] # if not defined turn it on
then
environement_variable_apply_clang_formatting=1
else
if [ "${CLANG_FORMAT_ON}" -eq "1" ]
then
environement_variable_apply_clang_formatting=1
else
environement_variable_apply_clang_formatting=0
fi
fi
# Exclude .h.in files controlled by CMake
apply_clang_formating_to_altered_files(){
if [[ $file == *".h" ]] || [[ $file == *".cc" ]]
then
echo "Applying clang style formatting to file "$file
clang-format -i -style=file $file
fi
}
################################################################################
# Formatting Files .cc
################################################################################
# Default is on
if [ -z ${CLANG_TIDY_ON} ] # if not defined turn it on
then
environement_variable_apply_clang_tidy=1
else
if [ "${CLANG_TIDY_ON}" -eq "1" ]
then
environement_variable_apply_clang_tidy=1
else
environement_variable_apply_clang_tidy=0
fi
fi
# Exclude .h.in files controlled by CMake
apply_clang_tidy_to_altered_files(){
if [[ $file == *".h" ]] || [[ $file == *".cc" ]]
then
echo "Applying clang tidy to file "$file
clang-tidy $file -checks=* -- -I${GIT_DIR}/include/votca
fi
}
################################################################################
# Formatting Files .md
################################################################################
# Default is on
if [ -z ${MARKDOWN_FORMAT_ON} ] # if not defined turn it on
then
environement_variable_apply_markdown_formatting=1
else
if [ "${MARKDOWN_FORMAT_ON}" -eq "1" ]
then
environement_variable_apply_markdown_formatting=1
else
environement_variable_apply_markdown_formatting=0
fi
fi
apply_markdown_formating_to_altered_files(){
if [[ $file == *".md" ]]
then
echo "Applying markdown formatting to file "$file
remark --setting '"listItemIndent":"1"' --setting '"bullet":"*"' $file -o $file
fi
}
################################################################################
# Formatting Files .py
################################################################################
# Default is on
if [ -z ${PYTHON_FORMAT_ON} ] # if not defined turn it on
then
environement_variable_apply_python_formatting=1
else
if [ "${PYTHON_FORMAT_ON}" -eq "1" ]
then
environement_variable_apply_python_formatting=1
else
environement_variable_apply_python_formatting=0
fi
fi
apply_python_formating_to_altered_files(){
if [[ $file == *".py" ]]
then
echo "Applying python formatting to file "$file
autopep8 -i $file
fi
}
################################################################################
# Main
################################################################################
commit_hash=$(git log -1 | grep commit | awk '{print $2}')
list_of_changed_header_and_source_files=$(git diff --diff-filter=d --cached --name-only $commit_hash | grep '\(\.cc\|\.h\)$')
year=$(date +"%Y")
for file in ${list_of_changed_header_and_source_files[@]}
do
if [ "1" -eq "${environement_variable_header_guard}" ]
then
check_header_guard_of_altered_files
fi
if [ "1" -eq "${environement_variable_update_license}" ]
then
update_license_dates_of_altered_files
fi
if [ "1" -eq "${environement_variable_apply_include_what_you_use}" ]
then
apply_include_what_you_use
fi
if [ "1" -eq "${environement_variable_apply_clang_formatting}" ]
then
apply_clang_formating_to_altered_files
fi
if [ "1" -eq "${environement_variable_apply_clang_tidy}" ]
then
apply_clang_tidy_to_altered_files
fi
git add $file
done
list_of_changed_markdown_files=$(git diff --diff-filter=d --cached --name-only $commit_hash | grep '\(\.md\|\.MD|\.Md\)$')
for file in ${list_of_changed_markdown_files[@]}
do
if [ "1" -eq "{environement_variable_apply_markdown_formatting}" ]
then
apply_markdown_formating_to_altered_files
fi
git add $file
done
list_of_changed_python_files=$(git diff --diff-filter=d --cached --name-only $commit_hash | grep '\.py$')
for file in ${list_of_changed_python_files[@]}
do
if [ "1" -eq "{environement_variable_apply_python_formatting}" ]
then
apply_python_formating_to_altered_files
fi
git add $file
done