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