On Tue, Apr 2, 2013 at 2:30 PM, Kirk, Benjamin (JSC-EG311) <benjamin.kirk-1@nasa.gov> wrote:
OK, at risk of entering dangerous territory…

I've been using John's recommended git config flags, and have become ashamed of the amount of trailing whitespace I leave in source files.

I've played around with emacs' whitespace cleanup stuff but I don't like it mainly because it removes all whitespace from a file you edit, generating huge diffs.

So I switched to using pre-commit hooks in git.
 
Name the following script "pre-commit", chmod a+x it, and save it in the .git/hooks directory of any git repo where you want to use it.  The great thing is that it only cleans up whitespace from *your commit* not the entire file.  I've been using it a few weeks now and would go back!

#!/bin/sh

if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
   against=HEAD
else
   # Initial commit: diff against an empty tree object
   against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Find files with trailing whitespace
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -E 's/:[0-9]+:.*//' | uniq` ; do
    # Fix them!
    sed -i '' -E 's/[[:space:]]*$//' "$FILE"
    git add "$FILE"
done



--
John