tags:

views:

86

answers:

2

I have my text editor to automatically trim trailing whitespace upon saving a file, and I am contributing to an OpenSource project that has severe problems with trailing whitespace.

Every time I try to submit a patch I must first ignore all whitespace-only changes by hand, to choose only the relevant information. Not only that, but when I run git rebase I usually run into several problems because of them.

As such I would like to be able to add to index only non-whitespace changes, in a way similar that git add -p does, but without having to pick all the changes myself.

Does anyone know how to do this?

EDIT: I cannot change the way the project works, and they have decided, after discussing it on the mailing list, to ignore this.

A: 

You should first consider if the trailing whitespace is intentional. Many projects, including the Linux kernel, Mozilla, Drupal, and Kerberos (to name a few from the Wikipedia page on style) prohibit trailing whitespace. From the Linux kernel documentation:

Get a decent editor and don't leave whitespace at the end of lines.

In your case, the problem is the other way around: previous commits (and maybe current ones) did not follow this guideline.

I'd wager that no one really wants the trailing whitespace, and fixing the problem might be a welcome change. Other users might also be experiencing the same problem you are. It's also likely that the contributor(s) who are adding trailing whitespace are unaware that they are doing so.

Rather than trying to reconfigure git to ignore the problem, or disabling the otherwise desirable functionality in your editor, I'd start off with a post to the project mailing list explaining the problem. Many editors (and git itself) can be configured to deal with trailing whitespace.

reemrevnivek
It's not intentional, but I cannot change the way 100+ people who contribute the project think. They don't mind it, and won't accept patches with 1000+ changes what only deal with trailing whitespace.They know about the problem and have decided to ignore it. This discussion already happened in the list and was closed. In this case, it's me who needs to adapt to them.
Edu Felipe
Then configure your editor such that it doesn't trim trailing whitespace when working on this project's code.
jamessan
A: 

I found a git pre-commit hook that removes trailing whitespace. However, if you can't get others to use this, then it might not be a valid solution.

  #!/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 -r 's/:[0-9]+:.*//' | uniq` ; do
     # Fix them!
     sed -i 's/[[:space:]]*$//' "$FILE"
  done
  exit
Casey
This question is asking how to preserve trailing whitespace.
Douglas