๐ŸŸ My personal dotfiles
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

User diff-highlight

Signed-off-by: Stephen Celis <stephen@stephencelis.com>

authored by

Stephen Celis and committed by davidcel.is 62500c02 f5062eac

+178
+4
gitconfig
··· 24 24 [merge] 25 25 conflictstyle = diff3 26 26 tool = vimdiff 27 + [pager] 28 + log = diff-highlight | less 29 + show = diff-highlight | less 30 + diff = diff-highlight | less 27 31 [push] 28 32 default = matching 29 33 [rebase]
+174
local/bin/diff-highlight
··· 1 + #!/usr/bin/perl 2 + # https://raw.github.com/git/git/master/contrib/diff-highlight/diff-highlight 3 + 4 + use warnings FATAL => 'all'; 5 + use strict; 6 + 7 + # Highlight by reversing foreground and background. You could do 8 + # other things like bold or underline if you prefer. 9 + my $HIGHLIGHT = "\x1b[7m"; 10 + my $UNHIGHLIGHT = "\x1b[27m"; 11 + my $COLOR = qr/\x1b\[[0-9;]*m/; 12 + my $BORING = qr/$COLOR|\s/; 13 + 14 + my @removed; 15 + my @added; 16 + my $in_hunk; 17 + 18 + while (<>) { 19 + if (!$in_hunk) { 20 + print; 21 + $in_hunk = /^$COLOR*\@/; 22 + } 23 + elsif (/^$COLOR*-/) { 24 + push @removed, $_; 25 + } 26 + elsif (/^$COLOR*\+/) { 27 + push @added, $_; 28 + } 29 + else { 30 + show_hunk(\@removed, \@added); 31 + @removed = (); 32 + @added = (); 33 + 34 + print; 35 + $in_hunk = /^$COLOR*[\@ ]/; 36 + } 37 + 38 + # Most of the time there is enough output to keep things streaming, 39 + # but for something like "git log -Sfoo", you can get one early 40 + # commit and then many seconds of nothing. We want to show 41 + # that one commit as soon as possible. 42 + # 43 + # Since we can receive arbitrary input, there's no optimal 44 + # place to flush. Flushing on a blank line is a heuristic that 45 + # happens to match git-log output. 46 + if (!length) { 47 + local $| = 1; 48 + } 49 + } 50 + 51 + # Flush any queued hunk (this can happen when there is no trailing context in 52 + # the final diff of the input). 53 + show_hunk(\@removed, \@added); 54 + 55 + exit 0; 56 + 57 + sub show_hunk { 58 + my ($a, $b) = @_; 59 + 60 + # If one side is empty, then there is nothing to compare or highlight. 61 + if (!@$a || !@$b) { 62 + print @$a, @$b; 63 + return; 64 + } 65 + 66 + # If we have mismatched numbers of lines on each side, we could try to 67 + # be clever and match up similar lines. But for now we are simple and 68 + # stupid, and only handle multi-line hunks that remove and add the same 69 + # number of lines. 70 + if (@$a != @$b) { 71 + print @$a, @$b; 72 + return; 73 + } 74 + 75 + my @queue; 76 + for (my $i = 0; $i < @$a; $i++) { 77 + my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]); 78 + print $rm; 79 + push @queue, $add; 80 + } 81 + print @queue; 82 + } 83 + 84 + sub highlight_pair { 85 + my @a = split_line(shift); 86 + my @b = split_line(shift); 87 + 88 + # Find common prefix, taking care to skip any ansi 89 + # color codes. 90 + my $seen_plusminus; 91 + my ($pa, $pb) = (0, 0); 92 + while ($pa < @a && $pb < @b) { 93 + if ($a[$pa] =~ /$COLOR/) { 94 + $pa++; 95 + } 96 + elsif ($b[$pb] =~ /$COLOR/) { 97 + $pb++; 98 + } 99 + elsif ($a[$pa] eq $b[$pb]) { 100 + $pa++; 101 + $pb++; 102 + } 103 + elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') { 104 + $seen_plusminus = 1; 105 + $pa++; 106 + $pb++; 107 + } 108 + else { 109 + last; 110 + } 111 + } 112 + 113 + # Find common suffix, ignoring colors. 114 + my ($sa, $sb) = ($#a, $#b); 115 + while ($sa >= $pa && $sb >= $pb) { 116 + if ($a[$sa] =~ /$COLOR/) { 117 + $sa--; 118 + } 119 + elsif ($b[$sb] =~ /$COLOR/) { 120 + $sb--; 121 + } 122 + elsif ($a[$sa] eq $b[$sb]) { 123 + $sa--; 124 + $sb--; 125 + } 126 + else { 127 + last; 128 + } 129 + } 130 + 131 + if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) { 132 + return highlight_line(\@a, $pa, $sa), 133 + highlight_line(\@b, $pb, $sb); 134 + } 135 + else { 136 + return join('', @a), 137 + join('', @b); 138 + } 139 + } 140 + 141 + sub split_line { 142 + local $_ = shift; 143 + return map { /$COLOR/ ? $_ : (split //) } 144 + split /($COLOR*)/; 145 + } 146 + 147 + sub highlight_line { 148 + my ($line, $prefix, $suffix) = @_; 149 + 150 + return join('', 151 + @{$line}[0..($prefix-1)], 152 + $HIGHLIGHT, 153 + @{$line}[$prefix..$suffix], 154 + $UNHIGHLIGHT, 155 + @{$line}[($suffix+1)..$#$line] 156 + ); 157 + } 158 + 159 + # Pairs are interesting to highlight only if we are going to end up 160 + # highlighting a subset (i.e., not the whole line). Otherwise, the highlighting 161 + # is just useless noise. We can detect this by finding either a matching prefix 162 + # or suffix (disregarding boring bits like whitespace and colorization). 163 + sub is_pair_interesting { 164 + my ($a, $pa, $sa, $b, $pb, $sb) = @_; 165 + my $prefix_a = join('', @$a[0..($pa-1)]); 166 + my $prefix_b = join('', @$b[0..($pb-1)]); 167 + my $suffix_a = join('', @$a[($sa+1)..$#$a]); 168 + my $suffix_b = join('', @$b[($sb+1)..$#$b]); 169 + 170 + return $prefix_a !~ /^$COLOR*-$BORING*$/ || 171 + $prefix_b !~ /^$COLOR*\+$BORING*$/ || 172 + $suffix_a !~ /^$BORING*$/ || 173 + $suffix_b !~ /^$BORING*$/; 174 + }