forked from timcharper/git-helpers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-strip-new-whitespace
executable file
·46 lines (39 loc) · 1.61 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
#!/bin/bash
# strip 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).
if [ ! -d ".git" ]; then
echo "This should be run from the root of your git project"
exit 1
fi
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)"
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 $BLEW_UP_FILE."
echo "Apply them back to your index with '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 wrong. Your changes were stashed at least :S"
fi
exit 0