gitweb: Push formatting diff lines to print_diff_chunk()

Now lines are formatted closer to place where we actually use HTML formatted output. This means that we put raw lines in the @chunk accumulator, rather than formatted lines. Because we still need to know class (type) of line when accumulating data to post-process and print, process_diff_line() subroutine was retired and replaced by diff_line_class() used in git_patchset_body() and new restructured format_diff_line() used in print_diff_chunk(). As a side effect, we have to pass \%from and \%to down to callstack. This is a preparation patch for diff refinement highlightning. It's not meant to change gitweb output. [jn: wrote commit message] Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com> Acked-by: Jakub Narębski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michał Kiedrowicz committed Apr 11, 2012 at 23:18 UTC f4a81026508a48a129bd936aef2c75ac1f1cdd97
1 file changed +18 -19
gitweb/gitweb.perl
+18 -19
@@ -2430,26 +2430,26 @@ sub format_cc_diff_chunk_header {
2430 }
2431
2432 # process patch (diff) line (not to be used for diff headers),
2433 -# returning class and HTML-formatted (but not wrapped) line
2434 -sub process_diff_line {
2435 - my $line = shift;
2436 - my ($from, $to) = @_;
2437 -
2438 - my $diff_class = diff_line_class($line, $from, $to);
2433 +# returning HTML-formatted (but not wrapped) line
2434 +sub format_diff_line {
2435 + my ($line, $diff_class, $from, $to) = @_;
2436
2437 chomp $line;
2438 $line = untabify($line);
2439
2440 if ($from && $to && $line =~ m/^\@{2} /) {
2441 $line = format_unidiff_chunk_header($line, $from, $to);
2445 - return $diff_class, $line;
2446 -
2442 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2443 $line = format_cc_diff_chunk_header($line, $from, $to);
2449 - return $diff_class, $line;
2450 -
2444 + } else {
2445 + $line = esc_html($line, -nbsp=>1);
2446 }
2452 - return $diff_class, esc_html($line, -nbsp=>1);
2447 +
2448 + my $diff_classes = "diff";
2449 + $diff_classes .= " $diff_class" if ($diff_class);
2450 + $line = "<div class=\"$diff_classes\">$line</div>\n";
2451 +
2452 + return $line;
2453 }
2454
2455 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
@@ -5068,7 +5068,7 @@ sub print_diff_lines {
5068 }
5069
5070 sub print_diff_chunk {
5071 - my ($diff_style, $is_combined, @chunk) = @_;
5071 + my ($diff_style, $is_combined, $from, $to, @chunk) = @_;
5072 my (@ctx, @rem, @add);
5073
5074 # The class of the previous line.
@@ -5090,6 +5090,8 @@ sub print_diff_chunk {
5090 foreach my $line_info (@chunk) {
5091 my ($class, $line) = @$line_info;
5092
5093 + $line = format_diff_line($line, $class, $from, $to);
5094 +
5095 # print chunk headers
5096 if ($class && $class eq 'chunk_header') {
5097 print $line;
@@ -5243,22 +5245,19 @@ sub git_patchset_body {
5245
5246 next PATCH if ($patch_line =~ m/^diff /);
5247
5246 - my ($class, $line) = process_diff_line($patch_line, \%from, \%to);
5247 - my $diff_classes = "diff";
5248 - $diff_classes .= " $class" if ($class);
5249 - $line = "<div class=\"$diff_classes\">$line</div>\n";
5248 + my $class = diff_line_class($patch_line, \%from, \%to);
5249
5250 if ($class eq 'chunk_header') {
5252 - print_diff_chunk($diff_style, $is_combined, @chunk);
5251 + print_diff_chunk($diff_style, $is_combined, \%from, \%to, @chunk);
5252 @chunk = ();
5253 }
5254
5256 - push @chunk, [ $class, $line ];
5255 + push @chunk, [ $class, $patch_line ];
5256 }
5257
5258 } continue {
5259 if (@chunk) {
5261 - print_diff_chunk($diff_style, $is_combined, @chunk);
5260 + print_diff_chunk($diff_style, $is_combined, \%from, \%to, @chunk);
5261 @chunk = ();
5262 }
5263 print "</div>\n"; # class="patch"