diff-highlight 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/perl
  2. use 5.008;
  3. use warnings FATAL => 'all';
  4. use strict;
  5. # Highlight by reversing foreground and background. You could do
  6. # other things like bold or underline if you prefer.
  7. my @OLD_HIGHLIGHT = (
  8. color_config('color.diff-highlight.oldnormal'),
  9. color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
  10. color_config('color.diff-highlight.oldreset', "\x1b[27m")
  11. );
  12. my @NEW_HIGHLIGHT = (
  13. color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
  14. color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
  15. color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
  16. );
  17. my $RESET = "\x1b[m";
  18. my $COLOR = qr/\x1b\[[0-9;]*m/;
  19. my $BORING = qr/$COLOR|\s/;
  20. my @removed;
  21. my @added;
  22. my $in_hunk;
  23. # Some scripts may not realize that SIGPIPE is being ignored when launching the
  24. # pager--for instance scripts written in Python.
  25. $SIG{PIPE} = 'DEFAULT';
  26. while (<>) {
  27. if (!$in_hunk) {
  28. print;
  29. $in_hunk = /^$COLOR*\@/;
  30. }
  31. elsif (/^$COLOR*-/) {
  32. push @removed, $_;
  33. }
  34. elsif (/^$COLOR*\+/) {
  35. push @added, $_;
  36. }
  37. else {
  38. show_hunk(\@removed, \@added);
  39. @removed = ();
  40. @added = ();
  41. print;
  42. $in_hunk = /^$COLOR*[\@ ]/;
  43. }
  44. # Most of the time there is enough output to keep things streaming,
  45. # but for something like "git log -Sfoo", you can get one early
  46. # commit and then many seconds of nothing. We want to show
  47. # that one commit as soon as possible.
  48. #
  49. # Since we can receive arbitrary input, there's no optimal
  50. # place to flush. Flushing on a blank line is a heuristic that
  51. # happens to match git-log output.
  52. if (!length) {
  53. local $| = 1;
  54. }
  55. }
  56. # Flush any queued hunk (this can happen when there is no trailing context in
  57. # the final diff of the input).
  58. show_hunk(\@removed, \@added);
  59. exit 0;
  60. # Ideally we would feed the default as a human-readable color to
  61. # git-config as the fallback value. But diff-highlight does
  62. # not otherwise depend on git at all, and there are reports
  63. # of it being used in other settings. Let's handle our own
  64. # fallback, which means we will work even if git can't be run.
  65. sub color_config {
  66. my ($key, $default) = @_;
  67. my $s = `git config --get-color $key 2>/dev/null`;
  68. return length($s) ? $s : $default;
  69. }
  70. sub show_hunk {
  71. my ($a, $b) = @_;
  72. # If one side is empty, then there is nothing to compare or highlight.
  73. if (!@$a || !@$b) {
  74. print @$a, @$b;
  75. return;
  76. }
  77. # If we have mismatched numbers of lines on each side, we could try to
  78. # be clever and match up similar lines. But for now we are simple and
  79. # stupid, and only handle multi-line hunks that remove and add the same
  80. # number of lines.
  81. if (@$a != @$b) {
  82. print @$a, @$b;
  83. return;
  84. }
  85. my @queue;
  86. for (my $i = 0; $i < @$a; $i++) {
  87. my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
  88. print $rm;
  89. push @queue, $add;
  90. }
  91. print @queue;
  92. }
  93. sub highlight_pair {
  94. my @a = split_line(shift);
  95. my @b = split_line(shift);
  96. # Find common prefix, taking care to skip any ansi
  97. # color codes.
  98. my $seen_plusminus;
  99. my ($pa, $pb) = (0, 0);
  100. while ($pa < @a && $pb < @b) {
  101. if ($a[$pa] =~ /$COLOR/) {
  102. $pa++;
  103. }
  104. elsif ($b[$pb] =~ /$COLOR/) {
  105. $pb++;
  106. }
  107. elsif ($a[$pa] eq $b[$pb]) {
  108. $pa++;
  109. $pb++;
  110. }
  111. elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
  112. $seen_plusminus = 1;
  113. $pa++;
  114. $pb++;
  115. }
  116. else {
  117. last;
  118. }
  119. }
  120. # Find common suffix, ignoring colors.
  121. my ($sa, $sb) = ($#a, $#b);
  122. while ($sa >= $pa && $sb >= $pb) {
  123. if ($a[$sa] =~ /$COLOR/) {
  124. $sa--;
  125. }
  126. elsif ($b[$sb] =~ /$COLOR/) {
  127. $sb--;
  128. }
  129. elsif ($a[$sa] eq $b[$sb]) {
  130. $sa--;
  131. $sb--;
  132. }
  133. else {
  134. last;
  135. }
  136. }
  137. if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
  138. return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
  139. highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
  140. }
  141. else {
  142. return join('', @a),
  143. join('', @b);
  144. }
  145. }
  146. sub split_line {
  147. local $_ = shift;
  148. return utf8::decode($_) ?
  149. map { utf8::encode($_); $_ }
  150. map { /$COLOR/ ? $_ : (split //) }
  151. split /($COLOR+)/ :
  152. map { /$COLOR/ ? $_ : (split //) }
  153. split /($COLOR+)/;
  154. }
  155. sub highlight_line {
  156. my ($line, $prefix, $suffix, $theme) = @_;
  157. my $start = join('', @{$line}[0..($prefix-1)]);
  158. my $mid = join('', @{$line}[$prefix..$suffix]);
  159. my $end = join('', @{$line}[($suffix+1)..$#$line]);
  160. # If we have a "normal" color specified, then take over the whole line.
  161. # Otherwise, we try to just manipulate the highlighted bits.
  162. if (defined $theme->[0]) {
  163. s/$COLOR//g for ($start, $mid, $end);
  164. chomp $end;
  165. return join('',
  166. $theme->[0], $start, $RESET,
  167. $theme->[1], $mid, $RESET,
  168. $theme->[0], $end, $RESET,
  169. "\n"
  170. );
  171. } else {
  172. return join('',
  173. $start,
  174. $theme->[1], $mid, $theme->[2],
  175. $end
  176. );
  177. }
  178. }
  179. # Pairs are interesting to highlight only if we are going to end up
  180. # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
  181. # is just useless noise. We can detect this by finding either a matching prefix
  182. # or suffix (disregarding boring bits like whitespace and colorization).
  183. sub is_pair_interesting {
  184. my ($a, $pa, $sa, $b, $pb, $sb) = @_;
  185. my $prefix_a = join('', @$a[0..($pa-1)]);
  186. my $prefix_b = join('', @$b[0..($pb-1)]);
  187. my $suffix_a = join('', @$a[($sa+1)..$#$a]);
  188. my $suffix_b = join('', @$b[($sb+1)..$#$b]);
  189. return $prefix_a !~ /^$COLOR*-$BORING*$/ ||
  190. $prefix_b !~ /^$COLOR*\+$BORING*$/ ||
  191. $suffix_a !~ /^$BORING*$/ ||
  192. $suffix_b !~ /^$BORING*$/;
  193. }