Changeset 628:586b65073025 in lemon-1.2 for scripts
- Timestamp:
- 04/28/09 15:01:00 (16 years ago)
- Branch:
- default
- Children:
- 630:936355864d6e, 631:d21b38647e53, 645:cb8270a98660
- Parents:
- 627:20dac2104519 (diff), 498:a76f55d7d397 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
scripts/unify-sources.sh
r498 r628 7 7 if [ -n "$(hg st $1)" ]; then 8 8 echo $YEAR 9 else 10 hg log -l 1 --template='{date|isodate}\n' $1 | 11 cut -d '-' -f 1 12 fi 13 } 14 15 function update_header() { 9 } 10 11 # file enumaration modes 12 13 function 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 19 function 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 25 function 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 40 function given_files() { 41 for file in $GIVEN_FILES 42 do 43 echo $file 44 done 45 } 46 47 # actions 48 49 function 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 56 fi 57 } 58 59 function update_warning() { 60 echo -n " [$2 warning]" 61 WARNED=YES 62 } 63 64 function update_init() { 65 echo Update source files... 66 TOTAL_FILES=0 67 CHANGED_FILES=0 68 WARNED_FILES=0 69 } 70 71 function update_done() { 72 echo $CHANGED_FILES out of $TOTAL_FILES files has been changed. 73 echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings. 74 } 75 76 function update_begin() { 77 ((TOTAL_FILES++)) 78 CHANGED=NO 79 WARNED=NO 80 } 81 82 function 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 93 function check_action() { 94 if [ "$3" == 'tabs' ] 95 then 96 if echo $2 | grep -q -v -E 'Makefile\.am$' 97 then 98 PATTERN=$(echo -e '\t') 99 else 100 PATTERN=' ' 101 fi 102 elif [ "$3" == 'trailing spaces' ] 103 then 104 PATTERN='\ +$' 105 else 106 PATTERN='*' 107 fi 108 109 if ! diff -q $1 $2 >/dev/null 110 then 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 119 fi 120 } 121 122 function check_warning() { 123 if [ "$2" == 'long lines' ] 124 then 125 grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g" 126 else 127 echo "$1: warning: $2" 128 fi 129 WARNED=YES 130 } 131 132 function check_init() { 133 echo Check source files... 134 FAILED_FILES=0 135 WARNED_FILES=0 136 TOTAL_FILES=0 137 } 138 139 function check_done() { 140 echo $FAILED_FILES out of $TOTAL_FILES files has been failed. 141 echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings. 142 143 if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ] 144 then 145 if [ "$WARNING" == 'INTERACTIVE' ] 146 then 147 echo -n "Are the files with errors/warnings acceptable? (yes/no) " 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 157 echo -n "Are the files with errors/warnings acceptable? (yes/no) " 158 done 159 elif [ "$WARNING" == 'WERROR' ] 160 then 161 return 1 162 fi 163 fi 164 } 165 166 function check_begin() { 167 ((TOTAL_FILES++)) 168 FAILED=NO 169 WARNED=NO 170 } 171 172 function 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 187 function header_check() { 188 if echo $1 | grep -q -E 'Makefile\.am$' 189 then 190 return 191 fi 192 16 193 TMP_FILE=`mktemp` 17 FILE_NAME=$118 194 19 195 (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*- … … 35 211 */ 36 212 " 37 213 awk 'BEGIN { pm=0; } 38 214 pm==3 { print } 39 215 /\/\* / && pm==0 { pm=1;} … … 41 217 /\*\// && pm==1 { pm=2;} 42 218 ' $1 43 ) >$TMP_FILE 44 45 HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` 46 47 rm $FILE_NAME 48 mv $TMP_FILE $FILE_NAME 49 } 50 51 function update_tabs() { 219 ) >$TMP_FILE 220 221 "$ACTION"_action "$TMP_FILE" "$1" header 222 } 223 224 function 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 52 233 TMP_FILE=`mktemp` 53 FILE_NAME=$1 54 55 cat $1 | 56 sed -e 's/\t/ /g' >$TMP_FILE 57 58 TABS_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` 59 60 rm $FILE_NAME 61 mv $TMP_FILE $FILE_NAME 62 } 63 64 function remove_trailing_space() { 234 cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE 235 236 "$ACTION"_action "$TMP_FILE" "$1" 'tabs' 237 } 238 239 function spaces_check() { 65 240 TMP_FILE=`mktemp` 66 FILE_NAME=$1 67 68 cat $1 | 69 sed -e 's/ \+$//g' >$TMP_FILE 70 71 SPACES_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` 72 73 rm $FILE_NAME 74 mv $TMP_FILE $FILE_NAME 75 } 76 77 function long_line_test() { 78 cat $1 |grep -q -E '.{81,}' 79 } 80 81 function update_file() { 82 echo -n ' update' $i ... 83 84 update_header $1 85 update_tabs $1 86 remove_trailing_space $1 87 88 CHANGED=NO; 89 if [[ $HEADER_CH = YES ]]; 90 then 91 echo -n ' [header updated]' 92 CHANGED=YES; 93 fi 94 if [[ $TABS_CH = YES ]]; 95 then 96 echo -n ' [tabs removed]' 97 CHANGED=YES; 98 fi 99 if [[ $SPACES_CH = YES ]]; 100 then 101 echo -n ' [trailing spaces removed]' 102 CHANGED=YES; 103 fi 104 if long_line_test $1 ; 105 then 106 echo -n ' [LONG LINES]' 107 ((LONG_LINE_FILES++)) 108 fi 109 echo 110 if [[ $CHANGED = YES ]]; 111 then 112 ((CHANGED_FILES++)) 113 fi 114 } 115 116 CHANGED_FILES=0 117 TOTAL_FILES=0 118 LONG_LINE_FILES=0 119 if [ $# == 0 ]; then 120 echo Update all source files... 121 for i in `hg manifest|grep -E '\.(cc|h|dox)$'` 241 cat $1 | sed -e 's/ \+$//g' >$TMP_FILE 242 243 "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces' 244 } 245 246 function long_lines_check() { 247 if cat $1 | grep -q -E '.{81,}' 248 then 249 "$ACTION"_warning $1 'long lines' 250 fi 251 } 252 253 # process the file 254 255 function process_file() { 256 if [ "$ACTION" == 'update' ] 257 then 258 echo -n " $ACTION $1..." 259 else 260 echo " $ACTION $1..." 261 fi 262 263 CHECKING="header tabs spaces long_lines" 264 265 "$ACTION"_begin $1 266 for check in $CHECKING 122 267 do 123 update_file $HGROOT/$i 124 ((TOTAL_FILES++)) 268 "$check"_check $1 125 269 done 126 echo ' done.' 127 else 128 for i in $* 270 "$ACTION"_end $1 271 if [ "$ACTION" == 'update' ] 272 then 273 echo 274 fi 275 } 276 277 function process_all { 278 "$ACTION"_init 279 while read file 129 280 do 130 update_file $i 131 ((TOTAL_FILES++)) 132 done 281 process_file $file 282 done < <($FILES) 283 "$ACTION"_done 284 } 285 286 while [ $# -gt 0 ] 287 do 288 289 if [ "$1" == '--help' ] || [ "$1" == '-h' ] 290 then 291 echo -n \ 292 "Usage: 293 $0 [OPTIONS] [files] 294 Options: 295 --dry-run|-n 296 Check the files, but do not modify them. 297 --interactive|-i 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. 301 --werror|-w 302 Make all warnings into errors. 303 --all|-a 304 Check all source files in the repository. 305 --modified|-m 306 Check only the modified (and new) source files. This option is 307 useful to check the modification before making a commit. 308 --changed|-c 309 Check only the changed source files compared to the parent(s) of 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. 313 314 [hooks] 315 pretxncommit.checksources = scripts/unify-sources.sh -c -n -i 316 317 --help|-h 318 Print this help message. 319 files 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). 323 " 324 exit 0 325 elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ] 326 then 327 [ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1 328 ACTION=check 329 elif [ "$1" == "--all" ] || [ "$1" == '-a' ] 330 then 331 [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1 332 FILES=all_files 333 elif [ "$1" == "--changed" ] || [ "$1" == '-c' ] 334 then 335 [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1 336 FILES=changed_files 337 elif [ "$1" == "--modified" ] || [ "$1" == '-m' ] 338 then 339 [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1 340 FILES=modified_files 341 elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ] 342 then 343 [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1 344 WARNING='INTERACTIVE' 345 elif [ "$1" == "--werror" ] || [ "$1" == "-w" ] 346 then 347 [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1 348 WARNING='WERROR' 349 elif [ $(echo x$1 | cut -c 2) == '-' ] 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 360 done 361 362 if [ -z $FILES ] 363 then 364 FILES=modified_files 133 365 fi 134 echo $CHANGED_FILES out of $TOTAL_FILES files has been changed. 135 if [[ $LONG_LINE_FILES -gt 1 ]]; then 136 echo 137 echo WARNING: $LONG_LINE_FILES files contains long lines! 138 echo 139 elif [[ $LONG_LINE_FILES -gt 0 ]]; then 140 echo 141 echo WARNING: a file contains long lines! 142 echo 366 367 if [ -z $ACTION ] 368 then 369 ACTION=update 143 370 fi 371 372 process_all -
scripts/unify-sources.sh
r554 r628 1 1 #!/bin/bash 2 2 3 YEAR=`date + 2003-%Y`3 YEAR=`date +%Y` 4 4 HGROOT=`hg root` 5 6 function hg_year() { 7 if [ -n "$(hg st $1)" ]; then 8 echo $YEAR 9 } 5 10 6 11 # file enumaration modes … … 192 197 * This file is a part of LEMON, a generic C++ optimization library. 193 198 * 194 * Copyright (C) "$YEAR"199 * Copyright (C) 2003-"$(hg_year $1)" 195 200 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 196 201 * (Egervary Research Group on Combinatorial Optimization, EGRES).
Note: See TracChangeset
for help on using the changeset viewer.