Created
March 16, 2011 17:07
-
-
Save cbandy/872850 to your computer and use it in GitHub Desktop.
Undo trailing whitespace changes in working copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
patch=$( mktemp .tmp.git.XXXXXX ) | |
git diff --ignore-space-at-eol $@ > $patch | |
if [ $? -eq 0 ]; then | |
# detect EOF change | |
tail -n 7 $patch | awk -F "\n" ' | |
FNR == 1 && ! /^@@ -[0-9]/ { exit 1 } | |
FNR == 5 && ! /^-/ { exit 1 } | |
FNR == 6 && ! /^\\ No newline at end of file$/ { exit 1 } | |
FNR == 7 && ! /^+/ { exit 1 } | |
FNR == 5 { a = substr($0, 2) } | |
FNR == 7 { if (a != substr($0, 2)) exit 1 } | |
' | |
# strip EOF change | |
if [ $? -eq 0 ]; then | |
tmp=$( mktemp .tmp.git.XXXXXX ) | |
head -n -7 $patch > $tmp | |
mv $tmp $patch | |
fi | |
git checkout $@ | |
if [ $? -eq 0 ]; then | |
[ $( wc -l < $patch ) -gt 2 ] && patch -p1 < $patch | |
[ $? -eq 0 ] && rm $patch | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment