COIN-OR::LEMON - Graph Library

source: lemon/scripts/unify-sources.sh @ 735:1f08e846df29

Last change on this file since 735:1f08e846df29 was 702:c706534d4740, checked in by Alpar Juttner <alpar@…>, 11 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
[538]3YEAR=`date +%Y`
[38]4HGROOT=`hg root`
5
[538]6function hg_year() {
7    if [ -n "$(hg st $1)" ]; then
8        echo $YEAR
[702]9    else
10        hg log -l 1 --template='{date|isodate}\n' $1 |
11        cut -d '-' -f 1
12    fi
[675]13}
14
[336]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
[538]60    fi
61}
62
[336]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.
[337]77    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
[336]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() {
[353]98    if [ "$3" == 'tabs' ]
99    then
[601]100        if echo $2 | grep -q -v -E 'Makefile\.am$'
101        then
102            PATTERN=$(echo -e '\t')
103        else
104            PATTERN='        '
105        fi
[353]106    elif [ "$3" == 'trailing spaces' ]
107    then
108        PATTERN='\ +$'
109    else
110        PATTERN='*'
111    fi
112
[336]113    if ! diff -q $1 $2 >/dev/null
114    then
[353]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
[336]123    fi
124}
125
126function check_warning() {
[341]127    if [ "$2" == 'long lines' ]
128    then
[353]129        grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
[341]130    else
[353]131        echo "$1: warning: $2"
[341]132    fi
[336]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.
[337]145    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
[336]146
[411]147    if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
[336]148    then
149        if [ "$WARNING" == 'INTERACTIVE' ]
150        then
[411]151            echo -n "Are the files with errors/warnings acceptable? (yes/no) "
[336]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
[411]161                echo -n "Are the files with errors/warnings acceptable? (yes/no) "
[336]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 *
[538]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"
[336]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
[336]223    ) >$TMP_FILE
[208]224
[336]225    "$ACTION"_action "$TMP_FILE" "$1" header
[38]226}
227
[336]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`
[336]238    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
[38]239
[336]240    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
[208]241}
242
[336]243function spaces_check() {
[208]244    TMP_FILE=`mktemp`
[336]245    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
[208]246
[340]247    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
[208]248}
249
[336]250function long_lines_check() {
251    if cat $1 | grep -q -E '.{81,}'
[208]252    then
[336]253        "$ACTION"_warning $1 'long lines'
[208]254    fi
255}
256
[336]257# process the file
258
259function process_file() {
[353]260    if [ "$ACTION" == 'update' ]
261    then
262        echo -n "    $ACTION $1..."
263    else
264        echo "    $ACTION $1..."
265    fi
[336]266
267    CHECKING="header tabs spaces long_lines"
268
269    "$ACTION"_begin $1
270    for check in $CHECKING
[38]271    do
[336]272        "$check"_check $1
[38]273    done
[336]274    "$ACTION"_end $1
[353]275    if [ "$ACTION" == 'update' ]
276    then
277        echo
278    fi
[336]279}
280
281function process_all {
282    "$ACTION"_init
283    while read file
[38]284    do
[336]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
[337]300     Check the files, but do not modify them.
[336]301  --interactive|-i
[337]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.
[336]305  --werror|-w
[337]306     Make all warnings into errors.
[336]307  --all|-a
[340]308     Check all source files in the repository.
[336]309  --modified|-m
[337]310     Check only the modified (and new) source files. This option is
311     useful to check the modification before making a commit.
[336]312  --changed|-c
313     Check only the changed source files compared to the parent(s) of
[337]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.
[336]317
318       [hooks]
319       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
320
321  --help|-h
322     Print this help message.
323  files
[340]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).
[336]327"
328        exit 0
329    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
330    then
[340]331        [ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
[336]332        ACTION=check
333    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
334    then
[340]335        [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
[336]336        FILES=all_files
337    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
338    then
[340]339        [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
[336]340        FILES=changed_files
341    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
342    then
[340]343        [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
[336]344        FILES=modified_files
345    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
346    then
[340]347        [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
[336]348        WARNING='INTERACTIVE'
349    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
350    then
[340]351        [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
[336]352        WARNING='WERROR'
[340]353    elif [ $(echo x$1 | cut -c 2) == '-' ]
[336]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
[336]370
371if [ -z $ACTION ]
372then
373    ACTION=update
[208]374fi
[336]375
376process_all
Note: See TracBrowser for help on using the repository browser.