]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewDiff.pm
treewide: "require" + "use" cleanup and docs
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
index 6b8d94372405486ca2223e0fc057e79cb24691ac..10d71004caab28e1d9920dff3e0c11714126756e 100644 (file)
@@ -12,7 +12,7 @@ use warnings;
 use base qw(Exporter);
 our @EXPORT_OK = qw(flush_diff);
 use URI::Escape qw(uri_escape_utf8);
-use PublicInbox::Hval qw(ascii_html to_attr from_attr);
+use PublicInbox::Hval qw(ascii_html to_attr);
 use PublicInbox::Git qw(git_unquote);
 
 # keep track of state so we can avoid redundant HTML tags for
@@ -41,6 +41,9 @@ my $OID_BLOB = '[a-f0-9]{7,40}';
 my $PATH_A = '"?a/.+|/dev/null';
 my $PATH_B = '"?b/.+|/dev/null';
 
+# cf. git diff.c :: get_compact_summary
+my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
+
 sub to_html ($$) {
        $_[0]->linkify_1($_[1]);
        $_[0]->linkify_2(ascii_html($_[1]));
@@ -55,12 +58,12 @@ sub diff_hunk ($$$$) {
        (defined($spfx) && defined($oid_a) && defined($oid_b)) or
                return "@@ $ca $cb @@";
 
-       my ($n) = ($ca =~ /^-(\d+)/);
+       my ($n) = ($ca =~ /^-([0-9]+)/);
        $n = defined($n) ? do { ++$n; "#n$n" } : '';
 
        my $rv = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
 
-       ($n) = ($cb =~ /^\+(\d+)/);
+       ($n) = ($cb =~ /^\+([0-9]+)/);
        $n = defined($n) ? do { ++$n; "#n$n" } : '';
 
        $rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
@@ -89,7 +92,7 @@ sub anchor0 ($$$$$) {
        # So only do best-effort handling of renames for common cases;
        # which works well in practice. If projects put "=>", or trailing
        # spaces in filenames, oh well :P
-       $fn =~ s/ +\z//s;
+       $fn =~ s/(?: *$DIFFSTAT_COMMENT)? *\z//so;
        $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
        $fn = git_unquote($fn);
 
@@ -101,8 +104,9 @@ sub anchor0 ($$$$$) {
 
        if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
                $ctx->{-anchors}->{$attr} = 1;
+               my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
                $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
-                       ascii_html($orig) . '</a>'.
+                       ascii_html($orig) . '</a>' . $spaces .
                        to_html($linkify, $rest);
                return 1;
        }
@@ -159,7 +163,7 @@ sub flush_diff ($$$) {
                        $state == DSTATE_INIT or
                                to_state($dst, $state, DSTATE_INIT);
                        $$dst .= $s;
-               } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
+               } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!o) {
                        my ($pa, $pb) = ($1, $2);
                        if ($state != DSTATE_HEAD) {
                                to_state($dst, $state, DSTATE_HEAD);
@@ -194,8 +198,8 @@ sub flush_diff ($$$) {
                        $$dst .= '</span>';
                        $state = DSTATE_CTX;
                        $$dst .= to_html($linkify, $s);
-               } elsif ($s =~ m!^--- (?:$PATH_A)! ||
-                        $s =~ m!^\+{3} (?:$PATH_B)!)  {
+               } elsif ($s =~ m!^--- (?:$PATH_A)!o ||
+                        $s =~ m!^\+{3} (?:$PATH_B)!o)  {
                        # color only (no oid link) if missing dctx->{oid_*}
                        $state <= DSTATE_STAT and
                                to_state($dst, $state, DSTATE_HEAD);