COIN-OR::LEMON - Graph Library

source: lemon-main/scripts/unify-sources.sh @ 675:c5dc4d3124aa

Last change on this file since 675:c5dc4d3124aa was 655:c706534d4740, checked in by Alpar Juttner <alpar@…>, 16 years ago

Fix the faulty merge of unify-sources.sh in [586b65073025]

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