user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/8] viewdiff: linkify diffstats for non-format-patch emails
Date: Tue, 23 Aug 2022 08:31:57 +0000	[thread overview]
Message-ID: <20220823083203.1128993-3-e@80x24.org> (raw)
In-Reply-To: <20220823083203.1128993-1-e@80x24.org>

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.
---
 lib/PublicInbox/ViewDiff.pm | 41 +++++++++++++++++--------------------
 lib/PublicInbox/ViewVCS.pm  |  2 +-
 2 files changed, 20 insertions(+), 23 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);
 	}
 }
 
diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm
index dcb5c67f..4165a1de 100644
--- a/lib/PublicInbox/ViewVCS.pm
+++ b/lib/PublicInbox/ViewVCS.pm
@@ -153,10 +153,10 @@ EOM
 		$buf = '';
 		$ctx->{obuf} = \$buf;
 		$ctx->{-apfx} = $ctx->{-spfx} = $upfx;
-		$ctx->{-anchors} = {};
 		$bdy = '';
 		read($fh, $bdy, -s _);
 		$bdy =~ s/\r?\n/\n/gs;
+		$ctx->{-anchors} = {} if $bdy =~ /^diff --git /sm;
 		flush_diff($ctx, \$bdy);
 		$ctx->zmore($buf);
 		undef $buf;

  parent reply	other threads:[~2022-08-23  8:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23  8:31 [PATCH 0/8] www: diff viewing enhancements Eric Wong
2022-08-23  8:31 ` [PATCH 1/8] view: generate query in single-message and commit views Eric Wong
2022-08-24  1:09   ` Kyle Meyer
2022-08-23  8:31 ` Eric Wong [this message]
2022-08-23  8:31 ` [PATCH 3/8] viewvcs: remove patchid line from commit header Eric Wong
2022-08-23  8:31 ` [PATCH 4/8] viewvcs: show commit titles for parent commits Eric Wong
2022-08-23  8:32 ` [PATCH 5/8] viewvcs: separate email and date with spaces Eric Wong
2022-08-23  8:32 ` [PATCH 6/8] ibx_async_cat: access ->{git} directly Eric Wong
2022-08-23  8:32 ` [PATCH 7/8] gzip_filter: ->zmore and ->zflush support multiple args Eric Wong
2022-08-23  8:32 ` [PATCH 8/8] viewvcs: don't wait on slow disks for git commit titles Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220823083203.1128993-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).