git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Problem with CRLF line endings and colored diff

From
JHJean-Luc Herren <jlh@gmx.ch>
Date
Oct 14, 2008, 21:13 UTC
Message-ID
<48F50B97.9090106@gmx.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
Next: Junio C Hamano
Message 1 of 3 in “Problem with CRLF line endings and colored diff”
  1. Jean-Luc HerrenOct 14, 2008
  2. Junio C HamanoOct 14, 2008
  3. Jean-Luc HerrenOct 14, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.