This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
forked from OpenAngelArena/oaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-fix-whitespace
88 lines (76 loc) · 2.19 KB
/
git-fix-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
me="$(basename $0)"
usage() {
cat << EOF
git-fix-whitespace - Fixes the whitespace issues that 'git-diff --check'
complains about.
Usage:
$me --cached
$me tree-ish tree-ish
Synopsis:
In its first form the index is modified to correct the whitespace issues
found in the files added to the index.
In its second form the index is modified to correct the whitespace issues
found in the files that changed between the 2 hashes given.
Notes:
1. In git terminology "tree-sh" refers to anything that can be resolved to a
tree, including a hash, tag, branch, or something like HEAD~3 (meaning 3
commits prior to HEAD).
2. This script does not use perl/awk/sed/etc. to do is modifications. It uses
git's own functionality. For this reason it should be very future-proof.
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
EOF
exit
}
apply_fail(){
echo "Failed to apply patch $PATCH"
echo
echo "Perhaps you tried to do a hash comparison with uncommitted changes in your"
echo "working tree."
exit $1
}
err_files(){
git diff $ARG1 $ARG2 --check | sed -E '/^(\+|-)/d;s/:.*//' | sort | uniq
}
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)"
git apply --whitespace=fix -R $PATCH || apply_fail 1
git apply --whitespace=fix $PATCH || apply_fail 2
git reset HEAD $FILES
git add $FILES
rm $PATCH
if [[ $(err_files 2 | wc -l) -gt 0 ]]; then
echo -e "Remaining files in index with whitespace errors:\n$(err_files 3)"
fi