Skip to content

Commit 4551fbb

Browse files
peffgitster
authored andcommitted
diff-highlight: detect --graph by indent
This patch fixes a corner case where diff-highlight may scramble some diffs when combined with --graph. Commit 7e4ffb4 (diff-highlight: add support for --graph output, 2016-08-29) taught diff-highlight to skip past the graph characters at the start of each line with this regex: ($COLOR?\|$COLOR?\s+)* I.e., any series of pipes separated by and followed by arbitrary whitespace. We need to match more than just a single space because the commit in question may be indented to accommodate other parts of the graph drawing. E.g.: * commit 1234abcd | ... | diff --git ... has only a single space, but for the last commit before a fork: | | | | * | commit 1234abcd | |/ ... | | diff --git the diff lines have more spaces between the pipes and the start of the diff. However, when we soak up all of those spaces with the $GRAPH regex, we may accidentally include the leading space for a context line. That means we may consider the actual contents of a context line as part of the diff syntax. In other words, something like this: normal context line -old line +new line -this is a context line with a leading dash would cause us to see that final context line as a removal line, and we'd end up showing the hunk in the wrong order: normal context line -old line -this is a context line with a leading dash +new line Instead, let's a be a little more clever about parsing the graph. We'll look for the actual "*" line that marks the start of a commit, and record the indentation we see there. Then we can skip past that indentation when checking whether the line is a hunk header, removal, addition, etc. There is one tricky thing: the indentation in bytes may be different for various lines of the graph due to coloring. E.g., the "*" on a commit line is generally shown without color, but on the actual diff lines, it will be replaced with a colorized "|" character, adding several bytes. We work around this here by counting "visible" bytes. This is unfortunately a bit more expensive, making us about twice as slow to handle --graph output. But since this is meant to be used interactively anyway, it's tolerably fast (and the non-graph case is unaffected). One alternative would be to search for hunk header lines and use their indentation (since they'd have the same colors as the diff lines which follow). But that just opens up different corner cases. If we see: | | @@ 1,2 1,3 @@ we cannot know if this is a real diff that has been indented due to the graph, or if it's a context line that happens to look like a diff header. We can only be sure of the indent on the "*" lines, since we know those don't contain arbitrary data (technically the user could include a bunch of extra indentation via --format, but that's rare enough to disregard). Reported-by: Phillip Wood <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 009a81e commit 4551fbb

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

contrib/diff-highlight/DiffHighlight.pm

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,82 @@ my $RESET = "\x1b[m";
2121
my $COLOR = qr/\x1b\[[0-9;]*m/;
2222
my $BORING = qr/$COLOR|\s/;
2323

24-
# The patch portion of git log -p --graph should only ever have preceding | and
25-
# not / or \ as merge history only shows up on the commit line.
26-
my $GRAPH = qr/$COLOR?\|$COLOR?\s+/;
27-
2824
my @removed;
2925
my @added;
3026
my $in_hunk;
27+
my $graph_indent = 0;
3128

3229
our $line_cb = sub { print @_ };
3330
our $flush_cb = sub { local $| = 1 };
3431

35-
sub handle_line {
32+
# Count the visible width of a string, excluding any terminal color sequences.
33+
sub visible_width {
3634
local $_ = shift;
35+
my $ret = 0;
36+
while (length) {
37+
if (s/^$COLOR//) {
38+
# skip colors
39+
} elsif (s/^.//) {
40+
$ret++;
41+
}
42+
}
43+
return $ret;
44+
}
45+
46+
# Return a substring of $str, omitting $len visible characters from the
47+
# beginning, where terminal color sequences do not count as visible.
48+
sub visible_substr {
49+
my ($str, $len) = @_;
50+
while ($len > 0) {
51+
if ($str =~ s/^$COLOR//) {
52+
next
53+
}
54+
$str =~ s/^.//;
55+
$len--;
56+
}
57+
return $str;
58+
}
59+
60+
sub handle_line {
61+
my $orig = shift;
62+
local $_ = $orig;
63+
64+
# match a graph line that begins a commit
65+
if (/^(?:$COLOR?\|$COLOR?[ ])* # zero or more leading "|" with space
66+
$COLOR?\*$COLOR?[ ] # a "*" with its trailing space
67+
(?:$COLOR?\|$COLOR?[ ])* # zero or more trailing "|"
68+
[ ]* # trailing whitespace for merges
69+
/x) {
70+
my $graph_prefix = $&;
71+
72+
# We must flush before setting graph indent, since the
73+
# new commit may be indented differently from what we
74+
# queued.
75+
flush();
76+
$graph_indent = visible_width($graph_prefix);
77+
78+
} elsif ($graph_indent) {
79+
if (length($_) < $graph_indent) {
80+
$graph_indent = 0;
81+
} else {
82+
$_ = visible_substr($_, $graph_indent);
83+
}
84+
}
3785

3886
if (!$in_hunk) {
39-
$line_cb->($_);
40-
$in_hunk = /^$GRAPH*$COLOR*\@\@ /;
87+
$line_cb->($orig);
88+
$in_hunk = /^$COLOR*\@\@ /;
4189
}
42-
elsif (/^$GRAPH*$COLOR*-/) {
43-
push @removed, $_;
90+
elsif (/^$COLOR*-/) {
91+
push @removed, $orig;
4492
}
45-
elsif (/^$GRAPH*$COLOR*\+/) {
46-
push @added, $_;
93+
elsif (/^$COLOR*\+/) {
94+
push @added, $orig;
4795
}
4896
else {
4997
flush();
50-
$line_cb->($_);
51-
$in_hunk = /^$GRAPH*$COLOR*[\@ ]/;
98+
$line_cb->($orig);
99+
$in_hunk = /^$COLOR*[\@ ]/;
52100
}
53101

54102
# Most of the time there is enough output to keep things streaming,
@@ -225,8 +273,8 @@ sub is_pair_interesting {
225273
my $suffix_a = join('', @$a[($sa+1)..$#$a]);
226274
my $suffix_b = join('', @$b[($sb+1)..$#$b]);
227275

228-
return $prefix_a !~ /^$GRAPH*$COLOR*-$BORING*$/ ||
229-
$prefix_b !~ /^$GRAPH*$COLOR*\+$BORING*$/ ||
276+
return visible_substr($prefix_a, $graph_indent) !~ /^$COLOR*-$BORING*$/ ||
277+
visible_substr($prefix_b, $graph_indent) !~ /^$COLOR*\+$BORING*$/ ||
230278
$suffix_a !~ /^$BORING*$/ ||
231279
$suffix_b !~ /^$BORING*$/;
232280
}

contrib/diff-highlight/t/t9400-diff-highlight.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,32 @@ test_expect_success 'diff-highlight ignores combined diffs' '
310310
test_cmp expect actual
311311
'
312312

313+
test_expect_success 'diff-highlight handles --graph with leading dash' '
314+
cat >file <<-\EOF &&
315+
before
316+
the old line
317+
-leading dash
318+
EOF
319+
git add file &&
320+
git commit -m before &&
321+
322+
sed s/old/new/ <file >file.tmp &&
323+
mv file.tmp file &&
324+
git add file &&
325+
git commit -m after &&
326+
327+
cat >expect <<-EOF &&
328+
--- a/file
329+
+++ b/file
330+
@@ -1,3 +1,3 @@
331+
before
332+
-the ${CW}old${CR} line
333+
+the ${CW}new${CR} line
334+
-leading dash
335+
EOF
336+
git log --graph -p -1 | "$DIFF_HIGHLIGHT" >actual.raw &&
337+
trim_graph <actual.raw | sed -n "/^---/,\$p" >actual &&
338+
test_cmp expect actual
339+
'
340+
313341
test_done

0 commit comments

Comments
 (0)