diff-highlight: refactor to prepare for multi-line hunks
The current code structure assumes that we will only look at a pair of lines at any given time, and that the end result should always be to output that pair. However, we want to eventually handle multi-line hunks, which will involve collating pairs of removed/added lines. Let's refactor the code to return highlighted pairs instead of printing them. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 13, 2012 at 17:33 UTC
6463fd7ed189f4100e1102062f29b969384f1436
1 file changed
+14
-8
contrib/diff-highlight/diff-highlight
+14
-8
@@ -23,7 +23,7 @@ while (<>) {
23
$window[2] =~ /^$COLOR*\+/ &&
24
$window[3] !~ /^$COLOR*\+/) {
25
print shift @window;
26
- show_pair(shift @window, shift @window);
26
+ show_hunk(shift @window, shift @window);
27
}
28
else {
29
print shift @window;
@@ -48,7 +48,7 @@ if (@window == 3 &&
48
$window[1] =~ /^$COLOR*-/ &&
49
$window[2] =~ /^$COLOR*\+/) {
50
print shift @window;
51
- show_pair(shift @window, shift @window);
51
+ show_hunk(shift @window, shift @window);
52
}
53
54
# And then flush any remaining lines.
@@ -58,7 +58,13 @@ while (@window) {
58
59
exit 0;
60
61
-sub show_pair {
61
+sub show_hunk {
62
+ my ($a, $b) = @_;
63
+
64
+ print highlight_pair($a, $b);
65
+}
66
+
67
+sub highlight_pair {
68
my @a = split_line(shift);
69
my @b = split_line(shift);
70
@@ -106,12 +112,12 @@ sub show_pair {
112
}
113
114
if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
109
- print highlight(\@a, $pa, $sa);
110
- print highlight(\@b, $pb, $sb);
115
+ return highlight_line(\@a, $pa, $sa),
116
+ highlight_line(\@b, $pb, $sb);
117
}
118
else {
113
- print join('', @a);
114
- print join('', @b);
119
+ return join('', @a),
120
+ join('', @b);
121
}
122
}
123
@@ -121,7 +127,7 @@ sub split_line {
127
split /($COLOR*)/;
128
}
129
124
-sub highlight {
130
+sub highlight_line {
131
my ($line, $prefix, $suffix) = @_;
132
133
return join('',