1 |
1 |
#!/bin/bash
|
|
2 |
#
|
|
3 |
# This file is a part of LEMON, a generic C++ optimization library.
|
|
4 |
#
|
|
5 |
# Copyright (C) 2003-2009
|
|
6 |
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
|
7 |
# (Egervary Research Group on Combinatorial Optimization, EGRES).
|
|
8 |
#
|
|
9 |
# Permission to use, modify and distribute this software is granted
|
|
10 |
# provided that this copyright notice appears in all copies. For
|
|
11 |
# precise terms see the accompanying LICENSE file.
|
|
12 |
#
|
|
13 |
# This software is provided "AS IS" with no warranty of any kind,
|
|
14 |
# express or implied, and with no claim as to its suitability for any
|
|
15 |
# purpose.
|
2 |
16 |
|
3 |
17 |
YEAR=`date +%Y`
|
4 |
18 |
HGROOT=`hg root`
|
5 |
19 |
|
6 |
20 |
function hg_year() {
|
7 |
21 |
if [ -n "$(hg st $1)" ]; then
|
8 |
22 |
echo $YEAR
|
9 |
23 |
else
|
10 |
24 |
hg log -l 1 --template='{date|isodate}\n' $1 |
|
11 |
25 |
cut -d '-' -f 1
|
12 |
26 |
fi
|
13 |
27 |
}
|
14 |
28 |
|
15 |
29 |
# file enumaration modes
|
16 |
30 |
|
17 |
31 |
function all_files() {
|
18 |
32 |
hg status -a -m -c |
|
19 |
33 |
cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
|
20 |
34 |
while read file; do echo $HGROOT/$file; done
|
21 |
35 |
}
|
22 |
36 |
|
23 |
37 |
function modified_files() {
|
24 |
38 |
hg status -a -m |
|
25 |
39 |
cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
|
26 |
40 |
while read file; do echo $HGROOT/$file; done
|
27 |
41 |
}
|
28 |
42 |
|
29 |
43 |
function changed_files() {
|
30 |
44 |
{
|
31 |
45 |
if [ -n "$HG_PARENT1" ]
|
32 |
46 |
then
|
33 |
47 |
hg status --rev $HG_PARENT1:$HG_NODE -a -m
|
34 |
48 |
fi
|
35 |
49 |
if [ -n "$HG_PARENT2" ]
|
36 |
50 |
then
|
37 |
51 |
hg status --rev $HG_PARENT2:$HG_NODE -a -m
|
38 |
52 |
fi
|
39 |
53 |
} | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
|
40 |
54 |
sort | uniq |
|
41 |
55 |
while read file; do echo $HGROOT/$file; done
|
42 |
56 |
}
|
43 |
57 |
|
44 |
58 |
function given_files() {
|
45 |
59 |
for file in $GIVEN_FILES
|
46 |
60 |
do
|
47 |
61 |
echo $file
|
48 |
62 |
done
|
49 |
63 |
}
|
50 |
64 |
|
51 |
65 |
# actions
|
52 |
66 |
|
53 |
67 |
function update_action() {
|
54 |
68 |
if ! diff -q $1 $2 >/dev/null
|
55 |
69 |
then
|
56 |
70 |
echo -n " [$3 updated]"
|
57 |
71 |
rm $2
|
58 |
72 |
mv $1 $2
|
59 |
73 |
CHANGED=YES
|
60 |
74 |
fi
|
61 |
75 |
}
|
62 |
76 |
|
63 |
77 |
function update_warning() {
|
64 |
78 |
echo -n " [$2 warning]"
|
65 |
79 |
WARNED=YES
|
66 |
80 |
}
|
67 |
81 |
|
68 |
82 |
function update_init() {
|
69 |
83 |
echo Update source files...
|
70 |
84 |
TOTAL_FILES=0
|
71 |
85 |
CHANGED_FILES=0
|
72 |
86 |
WARNED_FILES=0
|
73 |
87 |
}
|
74 |
88 |
|
75 |
89 |
function update_done() {
|
76 |
90 |
echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
|
77 |
91 |
echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
|
78 |
92 |
}
|
79 |
93 |
|
80 |
94 |
function update_begin() {
|
81 |
95 |
((TOTAL_FILES++))
|
82 |
96 |
CHANGED=NO
|
83 |
97 |
WARNED=NO
|
84 |
98 |
}
|
85 |
99 |
|
86 |
100 |
function update_end() {
|
87 |
101 |
if [ $CHANGED == YES ]
|
88 |
102 |
then
|
89 |
103 |
((++CHANGED_FILES))
|
90 |
104 |
fi
|
91 |
105 |
if [ $WARNED == YES ]
|
92 |
106 |
then
|
93 |
107 |
((++WARNED_FILES))
|
94 |
108 |
fi
|
95 |
109 |
}
|
96 |
110 |
|
97 |
111 |
function check_action() {
|
98 |
112 |
if [ "$3" == 'tabs' ]
|
99 |
113 |
then
|
100 |
114 |
if echo $2 | grep -q -v -E 'Makefile\.am$'
|
101 |
115 |
then
|
102 |
116 |
PATTERN=$(echo -e '\t')
|
103 |
117 |
else
|
104 |
118 |
PATTERN=' '
|
105 |
119 |
fi
|
106 |
120 |
elif [ "$3" == 'trailing spaces' ]
|
107 |
121 |
then
|
108 |
122 |
PATTERN='\ +$'
|
109 |
123 |
else
|
110 |
124 |
PATTERN='*'
|
111 |
125 |
fi
|
112 |
126 |
|
113 |
127 |
if ! diff -q $1 $2 >/dev/null
|
114 |
128 |
then
|
115 |
129 |
if [ "$PATTERN" == '*' ]
|
116 |
130 |
then
|
117 |
131 |
diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
|
118 |
132 |
sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
|
119 |
133 |
else
|
120 |
134 |
grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
|
121 |
135 |
fi
|
122 |
136 |
FAILED=YES
|
123 |
137 |
fi
|
124 |
138 |
}
|
125 |
139 |
|
126 |
140 |
function check_warning() {
|
127 |
141 |
if [ "$2" == 'long lines' ]
|
128 |
142 |
then
|
129 |
143 |
grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
|
130 |
144 |
else
|
131 |
145 |
echo "$1: warning: $2"
|
132 |
146 |
fi
|
133 |
147 |
WARNED=YES
|
134 |
148 |
}
|
135 |
149 |
|
136 |
150 |
function check_init() {
|
137 |
151 |
echo Check source files...
|
138 |
152 |
FAILED_FILES=0
|
139 |
153 |
WARNED_FILES=0
|
140 |
154 |
TOTAL_FILES=0
|
141 |
155 |
}
|
142 |
156 |
|
143 |
157 |
function check_done() {
|
144 |
158 |
echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
|
145 |
159 |
echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
|
146 |
160 |
|
147 |
161 |
if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
|
148 |
162 |
then
|
149 |
163 |
if [ "$WARNING" == 'INTERACTIVE' ]
|
150 |
164 |
then
|
151 |
165 |
echo -n "Are the files with errors/warnings acceptable? (yes/no) "
|
152 |
166 |
while read answer
|
153 |
167 |
do
|
154 |
168 |
if [ "$answer" == 'yes' ]
|
155 |
169 |
then
|
156 |
170 |
return 0
|
157 |
171 |
elif [ "$answer" == 'no' ]
|
158 |
172 |
then
|
159 |
173 |
return 1
|
160 |
174 |
fi
|
161 |
175 |
echo -n "Are the files with errors/warnings acceptable? (yes/no) "
|
162 |
176 |
done
|
163 |
177 |
elif [ "$WARNING" == 'WERROR' ]
|
164 |
178 |
then
|
165 |
179 |
return 1
|
166 |
180 |
fi
|
167 |
181 |
fi
|
168 |
182 |
}
|
169 |
183 |
|
170 |
184 |
function check_begin() {
|
171 |
185 |
((TOTAL_FILES++))
|
172 |
186 |
FAILED=NO
|
173 |
187 |
WARNED=NO
|
174 |
188 |
}
|
175 |
189 |
|
176 |
190 |
function check_end() {
|
177 |
191 |
if [ $FAILED == YES ]
|
178 |
192 |
then
|
179 |
193 |
((++FAILED_FILES))
|
180 |
194 |
fi
|
181 |
195 |
if [ $WARNED == YES ]
|
182 |
196 |
then
|
183 |
197 |
((++WARNED_FILES))
|
184 |
198 |
fi
|
185 |
199 |
}
|
186 |
200 |
|
187 |
201 |
|
188 |
202 |
|
189 |
203 |
# checks
|
190 |
204 |
|
191 |
205 |
function header_check() {
|
192 |
206 |
if echo $1 | grep -q -E 'Makefile\.am$'
|
193 |
207 |
then
|
194 |
208 |
return
|
195 |
209 |
fi
|
196 |
210 |
|
197 |
211 |
TMP_FILE=`mktemp`
|
198 |
212 |
|
199 |
213 |
(echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
200 |
214 |
*
|
201 |
215 |
* This file is a part of LEMON, a generic C++ optimization library.
|
202 |
216 |
*
|
203 |
217 |
* Copyright (C) 2003-"$(hg_year $1)"
|
204 |
218 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
205 |
219 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
206 |
220 |
*
|
207 |
221 |
* Permission to use, modify and distribute this software is granted
|
208 |
222 |
* provided that this copyright notice appears in all copies. For
|
209 |
223 |
* precise terms see the accompanying LICENSE file.
|
210 |
224 |
*
|
211 |
225 |
* This software is provided \"AS IS\" with no warranty of any kind,
|
212 |
226 |
* express or implied, and with no claim as to its suitability for any
|
213 |
227 |
* purpose.
|
214 |
228 |
*
|
215 |
229 |
*/
|
216 |
230 |
"
|
217 |
231 |
awk 'BEGIN { pm=0; }
|
218 |
232 |
pm==3 { print }
|
219 |
233 |
/\/\* / && pm==0 { pm=1;}
|
220 |
234 |
/[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
|
221 |
235 |
/\*\// && pm==1 { pm=2;}
|
222 |
236 |
' $1
|
223 |
237 |
) >$TMP_FILE
|
224 |
238 |
|
225 |
239 |
"$ACTION"_action "$TMP_FILE" "$1" header
|
226 |
240 |
}
|
227 |
241 |
|
228 |
242 |
function tabs_check() {
|
229 |
243 |
if echo $1 | grep -q -v -E 'Makefile\.am$'
|
230 |
244 |
then
|
231 |
245 |
OLD_PATTERN=$(echo -e '\t')
|
232 |
246 |
NEW_PATTERN=' '
|
233 |
247 |
else
|
234 |
248 |
OLD_PATTERN=' '
|
235 |
249 |
NEW_PATTERN=$(echo -e '\t')
|
236 |
250 |
fi
|
237 |
251 |
TMP_FILE=`mktemp`
|
238 |
252 |
cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
|
239 |
253 |
|
240 |
254 |
"$ACTION"_action "$TMP_FILE" "$1" 'tabs'
|
241 |
255 |
}
|
242 |
256 |
|
243 |
257 |
function spaces_check() {
|
244 |
258 |
TMP_FILE=`mktemp`
|
245 |
259 |
cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
|
246 |
260 |
|
247 |
261 |
"$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
|
248 |
262 |
}
|
249 |
263 |
|
250 |
264 |
function long_lines_check() {
|
251 |
265 |
if cat $1 | grep -q -E '.{81,}'
|
252 |
266 |
then
|
253 |
267 |
"$ACTION"_warning $1 'long lines'
|
254 |
268 |
fi
|
255 |
269 |
}
|
256 |
270 |
|
257 |
271 |
# process the file
|
258 |
272 |
|
259 |
273 |
function process_file() {
|
260 |
274 |
if [ "$ACTION" == 'update' ]
|
261 |
275 |
then
|
262 |
276 |
echo -n " $ACTION $1..."
|
263 |
277 |
else
|
264 |
278 |
echo " $ACTION $1..."
|
265 |
279 |
fi
|
266 |
280 |
|
267 |
281 |
CHECKING="header tabs spaces long_lines"
|
268 |
282 |
|
269 |
283 |
"$ACTION"_begin $1
|
270 |
284 |
for check in $CHECKING
|
271 |
285 |
do
|
272 |
286 |
"$check"_check $1
|
273 |
287 |
done
|
274 |
288 |
"$ACTION"_end $1
|
275 |
289 |
if [ "$ACTION" == 'update' ]
|
276 |
290 |
then
|
277 |
291 |
echo
|
278 |
292 |
fi
|
279 |
293 |
}
|
280 |
294 |
|
281 |
295 |
function process_all {
|
282 |
296 |
"$ACTION"_init
|
283 |
297 |
while read file
|
284 |
298 |
do
|
285 |
299 |
process_file $file
|
286 |
300 |
done < <($FILES)
|
287 |
301 |
"$ACTION"_done
|
288 |
302 |
}
|
289 |
303 |
|
290 |
304 |
while [ $# -gt 0 ]
|
291 |
305 |
do
|
292 |
306 |
|
293 |
307 |
if [ "$1" == '--help' ] || [ "$1" == '-h' ]
|
294 |
308 |
then
|
295 |
309 |
echo -n \
|
296 |
310 |
"Usage:
|
297 |
311 |
$0 [OPTIONS] [files]
|
298 |
312 |
Options:
|
299 |
313 |
--dry-run|-n
|
300 |
314 |
Check the files, but do not modify them.
|
301 |
315 |
--interactive|-i
|
302 |
316 |
If --dry-run is specified and the checker emits warnings,
|
303 |
317 |
then the user is asked if the warnings should be considered
|
304 |
318 |
errors.
|
305 |
319 |
--werror|-w
|
306 |
320 |
Make all warnings into errors.
|
307 |
321 |
--all|-a
|
308 |
322 |
Check all source files in the repository.
|
309 |
323 |
--modified|-m
|
310 |
324 |
Check only the modified (and new) source files. This option is
|
311 |
325 |
useful to check the modification before making a commit.
|
312 |
326 |
--changed|-c
|
313 |
327 |
Check only the changed source files compared to the parent(s) of
|
314 |
328 |
the current hg node. This option is useful as hg hook script.
|
315 |
329 |
To automatically check all your changes before making a commit,
|
316 |
330 |
add the following section to the appropriate .hg/hgrc file.
|
317 |
331 |
|
318 |
332 |
[hooks]
|
319 |
333 |
pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
|
320 |
334 |
|
321 |
335 |
--help|-h
|
322 |
336 |
Print this help message.
|
323 |
337 |
files
|
324 |
338 |
The files to check/unify. If no file names are given, the modified
|
325 |
339 |
source files will be checked/unified (just like using the
|
326 |
340 |
--modified|-m option).
|
327 |
341 |
"
|
328 |
342 |
exit 0
|
329 |
343 |
elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
|
330 |
344 |
then
|
331 |
345 |
[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
|
332 |
346 |
ACTION=check
|
333 |
347 |
elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
|
334 |
348 |
then
|
335 |
349 |
[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
|
336 |
350 |
FILES=all_files
|
337 |
351 |
elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
|
338 |
352 |
then
|
339 |
353 |
[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
|
340 |
354 |
FILES=changed_files
|
341 |
355 |
elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
|
342 |
356 |
then
|
343 |
357 |
[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
|
344 |
358 |
FILES=modified_files
|
345 |
359 |
elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
|
346 |
360 |
then
|
347 |
361 |
[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
|
348 |
362 |
WARNING='INTERACTIVE'
|
349 |
363 |
elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
|
350 |
364 |
then
|
351 |
365 |
[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
|
352 |
366 |
WARNING='WERROR'
|
353 |
367 |
elif [ $(echo x$1 | cut -c 2) == '-' ]
|
354 |
368 |
then
|
355 |
369 |
echo "Invalid option $1" >&2 && exit 1
|
356 |
370 |
else
|
357 |
371 |
[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
|
358 |
372 |
GIVEN_FILES=$@
|
359 |
373 |
FILES=given_files
|
360 |
374 |
break
|
361 |
375 |
fi
|
362 |
376 |
|
363 |
377 |
shift
|
364 |
378 |
done
|
365 |
379 |
|
366 |
380 |
if [ -z $FILES ]
|
367 |
381 |
then
|
368 |
382 |
FILES=modified_files
|
369 |
383 |
fi
|
370 |
384 |
|
371 |
385 |
if [ -z $ACTION ]
|
372 |
386 |
then
|
373 |
387 |
ACTION=update
|
374 |
388 |
fi
|
375 |
389 |
|
376 |
390 |
process_all
|