gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Improve unify-sources.sh (#186) Errors can also be accepted when the script is used as a hg hook. Sometimes it is useful (e.g. if someone else made the errors).
0 1 0
default
1 file changed with 3 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 384 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 91
        PATTERN=$(echo -e '\t')
92 92
    elif [ "$3" == 'trailing spaces' ]
93 93
    then
94 94
        PATTERN='\ +$'
95 95
    else
96 96
        PATTERN='*'
97 97
    fi
98 98

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

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

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

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

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

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

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

	
176 173

	
177 174

	
178 175
# checks
179 176

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

	
186 183
    TMP_FILE=`mktemp`
187 184

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

	
214 211
    "$ACTION"_action "$TMP_FILE" "$1" header
215 212
}
216 213

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

	
229 226
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
230 227
}
231 228

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

	
236 233
    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
237 234
}
238 235

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

	
246 243
# process the file
247 244

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

	
256 253
    CHECKING="header tabs spaces long_lines"
257 254

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

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

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

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

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