diff --git a/misc/whitespace/fix-commit-whitespace b/misc/whitespace/fix-commit-whitespace new file mode 100755 index 000000000..2f4fa3e5d --- /dev/null +++ b/misc/whitespace/fix-commit-whitespace @@ -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 < /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