COIN-OR::LEMON - Graph Library

source: lemon/scripts/unify-sources.sh @ 675:586b65073025

Last change on this file since 675:586b65073025 was 675:586b65073025, checked in by Alpar Juttner <alpar@…>, 15 years ago

Merge

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