-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-fix-whitespace
More file actions
executable file
·43 lines (37 loc) · 1.06 KB
/
Copy pathgit-fix-whitespace
File metadata and controls
executable file
·43 lines (37 loc) · 1.06 KB
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
#!/bin/bash
usage(){
cat << EOF
git-fix-whitespace - Fixes the whitespace issues that 'git-diff --check' complains about.
Usage:
$0 --all
Synopsis:
Fix all whitespace errors on modified lines in both staged and working files
EOF
}
apply_fail(){
echo "Failed to apply patch $PATCH"
echo "ERROR = $1"
exit $1
}
if [[ $# != 1 || "$1" != "--all" ]]; then
usage
exit
fi
EXCLUDE=--exclude=*.beam
if git diff --check HEAD > /dev/null; then
echo "No whitespace errors in work dir"
else
PATCH_w=$(mktemp ${0##*/}.patch.XXXXXX)
git diff --no-ext-diff HEAD > $PATCH_w
git apply -R $EXCLUDE $PATCH_w || apply_fail 1
git apply --whitespace=fix $EXCLUDE $PATCH_w || apply_fail 2
fi
if git diff --check --cached HEAD > /dev/null; then
echo "No whitespace errors in staged files"
else
PATCH_c=$(mktemp ${0##*/}.patch.XXXXXX)
git diff --no-ext-diff --cached HEAD > $PATCH_c
git apply --cached -R $EXCLUDE $PATCH_c || apply_fail 3
git apply --cached --whitespace=fix $EXCLUDE $PATCH_c || apply_fail 4
fi
rm $PATCH_c $PATCH_w