3 # $Id: colordiff.pl,v 1.4.2.10 2004/01/04 15:02:59 daveewart Exp $
5 ########################################################################
7 # ColorDiff - a wrapper/replacment for 'diff' producing #
10 # Copyright (C)2002-2004 Dave Ewart (davee@sungate.co.uk) #
12 ########################################################################
14 # This program is free software; you can redistribute it and/or modify #
15 # it under the terms of the GNU General Public License as published by #
16 # the Free Software Foundation; either version 2 of the License, or #
17 # (at your option) any later version. #
19 # This program is distributed in the hope that it will be useful, #
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
22 # GNU General Public License for more details. #
24 # You should have received a copy of the GNU General Public License #
25 # along with this program; if not, write to the Free Software #
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
28 ########################################################################
31 use Getopt
::Long
qw(:config pass_through);
34 my $app_name = 'colordiff';
35 my $version = '1.0.4';
36 my $author = 'Dave Ewart';
37 my $author_email = 'davee@sungate.co.uk';
38 my $app_www = 'http://colordiff.sourceforge.net/';
39 my $copyright = '(C)2002-2004';
42 # ANSI sequences for colours
44 $colour{white
} = "\033[1;37m";
45 $colour{yellow
} = "\033[1;33m";
46 $colour{green
} = "\033[1;32m";
47 $colour{blue
} = "\033[1;34m";
48 $colour{cyan
} = "\033[1;36m";
49 $colour{red
} = "\033[1;31m";
50 $colour{magenta
} = "\033[1;35m";
51 $colour{black
} = "\033[1;30m";
52 $colour{darkwhite
} = "\033[0;37m";
53 $colour{darkyellow
} = "\033[0;33m";
54 $colour{darkgreen
} = "\033[0;32m";
55 $colour{darkblue
} = "\033[0;34m";
56 $colour{darkcyan
} = "\033[0;36m";
57 $colour{darkred
} = "\033[0;31m";
58 $colour{darkmagenta
} = "\033[0;35m";
59 $colour{darkblack
} = "\033[0;30m";
60 $colour{OFF
} = "\033[0;0m";
62 # Default colours if /etc/colordiffrc or ~/.colordiffrc do not exist
63 my $plain_text = $colour{OFF
};
64 my $file_old = $colour{red
};
65 my $file_new = $colour{blue
};
66 my $diff_stuff = $colour{magenta
};
68 # Locations for personal and system-wide colour configurations
69 my $HOME = $ENV{HOME
};
72 my ($setting, $value);
73 my @config_files = ("$etcdir/colordiffrc", "$HOME/.colordiffrc");
76 foreach $config_file (@config_files) {
77 if (open(COLORDIFFRC
, "<$config_file")) {
78 while (<COLORDIFFRC
>) {
80 next if (/^#/ || /^$/);
82 ($setting, $value) = split ('=');
83 if ($setting eq 'banner') {
89 if (!defined $colour{$value}) {
90 print "Invalid colour specification ($value) in $config_file\n";
93 if ($setting eq 'plain') {
94 $plain_text = $colour{$value};
96 elsif ($setting eq 'oldtext') {
97 $file_old = $colour{$value};
99 elsif ($setting eq 'newtext') {
100 $file_new = $colour{$value};
102 elsif ($setting eq 'diffstuff') {
103 $diff_stuff = $colour{$value};
106 print "Unknown option in $etcdir/colordiffrc: $setting\n";
113 # colordiff specific options here. Need to pre-declare if using variables
115 "no-banner" => sub { $show_banner = 0 },
116 "plain-text=s" => \
&set_color
,
117 "file-old=s" => \
&set_color
,
118 "file-new=s" => \
&set_color
,
119 "diff-stuff=s" => \
&set_color
122 if ($show_banner == 1) {
123 print STDERR
"$app_name $version ($app_www)\n";
124 print STDERR
"$copyright $author, $author_email\n\n";
127 if (defined $ARGV[0]) {
128 # More reliable way of pulling in arguments
129 open2
(\
*INPUTSTREAM
, undef, "git", "diff", @ARGV);
132 *INPUTSTREAM
= \
*STDIN
;
137 my $inside_file_old = 1;
138 my $nparents = undef;
140 while (<INPUTSTREAM
>) {
142 if (/^(\@\@+) -[-+0-9, ]+ \1/) {
144 $nparents = length($1) - 1;
146 elsif (/^diff -/ || /^index / ||
147 /^old mode / || /^new mode / ||
148 /^deleted file mode / || /^new file mode / ||
149 /^similarity index / || /^dissimilarity index / ||
150 /^copy from / || /^copy to / ||
151 /^rename from / || /^rename to /) {
155 elsif (defined $nparents) {
156 if ($nparents == 1) {
167 elsif (/^ {$nparents}/) {
170 elsif (/^[+ ]{$nparents}/) {
173 elsif (/^[- ]{$nparents}/) {
180 elsif (/^--- / || /^\+\+\+ /) {
192 my ($type, $color) = @_;
195 eval "\$$type = \$colour{$color}";