Skip to content

Commit 44185f9

Browse files
mkiedrowiczgitster
authored andcommitted
gitweb: Use print_diff_chunk() for both side-by-side and inline diffs
This renames print_sidebyside_diff_chunk() to print_diff_chunk() and makes use of it for both side-by-side and inline diffs. Now diff lines are always accumulated before they are printed. This opens the possibility to preprocess diff output before it's printed, which is needed for diff refinement highlightning (implemented in incoming patches). If print_diff_chunk() was left as is, the new function print_inline_diff_lines() could reorder diff lines. It first prints all context lines, then all removed lines and finally all added lines. If the diff output consisted of mixed added and removed lines, gitweb would reorder these lines. This is true for combined diff output, for example: - removed line for first parent + added line for first parent -removed line for second parent ++added line for both parents would be rendered as: - removed line for first parent -removed line for second parent + added line for first parent ++added line for both parents To prevent gitweb from reordering lines, print_diff_chunk() calls print_diff_lines() as soon as it detects that both added and removed lines are present and there was a class change, and at the end of chunk. Signed-off-by: Michał Kiedrowicz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d21102c commit 44185f9

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

gitweb/gitweb.perl

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5048,10 +5048,32 @@ sub print_sidebyside_diff_lines {
50485048
}
50495049
}
50505050

5051-
sub print_sidebyside_diff_chunk {
5052-
my @chunk = @_;
5051+
# Print context lines and then rem/add lines in inline manner.
5052+
sub print_inline_diff_lines {
5053+
my ($ctx, $rem, $add) = @_;
5054+
5055+
print @$ctx, @$rem, @$add;
5056+
}
5057+
5058+
# Print context lines and then rem/add lines.
5059+
sub print_diff_lines {
5060+
my ($ctx, $rem, $add, $diff_style, $is_combined) = @_;
5061+
5062+
if ($diff_style eq 'sidebyside' && !$is_combined) {
5063+
print_sidebyside_diff_lines($ctx, $rem, $add);
5064+
} else {
5065+
# default 'inline' style and unknown styles
5066+
print_inline_diff_lines($ctx, $rem, $add);
5067+
}
5068+
}
5069+
5070+
sub print_diff_chunk {
5071+
my ($diff_style, $is_combined, @chunk) = @_;
50535072
my (@ctx, @rem, @add);
50545073

5074+
# The class of the previous line.
5075+
my $prev_class = '';
5076+
50555077
return unless @chunk;
50565078

50575079
# incomplete last line might be among removed or added lines,
@@ -5075,9 +5097,13 @@ sub print_sidebyside_diff_chunk {
50755097
}
50765098

50775099
## print from accumulator when have some add/rem lines or end
5078-
# of chunk (flush context lines)
5079-
if (!$class || ((@rem || @add) && $class eq 'ctx')) {
5080-
print_sidebyside_diff_lines(\@ctx, \@rem, \@add);
5100+
# of chunk (flush context lines), or when have add and rem
5101+
# lines and new block is reached (otherwise add/rem lines could
5102+
# be reordered)
5103+
if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5104+
(@rem && @add && $class ne $prev_class)) {
5105+
print_diff_lines(\@ctx, \@rem, \@add,
5106+
$diff_style, $is_combined);
50815107
@ctx = @rem = @add = ();
50825108
}
50835109

@@ -5094,6 +5120,8 @@ sub print_sidebyside_diff_chunk {
50945120
if ($class eq 'ctx') {
50955121
push @ctx, $line;
50965122
}
5123+
5124+
$prev_class = $class;
50975125
}
50985126
}
50995127

@@ -5220,22 +5248,17 @@ sub git_patchset_body {
52205248
$diff_classes .= " $class" if ($class);
52215249
$line = "<div class=\"$diff_classes\">$line</div>\n";
52225250

5223-
if ($diff_style eq 'sidebyside' && !$is_combined) {
5224-
if ($class eq 'chunk_header') {
5225-
print_sidebyside_diff_chunk(@chunk);
5226-
@chunk = ( [ $class, $line ] );
5227-
} else {
5228-
push @chunk, [ $class, $line ];
5229-
}
5230-
} else {
5231-
# default 'inline' style and unknown styles
5232-
print $line;
5251+
if ($class eq 'chunk_header') {
5252+
print_diff_chunk($diff_style, $is_combined, @chunk);
5253+
@chunk = ();
52335254
}
5255+
5256+
push @chunk, [ $class, $line ];
52345257
}
52355258

52365259
} continue {
52375260
if (@chunk) {
5238-
print_sidebyside_diff_chunk(@chunk);
5261+
print_diff_chunk($diff_style, $is_combined, @chunk);
52395262
@chunk = ();
52405263
}
52415264
print "</div>\n"; # class="patch"

0 commit comments

Comments
 (0)