Skip to content

Commit

Permalink
Fix argument processing
Browse files Browse the repository at this point in the history
This commit fixes the argument processing for this script.

1. Remove the if...elseif... block because this is notoriously slow
2. When passing only one argument and it's not "--cached", instead of
doing nothing at all, actually show that an "Invalid argument" was passed
and display the usage text
3. This script never set any variables if more than 2 arguments were
passed. I'm assuming more than two arguments is an error, and so the catch
all case statement will apply showing the help usage text.
4. In the case of no arguments being passed, show the help usage text and
exit.
5. Don't create a temp file if invalid arguments are passed.
6. Create the temp file in the directory where this script is invoked
from, not in the directory where the script resides.
  • Loading branch information
fourpastmidnight committed Apr 28, 2016
1 parent 685e466 commit b87dc31
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions git-fix-whitespace
@@ -1,9 +1,12 @@
#!/bin/bash

me="$(basename $0)"

usage() {
me="$(basename $0)"
cat << EOF
git-fix-whitespace - Fixes the whitespace issues that 'git-diff --check'
complains about.
Usage:
$me --cached
$me tree-ish tree-ish
Expand Down Expand Up @@ -44,17 +47,34 @@ err_files(){
git diff $ARG1 $ARG2 --check | sed -E '/^(\+|-)/d;s/:.*//' | sort | uniq
}

PATCH=$(mktemp ${0##*/}.patch.XXXXXX)
if [[ $# == 1 && "$1" == "--cached" ]]; then
ARG1=$1
ARG2=HEAD
elif [[ $# == 2 ]]; then
ARG1=$1
ARG2=$2
else
usage
exit
fi
case "$#" in
1)
test "$1" = "--cached" && ARG1=$1 || {
echo
echo "Inavlid arguments"
echo
echo
usage
}
ARG1=$1
ARG2=HEAD
;;
2)
ARG1=$1
ARG2=$2
;;
*)
test $# > 3 && {
echo
echo "Invalid arguments"
echo
echo
}
usage
;;
esac

PATCH=$(mktemp -p $(pwd -P) ${me}.patch.XXXXXX)
git diff $ARG1 $ARG2 > $PATCH
FILES=$(sed '/^+++ /!d;s?^+++ b/??' $PATCH)
echo -e "Files in index with whitespace errors:\n$(err_files 1)"
Expand Down

0 comments on commit b87dc31

Please sign in to comment.