4 use warnings FATAL => 'all';
7 # Highlight by reversing foreground and background. You could do
8 # other things like bold or underline if you prefer.
10 color_config('color.diff-highlight.oldnormal'),
11 color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
12 color_config('color.diff-highlight.oldreset', "\x1b[27m")
15 color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
16 color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
17 color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
21 my $COLOR = qr/\x1b\[[0-9;]*m/;
22 my $BORING = qr/$COLOR|\s/;
24 # The patch portion of git log -p --graph should only ever have preceding | and
25 # not / or \ as merge history only shows up on the commit line.
26 my $GRAPH = qr/$COLOR?\|$COLOR?\s+/;
32 # Some scripts may not realize that SIGPIPE is being ignored when launching the
33 # pager--for instance scripts written in Python.
34 $SIG{PIPE} = 'DEFAULT';
39 $in_hunk = /^$GRAPH*$COLOR*\@\@ /;
41 elsif (/^$GRAPH*$COLOR*-/) {
44 elsif (/^$GRAPH*$COLOR*\+/) {
48 show_hunk(\@removed, \@added);
53 $in_hunk = /^$GRAPH*$COLOR*[\@ ]/;
56 # Most of the time there is enough output to keep things streaming,
57 # but for something like "git log -Sfoo", you can get one early
58 # commit and then many seconds of nothing. We want to show
59 # that one commit as soon as possible.
61 # Since we can receive arbitrary input, there's no optimal
62 # place to flush. Flushing on a blank line is a heuristic that
63 # happens to match git-log output.
69 # Flush any queued hunk (this can happen when there is no trailing context in
70 # the final diff of the input).
71 show_hunk(\@removed, \@added);
75 # Ideally we would feed the default as a human-readable color to
76 # git-config as the fallback value. But diff-highlight does
77 # not otherwise depend on git at all, and there are reports
78 # of it being used in other settings. Let's handle our own
79 # fallback, which means we will work even if git can't be run.
81 my ($key, $default) = @_;
82 my $s = `git config --get-color $key 2>/dev/null`;
83 return length($s) ? $s : $default;
89 # If one side is empty, then there is nothing to compare or highlight.
95 # If we have mismatched numbers of lines on each side, we could try to
96 # be clever and match up similar lines. But for now we are simple and
97 # stupid, and only handle multi-line hunks that remove and add the same
105 for (my $i = 0; $i < @$a; $i++) {
106 my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
114 my @a = split_line(shift);
115 my @b = split_line(shift);
117 # Find common prefix, taking care to skip any ansi
120 my ($pa, $pb) = (0, 0);
121 while ($pa < @a && $pb < @b) {
122 if ($a[$pa] =~ /$COLOR/) {
125 elsif ($b[$pb] =~ /$COLOR/) {
128 elsif ($a[$pa] eq $b[$pb]) {
132 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
142 # Find common suffix, ignoring colors.
143 my ($sa, $sb) = ($#a, $#b);
144 while ($sa >= $pa && $sb >= $pb) {
145 if ($a[$sa] =~ /$COLOR/) {
148 elsif ($b[$sb] =~ /$COLOR/) {
151 elsif ($a[$sa] eq $b[$sb]) {
160 if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
161 return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
162 highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
170 # we split either by $COLOR or by character. This has the side effect of
171 # leaving in graph cruft. It works because the graph cruft does not contain "-"
175 return utf8::decode($_) ?
176 map { utf8::encode($_); $_ }
177 map { /$COLOR/ ? $_ : (split //) }
179 map { /$COLOR/ ? $_ : (split //) }
184 my ($line, $prefix, $suffix, $theme) = @_;
186 my $start = join('', @{$line}[0..($prefix-1)]);
187 my $mid = join('', @{$line}[$prefix..$suffix]);
188 my $end = join('', @{$line}[($suffix+1)..$#$line]);
190 # If we have a "normal" color specified, then take over the whole line.
191 # Otherwise, we try to just manipulate the highlighted bits.
192 if (defined $theme->[0]) {
193 s/$COLOR//g for ($start, $mid, $end);
196 $theme->[0], $start, $RESET,
197 $theme->[1], $mid, $RESET,
198 $theme->[0], $end, $RESET,
204 $theme->[1], $mid, $theme->[2],
210 # Pairs are interesting to highlight only if we are going to end up
211 # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
212 # is just useless noise. We can detect this by finding either a matching prefix
213 # or suffix (disregarding boring bits like whitespace and colorization).
214 sub is_pair_interesting {
215 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
216 my $prefix_a = join('', @$a[0..($pa-1)]);
217 my $prefix_b = join('', @$b[0..($pb-1)]);
218 my $suffix_a = join('', @$a[($sa+1)..$#$a]);
219 my $suffix_b = join('', @$b[($sb+1)..$#$b]);
221 return $prefix_a !~ /^$GRAPH*$COLOR*-$BORING*$/ ||
222 $prefix_b !~ /^$GRAPH*$COLOR*\+$BORING*$/ ||
223 $suffix_a !~ /^$BORING*$/ ||
224 $suffix_b !~ /^$BORING*$/;