gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fix in unify-sources.sh (#245)
0 1 0
default
1 file changed with 6 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 768 line context
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 89
    if [ "$3" == 'tabs' ]
90 90
    then
91
        PATTERN=$(echo -e '\t')
91
        if echo $2 | grep -q -v -E 'Makefile\.am$'
92
        then
93
            PATTERN=$(echo -e '\t')
94
        else
95
            PATTERN='        '
96
        fi
92 97
    elif [ "$3" == 'trailing spaces' ]
93 98
    then
94 99
        PATTERN='\ +$'
95 100
    else
96 101
        PATTERN='*'
97 102
    fi
98 103

	
99 104
    if ! diff -q $1 $2 >/dev/null
100 105
    then
101 106
        if [ "$PATTERN" == '*' ]
102 107
        then
103 108
            diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
104 109
              sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
105 110
        else
106 111
            grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
107 112
        fi
108 113
        FAILED=YES
109 114
    fi
110 115
}
111 116

	
112 117
function check_warning() {
113 118
    if [ "$2" == 'long lines' ]
114 119
    then
115 120
        grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
116 121
    else
117 122
        echo "$1: warning: $2"
118 123
    fi
119 124
    WARNED=YES
120 125
}
121 126

	
122 127
function check_init() {
123 128
    echo Check source files...
124 129
    FAILED_FILES=0
125 130
    WARNED_FILES=0
126 131
    TOTAL_FILES=0
127 132
}
128 133

	
129 134
function check_done() {
130 135
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
131 136
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
132 137

	
133 138
    if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
134 139
    then
135 140
	if [ "$WARNING" == 'INTERACTIVE' ]
136 141
	then
137 142
	    echo -n "Are the files with errors/warnings acceptable? (yes/no) "
138 143
	    while read answer
139 144
	    do
140 145
		if [ "$answer" == 'yes' ]
141 146
		then
142 147
		    return 0
143 148
		elif [ "$answer" == 'no' ]
144 149
		then
145 150
		    return 1
146 151
		fi
147 152
		echo -n "Are the files with errors/warnings acceptable? (yes/no) "
148 153
	    done
149 154
	elif [ "$WARNING" == 'WERROR' ]
150 155
	then
151 156
	    return 1
152 157
	fi
153 158
    fi
154 159
}
155 160

	
156 161
function check_begin() {
157 162
    ((TOTAL_FILES++))
158 163
    FAILED=NO
159 164
    WARNED=NO
160 165
}
161 166

	
162 167
function check_end() {
163 168
    if [ $FAILED == YES ]
164 169
    then
165 170
	((++FAILED_FILES))
166 171
    fi
167 172
    if [ $WARNED == YES ]
168 173
    then
169 174
	((++WARNED_FILES))
170 175
    fi
171 176
}
172 177

	
173 178

	
174 179

	
175 180
# checks
176 181

	
177 182
function header_check() {
178 183
    if echo $1 | grep -q -E 'Makefile\.am$'
179 184
    then
180 185
	return
181 186
    fi
182 187

	
183 188
    TMP_FILE=`mktemp`
184 189

	
185 190
    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
186 191
 *
187 192
 * This file is a part of LEMON, a generic C++ optimization library.
188 193
 *
189 194
 * Copyright (C) "$YEAR"
190 195
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
191 196
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
192 197
 *
193 198
 * Permission to use, modify and distribute this software is granted
194 199
 * provided that this copyright notice appears in all copies. For
195 200
 * precise terms see the accompanying LICENSE file.
196 201
 *
197 202
 * This software is provided \"AS IS\" with no warranty of any kind,
198 203
 * express or implied, and with no claim as to its suitability for any
199 204
 * purpose.
200 205
 *
201 206
 */
202 207
"
203 208
    awk 'BEGIN { pm=0; }
204 209
     pm==3 { print }
205 210
     /\/\* / && pm==0 { pm=1;}
206 211
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
207 212
     /\*\// && pm==1 { pm=2;}
208 213
    ' $1
209 214
    ) >$TMP_FILE
210 215

	
211 216
    "$ACTION"_action "$TMP_FILE" "$1" header
212 217
}
213 218

	
214 219
function tabs_check() {
215 220
    if echo $1 | grep -q -v -E 'Makefile\.am$'
216 221
    then
217 222
        OLD_PATTERN=$(echo -e '\t')
218 223
        NEW_PATTERN='        '
219 224
    else
220 225
        OLD_PATTERN='        '
221 226
        NEW_PATTERN=$(echo -e '\t')
222 227
    fi
223 228
    TMP_FILE=`mktemp`
224 229
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
225 230

	
226 231
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
227 232
}
228 233

	
229 234
function spaces_check() {
230 235
    TMP_FILE=`mktemp`
231 236
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
232 237

	
233 238
    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
234 239
}
235 240

	
236 241
function long_lines_check() {
237 242
    if cat $1 | grep -q -E '.{81,}'
238 243
    then
239 244
	"$ACTION"_warning $1 'long lines'
240 245
    fi
241 246
}
242 247

	
243 248
# process the file
244 249

	
245 250
function process_file() {
246 251
    if [ "$ACTION" == 'update' ]
247 252
    then
248 253
        echo -n "    $ACTION $1..."
249 254
    else
250 255
        echo "	  $ACTION $1..."
251 256
    fi
252 257

	
253 258
    CHECKING="header tabs spaces long_lines"
254 259

	
255 260
    "$ACTION"_begin $1
256 261
    for check in $CHECKING
257 262
    do
258 263
	"$check"_check $1
259 264
    done
260 265
    "$ACTION"_end $1
261 266
    if [ "$ACTION" == 'update' ]
262 267
    then
263 268
        echo
264 269
    fi
265 270
}
266 271

	
267 272
function process_all {
268 273
    "$ACTION"_init
269 274
    while read file
270 275
    do
271 276
	process_file $file
272 277
    done < <($FILES)
273 278
    "$ACTION"_done
274 279
}
275 280

	
276 281
while [ $# -gt 0 ]
277 282
do
278 283
    
279 284
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
280 285
    then
281 286
	echo -n \
282 287
"Usage:
283 288
  $0 [OPTIONS] [files]
284 289
Options:
285 290
  --dry-run|-n
286 291
     Check the files, but do not modify them.
287 292
  --interactive|-i
288 293
     If --dry-run is specified and the checker emits warnings,
289 294
     then the user is asked if the warnings should be considered
290 295
     errors.
291 296
  --werror|-w
292 297
     Make all warnings into errors.
293 298
  --all|-a
294 299
     Check all source files in the repository.
295 300
  --modified|-m
296 301
     Check only the modified (and new) source files. This option is
297 302
     useful to check the modification before making a commit.
298 303
  --changed|-c
299 304
     Check only the changed source files compared to the parent(s) of
300 305
     the current hg node.  This option is useful as hg hook script.
301 306
     To automatically check all your changes before making a commit,
302 307
     add the following section to the appropriate .hg/hgrc file.
303 308

	
304 309
       [hooks]
305 310
       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
306 311

	
307 312
  --help|-h
308 313
     Print this help message.
309 314
  files
310 315
     The files to check/unify. If no file names are given, the modified
311 316
     source files will be checked/unified (just like using the
312 317
     --modified|-m option).
313 318
"
314 319
        exit 0
315 320
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
316 321
    then
317 322
	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
318 323
	ACTION=check
319 324
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
320 325
    then
321 326
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
322 327
	FILES=all_files
323 328
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
324 329
    then
325 330
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
326 331
	FILES=changed_files
327 332
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
328 333
    then
329 334
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
330 335
	FILES=modified_files
331 336
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
332 337
    then
333 338
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
334 339
	WARNING='INTERACTIVE'
335 340
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
336 341
    then
337 342
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
338 343
	WARNING='WERROR'
339 344
    elif [ $(echo x$1 | cut -c 2) == '-' ]
340 345
    then
341 346
	echo "Invalid option $1" >&2 && exit 1
342 347
    else
343 348
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
344 349
	GIVEN_FILES=$@
345 350
	FILES=given_files
346 351
	break
347 352
    fi
348 353
    
349 354
    shift
350 355
done
351 356

	
352 357
if [ -z $FILES ]
353 358
then
354 359
    FILES=modified_files
355 360
fi
356 361

	
357 362
if [ -z $ACTION ]
358 363
then
359 364
    ACTION=update
360 365
fi
361 366

	
362 367
process_all
0 comments (0 inline)