Code

Merge branch 'jc/diff-algo-cleanup'
[git.git] / contrib / diff-highlight / diff-highlight
index d8938982e413a9bf994bd12386121249c888649d..c4404d49c9968608510809309b26e2d08eec8810 100755 (executable)
@@ -1,28 +1,37 @@
 #!/usr/bin/perl
 
+use warnings FATAL => 'all';
+use strict;
+
 # Highlight by reversing foreground and background. You could do
 # other things like bold or underline if you prefer.
 my $HIGHLIGHT   = "\x1b[7m";
 my $UNHIGHLIGHT = "\x1b[27m";
 my $COLOR = qr/\x1b\[[0-9;]*m/;
+my $BORING = qr/$COLOR|\s/;
 
-my @window;
+my @removed;
+my @added;
+my $in_hunk;
 
 while (<>) {
-       # We highlight only single-line changes, so we need
-       # a 4-line window to make a decision on whether
-       # to highlight.
-       push @window, $_;
-       next if @window < 4;
-       if ($window[0] =~ /^$COLOR*(\@| )/ &&
-           $window[1] =~ /^$COLOR*-/ &&
-           $window[2] =~ /^$COLOR*\+/ &&
-           $window[3] !~ /^$COLOR*\+/) {
-               print shift @window;
-               show_pair(shift @window, shift @window);
+       if (!$in_hunk) {
+               print;
+               $in_hunk = /^$COLOR*\@/;
+       }
+       elsif (/^$COLOR*-/) {
+               push @removed, $_;
+       }
+       elsif (/^$COLOR*\+/) {
+               push @added, $_;
        }
        else {
-               print shift @window;
+               show_hunk(\@removed, \@added);
+               @removed = ();
+               @added = ();
+
+               print;
+               $in_hunk = /^$COLOR*[\@ ]/;
        }
 
        # Most of the time there is enough output to keep things streaming,
@@ -38,23 +47,40 @@ while (<>) {
        }
 }
 
-# Special case a single-line hunk at the end of file.
-if (@window == 3 &&
-    $window[0] =~ /^$COLOR*(\@| )/ &&
-    $window[1] =~ /^$COLOR*-/ &&
-    $window[2] =~ /^$COLOR*\+/) {
-       print shift @window;
-       show_pair(shift @window, shift @window);
-}
-
-# And then flush any remaining lines.
-while (@window) {
-       print shift @window;
-}
+# Flush any queued hunk (this can happen when there is no trailing context in
+# the final diff of the input).
+show_hunk(\@removed, \@added);
 
 exit 0;
 
-sub show_pair {
+sub show_hunk {
+       my ($a, $b) = @_;
+
+       # If one side is empty, then there is nothing to compare or highlight.
+       if (!@$a || !@$b) {
+               print @$a, @$b;
+               return;
+       }
+
+       # If we have mismatched numbers of lines on each side, we could try to
+       # be clever and match up similar lines. But for now we are simple and
+       # stupid, and only handle multi-line hunks that remove and add the same
+       # number of lines.
+       if (@$a != @$b) {
+               print @$a, @$b;
+               return;
+       }
+
+       my @queue;
+       for (my $i = 0; $i < @$a; $i++) {
+               my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
+               print $rm;
+               push @queue, $add;
+       }
+       print @queue;
+}
+
+sub highlight_pair {
        my @a = split_line(shift);
        my @b = split_line(shift);
 
@@ -101,8 +127,14 @@ sub show_pair {
                }
        }
 
-       print highlight(\@a, $pa, $sa);
-       print highlight(\@b, $pb, $sb);
+       if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
+               return highlight_line(\@a, $pa, $sa),
+                      highlight_line(\@b, $pb, $sb);
+       }
+       else {
+               return join('', @a),
+                      join('', @b);
+       }
 }
 
 sub split_line {
@@ -111,7 +143,7 @@ sub split_line {
               split /($COLOR*)/;
 }
 
-sub highlight {
+sub highlight_line {
        my ($line, $prefix, $suffix) = @_;
 
        return join('',
@@ -122,3 +154,20 @@ sub highlight {
                @{$line}[($suffix+1)..$#$line]
        );
 }
+
+# Pairs are interesting to highlight only if we are going to end up
+# highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
+# is just useless noise. We can detect this by finding either a matching prefix
+# or suffix (disregarding boring bits like whitespace and colorization).
+sub is_pair_interesting {
+       my ($a, $pa, $sa, $b, $pb, $sb) = @_;
+       my $prefix_a = join('', @$a[0..($pa-1)]);
+       my $prefix_b = join('', @$b[0..($pb-1)]);
+       my $suffix_a = join('', @$a[($sa+1)..$#$a]);
+       my $suffix_b = join('', @$b[($sb+1)..$#$b]);
+
+       return $prefix_a !~ /^$COLOR*-$BORING*$/ ||
+              $prefix_b !~ /^$COLOR*\+$BORING*$/ ||
+              $suffix_a !~ /^$BORING*$/ ||
+              $suffix_b !~ /^$BORING*$/;
+}