The point in removing trailing whitespace is that if everyone does it always then you end up with a diff that is minimal, ie. it consists only of code changes and not whitespace changes.
However when working with other people who do not practice this, removing all trailing whitespace with your editor or a pre-commit hook results in an even worse diff. You are doing the opposite of your intention.
So I am asking here if there is a tool that I can run manually before I commit that unstages lines from staging that are only changes in whitespace.
Also a bonus would be to change the staged line to have trailing whitespace removed for lines that have code changes.
Also a bonus would be to not do this to Markdown files (as trailing space has meaning in Markdown).
I am asking here as I fully intend to write this tool if it doesn't already exist.
The following will get you most of the way there:
$ clean=`git diff --cached -b`; \ git apply --cached <(git diff --cached -R); \ echo "$clean" | git apply --cached -; \ clean=
For releases of git prior to 1.7.0, it fails if one or more files have all-whitespace changes. For example
$ git diff --cached -b diff --git a/file1 b/file1 index b2bd1a5..3b18e51 100644 diff --git a/file2 b/file2 new file mode 100644 index 0000000..092bfb9 --- /dev/null +++ b/file2 [...]
The empty delta (of file1
above, which really ought to be suppressed) makes git-apply
unhappy:
fatal: patch with only garbage at line 3
UPDATE: The 1.7.0 release of git fixes this issue.
Say our repository is in the following state:
$ git diff --cached diff --git a/foo b/foo index 3b18e51..a75018e 100644 --- a/foo +++ b/foo @@ -1 +1,2 @@ -hello world +hello world +howdy also
We could then run the above commands to fork the index and work tree:
$ git diff --cached diff --git a/foo b/foo index 3b18e51..1715a9b 100644 --- a/foo +++ b/foo @@ -1 +1,2 @@ hello world +howdy also $ git diff diff --git a/foo b/foo index 1715a9b..a75018e 100644 --- a/foo +++ b/foo @@ -1,2 +1,2 @@ -hello world +hello world howdy also
If all changes are whitespace-only, you'll see
error: No changes
I suspect fixing the index and leaving undesired changes in the work tree would surprise or even irritate most users, but that's the behavior the question asked for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With