scripts/unify-sources.sh
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 29 Sep 2009 12:03:02 +0200
changeset 824 5764dd9b6e18
parent 675 586b65073025
child 780 abf31e4af617
permissions -rwxr-xr-x
Add a new build() function to StaticDigraph (#68)

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