scripts/unify-sources.sh
author Balazs Dezso <deba@inf.elte.hu>
Thu, 24 Jun 2010 09:27:53 +0200
changeset 732 bb70ad62c95f
parent 623 586b65073025
permissions -rwxr-xr-x
Fix critical bug in preflow (#372)

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