scripts/unify-sources.sh
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 20 Oct 2008 12:17:24 +0200
changeset 328 cdbff91c2166
parent 324 e05633b02e40
child 329 d900fd1e760f
permissions -rwxr-xr-x
Small improvements in the unifier script
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.
deba@324
    68
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
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() {
deba@324
    89
    if ! diff -q $1 $2 >/dev/null
deba@324
    90
    then
deba@324
    91
	echo -n " [$3 failed]"
deba@324
    92
	FAILED=YES
deba@324
    93
    fi
deba@324
    94
}
deba@324
    95
deba@324
    96
function check_warning() {
deba@324
    97
    echo -n " [$2 warning]"
deba@324
    98
    WARNED=YES
deba@324
    99
}
deba@324
   100
deba@324
   101
function check_init() {
deba@324
   102
    echo Check source files...
deba@324
   103
    FAILED_FILES=0
deba@324
   104
    WARNED_FILES=0
deba@324
   105
    TOTAL_FILES=0
deba@324
   106
}
deba@324
   107
deba@324
   108
function check_done() {
deba@324
   109
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
deba@324
   110
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
deba@324
   111
deba@324
   112
    if [ $FAILED_FILES -gt 0 ]
deba@324
   113
    then
deba@324
   114
	return 1
deba@324
   115
    elif [ $WARNED_FILES -gt 0 ]
deba@324
   116
    then
deba@324
   117
	if [ "$WARNING" == 'INTERACTIVE' ]
deba@324
   118
	then
deba@324
   119
	    echo -n "Assume as normal behaviour? (yes/no) "
deba@324
   120
	    while read answer
deba@324
   121
	    do
deba@324
   122
		if [ "$answer" == 'yes' ]
deba@324
   123
		then
deba@324
   124
		    return 0
deba@324
   125
		elif [ "$answer" == 'no' ]
deba@324
   126
		then
deba@324
   127
		    return 1
deba@324
   128
		fi
deba@324
   129
		echo -n "Assume as normal behaviour? (yes/no) "		    
deba@324
   130
	    done
deba@324
   131
	elif [ "$WARNING" == 'WERROR' ]
deba@324
   132
	then
deba@324
   133
	    return 1
deba@324
   134
	fi
deba@324
   135
    fi
deba@324
   136
}
deba@324
   137
deba@324
   138
function check_begin() {
deba@324
   139
    ((TOTAL_FILES++))
deba@324
   140
    FAILED=NO
deba@324
   141
    WARNED=NO
deba@324
   142
}
deba@324
   143
deba@324
   144
function check_end() {
deba@324
   145
    if [ $FAILED == YES ]
deba@324
   146
    then
deba@324
   147
	((++FAILED_FILES))
deba@324
   148
    fi
deba@324
   149
    if [ $WARNED == YES ]
deba@324
   150
    then
deba@324
   151
	((++WARNED_FILES))
deba@324
   152
    fi
deba@324
   153
}
deba@324
   154
deba@324
   155
deba@324
   156
deba@324
   157
# checks
deba@324
   158
deba@324
   159
function header_check() {
deba@324
   160
    if echo $1 | grep -q -E 'Makefile\.am$'
deba@324
   161
    then
deba@324
   162
	return
deba@324
   163
    fi
deba@324
   164
alpar@38
   165
    TMP_FILE=`mktemp`
alpar@38
   166
alpar@208
   167
    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@38
   168
 *
alpar@208
   169
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@38
   170
 *
alpar@38
   171
 * Copyright (C) "$YEAR"
alpar@38
   172
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@38
   173
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@38
   174
 *
alpar@38
   175
 * Permission to use, modify and distribute this software is granted
alpar@38
   176
 * provided that this copyright notice appears in all copies. For
alpar@38
   177
 * precise terms see the accompanying LICENSE file.
alpar@38
   178
 *
alpar@38
   179
 * This software is provided \"AS IS\" with no warranty of any kind,
alpar@38
   180
 * express or implied, and with no claim as to its suitability for any
alpar@38
   181
 * purpose.
alpar@38
   182
 *
alpar@38
   183
 */
alpar@38
   184
"
deba@324
   185
    awk 'BEGIN { pm=0; }
alpar@38
   186
     pm==3 { print }
alpar@38
   187
     /\/\* / && pm==0 { pm=1;}
alpar@38
   188
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
alpar@38
   189
     /\*\// && pm==1 { pm=2;}
alpar@38
   190
    ' $1
deba@324
   191
    ) >$TMP_FILE
alpar@208
   192
deba@324
   193
    "$ACTION"_action "$TMP_FILE" "$1" header
alpar@38
   194
}
alpar@38
   195
deba@324
   196
function tabs_check() {
deba@324
   197
    if echo $1 | grep -q -v -E 'Makefile\.am$'
deba@324
   198
    then
deba@324
   199
        OLD_PATTERN=$(echo -e '\t')
deba@324
   200
        NEW_PATTERN='        '
deba@324
   201
    else
deba@324
   202
        OLD_PATTERN='        '
deba@324
   203
        NEW_PATTERN=$(echo -e '\t')
deba@324
   204
    fi
alpar@208
   205
    TMP_FILE=`mktemp`
deba@324
   206
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
alpar@38
   207
deba@324
   208
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
alpar@208
   209
}
alpar@208
   210
deba@324
   211
function spaces_check() {
alpar@208
   212
    TMP_FILE=`mktemp`
deba@324
   213
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
alpar@208
   214
kpeter@328
   215
    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
alpar@208
   216
}
alpar@208
   217
deba@324
   218
function long_lines_check() {
deba@324
   219
    if cat $1 | grep -q -E '.{81,}'
alpar@208
   220
    then
deba@324
   221
	"$ACTION"_warning $1 'long lines'
alpar@208
   222
    fi
alpar@208
   223
}
alpar@208
   224
deba@324
   225
# process the file
deba@324
   226
deba@324
   227
function process_file() {
kpeter@328
   228
    echo -n "    $ACTION $1..."
deba@324
   229
deba@324
   230
    CHECKING="header tabs spaces long_lines"
deba@324
   231
deba@324
   232
    "$ACTION"_begin $1
deba@324
   233
    for check in $CHECKING
alpar@38
   234
    do
deba@324
   235
	"$check"_check $1
alpar@38
   236
    done
deba@324
   237
    "$ACTION"_end $1
deba@324
   238
    echo
deba@324
   239
}
deba@324
   240
deba@324
   241
function process_all {
deba@324
   242
    "$ACTION"_init
deba@324
   243
    while read file
alpar@38
   244
    do
deba@324
   245
	process_file $file
deba@324
   246
    done < <($FILES)
deba@324
   247
    "$ACTION"_done
deba@324
   248
}
deba@324
   249
deba@324
   250
while [ $# -gt 0 ]
deba@324
   251
do
deba@324
   252
    
deba@324
   253
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
deba@324
   254
    then
deba@324
   255
	echo -n \
deba@324
   256
"Usage:
deba@324
   257
  $0 [OPTIONS] [files]
deba@324
   258
Options:
deba@324
   259
  --dry-run|-n
deba@324
   260
     Check the given files, but do not modify them.
deba@324
   261
  --interactive|-i
kpeter@328
   262
     If --dry-run is specified and files are warned, then a message is
deba@324
   263
     prompted whether the warnings should be turned to errors.
deba@324
   264
  --werror|-w
kpeter@328
   265
     If --dry-run is specified, the warnings are turned to errors.
deba@324
   266
  --all|-a
kpeter@328
   267
     Check all source files in the repository.
deba@324
   268
  --modified|-m
deba@324
   269
     Check only the modified source files. This option is proper to
deba@324
   270
     use before a commit. E.g. all files which are modified or added
deba@324
   271
     into the repository will be updated.
deba@324
   272
  --changed|-c
deba@324
   273
     Check only the changed source files compared to the parent(s) of
deba@324
   274
     the current hg node.  This option is proper to use as hg hook
deba@324
   275
     script. E.g. to check all your commited source files with this
deba@324
   276
     script add the following section to the appropriate .hg/hgrc
deba@324
   277
     file.
deba@324
   278
deba@324
   279
       [hooks]
deba@324
   280
       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
deba@324
   281
deba@324
   282
  --help|-h
deba@324
   283
     Print this help message.
deba@324
   284
  files
kpeter@328
   285
     The files to check/unify. If no file names are given, the modified
kpeter@328
   286
     source files will be checked/unified (just like using the
kpeter@328
   287
     --modified|-m option).
deba@324
   288
"
deba@324
   289
        exit 0
deba@324
   290
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
deba@324
   291
    then
kpeter@328
   292
	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
deba@324
   293
	ACTION=check
deba@324
   294
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
deba@324
   295
    then
kpeter@328
   296
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@324
   297
	FILES=all_files
deba@324
   298
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
deba@324
   299
    then
kpeter@328
   300
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@324
   301
	FILES=changed_files
deba@324
   302
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
deba@324
   303
    then
kpeter@328
   304
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@324
   305
	FILES=modified_files
deba@324
   306
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
deba@324
   307
    then
kpeter@328
   308
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
deba@324
   309
	WARNING='INTERACTIVE'
deba@324
   310
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
deba@324
   311
    then
kpeter@328
   312
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
deba@324
   313
	WARNING='WERROR'
kpeter@328
   314
    elif [ $(echo x$1 | cut -c 2) == '-' ]
deba@324
   315
    then
deba@324
   316
	echo "Invalid option $1" >&2 && exit 1
deba@324
   317
    else
deba@324
   318
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@324
   319
	GIVEN_FILES=$@
deba@324
   320
	FILES=given_files
deba@324
   321
	break
deba@324
   322
    fi
deba@324
   323
    
deba@324
   324
    shift
deba@324
   325
done
deba@324
   326
deba@324
   327
if [ -z $FILES ]
deba@324
   328
then
deba@324
   329
    FILES=modified_files
alpar@38
   330
fi
deba@324
   331
deba@324
   332
if [ -z $ACTION ]
deba@324
   333
then
deba@324
   334
    ACTION=update
alpar@208
   335
fi
deba@324
   336
deba@324
   337
process_all