about summary refs log tree commit homepage
path: root/lib/PublicInbox/ViewDiff.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-08-23 08:31:57 +0000
committerEric Wong <e@80x24.org>2022-08-23 22:40:54 +0000
commit12f07b0d3942064e4dbd0404102d1ff3d067abae (patch)
tree4dac52a58208c7eb9dbc47b9c1551126270c01e5 /lib/PublicInbox/ViewDiff.pm
parent66512e177390aedb4a9380484230768621528e57 (diff)
downloadpublic-inbox-12f07b0d3942064e4dbd0404102d1ff3d067abae.tar.gz
Some folks unfortunately use "git diff --stat -p" to generate
patches.  These messages lack the /^---$/ line and causing
diffstats to not get linkified properly.  We now treat the
/^---$/ as optional and rely on the presence of file lines with
/ \| / proceeding a /\d+ files? changed,/ line.
Diffstat (limited to 'lib/PublicInbox/ViewDiff.pm')
-rw-r--r--lib/PublicInbox/ViewDiff.pm41
1 files changed, 19 insertions, 22 deletions
diff --git a/lib/PublicInbox/ViewDiff.pm b/lib/PublicInbox/ViewDiff.pm
index 960f7e61..ee2d688c 100644
--- a/lib/PublicInbox/ViewDiff.pm
+++ b/lib/PublicInbox/ViewDiff.pm
@@ -166,29 +166,26 @@ sub diff_before_or_after ($$) {
         my ($ctx, $x) = @_;
         my $linkify = $ctx->{-linkify};
         my $dst = $ctx->{obuf};
-        my $anchors = exists($ctx->{-anchors}) ? 1 : 0;
-        for my $y (split(/(^---\n)/sm, $$x)) {
-                if ($y =~ /\A---\n\z/s) {
-                        $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
-                        $anchors |= 2;
-                } elsif ($anchors == 3 && $y =~ /^ [0-9]+ files? changed, /sm) {
-                        # ok, looks like a diffstat, go line-by-line:
-                        for my $l (split(/^/m, $y)) {
-                                if ($l =~ /^ (.+)( +\| .*\z)/s) {
-                                        anchor0($dst, $ctx, $1, $2) or
-                                                $$dst .= $linkify->to_html($l);
-                                } elsif ($l =~ s/^( [0-9]+ files? )changed,//) {
-                                        $$dst .= $1;
-                                        my $end = $ctx->{end_id} // 'related';
-                                        $$dst .= "<a href=#$end>changed</a>,";
-                                        $$dst .= ascii_html($l);
-                                } else {
-                                        $$dst .= $linkify->to_html($l);
-                                }
-                        }
-                } else { # commit message, notes, etc
-                        $$dst .= $linkify->to_html($y);
+        if (exists $ctx->{-anchors} && $$x =~ /\A(.*?) # likely "---\n"
+                        # diffstat lines:
+                        ((?:^\x20(?:[^\n]+?)(?:\x20+\|\x20[^\n]*\n))+)
+                        (\x20[0-9]+\x20files?\x20)changed,([^\n]+\n)
+                        (.*?)\z/msx) { # notes, commit message, etc
+                undef $$x;
+                my @x = ($5, $4, $3, $2, $1);
+                $$dst .= $linkify->to_html(pop @x); # uninteresting prefix
+                for my $l (split(/^/m, pop(@x))) { # per-file diffstat lines
+                        $l =~ /^ (.+)( +\| .*\z)/s and
+                                anchor0($dst, $ctx, $1, $2) and next;
+                        $$dst .= $linkify->to_html($l);
                 }
+                $$dst .= $x[2]; # $3 /^ \d+ files? /
+                my $end = $ctx->{end_id} // 'related';
+                $$dst .= "<a href=#$end>changed</a>,";
+                $$dst .= ascii_html($x[1]); # $4: insertions/deletions
+                $$dst .= $linkify->to_html($x[0]); # notes, commit message, etc
+        } else {
+                $$dst .= $linkify->to_html($$x);
         }
 }