3 # This file is a part of LEMON, a generic C++ optimization library.
 
     5 # Copyright (C) 2003-2009
 
     6 # Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7 # (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9 # Permission to use, modify and distribute this software is granted
 
    10 # provided that this copyright notice appears in all copies. For
 
    11 # precise terms see the accompanying LICENSE file.
 
    13 # This software is provided "AS IS" with no warranty of any kind,
 
    14 # express or implied, and with no claim as to its suitability for any
 
    21     if [ -n "$(hg st $1)" ]; then
 
    24         hg log -l 1 --template='{date|isodate}\n' $1 |
 
    29 # file enumaration modes
 
    31 function all_files() {
 
    33     cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
 
    34     while read file; do echo $HGROOT/$file; done
 
    37 function modified_files() {
 
    39     cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' |
 
    40     while read file; do echo $HGROOT/$file; done
 
    43 function changed_files() {
 
    45         if [ -n "$HG_PARENT1" ]
 
    47             hg status --rev $HG_PARENT1:$HG_NODE -a -m
 
    49         if [ -n "$HG_PARENT2" ]
 
    51             hg status --rev $HG_PARENT2:$HG_NODE -a -m
 
    53     } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | 
 
    55     while read file; do echo $HGROOT/$file; done
 
    58 function given_files() {
 
    59     for file in $GIVEN_FILES
 
    67 function update_action() {
 
    68     if ! diff -q $1 $2 >/dev/null
 
    70 	echo -n " [$3 updated]"
 
    77 function update_warning() {
 
    78     echo -n " [$2 warning]"
 
    82 function update_init() {
 
    83     echo Update source files...
 
    89 function update_done() {
 
    90     echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
 
    91     echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
 
    94 function update_begin() {
 
   100 function update_end() {
 
   101     if [ $CHANGED == YES ]
 
   105     if [ $WARNED == YES ]
 
   111 function check_action() {
 
   112     if [ "$3" == 'tabs' ]
 
   114         if echo $2 | grep -q -v -E 'Makefile\.am$'
 
   116             PATTERN=$(echo -e '\t')
 
   120     elif [ "$3" == 'trailing spaces' ]
 
   127     if ! diff -q $1 $2 >/dev/null
 
   129         if [ "$PATTERN" == '*' ]
 
   131             diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
 
   132               sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
 
   134             grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
 
   140 function check_warning() {
 
   141     if [ "$2" == 'long lines' ]
 
   143         grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
 
   145         echo "$1: warning: $2"
 
   150 function check_init() {
 
   151     echo Check source files...
 
   157 function check_done() {
 
   158     echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
 
   159     echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
 
   161     if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
 
   163 	if [ "$WARNING" == 'INTERACTIVE' ]
 
   165 	    echo -n "Are the files with errors/warnings acceptable? (yes/no) "
 
   168 		if [ "$answer" == 'yes' ]
 
   171 		elif [ "$answer" == 'no' ]
 
   175 		echo -n "Are the files with errors/warnings acceptable? (yes/no) "
 
   177 	elif [ "$WARNING" == 'WERROR' ]
 
   184 function check_begin() {
 
   190 function check_end() {
 
   191     if [ $FAILED == YES ]
 
   195     if [ $WARNED == YES ]
 
   205 function header_check() {
 
   206     if echo $1 | grep -q -E 'Makefile\.am$'
 
   213     (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
 
   215  * This file is a part of LEMON, a generic C++ optimization library.
 
   217  * Copyright (C) 2003-"$(hg_year $1)"
 
   218  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
   219  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
   221  * Permission to use, modify and distribute this software is granted
 
   222  * provided that this copyright notice appears in all copies. For
 
   223  * precise terms see the accompanying LICENSE file.
 
   225  * This software is provided \"AS IS\" with no warranty of any kind,
 
   226  * express or implied, and with no claim as to its suitability for any
 
   233      /\/\* / && pm==0 { pm=1;}
 
   234      /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
 
   235      /\*\// && pm==1 { pm=2;}
 
   239     "$ACTION"_action "$TMP_FILE" "$1" header
 
   242 function tabs_check() {
 
   243     if echo $1 | grep -q -v -E 'Makefile\.am$'
 
   245         OLD_PATTERN=$(echo -e '\t')
 
   249         NEW_PATTERN=$(echo -e '\t')
 
   252     cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
 
   254     "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
 
   257 function spaces_check() {
 
   259     cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
 
   261     "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
 
   264 function long_lines_check() {
 
   265     if cat $1 | grep -q -E '.{81,}'
 
   267 	"$ACTION"_warning $1 'long lines'
 
   273 function process_file() {
 
   274     if [ "$ACTION" == 'update' ]
 
   276         echo -n "    $ACTION $1..."
 
   278         echo "	  $ACTION $1..."
 
   281     CHECKING="header tabs spaces long_lines"
 
   284     for check in $CHECKING
 
   289     if [ "$ACTION" == 'update' ]
 
   295 function process_all {
 
   307     if [ "$1" == '--help' ] || [ "$1" == '-h' ]
 
   314      Check the files, but do not modify them.
 
   316      If --dry-run is specified and the checker emits warnings,
 
   317      then the user is asked if the warnings should be considered
 
   320      Make all warnings into errors.
 
   322      Check all source files in the repository.
 
   324      Check only the modified (and new) source files. This option is
 
   325      useful to check the modification before making a commit.
 
   327      Check only the changed source files compared to the parent(s) of
 
   328      the current hg node.  This option is useful as hg hook script.
 
   329      To automatically check all your changes before making a commit,
 
   330      add the following section to the appropriate .hg/hgrc file.
 
   333        pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
 
   336      Print this help message.
 
   338      The files to check/unify. If no file names are given, the modified
 
   339      source files will be checked/unified (just like using the
 
   340      --modified|-m option).
 
   343     elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
 
   345 	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
 
   347     elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
 
   349 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
 
   351     elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
 
   353 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
 
   355     elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
 
   357 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
 
   359     elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
 
   361 	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
 
   362 	WARNING='INTERACTIVE'
 
   363     elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
 
   365 	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
 
   367     elif [ $(echo x$1 | cut -c 2) == '-' ]
 
   369 	echo "Invalid option $1" >&2 && exit 1
 
   371 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1