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

Re: Adding color to git diff output.

From
sean <seanlkml@sympatico.ca>
Date
Apr 12, 2006, 00:38 UTC
Message-ID
<BAYC1-PASMTP119CAD2588D00764DB3EA3AEC20@CEZ.ICE>
In-Reply-To
<Pine.LNX.4.64.0604111725590.14565@g5.osdl.org>

On Tue, 11 Apr 2006 17:34:10 -0700 (PDT) Linus Torvalds <torvalds@osdl.org> wrote:

Show 37 quoted lines
> On Tue, 11 Apr 2006, Junio C Hamano wrote:
> 
> > sean <seanlkml@sympatico.ca> writes:
> > 
> > > Linus posted a colorize program a while back[1] but it wasn't taken into git.  
> > > The patch below takes a different approach, adding a GIT_DIFF_PAGER variable.
> > > You can use it by assigning a filter to the environment variable, like so:
> > >
> > > export GIT_DIFF_PAGER="colordiff | less -RS"
> > 
> > Sounds like a nice idea, even maybe suitable in a FAQ.
> > Unfortunately colordiff does not seem to grok diff --cc output,
> > but that is fine ;-).
> 
> Well, the real problem - at least as far as my usage is concerned - is 
> that I'd want colorization to be more integrated so that it can be turned 
> off when not appropriate.
> 
> Think "colorized 'ls'", where if you enable colorization by default, it 
> only colorizes when the output is a tty, so that you can still script 
> things and output things to a file or so, without it getting colorized. 
> 
> Because most "patch" programs (git-apply included) do not want to see 
> colorization ;)
> 
> So I'd suggest that the "git diff" script at a minimum first check whether 
> the output is to a tty before it decides to use GIT_DIFF_PAGER. With 
> perhaps an option to _force_ colorization if you want to.
> 
> Now, I don't actually enable ls-colorization by default, and I probably 
> wouldn't do it for git diff either, but at least for diffs I _might_. But 
> I'd definitely want it to be turned off automatically so that I can do
> 
> 	git diff .. > ~/patch-file
> 
> without having to remember to turn it off explicitly.
> 
What I have is a script ~/bin/gitcdiff:

#!/bin/sh tty -s <&1 || exec cat colordiff | less -RS

And then setting GIT_DIFF_PAGER="~/bin/gitcdiff". When piping to a file it just uses cat, but when the output is a terminal device it uses the colordiff.

I thought about integrating that logic into git-diff.sh, but i'm not sure it's always appropriate.

Sean
Previous: Linus TorvaldsNext: Linus Torvalds
Message 4 of 9 in “Adding color to git diff output.”
  1. seanApr 11, 2006
  2. Junio C HamanoApr 11, 2006
  3. Linus TorvaldsApr 12, 2006
  4. seanApr 12, 2006
  5. Linus TorvaldsApr 12, 2006
  6. Alex RiesenApr 12, 2006
  7. Linus TorvaldsApr 12, 2006
  8. Alex RiesenApr 12, 2006
  9. Junio C HamanoApr 12, 2006

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.