misc: add whitespace removal tool

Also-by: Alejandro Gomez <Alejandro.Gomez@data61.csiro.au>
This commit is contained in:
Joel Beeren 2017-03-29 17:43:53 +11:00 committed by Matthew Brecknell
parent c0a998140d
commit ce2220286b
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
#!/bin/bash
#
# Copyright 2017, Data61
#
# This software may be distributed and modified according to the terms of
# the BSD 2-Clause license. Note that NO WARRANTY is provided.
# See "LICENSE_BSD2.txt" for details.
#
# @TAG(DATA61_BSD)
#
DIFF_COMMAND="git diff"
REF=HEAD
case ${1} in
index) DIFF_COMMAND="git diff-index"
;;
help|-h|--help) cat <<EOF
usage: tws_rm [index|help] [ref]
index : use changes in the index
help : this message
ref : compare against ref (Default: HEAD)
EOF
exit 0
;;
*) if git rev-parse --verify -q ${1-HEAD} > /dev/null
then REF=${1-HEAD}
else echo "[ERROR] ${1} is not a valid reference"
exit 1
fi
;;
esac
[ "${REF}" = "HEAD" ] && [ ! -z $2 ] && REF=${2}
if ! git rev-parse --verify -q ${REF} > /dev/null
then echo "[ERROR] ${REF} is not a valid reference"
exit 1
fi
if [ ! -d .git/ ] || ! git rev-parse --git-dir > /dev/null
then echo "[ERROR] this is supposed to be run in a git repo"
exit 1
fi
if [ `uname` == "Darwin" ]; then
SED="/usr/bin/sed -i ''"
else
SED="sed -i"
fi
for TWS in $(${DIFF_COMMAND} --check ${REF} | grep "trailing whitespace" | cut -d: -f1-2)
do
FILE=$(echo ${TWS} | cut -d: -f1)
LINE=$(echo ${TWS} | cut -d: -f2)
$SED ${LINE}'s/[ \t]*$//' $FILE
done