Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@xxxxxxxxx> --- After looking at outputs I noticed that it can also ignore lines with prefixes/suffixes that consist only of punctuation (asterisk, semicolon, dot, etc), because otherwise whole line is highlighted except for terminating punctuation. contrib/diff-highlight/diff-highlight | 96 ++++++++++++++++++++++----------- 1 files changed, 65 insertions(+), 31 deletions(-) diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight index d893898..4811550 100755 --- a/contrib/diff-highlight/diff-highlight +++ b/contrib/diff-highlight/diff-highlight @@ -1,28 +1,40 @@ #!/usr/bin/perl +use warnings; +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 @window; +my $context; +my @removed = (); +my @added = (); +my $started = 0; 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); - } - else { - print shift @window; + if (/^$COLOR*-/) { + push @removed, $_; + } elsif (/^$COLOR*\+/) { + push @added, $_; + } else { + if ($started == 1 ) { + show_pairs(\@removed, \@added); + } else { + print @removed; + print @added; + } + @removed = (); + @added = (); + print $_; + + if (/^$COLOR*(\@| )/) { + $started = 1; + } else { + $started = 0; + } } # Most of the time there is enough output to keep things streaming, @@ -38,23 +50,33 @@ 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; -} +show_pairs(\@removed, \@added); exit 0; -sub show_pair { +sub show_pairs { + my $a = shift; + my $b = shift; + + if (scalar(@{$a}) == scalar(@{$b}) && scalar(@${a}) > 0) { + my @removed; + my @added; + + for(my $i = 0; $i < scalar(@{$a}); $i++) { + my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]); + push @removed, $rm; + push @added, $add; + } + + print @removed; + print @added; + } else { + print @{$a}; + print @{$b}; + } +} + +sub highlight_pair { my @a = split_line(shift); my @b = split_line(shift); @@ -101,8 +123,20 @@ sub show_pair { } } - print highlight(\@a, $pa, $sa); - print highlight(\@b, $pb, $sb); + my $prefa = join('', @a[0..($pa-1)]); + my $prefb = join('', @b[0..($pb-1)]); + my $sufa = join('', @a[($sa+1)..$#a]); + my $sufb = join('', @b[($sb+1)..$#b]); + + # Highlight only if prefix or suffix is interesting (i.e. not consisting + # of color and (for prefix) +/-). Otherwise we would highlight whole + # lines. + if ($prefa =~ /^($COLOR)*-(\s|$COLOR)*$/ && $sufa =~ /^(\s|$COLOR)*$/ + && $prefb =~ /^($COLOR)*\+(\s|$COLOR)*$/ && $sufb =~ /^(\s|$COLOR)*$/) { + return join('', @a), join('', @b); + } else { + return highlight(\@a, $pa, $sa), highlight(\@b, $pb, $sb); + } } sub split_line { -- 1.7.3.4 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html