Skip to content

Commit c533906

Browse files
committed
Space/Tab cleanup script - run before committing
Running this script will transform tabs into a pair of spaces and will eliminate trailing spaces. Use at your own risk!
1 parent 2dd0af0 commit c533906

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

gitignore.SAMPLE

+5
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727
/src/intellij/*.iml
2828
/src/intellij/*.ipr
2929
/src/intellij/*.iws
30+
/.cache
31+
/.idea
32+
/.settings
3033

34+
# bak files produced by ./cleanup-commit
35+
*.bak

tools/cleanup-commit

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/bash
2+
3+
##
4+
## The cleanup-commit script
5+
## -------------------------
6+
## This little script will cleanup your commit before you send it. You need to add the files to the staged area and
7+
## run this script. It will automatically cleanup tabs and trailing spaces for the files you added and then add the
8+
## clean versions to the staging area.
9+
##
10+
## Use at your own risk, I spent some time making the script error-proof so it will abort if sees any inconsistency,
11+
## but of course playing around with your commit might break things. Btw, it saves the original file to file.bak.
12+
##
13+
## Happy hacking!
14+
##
15+
16+
ABORT="Ab0rT0p3r4+|0n"
17+
18+
#
19+
# Cleanup function
20+
#
21+
function cleanup {
22+
echo Cleaning up $1...
23+
# prepare the ground
24+
rm -rf $1.bak
25+
# compress <TAB> into double <BLANK> and eliminate trailing <BLANC>s
26+
sed -i.bak -e 's/\t/ /g' -e 's/ *$//' $1
27+
}
28+
29+
30+
#
31+
# Get the git status for the current staged commit
32+
#
33+
FULLSTATUS=`git status --porcelain`
34+
35+
if [ $? -ne 0 ]
36+
then
37+
echo "Unable to run git. Check if:"
38+
echo " -- git is installed (you can run git in the command line)"
39+
echo " -- the current directory is a valid git repository"
40+
exit 1
41+
fi
42+
43+
echo
44+
45+
#
46+
# Based on the status decide what files will get cleaned up
47+
#
48+
CLEANUP_FILES=`echo "$FULLSTATUS" | while read LINE
49+
do
50+
51+
STATUS=$(echo $LINE | sed 's/^\(..\).*$/\1/')
52+
if [ $? -ne 0 ]
53+
then
54+
echo "Could not get the status for line: $LINE"
55+
echo " -- you have the basic unix tools installed (grep, cut, sed)"
56+
echo $ABORT # This goes to CLEANUP_FILES
57+
exit 1
58+
fi
59+
60+
FILES=$(echo $LINE | sed 's/^..//')
61+
FILE1=$(echo $FILES | cut -d ' ' -f 1)
62+
FILE2=$(echo $FILES | cut -d ' ' -f 3)
63+
64+
case "$STATUS" in
65+
[AMRDC]" ")
66+
case "$STATUS" in
67+
"A "|"M ")
68+
echo $FILE1
69+
;;
70+
"R ")
71+
echo $FILE2
72+
;;
73+
"D ")
74+
#nothing to do
75+
;;
76+
"C ")
77+
echo $FILE1
78+
echo $FILE2
79+
;;
80+
esac
81+
;;
82+
"??")
83+
# File is not tracked, no need to do anything about it
84+
# echo Untracked: $FILE1
85+
;;
86+
*)
87+
echo "Unstable status of file $FILE1 (\"$STATUS\")" >&2
88+
echo "Aborting cleanup!" >&2
89+
echo $ABORT # This goes to CLEANUP_FILES
90+
exit 1
91+
esac
92+
done; echo $CLEANUP_FILES`
93+
94+
95+
#
96+
# Perform actual cleanup
97+
#
98+
case $CLEANUP_FILES in
99+
*"$ABORT")
100+
echo
101+
exit 1
102+
;;
103+
"")
104+
echo Nothing to do!
105+
;;
106+
*)
107+
cd $(git rev-parse --show-toplevel)
108+
109+
if [ $? -ne 0 ]
110+
then
111+
echo Unexpected error: cannot cd to the repository root
112+
echo Aborting cleanup!
113+
exit 1
114+
fi
115+
116+
echo "$CLEANUP_FILES" | while read FILE
117+
do
118+
cleanup $FILE
119+
done
120+
121+
cd - &>/dev/null
122+
123+
echo
124+
echo "Cleanup done: "
125+
echo " - original files saved as .bak"
126+
echo " - you can do \"git diff\" to see the changes the script did"
127+
echo " - you can do \"git commit -a\" to commit the cleaned up files"
128+
echo
129+
;;
130+
esac

0 commit comments

Comments
 (0)