-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-whitespace
executable file
·56 lines (46 loc) · 1.3 KB
/
git-whitespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
#
# Copyright (c) 2013 Daniel Gryniewicz
#
# Remove whitespace errors from uncommitted code. Basically, this means:
# git diff > tmp.patch
# git checkout .
# git apply --whitespace=fix tmp.patch
# Set usage output
DESCRIPTION="Remove whitespace errors from uncommitted code"
USAGE="[-h |--help] [-f | --force]"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-f, --force\n\t\tFix whitespace even if there is staged changes"
# Standard functions
source ${SCRIPTS}/functions.sh
# Script name
ME=$(basename $0)
# Parse arguments
ARGS=`getopt -o hf --long help,force -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-f|--force) FORCE="yes"; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
# Remaining arguments are in $1, $2, etc. as normal
SUBDIRECTORY_OK=Yes
OPTIONS_SPEC=
LONG_USAGE="${LONGUSAGE}"
# Source the git environment
GIT_PATH=
source "$(git --exec-path )/git-sh-setup"
if [ -z "${FORCE}" ]; then
git status | grep "git reset HEAD" > /dev/null && die "Current branch has staged changes"
fi
TMPFILE=$(mktemp)
git diff > "${TMPFILE}" || die "Failed to diff"
git checkout . || die "Failed to checkout"
git apply --whitespace=fix "${TMPFILE}" || die "Failed to apply"
rm -f ${TMPFILE}