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