Git development
 help / color / mirror / Atom feed
* Adding color to git diff output.
       [not found] <20060411183245.7723c70c.seanlkml@sympatico.ca>
@ 2006-04-11 22:32 ` sean
  2006-04-11 23:12   ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: sean @ 2006-04-11 22:32 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano


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"

Sean

P.S.  .gitignore is missing a few entries: "git.spec" (rpm build), "tags" 
      (ctags), and ".*.swp" (vi editor temp files).

[1] http://marc.theaimsgroup.com/?l=git&m=114315063918024&w=2


diff --git a/git-diff.sh b/git-diff.sh
index dc0dd31..f792340 100755
--- a/git-diff.sh
+++ b/git-diff.sh
@@ -69,4 +69,8 @@ case "$rev" in
 	;;
 esac
 
+if [ -n "$GIT_DIFF_PAGER" ]; then
+	cmd="$cmd | $GIT_DIFF_PAGER"
+fi
+
 eval "$cmd"

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-11 22:32 ` Adding color to git diff output sean
@ 2006-04-11 23:12   ` Junio C Hamano
  2006-04-12  0:34     ` Linus Torvalds
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2006-04-11 23:12 UTC (permalink / raw)
  To: sean; +Cc: git

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 ;-).

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-11 23:12   ` Junio C Hamano
@ 2006-04-12  0:34     ` Linus Torvalds
       [not found]       ` <20060411203841.3b824062.seanlkml@sympatico.ca>
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-04-12  0:34 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: sean, git



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.

		Linus

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
       [not found]       ` <20060411203841.3b824062.seanlkml@sympatico.ca>
@ 2006-04-12  0:38         ` sean
  2006-04-12  1:03           ` Linus Torvalds
  2006-04-12  6:19           ` Junio C Hamano
  0 siblings, 2 replies; 9+ messages in thread
From: sean @ 2006-04-12  0:38 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: junkio, git

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

> 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-12  0:38         ` sean
@ 2006-04-12  1:03           ` Linus Torvalds
  2006-04-12  7:38             ` Alex Riesen
  2006-04-12  6:19           ` Junio C Hamano
  1 sibling, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-04-12  1:03 UTC (permalink / raw)
  To: sean; +Cc: junkio, git



On Tue, 11 Apr 2006, sean wrote:
> 
> 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.

Yeah, that's close to what I'd have suggested:

		Linus

----
diff --git a/git-diff.sh b/git-diff.sh
index dc0dd31..f0bea80 100755
--- a/git-diff.sh
+++ b/git-diff.sh
@@ -69,4 +69,8 @@ case "$rev" in
 	;;
 esac
 
+if [ "$GIT_DIFF_PAGER" ] && tty -s <&1; then
+	cmd="$cmd | $GIT_DIFF_PAGER"
+fi
+
 eval "$cmd"

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-12  0:38         ` sean
  2006-04-12  1:03           ` Linus Torvalds
@ 2006-04-12  6:19           ` Junio C Hamano
  1 sibling, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2006-04-12  6:19 UTC (permalink / raw)
  To: sean; +Cc: Linus Torvalds, git

sean <seanlkml@sympatico.ca> writes:

> 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.

What Linus posted seems sensible.  And here is a hacked version
that is git-aware ;-).

-- >8 --
Subject: [PATCH] Add colordiff for git to contrib/colordiff.

I hacked it up to teach it the git extended diff headers, made
it not to read the whole patch in the array.

Also, the original program, when arguments are given, ran "diff"
with the given arguments and showed the output from it.  Of
course, I changed it to run "git diff" ;-).

Signed-off-by: Junio C Hamano <junkio@cox.net>

---

 contrib/colordiff/README         |    2 
 contrib/colordiff/colordiff.perl |  196 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 198 insertions(+), 0 deletions(-)
 create mode 100644 contrib/colordiff/README
 create mode 100755 contrib/colordiff/colordiff.perl

2ee04989119dd5c00e066f740efb8f2155d81ede
diff --git a/contrib/colordiff/README b/contrib/colordiff/README
new file mode 100644
index 0000000..2678fdf
--- /dev/null
+++ b/contrib/colordiff/README
@@ -0,0 +1,2 @@
+This is "colordiff" (http://colordiff.sourceforge.net/) by Dave
+Ewart <davee@sungate.co.uk>, modified specifically for git.
diff --git a/contrib/colordiff/colordiff.perl b/contrib/colordiff/colordiff.perl
new file mode 100755
index 0000000..37c3559
--- /dev/null
+++ b/contrib/colordiff/colordiff.perl
@@ -0,0 +1,196 @@
+#!/usr/bin/perl -w
+# 
+# $Id: colordiff.pl,v 1.4.2.10 2004/01/04 15:02:59 daveewart Exp $
+
+########################################################################
+#                                                                      #
+# ColorDiff - a wrapper/replacment for 'diff' producing                #
+#             colourful output                                         #
+#                                                                      #
+# Copyright (C)2002-2004 Dave Ewart (davee@sungate.co.uk)              #
+#                                                                      #
+########################################################################
+#                                                                      #
+# This program is free software; you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation; either version 2 of the License, or    #
+# (at your option) any later version.                                  #
+#                                                                      #
+# This program is distributed in the hope that it will be useful,      #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
+# GNU General Public License for more details.                         #
+#                                                                      #
+# You should have received a copy of the GNU General Public License    #
+# along with this program; if not, write to the Free Software          #
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            #
+#                                                                      #
+########################################################################
+
+use strict;
+use Getopt::Long qw(:config pass_through);
+use IPC::Open2;
+
+my $app_name     = 'colordiff';
+my $version      = '1.0.4';
+my $author       = 'Dave Ewart';
+my $author_email = 'davee@sungate.co.uk';
+my $app_www      = 'http://colordiff.sourceforge.net/';
+my $copyright    = '(C)2002-2004';
+my $show_banner  = 1;
+
+# ANSI sequences for colours
+my %colour;
+$colour{white}       = "\033[1;37m";
+$colour{yellow}      = "\033[1;33m";
+$colour{green}       = "\033[1;32m";
+$colour{blue}        = "\033[1;34m";
+$colour{cyan}        = "\033[1;36m";
+$colour{red}         = "\033[1;31m";
+$colour{magenta}     = "\033[1;35m";
+$colour{black}       = "\033[1;30m";
+$colour{darkwhite}   = "\033[0;37m";
+$colour{darkyellow}  = "\033[0;33m";
+$colour{darkgreen}   = "\033[0;32m";
+$colour{darkblue}    = "\033[0;34m";
+$colour{darkcyan}    = "\033[0;36m";
+$colour{darkred}     = "\033[0;31m";
+$colour{darkmagenta} = "\033[0;35m";
+$colour{darkblack}   = "\033[0;30m";
+$colour{OFF}         = "\033[0;0m";
+
+# Default colours if /etc/colordiffrc or ~/.colordiffrc do not exist
+my $plain_text = $colour{OFF};
+my $file_old   = $colour{red};
+my $file_new   = $colour{blue};
+my $diff_stuff = $colour{magenta};
+
+# Locations for personal and system-wide colour configurations
+my $HOME   = $ENV{HOME};
+my $etcdir = '/etc';
+
+my ($setting, $value);
+my @config_files = ("$etcdir/colordiffrc", "$HOME/.colordiffrc");
+my $config_file;
+
+foreach $config_file (@config_files) {
+    if (open(COLORDIFFRC, "<$config_file")) {
+        while (<COLORDIFFRC>) {
+            chop;
+            next if (/^#/ || /^$/);
+            s/\s+//g;
+            ($setting, $value) = split ('=');
+            if ($setting eq 'banner') {
+                if ($value eq 'no') {
+                    $show_banner = 0;
+                }
+                next;
+            }
+            if (!defined $colour{$value}) {
+                print "Invalid colour specification ($value) in $config_file\n";
+                next;
+            }
+            if ($setting eq 'plain') {
+                $plain_text = $colour{$value};
+            }
+            elsif ($setting eq 'oldtext') {
+                $file_old = $colour{$value};
+            }
+            elsif ($setting eq 'newtext') {
+                $file_new = $colour{$value};
+            }
+            elsif ($setting eq 'diffstuff') {
+                $diff_stuff = $colour{$value};
+            }
+            else {
+                print "Unknown option in $etcdir/colordiffrc: $setting\n";
+            }
+        }
+        close COLORDIFFRC;
+    }
+}
+
+# colordiff specfic options here.  Need to pre-declare if using variables
+GetOptions(
+    "no-banner" => sub { $show_banner = 0 },
+    "plain-text=s" => \&set_color,
+    "file-old=s"   => \&set_color,
+    "file-new=s"   => \&set_color,
+    "diff-stuff=s" => \&set_color
+);
+
+if ($show_banner == 1) {
+    print STDERR "$app_name $version ($app_www)\n";
+    print STDERR "$copyright $author, $author_email\n\n";
+}
+
+if (defined $ARGV[0]) {
+    # More reliable way of pulling in arguments
+    open2(\*INPUTSTREAM, undef, "git", "diff", @ARGV);
+}
+else {
+    *INPUTSTREAM = \*STDIN;
+}
+
+my $record;
+my $nrecs           = 0;
+my $inside_file_old = 1;
+my $nparents        = undef;
+
+while (<INPUTSTREAM>) {
+    $nrecs++;
+    if (/^(\@\@+) -[-+0-9, ]+ \1/) {
+	    print "$diff_stuff";
+	    $nparents = length($1) - 1;
+    }
+    elsif (/^diff -/ || /^index / ||
+	   /^old mode / || /^new mode / ||
+	   /^deleted file mode / || /^new file mode / ||
+	   /^similarity index / || /^dissimilarity index / ||
+	   /^copy from / || /^copy to / ||
+	   /^rename from / || /^rename to /) {
+	    $nparents = undef;
+	    print "$diff_stuff";
+    }
+    elsif (defined $nparents) {
+	    if ($nparents == 1) {
+		    if (/^\+/) {
+			    print $file_new;
+		    }
+		    elsif (/^-/) {
+			    print $file_old;
+		    }
+		    else {
+			    print $plain_text;
+		    }
+	    }
+	    elsif (/^ {$nparents}/) {
+		    print "$plain_text";
+	    }
+	    elsif (/^[+ ]{$nparents}/) {
+		    print "$file_new";
+	    }
+	    elsif (/^[- ]{$nparents}/) {
+		    print "$file_old";
+	    }
+	    else {
+		    print $plain_text;
+	    }
+    }
+    elsif (/^--- / || /^\+\+\+ /) {
+	    print $diff_stuff;
+    }
+    else {
+	    print "$plain_text";
+    }
+    s/$/$colour{OFF}/;
+    print "$_";
+}
+close INPUTSTREAM;
+
+sub set_color {
+    my ($type, $color) = @_;
+
+    $type =~ s/-/_/;
+    eval "\$$type = \$colour{$color}";
+}
-- 
1.3.0.rc3.g72c1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-12  1:03           ` Linus Torvalds
@ 2006-04-12  7:38             ` Alex Riesen
  2006-04-12 15:46               ` Linus Torvalds
  0 siblings, 1 reply; 9+ messages in thread
From: Alex Riesen @ 2006-04-12  7:38 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: sean, junkio, git

On 4/12/06, Linus Torvalds <torvalds@osdl.org> wrote:
> +if [ "$GIT_DIFF_PAGER" ] && tty -s <&1; then

Maybe use "-t" here? I have at least one system which has no tty installed.
Like this:

  if [ -n "$GIT_DIFF_PAGER" -a -t ]; then
...

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-12  7:38             ` Alex Riesen
@ 2006-04-12 15:46               ` Linus Torvalds
  2006-04-12 15:52                 ` Alex Riesen
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-04-12 15:46 UTC (permalink / raw)
  To: Alex Riesen; +Cc: sean, junkio, git



On Wed, 12 Apr 2006, Alex Riesen wrote:
>
> Maybe use "-t" here? I have at least one system which has no tty installed.
> Like this:
> 
>   if [ -n "$GIT_DIFF_PAGER" -a -t ]; then

I assume you mean "-t 1". It needs the FD number. But yes.

		Linus

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Adding color to git diff output.
  2006-04-12 15:46               ` Linus Torvalds
@ 2006-04-12 15:52                 ` Alex Riesen
  0 siblings, 0 replies; 9+ messages in thread
From: Alex Riesen @ 2006-04-12 15:52 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: sean, junkio, git

On 4/12/06, Linus Torvalds <torvalds@osdl.org> wrote:
> >
> > Maybe use "-t" here? I have at least one system which has no tty installed.
> > Like this:
> >
> >   if [ -n "$GIT_DIFF_PAGER" -a -t ]; then
>
> I assume you mean "-t 1". It needs the FD number. But yes.
>

yes, tpyo
(bash doesn't give an error here, btw. Just always 0)

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-04-12 15:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20060411183245.7723c70c.seanlkml@sympatico.ca>
2006-04-11 22:32 ` Adding color to git diff output sean
2006-04-11 23:12   ` Junio C Hamano
2006-04-12  0:34     ` Linus Torvalds
     [not found]       ` <20060411203841.3b824062.seanlkml@sympatico.ca>
2006-04-12  0:38         ` sean
2006-04-12  1:03           ` Linus Torvalds
2006-04-12  7:38             ` Alex Riesen
2006-04-12 15:46               ` Linus Torvalds
2006-04-12 15:52                 ` Alex Riesen
2006-04-12  6:19           ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox