5

Currently when doing git diff with --color option enabled git shows line endings such as ^M and trailing whitespaces only in added lines. Is it possible to make git show these also in removed lines?

Arve
  • 8,058
  • 2
  • 22
  • 25
user1042840
  • 1,925
  • 2
  • 16
  • 32
  • See the comments of [this answer](http://stackoverflow.com/a/5259137/498996). – LazarusX Sep 17 '13 at 08:24
  • Yes, I already saw this comment but it's from 2011. I didn't find any option to do what I want in today's git neither but maybe there is some trick to achieve this. – user1042840 Sep 17 '13 at 08:54
  • This is really confusing behavior. While only line content changed, one is tricked that also the line ending style changed. – zedoo Jul 24 '14 at 09:35
  • @user1042840 as far as I know that behavior hasn't changed one bit since 2011. According to [this answer](http://stackoverflow.com/a/11509388/456814), you can just invert the diff to "trick" Git into showing deleted lines as added lines, thus revealing the whitespaces...but this is inconvenient when you also want to see non-white-space changes too, since they'll also be inverted. –  Jul 24 '14 at 19:48
  • Maybe you should configure git to use 3rd party diff tool like p4merge (just an example) and enable line endings/special symbols display in this utility? – Alex Sorokoletov Jul 30 '14 at 21:07
  • After turning my head around a bit, I think it's no longer confusing and that the best answers were given in the other question and here: http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html – zedoo Aug 01 '14 at 08:58

2 Answers2

1

It's not the prettiest workaround, but you can create a shell alias to make it bearable. Sadly, if you mix tabs and spaces on the output, it does not render correctly in the terminal. This appears to be an issue with Gnome terminal (you'll notice the same behavior with the lines added in the diff as well).

Here's the command you want:

git diff --color | \
sed 's/^\(\x1B\[31m-.*[^ \t]\)\([ \t]\+\)\(\x1B\[m\)$/\1\x1B[41m\2\3/g'

Or more conveniently, add this to your .bashrc:

alias coloreol="sed 's/^\(\x1B\[31m-.*[^ \t]\)\([ \t]\+\)\(\x1B\[m\)$/\1\x1B[41m\2\3/g'"

That way you can type git diff --color|coloreol at the shell prompt.

Essentially, it matches the ANSI red color code, a minus sign followed by anything up to a non-whitespace character. It then matches one or more whitespace characters until it encounters the ANSI reset flag. Sed will insert an ANSI red-invert code between the whitespace and the end of the line.

I've broken down the reg ex for you so that you can modify it to your needs:

The first part, broken down into three groups:

Match the start of the line and ANSI code for red: 
   ^\(\x1B\[31m
          -.*[^ \t]  <-- Followed by the hyphen, anything, then a non-whitespace character
    \)
 Now match one or more space or tab characters for group 2.
 \([ \t]\+\)
 Finally the ANSI reset code and EOL for group 3
 \(\x1B\[m\)$

We then replace it with:

 \1  \x1B[41m  \2  \3

That is, the first match, the invert-red ANSI code (\x1B[41m), followed by the whitespace and ANSI reset.

Kunal
  • 85
  • 7
1

This isn't exactly what you're looking for, but if you use Vim, you can add this to your vimrc file, and it will show you whitespace. Then, if you use vimdiff or use git diff with Vim (I recommend the plugin called Fugitive), it will show the whitespace.

Here's what you have to add to your vimrc:

highlight RedundantWhitespace ctermbg=red guibg=red
highlight ExtraWhitespace ctermbg=red guibg=red
match RedundantWhitespace /[^\t]\zs\t\+/
match ExtraWhitespace /\s\+$\| \+\ze\t/

And here's something you can use to automatically remove whitespace:

autocmd BufWritePre * :%s/\s\+$//e
trippelganger
  • 159
  • 1
  • 8