trailing whitespace
One of the little things that is annoying me at the moment is getting commits in git that are only about whitespace. This is pure noise and makes the commit harder to work with. I’ve looked at a number of ways of fixing this, but have mostly failed.
Using textmate
Textmate has a text bundle which contains a command to remove trailing whitespace. This can be combined into a save macro, so that every time you save a file it has its trailing whitespace removed. Only problem is that I can’t assign (cmd-s, OSX default save) to this macro, so I have to remember to use my new key combination (which sucks).
Using find to clean up everything
Find is so damn dangerous, especially when your getting it to execute an action which changes files, so you really have to be very careful with the following commands. I completely trashed a local git repository on my way to getting these commands working, and you could do worse!
Remove trailing spaces from selected file types
Because find
is so dangerous I’ve taken the approach of including the files I want, rather than trying to exclude files.
find . -type f -regex './.*rb' -or -regex ./.*feature -or -regex ./.*haml -print0 |xargs -0 sed -i .bak -E "s/[[:space:]]*$//"
The sed
command creates backup files for each file it changes you can get rid of these using the following
find . -type f -regex ./.*bak -print0 | xargs -0 rm
This approach seems to work, but I’m not very happy with it. Its trail and error bash hackery, using two commands I don’t understand properly (find and sed) and its prone to catastrophic mistakes if you mistype.
Using Git to cleanup
Git has a number of whitespace options which I don’t understand. From what I’ve read so far I think the safest approach is to let GIT warn you when you make a commit with trailing whitespace. This will be annoying, but if we aware of the problem we can learn how to deal with it.
Other approaches include using a pre-commit hook to cleanup offending files in a commit. This will require quite alot of googling and work.