COIN-OR::LEMON - Graph Library

source: lemon/scripts/unify-sources.sh @ 674:20dac2104519

Last change on this file since 674:20dac2104519 was 601:6b9c1f6eb1e5, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Bug fix in unify-sources.sh (#245)

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