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