# Problem with CRLF line endings and colored diff

3 messages from 2008-10-14 to 2008-10-14. Participants: Jean-Luc Herren, Junio C Hamano.
Thread: https://gitlist.dev/t/15900

## Jean-Luc Herren, 2008-10-14 21:13

Subject: Problem with CRLF line endings and colored diff
Message-ID: <48F50B97.9090106@gmx.ch>
URL: https://gitlist.dev/e/48F50B97.9090106%40gmx.ch

```
Hello list!

I'm using CRLF line endings on the files of a project and I'm
running into odd output from 'git diff'.  Here are the steps to
reproduce:

    git init
    echo -e 'line1\r' > file
    git add file
    echo -e 'line2\r' > file
    PAGER='less -R' git diff --color

The output looks like this:

    diff --git a/file b/file
    index 495181c..a1413a4 100644
    --- a/file
    +++ b/file
    @@ -1 +1 @@
    -line1
    +line2^M

Note the trailing ^M on the '+' line.  After examining that line
it seems it's a bug in git, as that line really looks like this
(<...> denoting ANSI sequences or CR/LF):

    <GREEN FOREGROUND>+<RESET><GREEN FOREGROUND>
    line2<RESET><RED BACKGROUND><CR><RESET><LF>

The CR and the LF are not after each other, there's an ANSI
sequence in between (because the CR is being highlighted as white
space).  That confuses 'less'.  The '-' line above does not do
that and thus doesn't have this problem.  It also doesn't reset
the color between the '-' and the line:

    <RED FOREGROUND>-line1<RESET><CR><LF>

I tried poking around the code, but the diff machinery is more
complex than could be quickly grasped, so I'll let someone that
knows it already have a look.

Using git 1.6.0.2, but have the same problem with HEAD.

jlh

```

## Junio C Hamano, 2008-10-14 22:20

Subject: Re: Problem with CRLF line endings and colored diff
Message-ID: <7v4p3e7r9e.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v4p3e7r9e.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <48F50B97.9090106@gmx.ch>

```
Jean-Luc Herren <jlh@gmx.ch> writes:

>     <GREEN FOREGROUND>+<RESET><GREEN FOREGROUND>
>     line2<RESET><RED BACKGROUND><CR><RESET><LF>

This is expected and deliberate, as you haven't told about your desire to
consider carriage-returns not as line termination letter.  By default CR
is just one of the whitespace letters, and colored diff output is
reminding you that you introduced a line with a trailing whitespace.

Check git-config(1) and look for "core.whitespace", paying special
attention to "cr-at-eol".

```

## Jean-Luc Herren, 2008-10-14 23:08

Subject: Re: Problem with CRLF line endings and colored diff
Message-ID: <48F52661.4010800@gmx.ch>
URL: https://gitlist.dev/e/48F52661.4010800%40gmx.ch
In-Reply-To: <7v4p3e7r9e.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Jean-Luc Herren <jlh@gmx.ch> writes:
>>     <GREEN FOREGROUND>+<RESET><GREEN FOREGROUND>
>>     line2<RESET><RED BACKGROUND><CR><RESET><LF>
> 
> This is expected and deliberate, as you haven't told about your desire to
> consider carriage-returns not as line termination letter.  By default CR
> is just one of the whitespace letters, and colored diff output is
> reminding you that you introduced a line with a trailing whitespace.

Ah, I see, I didn't know I have to tell it that I want to use
CRLF...  Works fine now.  Sorry for the noise.

jlh

```
