gitweb: Extract print_sidebyside_diff_lines()

Currently, print_sidebyside_diff_chunk() does two things: it accumulates diff lines and prints them. Accumulation may be used to perform additional operations on diff lines, so it makes sense to split these two things. Thus, whole code that formats and prints diff lines in the 'side-by-side' manner is moved out of print_sidebyside_diff_chunk() to a separate subroutine and two conditions that control printing diff liens are merged. Thanks to that, we can easily (in later patches) replace call to that subroutine with a call to more generic print_diff_lines() that will control whether 'inline' or 'side-by-side' diff should be printed. As a side effect, context lines are printed just before printing added and removed lines, and at the end of chunk (previously, they were printed immediately on the class change). However, this doesn't change gitweb output. The outcome of this patch is that print_sidebyside_diff_chunk() is now much shorter and easier to read. While at it, drop the '# assume that it is change' comment. According to Jakub Narębski: What I meant here when I was writing it that they are lines that changed between two versions, like '!' in original (not unified) context format. We can omit this comment. 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 d21102c9ffd6beb0408693b6f1b633f8e494c921
1 file changed +51 -45
gitweb/gitweb.perl
+51 -45
@@ -5002,6 +5002,52 @@ sub git_difftree_body {
5002 print "</table>\n";
5003 }
5004
5005 +# Print context lines and then rem/add lines in a side-by-side manner.
5006 +sub print_sidebyside_diff_lines {
5007 + my ($ctx, $rem, $add) = @_;
5008 +
5009 + # print context block before add/rem block
5010 + if (@$ctx) {
5011 + print join '',
5012 + '<div class="chunk_block ctx">',
5013 + '<div class="old">',
5014 + @$ctx,
5015 + '</div>',
5016 + '<div class="new">',
5017 + @$ctx,
5018 + '</div>',
5019 + '</div>';
5020 + }
5021 +
5022 + if (!@$add) {
5023 + # pure removal
5024 + print join '',
5025 + '<div class="chunk_block rem">',
5026 + '<div class="old">',
5027 + @$rem,
5028 + '</div>',
5029 + '</div>';
5030 + } elsif (!@$rem) {
5031 + # pure addition
5032 + print join '',
5033 + '<div class="chunk_block add">',
5034 + '<div class="new">',
5035 + @$add,
5036 + '</div>',
5037 + '</div>';
5038 + } else {
5039 + print join '',
5040 + '<div class="chunk_block chg">',
5041 + '<div class="old">',
5042 + @$rem,
5043 + '</div>',
5044 + '<div class="new">',
5045 + @$add,
5046 + '</div>',
5047 + '</div>';
5048 + }
5049 +}
5050 +
5051 sub print_sidebyside_diff_chunk {
5052 my @chunk = @_;
5053 my (@ctx, @rem, @add);
@@ -5028,51 +5074,11 @@ sub print_sidebyside_diff_chunk {
5074 next;
5075 }
5076
5031 - ## print from accumulator when type of class of lines change
5032 - # empty contents block on start rem/add block, or end of chunk
5033 - if (@ctx && (!$class || $class eq 'rem' || $class eq 'add')) {
5034 - print join '',
5035 - '<div class="chunk_block ctx">',
5036 - '<div class="old">',
5037 - @ctx,
5038 - '</div>',
5039 - '<div class="new">',
5040 - @ctx,
5041 - '</div>',
5042 - '</div>';
5043 - @ctx = ();
5044 - }
5045 - # empty add/rem block on start context block, or end of chunk
5046 - if ((@rem || @add) && (!$class || $class eq 'ctx')) {
5047 - if (!@add) {
5048 - # pure removal
5049 - print join '',
5050 - '<div class="chunk_block rem">',
5051 - '<div class="old">',
5052 - @rem,
5053 - '</div>',
5054 - '</div>';
5055 - } elsif (!@rem) {
5056 - # pure addition
5057 - print join '',
5058 - '<div class="chunk_block add">',
5059 - '<div class="new">',
5060 - @add,
5061 - '</div>',
5062 - '</div>';
5063 - } else {
5064 - # assume that it is change
5065 - print join '',
5066 - '<div class="chunk_block chg">',
5067 - '<div class="old">',
5068 - @rem,
5069 - '</div>',
5070 - '<div class="new">',
5071 - @add,
5072 - '</div>',
5073 - '</div>';
5074 - }
5075 - @rem = @add = ();
5077 + ## 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);
5081 + @ctx = @rem = @add = ();
5082 }
5083
5084 ## adding lines to accumulator