summaryrefslogtreecommitdiff
path: root/lemon/scripts/unify-sources.sh
blob: 6aae63ab64137c72463b56cb00de96e49a18954c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
#
# This file is a part of LEMON, a generic C++ optimization library.
#
# Copyright (C) 2003-2009
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
# (Egervary Research Group on Combinatorial Optimization, EGRES).
#
# Permission to use, modify and distribute this software is granted
# provided that this copyright notice appears in all copies. For
# precise terms see the accompanying LICENSE file.
#
# This software is provided "AS IS" with no warranty of any kind,
# express or implied, and with no claim as to its suitability for any
# purpose.

YEAR=`date +%Y`
HGROOT=`hg root`

function hg_year() {
    if [ -n "$(hg st $1)" ]; then
        echo $YEAR
    else
        hg log -l 1 --template='{date|isodate}\n' $1 |
        cut -d '-' -f 1
    fi
}

# file enumaration modes

function all_files() {
    hg status -a -m -c |
    cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
    while read file; do echo $HGROOT/$file; done
}

function modified_files() {
    hg status -a -m |
    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' |
    while read file; do echo $HGROOT/$file; done
}

function changed_files() {
    {
        if [ -n "$HG_PARENT1" ]
        then
            hg status --rev $HG_PARENT1:$HG_NODE -a -m
        fi
        if [ -n "$HG_PARENT2" ]
        then
            hg status --rev $HG_PARENT2:$HG_NODE -a -m
        fi
    } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | 
    sort | uniq |
    while read file; do echo $HGROOT/$file; done
}

function given_files() {
    for file in $GIVEN_FILES
    do
	echo $file
    done
}

# actions

function update_action() {
    if ! diff -q $1 $2 >/dev/null
    then
	echo -n " [$3 updated]"
	rm $2
	mv $1 $2
	CHANGED=YES
    fi
}

function update_warning() {
    echo -n " [$2 warning]"
    WARNED=YES
}

function update_init() {
    echo Update source files...
    TOTAL_FILES=0
    CHANGED_FILES=0
    WARNED_FILES=0
}

function update_done() {
    echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
}

function update_begin() {
    ((TOTAL_FILES++))
    CHANGED=NO
    WARNED=NO
}

function update_end() {
    if [ $CHANGED == YES ]
    then
	((++CHANGED_FILES))
    fi
    if [ $WARNED == YES ]
    then
	((++WARNED_FILES))
    fi
}

function check_action() {
    if [ "$3" == 'tabs' ]
    then
        if echo $2 | grep -q -v -E 'Makefile\.am$'
        then
            PATTERN=$(echo -e '\t')
        else
            PATTERN='        '
        fi
    elif [ "$3" == 'trailing spaces' ]
    then
        PATTERN='\ +$'
    else
        PATTERN='*'
    fi

    if ! diff -q $1 $2 >/dev/null
    then
        if [ "$PATTERN" == '*' ]
        then
            diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
              sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
        else
            grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
        fi
        FAILED=YES
    fi
}

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

function check_init() {
    echo Check source files...
    FAILED_FILES=0
    WARNED_FILES=0
    TOTAL_FILES=0
}

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

    if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
    then
	if [ "$WARNING" == 'INTERACTIVE' ]
	then
	    echo -n "Are the files with errors/warnings acceptable? (yes/no) "
	    while read answer
	    do
		if [ "$answer" == 'yes' ]
		then
		    return 0
		elif [ "$answer" == 'no' ]
		then
		    return 1
		fi
		echo -n "Are the files with errors/warnings acceptable? (yes/no) "
	    done
	elif [ "$WARNING" == 'WERROR' ]
	then
	    return 1
	fi
    fi
}

function check_begin() {
    ((TOTAL_FILES++))
    FAILED=NO
    WARNED=NO
}

function check_end() {
    if [ $FAILED == YES ]
    then
	((++FAILED_FILES))
    fi
    if [ $WARNED == YES ]
    then
	((++WARNED_FILES))
    fi
}



# checks

function header_check() {
    if echo $1 | grep -q -E 'Makefile\.am$'
    then
	return
    fi

    TMP_FILE=`mktemp`

    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-"$(hg_year $1)"
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided \"AS IS\" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */
"
    awk 'BEGIN { pm=0; }
     pm==3 { print }
     /\/\* / && pm==0 { pm=1;}
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
     /\*\// && pm==1 { pm=2;}
    ' $1
    ) >$TMP_FILE

    "$ACTION"_action "$TMP_FILE" "$1" header
}

function tabs_check() {
    if echo $1 | grep -q -v -E 'Makefile\.am$'
    then
        OLD_PATTERN=$(echo -e '\t')
        NEW_PATTERN='        '
    else
        OLD_PATTERN='        '
        NEW_PATTERN=$(echo -e '\t')
    fi
    TMP_FILE=`mktemp`
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE

    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
}

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

    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
}

function long_lines_check() {
    if cat $1 | grep -q -E '.{81,}'
    then
	"$ACTION"_warning $1 'long lines'
    fi
}

# process the file

function process_file() {
    if [ "$ACTION" == 'update' ]
    then
        echo -n "    $ACTION $1..."
    else
        echo "	  $ACTION $1..."
    fi

    CHECKING="header tabs spaces long_lines"

    "$ACTION"_begin $1
    for check in $CHECKING
    do
	"$check"_check $1
    done
    "$ACTION"_end $1
    if [ "$ACTION" == 'update' ]
    then
        echo
    fi
}

function process_all {
    "$ACTION"_init
    while read file
    do
	process_file $file
    done < <($FILES)
    "$ACTION"_done
}

while [ $# -gt 0 ]
do
    
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
    then
	echo -n \
"Usage:
  $0 [OPTIONS] [files]
Options:
  --dry-run|-n
     Check the files, but do not modify them.
  --interactive|-i
     If --dry-run is specified and the checker emits warnings,
     then the user is asked if the warnings should be considered
     errors.
  --werror|-w
     Make all warnings into errors.
  --all|-a
     Check all source files in the repository.
  --modified|-m
     Check only the modified (and new) source files. This option is
     useful to check the modification before making a commit.
  --changed|-c
     Check only the changed source files compared to the parent(s) of
     the current hg node.  This option is useful as hg hook script.
     To automatically check all your changes before making a commit,
     add the following section to the appropriate .hg/hgrc file.

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

  --help|-h
     Print this help message.
  files
     The files to check/unify. If no file names are given, the modified
     source files will be checked/unified (just like using the
     --modified|-m option).
"
        exit 0
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
    then
	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
	ACTION=check
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
    then
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
	FILES=all_files
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
    then
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
	FILES=changed_files
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
    then
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
	FILES=modified_files
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
    then
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
	WARNING='INTERACTIVE'
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
    then
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
	WARNING='WERROR'
    elif [ $(echo x$1 | cut -c 2) == '-' ]
    then
	echo "Invalid option $1" >&2 && exit 1
    else
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
	GIVEN_FILES=$@
	FILES=given_files
	break
    fi
    
    shift
done

if [ -z $FILES ]
then
    FILES=modified_files
fi

if [ -z $ACTION ]
then
    ACTION=update
fi

process_all