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 <michal.kiedrowicz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michał Kiedrowicz committed Apr 11, 2012 at 23:18 UTC 44185f93f46af7077585ef7ccf5339334a4ab487
1 file changed +39 -16
gitweb/gitweb.perl
+39 -16
@@ -5048,10 +5048,32 @@ sub print_sidebyside_diff_lines {
5048 }
5049 }
5050
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) = @_;
5072 my (@ctx, @rem, @add);
5073
5074 + # The class of the previous line.
5075 + my $prev_class = '';
5076 +
5077 return unless @chunk;
5078
5079 # incomplete last line might be among removed or added lines,
@@ -5075,9 +5097,13 @@ sub print_sidebyside_diff_chunk {
5097 }
5098
5099 ## 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);
5107 @ctx = @rem = @add = ();
5108 }
5109
@@ -5094,6 +5120,8 @@ sub print_sidebyside_diff_chunk {
5120 if ($class eq 'ctx') {
5121 push @ctx, $line;
5122 }
5123 +
5124 + $prev_class = $class;
5125 }
5126 }
5127
@@ -5220,22 +5248,17 @@ sub git_patchset_body {
5248 $diff_classes .= " $class" if ($class);
5249 $line = "<div class=\"$diff_classes\">$line</div>\n";
5250
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 = ();
5254 }
5255 +
5256 + push @chunk, [ $class, $line ];
5257 }
5258
5259 } continue {
5260 if (@chunk) {
5238 - print_sidebyside_diff_chunk(@chunk);
5261 + print_diff_chunk($diff_style, $is_combined, @chunk);
5262 @chunk = ();
5263 }
5264 print "</div>\n"; # class="patch"