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