{"thread":{"id":"3852","subject":"Adding color to git diff output.","startedAt":"2006-04-11T22:32:45Z","lastAt":"2006-04-12T15:52:36Z","messageCount":9,"participants":["sean","Junio C Hamano","Linus Torvalds","Alex Riesen"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"18572","messageId":"BAYC1-PASMTP08B0DB32592225AAD0838AAECD0@CEZ.ICE","threadId":"3852","inReplyTo":null,"subject":"Adding color to git diff output.","fromName":"sean","fromEmail":"seanlkml@sympatico.ca","sentAt":"2006-04-11T22:32:45Z","receivedAt":"2006-04-11T22:32:45Z","isPatch":false,"sender":{"key":"seanlkml@sympatico.ca","avatar":"https://gravatar.com/avatar/f92923f54fc08c401fc59b71829d4b89e9b8087fbba45ff87c82e6a83aee02ae?d=mp&s=160"},"body":"\nLinus posted a colorize program a while back[1] but it wasn't taken into git.  \nThe patch below takes a different approach, adding a GIT_DIFF_PAGER variable.\nYou can use it by assigning a filter to the environment variable, like so:\n\nexport GIT_DIFF_PAGER=\"colordiff | less -RS\"\n\nSean\n\nP.S.  .gitignore is missing a few entries: \"git.spec\" (rpm build), \"tags\" \n      (ctags), and \".*.swp\" (vi editor temp files).\n\n[1] http://marc.theaimsgroup.com/?l=git&m=114315063918024&w=2\n\n\ndiff --git a/git-diff.sh b/git-diff.sh\nindex dc0dd31..f792340 100755\n--- a/git-diff.sh\n+++ b/git-diff.sh\n@@ -69,4 +69,8 @@ case \"$rev\" in\n \t;;\n esac\n \n+if [ -n \"$GIT_DIFF_PAGER\" ]; then\n+\tcmd=\"$cmd | $GIT_DIFF_PAGER\"\n+fi\n+\n eval \"$cmd\"\n"},{"id":"18573","messageId":"7virpf4sg4.fsf@assigned-by-dhcp.cox.net","threadId":"3852","inReplyTo":"BAYC1-PASMTP08B0DB32592225AAD0838AAECD0@CEZ.ICE","subject":"Re: Adding color to git diff output.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-04-11T23:12:59Z","receivedAt":"2006-04-11T23:12:59Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"sean <seanlkml@sympatico.ca> writes:\n\n> Linus posted a colorize program a while back[1] but it wasn't taken into git.  \n> The patch below takes a different approach, adding a GIT_DIFF_PAGER variable.\n> You can use it by assigning a filter to the environment variable, like so:\n>\n> export GIT_DIFF_PAGER=\"colordiff | less -RS\"\n\nSounds like a nice idea, even maybe suitable in a FAQ.\nUnfortunately colordiff does not seem to grok diff --cc output,\nbut that is fine ;-).\n"},{"id":"18574","messageId":"Pine.LNX.4.64.0604111725590.14565@g5.osdl.org","threadId":"3852","inReplyTo":"7virpf4sg4.fsf@assigned-by-dhcp.cox.net","subject":"Re: Adding color to git diff output.","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-04-12T00:34:10Z","receivedAt":"2006-04-12T00:34:10Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Tue, 11 Apr 2006, Junio C Hamano wrote:\n\n> sean <seanlkml@sympatico.ca> writes:\n> \n> > Linus posted a colorize program a while back[1] but it wasn't taken into git.  \n> > The patch below takes a different approach, adding a GIT_DIFF_PAGER variable.\n> > You can use it by assigning a filter to the environment variable, like so:\n> >\n> > export GIT_DIFF_PAGER=\"colordiff | less -RS\"\n> \n> Sounds like a nice idea, even maybe suitable in a FAQ.\n> Unfortunately colordiff does not seem to grok diff --cc output,\n> but that is fine ;-).\n\nWell, the real problem - at least as far as my usage is concerned - is \nthat I'd want colorization to be more integrated so that it can be turned \noff when not appropriate.\n\nThink \"colorized 'ls'\", where if you enable colorization by default, it \nonly colorizes when the output is a tty, so that you can still script \nthings and output things to a file or so, without it getting colorized. \n\nBecause most \"patch\" programs (git-apply included) do not want to see \ncolorization ;)\n\nSo I'd suggest that the \"git diff\" script at a minimum first check whether \nthe output is to a tty before it decides to use GIT_DIFF_PAGER. With \nperhaps an option to _force_ colorization if you want to.\n\nNow, I don't actually enable ls-colorization by default, and I probably \nwouldn't do it for git diff either, but at least for diffs I _might_. But \nI'd definitely want it to be turned off automatically so that I can do\n\n\tgit diff .. > ~/patch-file\n\nwithout having to remember to turn it off explicitly.\n\n\t\tLinus\n"},{"id":"18575","messageId":"BAYC1-PASMTP119CAD2588D00764DB3EA3AEC20@CEZ.ICE","threadId":"3852","inReplyTo":"Pine.LNX.4.64.0604111725590.14565@g5.osdl.org","subject":"Re: Adding color to git diff output.","fromName":"sean","fromEmail":"seanlkml@sympatico.ca","sentAt":"2006-04-12T00:38:41Z","receivedAt":"2006-04-12T00:38:41Z","isPatch":false,"sender":{"key":"seanlkml@sympatico.ca","avatar":"https://gravatar.com/avatar/f92923f54fc08c401fc59b71829d4b89e9b8087fbba45ff87c82e6a83aee02ae?d=mp&s=160"},"body":"On Tue, 11 Apr 2006 17:34:10 -0700 (PDT)\nLinus Torvalds <torvalds@osdl.org> wrote:\n\n> On Tue, 11 Apr 2006, Junio C Hamano wrote:\n> \n> > sean <seanlkml@sympatico.ca> writes:\n> > \n> > > Linus posted a colorize program a while back[1] but it wasn't taken into git.  \n> > > The patch below takes a different approach, adding a GIT_DIFF_PAGER variable.\n> > > You can use it by assigning a filter to the environment variable, like so:\n> > >\n> > > export GIT_DIFF_PAGER=\"colordiff | less -RS\"\n> > \n> > Sounds like a nice idea, even maybe suitable in a FAQ.\n> > Unfortunately colordiff does not seem to grok diff --cc output,\n> > but that is fine ;-).\n> \n> Well, the real problem - at least as far as my usage is concerned - is \n> that I'd want colorization to be more integrated so that it can be turned \n> off when not appropriate.\n> \n> Think \"colorized 'ls'\", where if you enable colorization by default, it \n> only colorizes when the output is a tty, so that you can still script \n> things and output things to a file or so, without it getting colorized. \n> \n> Because most \"patch\" programs (git-apply included) do not want to see \n> colorization ;)\n> \n> So I'd suggest that the \"git diff\" script at a minimum first check whether \n> the output is to a tty before it decides to use GIT_DIFF_PAGER. With \n> perhaps an option to _force_ colorization if you want to.\n> \n> Now, I don't actually enable ls-colorization by default, and I probably \n> wouldn't do it for git diff either, but at least for diffs I _might_. But \n> I'd definitely want it to be turned off automatically so that I can do\n> \n> \tgit diff .. > ~/patch-file\n> \n> without having to remember to turn it off explicitly.\n> \n\nWhat I have is a script ~/bin/gitcdiff:\n\n#!/bin/sh\ntty -s <&1 || exec cat\ncolordiff | less -RS\n\nAnd then setting GIT_DIFF_PAGER=\"~/bin/gitcdiff\".  When piping to a file \nit just uses cat, but when the output is a terminal device it uses the \ncolordiff.\n\nI thought about integrating that logic into git-diff.sh, but i'm not sure\nit's always appropriate.\n\nSean\n"},{"id":"18576","messageId":"Pine.LNX.4.64.0604111801270.14565@g5.osdl.org","threadId":"3852","inReplyTo":"BAYC1-PASMTP119CAD2588D00764DB3EA3AEC20@CEZ.ICE","subject":"Re: Adding color to git diff output.","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-04-12T01:03:35Z","receivedAt":"2006-04-12T01:03:35Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Tue, 11 Apr 2006, sean wrote:\n> \n> What I have is a script ~/bin/gitcdiff:\n> \n> #!/bin/sh\n> tty -s <&1 || exec cat\n> colordiff | less -RS\n> \n> And then setting GIT_DIFF_PAGER=\"~/bin/gitcdiff\".  When piping to a file \n> it just uses cat, but when the output is a terminal device it uses the \n> colordiff.\n> \n> I thought about integrating that logic into git-diff.sh, but i'm not sure\n> it's always appropriate.\n\nYeah, that's close to what I'd have suggested:\n\n\t\tLinus\n\n----\ndiff --git a/git-diff.sh b/git-diff.sh\nindex dc0dd31..f0bea80 100755\n--- a/git-diff.sh\n+++ b/git-diff.sh\n@@ -69,4 +69,8 @@ case \"$rev\" in\n \t;;\n esac\n \n+if [ \"$GIT_DIFF_PAGER\" ] && tty -s <&1; then\n+\tcmd=\"$cmd | $GIT_DIFF_PAGER\"\n+fi\n+\n eval \"$cmd\"\n"},{"id":"18577","messageId":"7v8xqb48pb.fsf@assigned-by-dhcp.cox.net","threadId":"3852","inReplyTo":"BAYC1-PASMTP119CAD2588D00764DB3EA3AEC20@CEZ.ICE","subject":"Re: Adding color to git diff output.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-04-12T06:19:28Z","receivedAt":"2006-04-12T06:19:28Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"sean <seanlkml@sympatico.ca> writes:\n\n> What I have is a script ~/bin/gitcdiff:\n>\n> #!/bin/sh\n> tty -s <&1 || exec cat\n> colordiff | less -RS\n>\n> And then setting GIT_DIFF_PAGER=\"~/bin/gitcdiff\".  When piping to a file \n> it just uses cat, but when the output is a terminal device it uses the \n> colordiff.\n>\n> I thought about integrating that logic into git-diff.sh, but i'm not sure\n> it's always appropriate.\n\nWhat Linus posted seems sensible.  And here is a hacked version\nthat is git-aware ;-).\n\n-- >8 --\nSubject: [PATCH] Add colordiff for git to contrib/colordiff.\n\nI hacked it up to teach it the git extended diff headers, made\nit not to read the whole patch in the array.\n\nAlso, the original program, when arguments are given, ran \"diff\"\nwith the given arguments and showed the output from it.  Of\ncourse, I changed it to run \"git diff\" ;-).\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n contrib/colordiff/README         |    2 \n contrib/colordiff/colordiff.perl |  196 ++++++++++++++++++++++++++++++++++++++\n 2 files changed, 198 insertions(+), 0 deletions(-)\n create mode 100644 contrib/colordiff/README\n create mode 100755 contrib/colordiff/colordiff.perl\n\n2ee04989119dd5c00e066f740efb8f2155d81ede\ndiff --git a/contrib/colordiff/README b/contrib/colordiff/README\nnew file mode 100644\nindex 0000000..2678fdf\n--- /dev/null\n+++ b/contrib/colordiff/README\n@@ -0,0 +1,2 @@\n+This is \"colordiff\" (http://colordiff.sourceforge.net/) by Dave\n+Ewart <davee@sungate.co.uk>, modified specifically for git.\ndiff --git a/contrib/colordiff/colordiff.perl b/contrib/colordiff/colordiff.perl\nnew file mode 100755\nindex 0000000..37c3559\n--- /dev/null\n+++ b/contrib/colordiff/colordiff.perl\n@@ -0,0 +1,196 @@\n+#!/usr/bin/perl -w\n+# \n+# $Id: colordiff.pl,v 1.4.2.10 2004/01/04 15:02:59 daveewart Exp $\n+\n+########################################################################\n+#                                                                      #\n+# ColorDiff - a wrapper/replacment for 'diff' producing                #\n+#             colourful output                                         #\n+#                                                                      #\n+# Copyright (C)2002-2004 Dave Ewart (davee@sungate.co.uk)              #\n+#                                                                      #\n+########################################################################\n+#                                                                      #\n+# This program is free software; you can redistribute it and/or modify #\n+# it under the terms of the GNU General Public License as published by #\n+# the Free Software Foundation; either version 2 of the License, or    #\n+# (at your option) any later version.                                  #\n+#                                                                      #\n+# This program is distributed in the hope that it will be useful,      #\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of       #\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #\n+# GNU General Public License for more details.                         #\n+#                                                                      #\n+# You should have received a copy of the GNU General Public License    #\n+# along with this program; if not, write to the Free Software          #\n+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            #\n+#                                                                      #\n+########################################################################\n+\n+use strict;\n+use Getopt::Long qw(:config pass_through);\n+use IPC::Open2;\n+\n+my $app_name     = 'colordiff';\n+my $version      = '1.0.4';\n+my $author       = 'Dave Ewart';\n+my $author_email = 'davee@sungate.co.uk';\n+my $app_www      = 'http://colordiff.sourceforge.net/';\n+my $copyright    = '(C)2002-2004';\n+my $show_banner  = 1;\n+\n+# ANSI sequences for colours\n+my %colour;\n+$colour{white}       = \"\\033[1;37m\";\n+$colour{yellow}      = \"\\033[1;33m\";\n+$colour{green}       = \"\\033[1;32m\";\n+$colour{blue}        = \"\\033[1;34m\";\n+$colour{cyan}        = \"\\033[1;36m\";\n+$colour{red}         = \"\\033[1;31m\";\n+$colour{magenta}     = \"\\033[1;35m\";\n+$colour{black}       = \"\\033[1;30m\";\n+$colour{darkwhite}   = \"\\033[0;37m\";\n+$colour{darkyellow}  = \"\\033[0;33m\";\n+$colour{darkgreen}   = \"\\033[0;32m\";\n+$colour{darkblue}    = \"\\033[0;34m\";\n+$colour{darkcyan}    = \"\\033[0;36m\";\n+$colour{darkred}     = \"\\033[0;31m\";\n+$colour{darkmagenta} = \"\\033[0;35m\";\n+$colour{darkblack}   = \"\\033[0;30m\";\n+$colour{OFF}         = \"\\033[0;0m\";\n+\n+# Default colours if /etc/colordiffrc or ~/.colordiffrc do not exist\n+my $plain_text = $colour{OFF};\n+my $file_old   = $colour{red};\n+my $file_new   = $colour{blue};\n+my $diff_stuff = $colour{magenta};\n+\n+# Locations for personal and system-wide colour configurations\n+my $HOME   = $ENV{HOME};\n+my $etcdir = '/etc';\n+\n+my ($setting, $value);\n+my @config_files = (\"$etcdir/colordiffrc\", \"$HOME/.colordiffrc\");\n+my $config_file;\n+\n+foreach $config_file (@config_files) {\n+    if (open(COLORDIFFRC, \"<$config_file\")) {\n+        while (<COLORDIFFRC>) {\n+            chop;\n+            next if (/^#/ || /^$/);\n+            s/\\s+//g;\n+            ($setting, $value) = split ('=');\n+            if ($setting eq 'banner') {\n+                if ($value eq 'no') {\n+                    $show_banner = 0;\n+                }\n+                next;\n+            }\n+            if (!defined $colour{$value}) {\n+                print \"Invalid colour specification ($value) in $config_file\\n\";\n+                next;\n+            }\n+            if ($setting eq 'plain') {\n+                $plain_text = $colour{$value};\n+            }\n+            elsif ($setting eq 'oldtext') {\n+                $file_old = $colour{$value};\n+            }\n+            elsif ($setting eq 'newtext') {\n+                $file_new = $colour{$value};\n+            }\n+            elsif ($setting eq 'diffstuff') {\n+                $diff_stuff = $colour{$value};\n+            }\n+            else {\n+                print \"Unknown option in $etcdir/colordiffrc: $setting\\n\";\n+            }\n+        }\n+        close COLORDIFFRC;\n+    }\n+}\n+\n+# colordiff specfic options here.  Need to pre-declare if using variables\n+GetOptions(\n+    \"no-banner\" => sub { $show_banner = 0 },\n+    \"plain-text=s\" => \\&set_color,\n+    \"file-old=s\"   => \\&set_color,\n+    \"file-new=s\"   => \\&set_color,\n+    \"diff-stuff=s\" => \\&set_color\n+);\n+\n+if ($show_banner == 1) {\n+    print STDERR \"$app_name $version ($app_www)\\n\";\n+    print STDERR \"$copyright $author, $author_email\\n\\n\";\n+}\n+\n+if (defined $ARGV[0]) {\n+    # More reliable way of pulling in arguments\n+    open2(\\*INPUTSTREAM, undef, \"git\", \"diff\", @ARGV);\n+}\n+else {\n+    *INPUTSTREAM = \\*STDIN;\n+}\n+\n+my $record;\n+my $nrecs           = 0;\n+my $inside_file_old = 1;\n+my $nparents        = undef;\n+\n+while (<INPUTSTREAM>) {\n+    $nrecs++;\n+    if (/^(\\@\\@+) -[-+0-9, ]+ \\1/) {\n+\t    print \"$diff_stuff\";\n+\t    $nparents = length($1) - 1;\n+    }\n+    elsif (/^diff -/ || /^index / ||\n+\t   /^old mode / || /^new mode / ||\n+\t   /^deleted file mode / || /^new file mode / ||\n+\t   /^similarity index / || /^dissimilarity index / ||\n+\t   /^copy from / || /^copy to / ||\n+\t   /^rename from / || /^rename to /) {\n+\t    $nparents = undef;\n+\t    print \"$diff_stuff\";\n+    }\n+    elsif (defined $nparents) {\n+\t    if ($nparents == 1) {\n+\t\t    if (/^\\+/) {\n+\t\t\t    print $file_new;\n+\t\t    }\n+\t\t    elsif (/^-/) {\n+\t\t\t    print $file_old;\n+\t\t    }\n+\t\t    else {\n+\t\t\t    print $plain_text;\n+\t\t    }\n+\t    }\n+\t    elsif (/^ {$nparents}/) {\n+\t\t    print \"$plain_text\";\n+\t    }\n+\t    elsif (/^[+ ]{$nparents}/) {\n+\t\t    print \"$file_new\";\n+\t    }\n+\t    elsif (/^[- ]{$nparents}/) {\n+\t\t    print \"$file_old\";\n+\t    }\n+\t    else {\n+\t\t    print $plain_text;\n+\t    }\n+    }\n+    elsif (/^--- / || /^\\+\\+\\+ /) {\n+\t    print $diff_stuff;\n+    }\n+    else {\n+\t    print \"$plain_text\";\n+    }\n+    s/$/$colour{OFF}/;\n+    print \"$_\";\n+}\n+close INPUTSTREAM;\n+\n+sub set_color {\n+    my ($type, $color) = @_;\n+\n+    $type =~ s/-/_/;\n+    eval \"\\$$type = \\$colour{$color}\";\n+}\n-- \n1.3.0.rc3.g72c1\n"},{"id":"18578","messageId":"81b0412b0604120038q2e4aef8cn55ba4cfa68e18b34@mail.gmail.com","threadId":"3852","inReplyTo":"Pine.LNX.4.64.0604111801270.14565@g5.osdl.org","subject":"Re: Adding color to git diff output.","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2006-04-12T07:38:55Z","receivedAt":"2006-04-12T07:38:55Z","isPatch":false,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"On 4/12/06, Linus Torvalds <torvalds@osdl.org> wrote:\n> +if [ \"$GIT_DIFF_PAGER\" ] && tty -s <&1; then\n\nMaybe use \"-t\" here? I have at least one system which has no tty installed.\nLike this:\n\n  if [ -n \"$GIT_DIFF_PAGER\" -a -t ]; then\n...\n"},{"id":"18583","messageId":"Pine.LNX.4.64.0604120846000.14565@g5.osdl.org","threadId":"3852","inReplyTo":"81b0412b0604120038q2e4aef8cn55ba4cfa68e18b34@mail.gmail.com","subject":"Re: Adding color to git diff output.","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-04-12T15:46:28Z","receivedAt":"2006-04-12T15:46:28Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Wed, 12 Apr 2006, Alex Riesen wrote:\n>\n> Maybe use \"-t\" here? I have at least one system which has no tty installed.\n> Like this:\n> \n>   if [ -n \"$GIT_DIFF_PAGER\" -a -t ]; then\n\nI assume you mean \"-t 1\". It needs the FD number. But yes.\n\n\t\tLinus\n"},{"id":"18584","messageId":"81b0412b0604120852gaa61e91s5f9ae9f4e061bd56@mail.gmail.com","threadId":"3852","inReplyTo":"Pine.LNX.4.64.0604120846000.14565@g5.osdl.org","subject":"Re: Adding color to git diff output.","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2006-04-12T15:52:36Z","receivedAt":"2006-04-12T15:52:36Z","isPatch":false,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"On 4/12/06, Linus Torvalds <torvalds@osdl.org> wrote:\n> >\n> > Maybe use \"-t\" here? I have at least one system which has no tty installed.\n> > Like this:\n> >\n> >   if [ -n \"$GIT_DIFF_PAGER\" -a -t ]; then\n>\n> I assume you mean \"-t 1\". It needs the FD number. But yes.\n>\n\nyes, tpyo\n(bash doesn't give an error here, btw. Just always 0)\n"}]}