Merge branch 'jk/colors'
[alt-git.git] / contrib / diff-highlight / diff-highlight
blob08c88bbc87e51e25cacc5826b26a223f3bfbd382
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 # 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';
31 while (<>) {
32 if (!$in_hunk) {
33 print;
34 $in_hunk = /^$COLOR*\@/;
36 elsif (/^$COLOR*-/) {
37 push @removed, $_;
39 elsif (/^$COLOR*\+/) {
40 push @added, $_;
42 else {
43 show_hunk(\@removed, \@added);
44 @removed = ();
45 @added = ();
47 print;
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.
59 if (!length) {
60 local $| = 1;
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);
68 exit 0;
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.
75 sub color_config {
76 my ($key, $default) = @_;
77 my $s = `git config --get-color $key 2>/dev/null`;
78 return length($s) ? $s : $default;
81 sub show_hunk {
82 my ($a, $b) = @_;
84 # If one side is empty, then there is nothing to compare or highlight.
85 if (!@$a || !@$b) {
86 print @$a, @$b;
87 return;
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
93 # number of lines.
94 if (@$a != @$b) {
95 print @$a, @$b;
96 return;
99 my @queue;
100 for (my $i = 0; $i < @$a; $i++) {
101 my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
102 print $rm;
103 push @queue, $add;
105 print @queue;
108 sub highlight_pair {
109 my @a = split_line(shift);
110 my @b = split_line(shift);
112 # Find common prefix, taking care to skip any ansi
113 # color codes.
114 my $seen_plusminus;
115 my ($pa, $pb) = (0, 0);
116 while ($pa < @a && $pb < @b) {
117 if ($a[$pa] =~ /$COLOR/) {
118 $pa++;
120 elsif ($b[$pb] =~ /$COLOR/) {
121 $pb++;
123 elsif ($a[$pa] eq $b[$pb]) {
124 $pa++;
125 $pb++;
127 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
128 $seen_plusminus = 1;
129 $pa++;
130 $pb++;
132 else {
133 last;
137 # Find common suffix, ignoring colors.
138 my ($sa, $sb) = ($#a, $#b);
139 while ($sa >= $pa && $sb >= $pb) {
140 if ($a[$sa] =~ /$COLOR/) {
141 $sa--;
143 elsif ($b[$sb] =~ /$COLOR/) {
144 $sb--;
146 elsif ($a[$sa] eq $b[$sb]) {
147 $sa--;
148 $sb--;
150 else {
151 last;
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);
159 else {
160 return join('', @a),
161 join('', @b);
165 sub split_line {
166 local $_ = shift;
167 return map { /$COLOR/ ? $_ : (split //) }
168 split /($COLOR*)/;
171 sub highlight_line {
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);
182 chomp $end;
183 return join('',
184 $theme->[0], $start, $RESET,
185 $theme->[1], $mid, $RESET,
186 $theme->[0], $end, $RESET,
187 "\n"
189 } else {
190 return join('',
191 $start,
192 $theme->[1], $mid, $theme->[2],
193 $end
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*$/;