scripts/unify-sources.sh
author Balazs Dezso <deba@inf.elte.hu>
Sun, 19 Oct 2008 16:19:32 +0200
changeset 336 e05633b02e40
parent 208 4317d277ba21
child 337 0fbbb4bc42dd
child 340 cdbff91c2166
permissions -rwxr-xr-x
Improved unifier and checker script

- Unify and check modes
- Several file listing modes
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.
deba@336
    68
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
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
deba@336
    91
	echo -n " [$3 failed]"
deba@336
    92
	FAILED=YES
deba@336
    93
    fi
deba@336
    94
}
deba@336
    95
deba@336
    96
function check_warning() {
deba@336
    97
    echo -n " [$2 warning]"
deba@336
    98
    WARNED=YES
deba@336
    99
}
deba@336
   100
deba@336
   101
function check_init() {
deba@336
   102
    echo Check source files...
deba@336
   103
    FAILED_FILES=0
deba@336
   104
    WARNED_FILES=0
deba@336
   105
    TOTAL_FILES=0
deba@336
   106
}
deba@336
   107
deba@336
   108
function check_done() {
deba@336
   109
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
deba@336
   110
    echo $WARNED_FILES out of $TOTAL_FILES files has been warned.
deba@336
   111
deba@336
   112
    if [ $FAILED_FILES -gt 0 ]
deba@336
   113
    then
deba@336
   114
	return 1
deba@336
   115
    elif [ $WARNED_FILES -gt 0 ]
deba@336
   116
    then
deba@336
   117
	if [ "$WARNING" == 'INTERACTIVE' ]
deba@336
   118
	then
deba@336
   119
	    echo -n "Assume as normal behaviour? (yes/no) "
deba@336
   120
	    while read answer
deba@336
   121
	    do
deba@336
   122
		if [ "$answer" == 'yes' ]
deba@336
   123
		then
deba@336
   124
		    return 0
deba@336
   125
		elif [ "$answer" == 'no' ]
deba@336
   126
		then
deba@336
   127
		    return 1
deba@336
   128
		fi
deba@336
   129
		echo -n "Assume as normal behaviour? (yes/no) "		    
deba@336
   130
	    done
deba@336
   131
	elif [ "$WARNING" == 'WERROR' ]
deba@336
   132
	then
deba@336
   133
	    return 1
deba@336
   134
	fi
deba@336
   135
    fi
deba@336
   136
}
deba@336
   137
deba@336
   138
function check_begin() {
deba@336
   139
    ((TOTAL_FILES++))
deba@336
   140
    FAILED=NO
deba@336
   141
    WARNED=NO
deba@336
   142
}
deba@336
   143
deba@336
   144
function check_end() {
deba@336
   145
    if [ $FAILED == YES ]
deba@336
   146
    then
deba@336
   147
	((++FAILED_FILES))
deba@336
   148
    fi
deba@336
   149
    if [ $WARNED == YES ]
deba@336
   150
    then
deba@336
   151
	((++WARNED_FILES))
deba@336
   152
    fi
deba@336
   153
}
deba@336
   154
deba@336
   155
deba@336
   156
deba@336
   157
# checks
deba@336
   158
deba@336
   159
function header_check() {
deba@336
   160
    if echo $1 | grep -q -E 'Makefile\.am$'
deba@336
   161
    then
deba@336
   162
	return
deba@336
   163
    fi
deba@336
   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@336
   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@336
   191
    ) >$TMP_FILE
alpar@208
   192
deba@336
   193
    "$ACTION"_action "$TMP_FILE" "$1" header
alpar@38
   194
}
alpar@38
   195
deba@336
   196
function tabs_check() {
deba@336
   197
    if echo $1 | grep -q -v -E 'Makefile\.am$'
deba@336
   198
    then
deba@336
   199
        OLD_PATTERN=$(echo -e '\t')
deba@336
   200
        NEW_PATTERN='        '
deba@336
   201
    else
deba@336
   202
        OLD_PATTERN='        '
deba@336
   203
        NEW_PATTERN=$(echo -e '\t')
deba@336
   204
    fi
alpar@208
   205
    TMP_FILE=`mktemp`
deba@336
   206
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
alpar@38
   207
deba@336
   208
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
alpar@208
   209
}
alpar@208
   210
deba@336
   211
function spaces_check() {
alpar@208
   212
    TMP_FILE=`mktemp`
deba@336
   213
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
alpar@208
   214
deba@336
   215
    "$ACTION"_action "$TMP_FILE" "$1" 'spaces'
alpar@208
   216
}
alpar@208
   217
deba@336
   218
function long_lines_check() {
deba@336
   219
    if cat $1 | grep -q -E '.{81,}'
alpar@208
   220
    then
deba@336
   221
	"$ACTION"_warning $1 'long lines'
alpar@208
   222
    fi
alpar@208
   223
}
alpar@208
   224
deba@336
   225
# process the file
deba@336
   226
deba@336
   227
function process_file() {
deba@336
   228
    echo -n "    $ACTION " $1...
deba@336
   229
deba@336
   230
    CHECKING="header tabs spaces long_lines"
deba@336
   231
deba@336
   232
    "$ACTION"_begin $1
deba@336
   233
    for check in $CHECKING
alpar@38
   234
    do
deba@336
   235
	"$check"_check $1
alpar@38
   236
    done
deba@336
   237
    "$ACTION"_end $1
deba@336
   238
    echo
deba@336
   239
}
deba@336
   240
deba@336
   241
function process_all {
deba@336
   242
    "$ACTION"_init
deba@336
   243
    while read file
alpar@38
   244
    do
deba@336
   245
	process_file $file
deba@336
   246
    done < <($FILES)
deba@336
   247
    "$ACTION"_done
deba@336
   248
}
deba@336
   249
deba@336
   250
while [ $# -gt 0 ]
deba@336
   251
do
deba@336
   252
    
deba@336
   253
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
deba@336
   254
    then
deba@336
   255
	echo -n \
deba@336
   256
"Usage:
deba@336
   257
  $0 [OPTIONS] [files]
deba@336
   258
Options:
deba@336
   259
  --dry-run|-n
deba@336
   260
     Check the given files, but do not modify them.
deba@336
   261
  --interactive|-i
deba@336
   262
     If --dry-run is specified and files are warned then a message is
deba@336
   263
     prompted whether the warnings should be turned to errors.
deba@336
   264
  --werror|-w
deba@336
   265
     If --dry-run is specified and the warnings are turned to errors.
deba@336
   266
  --all|-a
deba@336
   267
     All files in the repository will be checked.
deba@336
   268
  --modified|-m
deba@336
   269
     Check only the modified source files. This option is proper to
deba@336
   270
     use before a commit. E.g. all files which are modified or added
deba@336
   271
     into the repository will be updated.
deba@336
   272
  --changed|-c
deba@336
   273
     Check only the changed source files compared to the parent(s) of
deba@336
   274
     the current hg node.  This option is proper to use as hg hook
deba@336
   275
     script. E.g. to check all your commited source files with this
deba@336
   276
     script add the following section to the appropriate .hg/hgrc
deba@336
   277
     file.
deba@336
   278
deba@336
   279
       [hooks]
deba@336
   280
       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
deba@336
   281
deba@336
   282
  --help|-h
deba@336
   283
     Print this help message.
deba@336
   284
  files
deba@336
   285
     The files to check/unify. If no file names are given, the
deba@336
   286
     modified source will be checked/unified
deba@336
   287
deba@336
   288
"
deba@336
   289
        exit 0
deba@336
   290
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
deba@336
   291
    then
deba@336
   292
	[ -n "$ACTION" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   293
	ACTION=check
deba@336
   294
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
deba@336
   295
    then
deba@336
   296
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   297
	FILES=all_files
deba@336
   298
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
deba@336
   299
    then
deba@336
   300
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   301
	FILES=changed_files
deba@336
   302
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
deba@336
   303
    then
deba@336
   304
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   305
	FILES=modified_files
deba@336
   306
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
deba@336
   307
    then
deba@336
   308
	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   309
	WARNING='INTERACTIVE'
deba@336
   310
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
deba@336
   311
    then
deba@336
   312
	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   313
	WARNING='WERROR'
deba@336
   314
    elif [ $(echo $1 | cut -c 1) == '-' ]
deba@336
   315
    then
deba@336
   316
	echo "Invalid option $1" >&2 && exit 1
deba@336
   317
    else
deba@336
   318
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   319
	GIVEN_FILES=$@
deba@336
   320
	FILES=given_files
deba@336
   321
	break
deba@336
   322
    fi
deba@336
   323
    
deba@336
   324
    shift
deba@336
   325
done
deba@336
   326
deba@336
   327
if [ -z $FILES ]
deba@336
   328
then
deba@336
   329
    FILES=modified_files
alpar@38
   330
fi
deba@336
   331
deba@336
   332
if [ -z $ACTION ]
deba@336
   333
then
deba@336
   334
    ACTION=update
alpar@208
   335
fi
deba@336
   336
deba@336
   337
process_all