Git and Trailing Whitespace

Dear Lazyweb,

A git repo I work with is very particular and fussy about trailing whitespace. Unfortunately, my editor of choice is quite sloppy with the same. It's like watching an episode of the Odd Couple.

How do you automate stripping of white space? Are there tools built into git that will sanitize my patch for me, or some other tool that I can run on my own?

4 flames:

bbbush zei

I use git in cygwin, and here is my solution to this problem:

but my "sed" command often fails and I don't know why.

Victor Bogado zei

doesn't git it self corrects the white space problems? Here when I have them, they are corrected before the commit takes place. The only problem is that it doesn't correct them in the working tree, so I need to checkout the correct version out of the repository.

Mike zei

You didn't say what editor. Can you configure it to show trailing whitespace? In vim, I use 'set list lcs=tab:»· lcs+=trail:·'. If you can do something similar, then at least you'll see when you're adding it.

Also, git-diff --check will warn you about such things.

It should be simple to script the removal of trailing whitespace.

% sed -e 's/[[:space:]]*$//'

out to do it.

Yankee zei

The sed bits worked. I have to run it manually on each file git complains about, which is still a PITA, but I'll cope. Perhaps I can hook it into git somewhere.

You're right; I didn't mention which editor I was using. How silly of me. :P