]> 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 38cb5a18d711b980b87b5eafea55f973f11e2a47..10d71004caab28e1d9920dff3e0c11714126756e 100644 (file)
@@ -12,15 +12,19 @@ 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
+# identically-classed lines
 sub DSTATE_INIT () { 0 }
 sub DSTATE_STAT () { 1 }
 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
 sub DSTATE_CTX () { 3 } # /^ /
 sub DSTATE_ADD () { 4 } # /^\+/
 sub DSTATE_DEL () { 5 } # /^\-/
+
+# maps the DSTATE_* to CSS class names compatible with what cgit uses:
 my @state2class = (
        '', # init
        '', # stat
@@ -37,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]));
@@ -51,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> @@);
@@ -76,11 +83,30 @@ sub to_state ($$$) {
 }
 
 sub anchor0 ($$$$$) {
-       my ($dst, $anchors, $linkify, $fn, $rest) = @_;
-       if (my $attr = to_attr($fn)) {
-               $anchors->{$attr} = 1;
-               $$dst .= " <a\nhref=#$attr>" .
-                       ascii_html($fn) . '</a>'.
+       my ($dst, $ctx, $linkify, $fn, $rest) = @_;
+
+       my $orig = $fn;
+
+       # normal git diffstat output is impossible to parse reliably
+       # without --numstat, and that isn't the default for format-patch.
+       # 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/(?: *$DIFFSTAT_COMMENT)? *\z//so;
+       $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
+       $fn = git_unquote($fn);
+
+       # long filenames will require us to walk backwards in anchor1
+       if ($fn =~ s!\A\.\.\./?!!) {
+               my $lp = $ctx->{-long_path} ||= {};
+               $lp->{$fn} = qr/\Q$fn\E\z/s;
+       }
+
+       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>' . $spaces .
                        to_html($linkify, $rest);
                return 1;
        }
@@ -88,33 +114,47 @@ sub anchor0 ($$$$$) {
 }
 
 sub anchor1 ($$$$$) {
-       my ($dst, $anchors, $linkify, $pb, $s) = @_;
-       my $attr = to_attr($pb) or return;
+       my ($dst, $ctx, $linkify, $pb, $s) = @_;
+       my $attr = to_attr($ctx->{-apfx}.$pb) or return;
        my $line = to_html($linkify, $s);
 
-       if (delete $anchors->{$attr} && $line =~ s/^diff //) {
-               $$dst .= "<a\nhref=#ds\nid=$attr>diff</a> ".$line;
+       my $ok = delete $ctx->{-anchors}->{$attr};
+
+       # unlikely, check the end of all long path names we captured:
+       unless ($ok) {
+               my $lp = $ctx->{-long_path} or return;
+               foreach my $fn (keys %$lp) {
+                       $pb =~ $lp->{$fn} or next;
+
+                       delete $lp->{$fn};
+                       $attr = to_attr($ctx->{-apfx}.$fn) or return;
+                       $ok = delete $ctx->{-anchors}->{$attr} or return;
+                       last;
+               }
+       }
+       if ($ok && $line =~ s/^diff //) {
+               $$dst .= "<a\nhref=#i$attr\nid=$attr>diff</a> ".$line;
                return 1;
        }
        undef
 }
 
-sub flush_diff ($$$$) {
-       my ($dst, $spfx, $linkify, $diff) = @_;
+sub flush_diff ($$$) {
+       my ($dst, $ctx, $linkify) = @_;
+       my $diff = $ctx->{-diff};
+       my $spfx = $ctx->{-spfx};
        my $state = DSTATE_INIT;
        my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
-       my $anchors = {}; # attr => filename
 
        foreach my $s (@$diff) {
                if ($s =~ /^---$/) {
                        to_state($dst, $state, DSTATE_STAT);
-                       $$dst .= "<span\nid=ds>" . $s . '</span>';
-               } elsif ($s =~ /^ /) {
+                       $$dst .= $s;
+               } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) {
                        # works for common cases, but not weird/long filenames
                        if ($state == DSTATE_STAT &&
-                                       $s =~ /^ (\S+)(\s+\|.*\z)/s) {
-                               anchor0($dst, $anchors, $linkify, $1, $2)
-                                       and next;
+                                       $s =~ /^ (.+)( +\| .*\z)/s) {
+                               anchor0($dst, $ctx, $linkify, $1, $2) and next;
                        } elsif ($state2class[$state]) {
                                to_state($dst, $state, DSTATE_CTX);
                        }
@@ -123,22 +163,21 @@ 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) {
-                               my ($pa, $pb) = ($1, $2);
                                to_state($dst, $state, DSTATE_HEAD);
-                               $pa = (split('/', git_unquote($pa), 2))[1];
-                               $pb = (split('/', git_unquote($pb), 2))[1];
-                               $dctx = {
-                                       Q => "?b=".uri_escape_utf8($pb, UNSAFE),
-                               };
-                               if ($pa ne $pb) {
-                                       $dctx->{Q} .=
-                                            "&a=".uri_escape_utf8($pa, UNSAFE);
-                               }
-                               anchor1($dst, $anchors, $linkify, $pb, $s)
-                                       and next;
                        }
+                       $pa = (split('/', git_unquote($pa), 2))[1];
+                       $pb = (split('/', git_unquote($pb), 2))[1];
+                       $dctx = {
+                               Q => "?b=".uri_escape_utf8($pb, UNSAFE),
+                       };
+                       if ($pa ne $pb) {
+                               $dctx->{Q} .= '&amp;a='.
+                                       uri_escape_utf8($pa, UNSAFE);
+                       }
+                       anchor1($dst, $ctx, $linkify, $pb, $s) and next;
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
                        $$dst .= $1 . oid($dctx, $spfx, $2);
@@ -159,9 +198,9 @@ sub flush_diff ($$$$) {
                        $$dst .= '</span>';
                        $state = DSTATE_CTX;
                        $$dst .= to_html($linkify, $s);
-               } elsif ($s =~ m!^--- $PATH_A! ||
-                        $s =~ m!^\+{3} $PATH_B!)  {
-                       # color only (no oid link)
+               } 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);
                        $$dst .= to_html($linkify, $s);