3 use warnings FATAL => 'all';
6 # Highlight by reversing foreground and background. You could do
7 # other things like bold or underline if you prefer.
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")
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])
20 my $COLOR = qr/\x1b\[[0-9;]*m/;
21 my $BORING = qr/$COLOR|\s/;
27 # Some scripts may not realize that SIGPIPE is being ignored when launching the
28 # pager--for instance scripts written in Python.
29 $SIG{PIPE} = 'DEFAULT';
34 $in_hunk = /^$COLOR*\@/;
39 elsif (/^$COLOR*\+/) {
43 show_hunk(\@removed, \@added);
48 $in_hunk = /^$COLOR*[\@ ]/;
51 # Most of the time there is enough output to keep things streaming,
52 # but for something like "git log -Sfoo", you can get one early
53 # commit and then many seconds of nothing. We want to show
54 # that one commit as soon as possible.
56 # Since we can receive arbitrary input, there's no optimal
57 # place to flush. Flushing on a blank line is a heuristic that
58 # happens to match git-log output.
64 # Flush any queued hunk (this can happen when there is no trailing context in
65 # the final diff of the input).
66 show_hunk(\@removed, \@added);
70 # Ideally we would feed the default as a human-readable color to
71 # git-config as the fallback value. But diff-highlight does
72 # not otherwise depend on git at all, and there are reports
73 # of it being used in other settings. Let's handle our own
74 # fallback, which means we will work even if git can't be run.
76 my ($key, $default) = @_;
77 my $s = `git config --get-color $key 2>/dev/null`;
78 return length($s) ? $s : $default;
84 # If one side is empty, then there is nothing to compare or highlight.
90 # If we have mismatched numbers of lines on each side, we could try to
91 # be clever and match up similar lines. But for now we are simple and
92 # stupid, and only handle multi-line hunks that remove and add the same
100 for (my $i = 0; $i < @$a; $i++) {
101 my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
109 my @a = split_line(shift);
110 my @b = split_line(shift);
112 # Find common prefix, taking care to skip any ansi
115 my ($pa, $pb) = (0, 0);
116 while ($pa < @a && $pb < @b) {
117 if ($a[$pa] =~ /$COLOR/) {
120 elsif ($b[$pb] =~ /$COLOR/) {
123 elsif ($a[$pa] eq $b[$pb]) {
127 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
137 # Find common suffix, ignoring colors.
138 my ($sa, $sb) = ($#a, $#b);
139 while ($sa >= $pa && $sb >= $pb) {
140 if ($a[$sa] =~ /$COLOR/) {
143 elsif ($b[$sb] =~ /$COLOR/) {
146 elsif ($a[$sa] eq $b[$sb]) {
155 if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
156 return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
157 highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
167 return map { /$COLOR/ ? $_ : (split //) }
172 my ($line, $prefix, $suffix, $theme) = @_;
174 my $start = join('', @{$line}[0..($prefix-1)]);
175 my $mid = join('', @{$line}[$prefix..$suffix]);
176 my $end = join('', @{$line}[($suffix+1)..$#$line]);
178 # If we have a "normal" color specified, then take over the whole line.
179 # Otherwise, we try to just manipulate the highlighted bits.
180 if (defined $theme->[0]) {
181 s/$COLOR//g for ($start, $mid, $end);
184 $theme->[0], $start, $RESET,
185 $theme->[1], $mid, $RESET,
186 $theme->[0], $end, $RESET,
192 $theme->[1], $mid, $theme->[2],
198 # Pairs are interesting to highlight only if we are going to end up
199 # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
200 # is just useless noise. We can detect this by finding either a matching prefix
201 # or suffix (disregarding boring bits like whitespace and colorization).
202 sub is_pair_interesting {
203 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
204 my $prefix_a = join('', @$a[0..($pa-1)]);
205 my $prefix_b = join('', @$b[0..($pb-1)]);
206 my $suffix_a = join('', @$a[($sa+1)..$#$a]);
207 my $suffix_b = join('', @$b[($sb+1)..$#$b]);
209 return $prefix_a !~ /^$COLOR*-$BORING*$/ ||
210 $prefix_b !~ /^$COLOR*\+$BORING*$/ ||
211 $suffix_a !~ /^$BORING*$/ ||
212 $suffix_b !~ /^$BORING*$/;