]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewDiff.pm
viewdiff: diffstat links to diff anchors
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
index 0d1aefb78bdea037425867bc9f3366a7ed7b1fc5..38cb5a18d711b980b87b5eafea55f973f11e2a47 100644 (file)
@@ -12,16 +12,24 @@ 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);
+use PublicInbox::Hval qw(ascii_html to_attr from_attr);
 use PublicInbox::Git qw(git_unquote);
 
 sub DSTATE_INIT () { 0 }
-sub DSTATE_STAT () { 1 } # TODO
+sub DSTATE_STAT () { 1 }
 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
-sub DSTATE_HUNK () { 3 } # /^@@ /
-sub DSTATE_CTX () { 4 } # /^ /
-sub DSTATE_ADD () { 5 } # /^\+/
-sub DSTATE_DEL () { 6 } # /^\-/
+sub DSTATE_CTX () { 3 } # /^ /
+sub DSTATE_ADD () { 4 } # /^\+/
+sub DSTATE_DEL () { 5 } # /^\-/
+my @state2class = (
+       '', # init
+       '', # stat
+       'head',
+       '', # ctx
+       'add',
+       'del'
+);
+
 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
 
 my $OID_NULL = '0{7,40}';
@@ -46,44 +54,79 @@ sub diff_hunk ($$$$) {
        my ($n) = ($ca =~ /^-(\d+)/);
        $n = defined($n) ? do { ++$n; "#n$n" } : '';
 
-       my $rv = qq(@@ <a\nhref=$spfx$oid_a/s/$dctx->{Q}$n>$ca</a>);
+       my $rv = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
 
        ($n) = ($cb =~ /^\+(\d+)/);
        $n = defined($n) ? do { ++$n; "#n$n" } : '';
 
-       $rv .= qq( <a\nhref=$spfx$oid_b/s/$dctx->{Q}$n>$cb</a> @@);
+       $rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
 }
 
 sub oid ($$$) {
        my ($dctx, $spfx, $oid) = @_;
-       defined($spfx) ? qq(<a\nhref=$spfx$oid/s/$dctx->{Q}>$oid</a>) : $oid;
+       defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
+}
+
+sub to_state ($$$) {
+       my ($dst, $state, $new_state) = @_;
+       $$dst .= '</span>' if $state2class[$state];
+       $_[1] = $new_state;
+       my $class = $state2class[$new_state] or return;
+       $$dst .= qq(<span\nclass="$class">);
+}
+
+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>'.
+                       to_html($linkify, $rest);
+               return 1;
+       }
+       undef;
+}
+
+sub anchor1 ($$$$$) {
+       my ($dst, $anchors, $linkify, $pb, $s) = @_;
+       my $attr = to_attr($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;
+               return 1;
+       }
+       undef
 }
 
 sub flush_diff ($$$$) {
        my ($dst, $spfx, $linkify, $diff) = @_;
        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 =~ /^ /) {
-                       if ($state == DSTATE_HUNK || $state == DSTATE_ADD ||
-                           $state == DSTATE_DEL || $state == DSTATE_HEAD) {
-                               $$dst .= "</span><span\nclass=ctx>";
-                               $state = DSTATE_CTX;
+               if ($s =~ /^---$/) {
+                       to_state($dst, $state, DSTATE_STAT);
+                       $$dst .= "<span\nid=ds>" . $s . '</span>';
+               } elsif ($s =~ /^ /) {
+                       # 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;
+                       } elsif ($state2class[$state]) {
+                               to_state($dst, $state, DSTATE_CTX);
                        }
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ /^-- $/) { # email signature begins
-                       if ($state != DSTATE_INIT) {
-                               $state = DSTATE_INIT;
-                               $$dst .= '</span>';
-                       }
+                       $state == DSTATE_INIT or
+                               to_state($dst, $state, DSTATE_INIT);
                        $$dst .= $s;
                } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
                        if ($state != DSTATE_HEAD) {
                                my ($pa, $pb) = ($1, $2);
-                               $$dst .= '</span>' if $state != DSTATE_INIT;
-                               $$dst .= "<span\nclass=head>";
-                               $state = DSTATE_HEAD;
+                               to_state($dst, $state, DSTATE_HEAD);
                                $pa = (split('/', git_unquote($pa), 2))[1];
                                $pb = (split('/', git_unquote($pb), 2))[1];
                                $dctx = {
@@ -93,51 +136,43 @@ sub flush_diff ($$$$) {
                                        $dctx->{Q} .=
                                             "&a=".uri_escape_utf8($pa, UNSAFE);
                                }
+                               anchor1($dst, $anchors, $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);
+                       $dctx = { Q => '' };
                        $$dst .= to_html($linkify, $s) ;
-               } elsif ($s =~ s/^index ($OID_NULL)(\.\.$OID_BLOB)\b//o) {
+               } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
                        $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
+                       $dctx = { Q => '' };
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
                        $dctx->{oid_a} = $1;
                        $dctx->{oid_b} = $2;
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
-                       my ($ca, $cb) = ($1, $2);
-                       if ($state == DSTATE_HEAD || $state == DSTATE_CTX ||
-                           $state == DSTATE_ADD || $state == DSTATE_DEL) {
-                               $$dst .= "</span><span\nclass=hunk>";
-                               $state = DSTATE_HUNK;
-                               $$dst .= diff_hunk($dctx, $spfx, $ca, $cb);
-                       } else {
-                               $$dst .= to_html($linkify, "@@ $ca $cb @@");
-                       }
+                       $$dst .= '</span>' if $state2class[$state];
+                       $$dst .= qq(<span\nclass="hunk">);
+                       $$dst .= diff_hunk($dctx, $spfx, $1, $2);
+                       $$dst .= '</span>';
+                       $state = DSTATE_CTX;
                        $$dst .= to_html($linkify, $s);
-               } elsif ($s =~ m!^--- $PATH_A!) {
-                       if ($state == DSTATE_INIT) { # color only (no oid link)
-                               $state = DSTATE_HEAD;
-                               $$dst .= "<span\nclass=head>";
-                       }
-                       $$dst .= to_html($linkify, $s);
-               } elsif ($s =~ m!^\+{3} $PATH_B!)  {
-                       if ($state == DSTATE_INIT) { # color only (no oid link)
-                               $state = DSTATE_HEAD;
-                               $$dst .= "<span\nclass=head>";
-                       }
+               } elsif ($s =~ m!^--- $PATH_A! ||
+                        $s =~ m!^\+{3} $PATH_B!)  {
+                       # color only (no oid link)
+                       $state <= DSTATE_STAT and
+                               to_state($dst, $state, DSTATE_HEAD);
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ /^\+/) {
-                       if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
-                               $$dst .= "</span><span\nclass=add>";
-                               $state = DSTATE_ADD;
+                       if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
+                               to_state($dst, $state, DSTATE_ADD);
                        }
                        $$dst .= to_html($linkify, $s);
                } elsif ($s =~ /^-/) {
-                       if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
-                               $$dst .= "</span><span\nclass=del>";
-                               $state = DSTATE_DEL;
+                       if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
+                               to_state($dst, $state, DSTATE_DEL);
                        }
                        $$dst .= to_html($linkify, $s);
                # ignore the following lines in headers:
@@ -148,15 +183,13 @@ sub flush_diff ($$$$) {
                         $s =~ /^(?:dis)?similarity index /) {
                        $$dst .= to_html($linkify, $s);
                } else {
-                       if ($state != DSTATE_INIT) {
-                               $$dst .= '</span>';
-                               $state = DSTATE_INIT;
-                       }
+                       $state <= DSTATE_STAT or
+                               to_state($dst, $state, DSTATE_INIT);
                        $$dst .= to_html($linkify, $s);
                }
        }
        @$diff = ();
-       $$dst .= '</span>' if $state != DSTATE_INIT;
+       $$dst .= '</span>' if $state2class[$state];
        undef;
 }