gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Better help message and user question in unify-sources.sh
0 1 0
default
1 file changed with 14 insertions and 15 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
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
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 ! diff -q $1 $2 >/dev/null
90 90
    then
91 91
	echo -n " [$3 failed]"
92 92
	FAILED=YES
93 93
    fi
94 94
}
95 95

	
96 96
function check_warning() {
97 97
    echo -n " [$2 warning]"
98 98
    WARNED=YES
99 99
}
100 100

	
101 101
function check_init() {
102 102
    echo Check source files...
103 103
    FAILED_FILES=0
104 104
    WARNED_FILES=0
105 105
    TOTAL_FILES=0
106 106
}
107 107

	
108 108
function check_done() {
109 109
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
110
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
110
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
111 111

	
112 112
    if [ $FAILED_FILES -gt 0 ]
113 113
    then
114 114
	return 1
115 115
    elif [ $WARNED_FILES -gt 0 ]
116 116
    then
117 117
	if [ "$WARNING" == 'INTERACTIVE' ]
118 118
	then
119
	    echo -n "Assume as normal behaviour? (yes/no) "
119
	    echo -n "Are the files with warnings acceptable? (yes/no) "
120 120
	    while read answer
121 121
	    do
122 122
		if [ "$answer" == 'yes' ]
123 123
		then
124 124
		    return 0
125 125
		elif [ "$answer" == 'no' ]
126 126
		then
127 127
		    return 1
128 128
		fi
129
		echo -n "Assume as normal behaviour? (yes/no) "		    
129
		echo -n "Are the files with warnings acceptable? (yes/no) "
130 130
	    done
131 131
	elif [ "$WARNING" == 'WERROR' ]
132 132
	then
133 133
	    return 1
134 134
	fi
135 135
    fi
136 136
}
137 137

	
138 138
function check_begin() {
139 139
    ((TOTAL_FILES++))
140 140
    FAILED=NO
141 141
    WARNED=NO
142 142
}
143 143

	
144 144
function check_end() {
145 145
    if [ $FAILED == YES ]
146 146
    then
147 147
	((++FAILED_FILES))
148 148
    fi
149 149
    if [ $WARNED == YES ]
150 150
    then
151 151
	((++WARNED_FILES))
152 152
    fi
153 153
}
154 154

	
155 155

	
156 156

	
157 157
# checks
158 158

	
159 159
function header_check() {
160 160
    if echo $1 | grep -q -E 'Makefile\.am$'
161 161
    then
162 162
	return
163 163
    fi
164 164

	
165 165
    TMP_FILE=`mktemp`
166 166

	
167 167
    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
168 168
 *
169 169
 * This file is a part of LEMON, a generic C++ optimization library.
170 170
 *
171 171
 * Copyright (C) "$YEAR"
172 172
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
173 173
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
174 174
 *
175 175
 * Permission to use, modify and distribute this software is granted
176 176
 * provided that this copyright notice appears in all copies. For
177 177
 * precise terms see the accompanying LICENSE file.
178 178
 *
179 179
 * This software is provided \"AS IS\" with no warranty of any kind,
180 180
 * express or implied, and with no claim as to its suitability for any
181 181
 * purpose.
182 182
 *
183 183
 */
184 184
"
185 185
    awk 'BEGIN { pm=0; }
186 186
     pm==3 { print }
187 187
     /\/\* / && pm==0 { pm=1;}
188 188
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
189 189
     /\*\// && pm==1 { pm=2;}
190 190
    ' $1
191 191
    ) >$TMP_FILE
192 192

	
193 193
    "$ACTION"_action "$TMP_FILE" "$1" header
194 194
}
195 195

	
196 196
function tabs_check() {
197 197
    if echo $1 | grep -q -v -E 'Makefile\.am$'
198 198
    then
199 199
        OLD_PATTERN=$(echo -e '\t')
200 200
        NEW_PATTERN='        '
201 201
    else
202 202
        OLD_PATTERN='        '
203 203
        NEW_PATTERN=$(echo -e '\t')
204 204
    fi
205 205
    TMP_FILE=`mktemp`
206 206
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
207 207

	
208 208
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
209 209
}
210 210

	
211 211
function spaces_check() {
212 212
    TMP_FILE=`mktemp`
213 213
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
214 214

	
215 215
    "$ACTION"_action "$TMP_FILE" "$1" 'spaces'
216 216
}
217 217

	
218 218
function long_lines_check() {
219 219
    if cat $1 | grep -q -E '.{81,}'
220 220
    then
221 221
	"$ACTION"_warning $1 'long lines'
222 222
    fi
223 223
}
224 224

	
225 225
# process the file
226 226

	
227 227
function process_file() {
228 228
    echo -n "    $ACTION " $1...
229 229

	
230 230
    CHECKING="header tabs spaces long_lines"
231 231

	
232 232
    "$ACTION"_begin $1
233 233
    for check in $CHECKING
234 234
    do
235 235
	"$check"_check $1
236 236
    done
237 237
    "$ACTION"_end $1
238 238
    echo
239 239
}
240 240

	
241 241
function process_all {
242 242
    "$ACTION"_init
243 243
    while read file
244 244
    do
245 245
	process_file $file
246 246
    done < <($FILES)
247 247
    "$ACTION"_done
248 248
}
249 249

	
250 250
while [ $# -gt 0 ]
251 251
do
252 252
    
253 253
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
254 254
    then
255 255
	echo -n \
256 256
"Usage:
257 257
  $0 [OPTIONS] [files]
258 258
Options:
259 259
  --dry-run|-n
260
     Check the given files, but do not modify them.
260
     Check the files, but do not modify them.
261 261
  --interactive|-i
262
     If --dry-run is specified and files are warned then a message is
263
     prompted whether the warnings should be turned to errors.
262
     If --dry-run is specified and the checker emits warnings,
263
     then the user is asked if the warnings should be considered
264
     errors.
264 265
  --werror|-w
265
     If --dry-run is specified and the warnings are turned to errors.
266
     Make all warnings into errors.
266 267
  --all|-a
267 268
     All files in the repository will be checked.
268 269
  --modified|-m
269
     Check only the modified source files. This option is proper to
270
     use before a commit. E.g. all files which are modified or added
271
     into the repository will be updated.
270
     Check only the modified (and new) source files. This option is
271
     useful to check the modification before making a commit.
272 272
  --changed|-c
273 273
     Check only the changed source files compared to the parent(s) of
274
     the current hg node.  This option is proper to use as hg hook
275
     script. E.g. to check all your commited source files with this
276
     script add the following section to the appropriate .hg/hgrc
277
     file.
274
     the current hg node.  This option is useful as hg hook script.
275
     To automatically check all your changes before making a commit,
276
     add the following section to the appropriate .hg/hgrc file.
278 277

	
279 278
       [hooks]
280 279
       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
281 280

	
282 281
  --help|-h
283 282
     Print this help message.
284 283
  files
285 284
     The files to check/unify. If no file names are given, the
286 285
     modified source will be checked/unified
287 286

	
288 287
"
289 288
        exit 0
290 289
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
291 290
    then
292 291
	[ -n "$ACTION" ] && echo "Invalid option $1" >&2 && exit 1
293 292
	ACTION=check
294 293
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
295 294
    then
296 295
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
297 296
	FILES=all_files
298 297
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
299 298
    then
300 299
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
301 300
	FILES=changed_files
302 301
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
303 302
    then
304 303
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
305 304
	FILES=modified_files
306 305
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
307 306
    then
308 307
	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
309 308
	WARNING='INTERACTIVE'
310 309
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
311 310
    then
312 311
	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
313 312
	WARNING='WERROR'
314 313
    elif [ $(echo $1 | cut -c 1) == '-' ]
315 314
    then
316 315
	echo "Invalid option $1" >&2 && exit 1
317 316
    else
318 317
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
319 318
	GIVEN_FILES=$@
320 319
	FILES=given_files
321 320
	break
322 321
    fi
323 322
    
324 323
    shift
325 324
done
326 325

	
327 326
if [ -z $FILES ]
328 327
then
329 328
    FILES=modified_files
330 329
fi
331 330

	
332 331
if [ -z $ACTION ]
333 332
then
334 333
    ACTION=update
335 334
fi
336 335

	
337 336
process_all
0 comments (0 inline)