Skip to content

Commit 6463fd7

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 097128d commit 6463fd7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

contrib/diff-highlight/diff-highlight

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ while (<>) {
2323
$window[2] =~ /^$COLOR*\+/ &&
2424
$window[3] !~ /^$COLOR*\+/) {
2525
print shift @window;
26-
show_pair(shift @window, shift @window);
26+
show_hunk(shift @window, shift @window);
2727
}
2828
else {
2929
print shift @window;
@@ -48,7 +48,7 @@ if (@window == 3 &&
4848
$window[1] =~ /^$COLOR*-/ &&
4949
$window[2] =~ /^$COLOR*\+/) {
5050
print shift @window;
51-
show_pair(shift @window, shift @window);
51+
show_hunk(shift @window, shift @window);
5252
}
5353

5454
# And then flush any remaining lines.
@@ -58,7 +58,13 @@ while (@window) {
5858

5959
exit 0;
6060

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 {
6268
my @a = split_line(shift);
6369
my @b = split_line(shift);
6470

@@ -106,12 +112,12 @@ sub show_pair {
106112
}
107113

108114
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);
111117
}
112118
else {
113-
print join('', @a);
114-
print join('', @b);
119+
return join('', @a),
120+
join('', @b);
115121
}
116122
}
117123

@@ -121,7 +127,7 @@ sub split_line {
121127
split /($COLOR*)/;
122128
}
123129

124-
sub highlight {
130+
sub highlight_line {
125131
my ($line, $prefix, $suffix) = @_;
126132

127133
return join('',

0 commit comments

Comments
 (0)