-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-strip-new-whitespace
executable file
·53 lines (46 loc) · 1.62 KB
/
git-strip-new-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
#!/bin/bash
# Strips trailing whitespace from any lines you've introduced. If
# you've staged changes, it strips them from the index (and they stay
# in your working copy).
#
# Why would you want to do this? Keep your patches clean. Why not
# just strip all whitespaces from your project? Well... then you'd
# have a big nasty commit, wouldn't you? And some code generators
# might rely on the whitespace (like annotate-models).
while [ $(pwd) != "/" ] && [ ! -d ".git" ]; do
cd ..
done
if [ -f ".git/MERGE_MODE" ]; then
echo "You are currently in the middle of a merge, and running this would mess you up!"
exit 1
fi
STAGED_CHANGES="$(git diff --cached --ignore-space-at-eol)"
if [ "$STAGED_CHANGES" != "" ]; then
echo "Stripping trailing spaces from index"
git reset 2> /dev/null 1> /dev/null
if (echo "$STAGED_CHANGES" | git apply --whitespace=fix --cached); then
echo "Successfully stripped all new whitespaces from the index"
else
BLEW_UP_FILE="/tmp/git-strip-new-whitespace-blew-up"
echo "$STAGED_CHANGES" > "$BLEW_UP_FILE"
echo "Something went wrong. I wrote the patch for your staged changes to:"
echo " $BLEW_UP_FILE"
echo "Apply them back to your index with:"
echo " cat $BLEW_UP_FILE | git apply --cached"
exit 1
fi
exit 0
fi
if [ "$(git diff)" == "" ]; then
echo "Working directory is clean"
exit 1
fi
git stash
if (git stash show -p | git apply --whitespace=fix); then
echo "Successfully stripped all new whitespaces"
git reset 2> /dev/null 1> /dev/null
git stash drop 2> /dev/null 1> /dev/null
else
echo "Something went horribly wrong. Your changes were stashed."
fi
exit 0