4 use warnings FATAL
=> 'all';
7 # Use the correct value for both UNIX and Windows (/dev/null vs nul)
10 my $NULL = File
::Spec
->devnull();
12 # Highlight by reversing foreground and background. You could do
13 # other things like bold or underline if you prefer.
15 color_config
('color.diff-highlight.oldnormal'),
16 color_config
('color.diff-highlight.oldhighlight', "\x1b[7m"),
17 color_config
('color.diff-highlight.oldreset', "\x1b[27m")
20 color_config
('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
21 color_config
('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
22 color_config
('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
26 my $COLOR = qr/\x1b\[[0-9;]*m/;
27 my $BORING = qr/$COLOR|\s/;
34 our $line_cb = sub { print @_ };
35 our $flush_cb = sub { local $| = 1 };
37 # Count the visible width of a string, excluding any terminal color sequences.
51 # Return a substring of $str, omitting $len visible characters from the
52 # beginning, where terminal color sequences do not count as visible.
56 if ($str =~ s/^$COLOR//) {
69 # match a graph line that begins a commit
70 if (/^(?
:$COLOR?\
|$COLOR?
[ ])* # zero or more leading "|" with space
71 $COLOR?\
*$COLOR?
[ ] # a "*" with its trailing space
72 (?
:$COLOR?\
|$COLOR?
[ ])* # zero or more trailing "|"
73 [ ]* # trailing whitespace for merges
75 my $graph_prefix = $&;
77 # We must flush before setting graph indent, since the
78 # new commit may be indented differently from what we
81 $graph_indent = visible_width
($graph_prefix);
83 } elsif ($graph_indent) {
84 if (length($_) < $graph_indent) {
87 $_ = visible_substr
($_, $graph_indent);
93 $in_hunk = /^$COLOR*\@\@ /;
98 elsif (/^$COLOR*\+/) {
104 $in_hunk = /^$COLOR*[\@ ]/;
107 # Most of the time there is enough output to keep things streaming,
108 # but for something like "git log -Sfoo", you can get one early
109 # commit and then many seconds of nothing. We want to show
110 # that one commit as soon as possible.
112 # Since we can receive arbitrary input, there's no optimal
113 # place to flush. Flushing on a blank line is a heuristic that
114 # happens to match git-log output.
121 # Flush any queued hunk (this can happen when there is no trailing
122 # context in the final diff of the input).
123 show_hunk
(\
@removed, \
@added);
128 sub highlight_stdin
{
135 # Ideally we would feed the default as a human-readable color to
136 # git-config as the fallback value. But diff-highlight does
137 # not otherwise depend on git at all, and there are reports
138 # of it being used in other settings. Let's handle our own
139 # fallback, which means we will work even if git can't be run.
141 my ($key, $default) = @_;
142 my $s = `git config --get-color $key 2>$NULL`;
143 return length($s) ?
$s : $default;
149 # If one side is empty, then there is nothing to compare or highlight.
151 $line_cb->(@
$a, @
$b);
155 # If we have mismatched numbers of lines on each side, we could try to
156 # be clever and match up similar lines. But for now we are simple and
157 # stupid, and only handle multi-line hunks that remove and add the same
160 $line_cb->(@
$a, @
$b);
165 for (my $i = 0; $i < @
$a; $i++) {
166 my ($rm, $add) = highlight_pair
($a->[$i], $b->[$i]);
174 my @a = split_line
(shift);
175 my @b = split_line
(shift);
177 # Find common prefix, taking care to skip any ansi
180 my ($pa, $pb) = (0, 0);
181 while ($pa < @a && $pb < @b) {
182 if ($a[$pa] =~ /$COLOR/) {
185 elsif ($b[$pb] =~ /$COLOR/) {
188 elsif ($a[$pa] eq $b[$pb]) {
192 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
202 # Find common suffix, ignoring colors.
203 my ($sa, $sb) = ($#a, $#b);
204 while ($sa >= $pa && $sb >= $pb) {
205 if ($a[$sa] =~ /$COLOR/) {
208 elsif ($b[$sb] =~ /$COLOR/) {
211 elsif ($a[$sa] eq $b[$sb]) {
220 if (is_pair_interesting
(\
@a, $pa, $sa, \
@b, $pb, $sb)) {
221 return highlight_line
(\
@a, $pa, $sa, \
@OLD_HIGHLIGHT),
222 highlight_line
(\
@b, $pb, $sb, \
@NEW_HIGHLIGHT);
230 # we split either by $COLOR or by character. This has the side effect of
231 # leaving in graph cruft. It works because the graph cruft does not contain "-"
235 return utf8
::decode
($_) ?
236 map { utf8
::encode
($_); $_ }
237 map { /$COLOR/ ?
$_ : (split //) }
239 map { /$COLOR/ ?
$_ : (split //) }
244 my ($line, $prefix, $suffix, $theme) = @_;
246 my $start = join('', @
{$line}[0..($prefix-1)]);
247 my $mid = join('', @
{$line}[$prefix..$suffix]);
248 my $end = join('', @
{$line}[($suffix+1)..$#$line]);
250 # If we have a "normal" color specified, then take over the whole line.
251 # Otherwise, we try to just manipulate the highlighted bits.
252 if (defined $theme->[0]) {
253 s/$COLOR//g for ($start, $mid, $end);
256 $theme->[0], $start, $RESET,
257 $theme->[1], $mid, $RESET,
258 $theme->[0], $end, $RESET,
264 $theme->[1], $mid, $theme->[2],
270 # Pairs are interesting to highlight only if we are going to end up
271 # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
272 # is just useless noise. We can detect this by finding either a matching prefix
273 # or suffix (disregarding boring bits like whitespace and colorization).
274 sub is_pair_interesting
{
275 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
276 my $prefix_a = join('', @
$a[0..($pa-1)]);
277 my $prefix_b = join('', @
$b[0..($pb-1)]);
278 my $suffix_a = join('', @
$a[($sa+1)..$#$a]);
279 my $suffix_b = join('', @
$b[($sb+1)..$#$b]);
281 return visible_substr
($prefix_a, $graph_indent) !~ /^$COLOR*-$BORING*$/ ||
282 visible_substr
($prefix_b, $graph_indent) !~ /^$COLOR*\+$BORING*$/ ||
283 $suffix_a !~ /^$BORING*$/ ||
284 $suffix_b !~ /^$BORING*$/;