Skip to content

Commit f1310cf

Browse files
jnarebgitster
authored andcommitted
gitweb: Extract formatting of diff chunk header
Refactor main parts of HTML-formatting for diff chunk headers (formatting means here adding links and syntax hightlighting) into separate subroutines: * format_unidiff_chunk_header for ordinary diff, * format_cc_diff_chunk_header for combined diff (more than one parent) This makes format_diff_line() subroutine easier to follow. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 20a864c commit f1310cf

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

gitweb/gitweb.perl

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,69 @@ sub diff_line_class {
22542254
return "";
22552255
}
22562256

2257+
# assumes that $from and $to are defined and correctly filled,
2258+
# and that $line holds a line of chunk header for unified diff
2259+
sub format_unidiff_chunk_header {
2260+
my ($line, $from, $to) = @_;
2261+
2262+
my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2263+
$line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2264+
2265+
$from_lines = 0 unless defined $from_lines;
2266+
$to_lines = 0 unless defined $to_lines;
2267+
2268+
if ($from->{'href'}) {
2269+
$from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2270+
-class=>"list"}, $from_text);
2271+
}
2272+
if ($to->{'href'}) {
2273+
$to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2274+
-class=>"list"}, $to_text);
2275+
}
2276+
$line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2277+
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2278+
return $line;
2279+
}
2280+
2281+
# assumes that $from and $to are defined and correctly filled,
2282+
# and that $line holds a line of chunk header for combined diff
2283+
sub format_cc_diff_chunk_header {
2284+
my ($line, $from, $to) = @_;
2285+
2286+
my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2287+
my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2288+
2289+
@from_text = split(' ', $ranges);
2290+
for (my $i = 0; $i < @from_text; ++$i) {
2291+
($from_start[$i], $from_nlines[$i]) =
2292+
(split(',', substr($from_text[$i], 1)), 0);
2293+
}
2294+
2295+
$to_text = pop @from_text;
2296+
$to_start = pop @from_start;
2297+
$to_nlines = pop @from_nlines;
2298+
2299+
$line = "<span class=\"chunk_info\">$prefix ";
2300+
for (my $i = 0; $i < @from_text; ++$i) {
2301+
if ($from->{'href'}[$i]) {
2302+
$line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2303+
-class=>"list"}, $from_text[$i]);
2304+
} else {
2305+
$line .= $from_text[$i];
2306+
}
2307+
$line .= " ";
2308+
}
2309+
if ($to->{'href'}) {
2310+
$line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2311+
-class=>"list"}, $to_text);
2312+
} else {
2313+
$line .= $to_text;
2314+
}
2315+
$line .= " $prefix</span>" .
2316+
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2317+
return $line;
2318+
}
2319+
22572320
# format patch (diff) line (not to be used for diff headers)
22582321
sub format_diff_line {
22592322
my $line = shift;
@@ -2267,56 +2330,13 @@ sub format_diff_line {
22672330
$line = untabify($line);
22682331

22692332
if ($from && $to && $line =~ m/^\@{2} /) {
2270-
my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2271-
$line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2272-
2273-
$from_lines = 0 unless defined $from_lines;
2274-
$to_lines = 0 unless defined $to_lines;
2275-
2276-
if ($from->{'href'}) {
2277-
$from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2278-
-class=>"list"}, $from_text);
2279-
}
2280-
if ($to->{'href'}) {
2281-
$to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2282-
-class=>"list"}, $to_text);
2283-
}
2284-
$line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2285-
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2333+
$line = format_unidiff_chunk_header($line, $from, $to);
22862334
return "<div class=\"$diff_classes\">$line</div>\n";
2287-
} elsif ($from && $to && $line =~ m/^\@{3}/) {
2288-
my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2289-
my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2290-
2291-
@from_text = split(' ', $ranges);
2292-
for (my $i = 0; $i < @from_text; ++$i) {
2293-
($from_start[$i], $from_nlines[$i]) =
2294-
(split(',', substr($from_text[$i], 1)), 0);
2295-
}
22962335

2297-
$to_text = pop @from_text;
2298-
$to_start = pop @from_start;
2299-
$to_nlines = pop @from_nlines;
2300-
2301-
$line = "<span class=\"chunk_info\">$prefix ";
2302-
for (my $i = 0; $i < @from_text; ++$i) {
2303-
if ($from->{'href'}[$i]) {
2304-
$line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2305-
-class=>"list"}, $from_text[$i]);
2306-
} else {
2307-
$line .= $from_text[$i];
2308-
}
2309-
$line .= " ";
2310-
}
2311-
if ($to->{'href'}) {
2312-
$line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2313-
-class=>"list"}, $to_text);
2314-
} else {
2315-
$line .= $to_text;
2316-
}
2317-
$line .= " $prefix</span>" .
2318-
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2336+
} elsif ($from && $to && $line =~ m/^\@{3}/) {
2337+
$line = format_cc_diff_chunk_header($line, $from, $to);
23192338
return "<div class=\"$diff_classes\">$line</div>\n";
2339+
23202340
}
23212341
return "<div class=\"$diff_classes\">" . esc_html($line, -nbsp=>1) . "</div>\n";
23222342
}

0 commit comments

Comments
 (0)