scripts/unify-sources.sh
changeset 349 560e4b6d020d
parent 337 0fbbb4bc42dd
parent 341 d900fd1e760f
child 353 f8832dc16d45
equal deleted inserted replaced
2:53eb5b428468 5:32ecc0715318
    86 }
    86 }
    87 
    87 
    88 function check_action() {
    88 function check_action() {
    89     if ! diff -q $1 $2 >/dev/null
    89     if ! diff -q $1 $2 >/dev/null
    90     then
    90     then
    91 	echo -n " [$3 failed]"
    91 	echo
       
    92 	echo -n "      $3 failed at line(s): "
       
    93 	echo -n $(diff $1 $2 | grep '^[0-9]' | sed "s/^\(.*\)c.*$/ \1/g" | 
       
    94 	          sed "s/,/-/g" | paste -s -d',')
    92 	FAILED=YES
    95 	FAILED=YES
    93     fi
    96     fi
    94 }
    97 }
    95 
    98 
    96 function check_warning() {
    99 function check_warning() {
    97     echo -n " [$2 warning]"
   100     echo
       
   101     if [ "$2" == 'long lines' ]
       
   102     then
       
   103         echo -n "      $2 warning at line(s): "
       
   104         echo -n $(grep -n -E '.{81,}' $1 | sed "s/^\([0-9]*\)/ \1\t/g" | 
       
   105                   cut -f 1 | paste -s -d',')
       
   106     else
       
   107         echo -n "      $2 warning"
       
   108     fi
    98     WARNED=YES
   109     WARNED=YES
    99 }
   110 }
   100 
   111 
   101 function check_init() {
   112 function check_init() {
   102     echo Check source files...
   113     echo Check source files...
   210 
   221 
   211 function spaces_check() {
   222 function spaces_check() {
   212     TMP_FILE=`mktemp`
   223     TMP_FILE=`mktemp`
   213     cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
   224     cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
   214 
   225 
   215     "$ACTION"_action "$TMP_FILE" "$1" 'spaces'
   226     "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
   216 }
   227 }
   217 
   228 
   218 function long_lines_check() {
   229 function long_lines_check() {
   219     if cat $1 | grep -q -E '.{81,}'
   230     if cat $1 | grep -q -E '.{81,}'
   220     then
   231     then
   223 }
   234 }
   224 
   235 
   225 # process the file
   236 # process the file
   226 
   237 
   227 function process_file() {
   238 function process_file() {
   228     echo -n "    $ACTION " $1...
   239     echo -n "    $ACTION $1..."
   229 
   240 
   230     CHECKING="header tabs spaces long_lines"
   241     CHECKING="header tabs spaces long_lines"
   231 
   242 
   232     "$ACTION"_begin $1
   243     "$ACTION"_begin $1
   233     for check in $CHECKING
   244     for check in $CHECKING
   263      then the user is asked if the warnings should be considered
   274      then the user is asked if the warnings should be considered
   264      errors.
   275      errors.
   265   --werror|-w
   276   --werror|-w
   266      Make all warnings into errors.
   277      Make all warnings into errors.
   267   --all|-a
   278   --all|-a
   268      All files in the repository will be checked.
   279      Check all source files in the repository.
   269   --modified|-m
   280   --modified|-m
   270      Check only the modified (and new) source files. This option is
   281      Check only the modified (and new) source files. This option is
   271      useful to check the modification before making a commit.
   282      useful to check the modification before making a commit.
   272   --changed|-c
   283   --changed|-c
   273      Check only the changed source files compared to the parent(s) of
   284      Check only the changed source files compared to the parent(s) of
   279        pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
   290        pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
   280 
   291 
   281   --help|-h
   292   --help|-h
   282      Print this help message.
   293      Print this help message.
   283   files
   294   files
   284      The files to check/unify. If no file names are given, the
   295      The files to check/unify. If no file names are given, the modified
   285      modified source will be checked/unified
   296      source files will be checked/unified (just like using the
   286 
   297      --modified|-m option).
   287 "
   298 "
   288         exit 0
   299         exit 0
   289     elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
   300     elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
   290     then
   301     then
   291 	[ -n "$ACTION" ] && echo "Invalid option $1" >&2 && exit 1
   302 	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
   292 	ACTION=check
   303 	ACTION=check
   293     elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
   304     elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
   294     then
   305     then
   295 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
   306 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
   296 	FILES=all_files
   307 	FILES=all_files
   297     elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
   308     elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
   298     then
   309     then
   299 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
   310 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
   300 	FILES=changed_files
   311 	FILES=changed_files
   301     elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
   312     elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
   302     then
   313     then
   303 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
   314 	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
   304 	FILES=modified_files
   315 	FILES=modified_files
   305     elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
   316     elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
   306     then
   317     then
   307 	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
   318 	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
   308 	WARNING='INTERACTIVE'
   319 	WARNING='INTERACTIVE'
   309     elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
   320     elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
   310     then
   321     then
   311 	[ -n "$WARNING" ] && echo "Invalid option $1" >&2 && exit 1
   322 	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
   312 	WARNING='WERROR'
   323 	WARNING='WERROR'
   313     elif [ $(echo $1 | cut -c 1) == '-' ]
   324     elif [ $(echo x$1 | cut -c 2) == '-' ]
   314     then
   325     then
   315 	echo "Invalid option $1" >&2 && exit 1
   326 	echo "Invalid option $1" >&2 && exit 1
   316     else
   327     else
   317 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
   328 	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
   318 	GIVEN_FILES=$@
   329 	GIVEN_FILES=$@