diff-highlight: allow configurable colors
[git.git] / contrib / diff-highlight / diff-highlight
blob4a5f317b7c5412624a5cc463899a9f743b46cc9a
1 #!/usr/bin/perl
3 use warnings FATAL => 'all';
4 use strict;
6 # Highlight by reversing foreground and background. You could do
7 # other things like bold or underline if you prefer.
8 my @OLD_HIGHLIGHT = (
9 color_config('color.diff-highlight.oldnormal'),
10 color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
11 color_config('color.diff-highlight.oldreset', "\x1b[27m")
13 my @NEW_HIGHLIGHT = (
14 color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
15 color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
16 color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
19 my $RESET = "\x1b[m";
20 my $COLOR = qr/\x1b\[[0-9;]*m/;
21 my $BORING = qr/$COLOR|\s/;
23 my @removed;
24 my @added;
25 my $in_hunk;
27 while (<>) {
28 if (!$in_hunk) {
29 print;
30 $in_hunk = /^$COLOR*\@/;
32 elsif (/^$COLOR*-/) {
33 push @removed, $_;
35 elsif (/^$COLOR*\+/) {
36 push @added, $_;
38 else {
39 show_hunk(\@removed, \@added);
40 @removed = ();
41 @added = ();
43 print;
44 $in_hunk = /^$COLOR*[\@ ]/;
47 # Most of the time there is enough output to keep things streaming,
48 # but for something like "git log -Sfoo", you can get one early
49 # commit and then many seconds of nothing. We want to show
50 # that one commit as soon as possible.
52 # Since we can receive arbitrary input, there's no optimal
53 # place to flush. Flushing on a blank line is a heuristic that
54 # happens to match git-log output.
55 if (!length) {
56 local $| = 1;
60 # Flush any queued hunk (this can happen when there is no trailing context in
61 # the final diff of the input).
62 show_hunk(\@removed, \@added);
64 exit 0;
66 # Ideally we would feed the default as a human-readable color to
67 # git-config as the fallback value. But diff-highlight does
68 # not otherwise depend on git at all, and there are reports
69 # of it being used in other settings. Let's handle our own
70 # fallback, which means we will work even if git can't be run.
71 sub color_config {
72 my ($key, $default) = @_;
73 my $s = `git config --get-color $key 2>/dev/null`;
74 return length($s) ? $s : $default;
77 sub show_hunk {
78 my ($a, $b) = @_;
80 # If one side is empty, then there is nothing to compare or highlight.
81 if (!@$a || !@$b) {
82 print @$a, @$b;
83 return;
86 # If we have mismatched numbers of lines on each side, we could try to
87 # be clever and match up similar lines. But for now we are simple and
88 # stupid, and only handle multi-line hunks that remove and add the same
89 # number of lines.
90 if (@$a != @$b) {
91 print @$a, @$b;
92 return;
95 my @queue;
96 for (my $i = 0; $i < @$a; $i++) {
97 my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
98 print $rm;
99 push @queue, $add;
101 print @queue;
104 sub highlight_pair {
105 my @a = split_line(shift);
106 my @b = split_line(shift);
108 # Find common prefix, taking care to skip any ansi
109 # color codes.
110 my $seen_plusminus;
111 my ($pa, $pb) = (0, 0);
112 while ($pa < @a && $pb < @b) {
113 if ($a[$pa] =~ /$COLOR/) {
114 $pa++;
116 elsif ($b[$pb] =~ /$COLOR/) {
117 $pb++;
119 elsif ($a[$pa] eq $b[$pb]) {
120 $pa++;
121 $pb++;
123 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
124 $seen_plusminus = 1;
125 $pa++;
126 $pb++;
128 else {
129 last;
133 # Find common suffix, ignoring colors.
134 my ($sa, $sb) = ($#a, $#b);
135 while ($sa >= $pa && $sb >= $pb) {
136 if ($a[$sa] =~ /$COLOR/) {
137 $sa--;
139 elsif ($b[$sb] =~ /$COLOR/) {
140 $sb--;
142 elsif ($a[$sa] eq $b[$sb]) {
143 $sa--;
144 $sb--;
146 else {
147 last;
151 if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
152 return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
153 highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
155 else {
156 return join('', @a),
157 join('', @b);
161 sub split_line {
162 local $_ = shift;
163 return map { /$COLOR/ ? $_ : (split //) }
164 split /($COLOR*)/;
167 sub highlight_line {
168 my ($line, $prefix, $suffix, $theme) = @_;
170 my $start = join('', @{$line}[0..($prefix-1)]);
171 my $mid = join('', @{$line}[$prefix..$suffix]);
172 my $end = join('', @{$line}[($suffix+1)..$#$line]);
174 # If we have a "normal" color specified, then take over the whole line.
175 # Otherwise, we try to just manipulate the highlighted bits.
176 if (defined $theme->[0]) {
177 s/$COLOR//g for ($start, $mid, $end);
178 chomp $end;
179 return join('',
180 $theme->[0], $start, $RESET,
181 $theme->[1], $mid, $RESET,
182 $theme->[0], $end, $RESET,
183 "\n"
185 } else {
186 return join('',
187 $start,
188 $theme->[1], $mid, $theme->[2],
189 $end
194 # Pairs are interesting to highlight only if we are going to end up
195 # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
196 # is just useless noise. We can detect this by finding either a matching prefix
197 # or suffix (disregarding boring bits like whitespace and colorization).
198 sub is_pair_interesting {
199 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
200 my $prefix_a = join('', @$a[0..($pa-1)]);
201 my $prefix_b = join('', @$b[0..($pb-1)]);
202 my $suffix_a = join('', @$a[($sa+1)..$#$a]);
203 my $suffix_b = join('', @$b[($sb+1)..$#$b]);
205 return $prefix_a !~ /^$COLOR*-$BORING*$/ ||
206 $prefix_b !~ /^$COLOR*\+$BORING*$/ ||
207 $suffix_a !~ /^$BORING*$/ ||
208 $suffix_b !~ /^$BORING*$/;