]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewDiff.pm
viewdiff: use autovivification for long_path hash
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
index 0cce952d99011800e94904c80728f8911bc1278c..ece95f4c2b9b318de5ccc02db8e53bdb27e99bc5 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
@@ -38,13 +38,10 @@ sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
 
 my $OID_NULL = '0{7,40}';
 my $OID_BLOB = '[a-f0-9]{7,40}';
-my $PATH_A = '"?a/.+|/dev/null';
-my $PATH_B = '"?b/.+|/dev/null';
+my $PATH_X = '"?[^/]+/.+|/dev/null';
 
-sub to_html ($$) {
-       $_[0]->linkify_1($_[1]);
-       $_[0]->linkify_2(ascii_html($_[1]));
-}
+# cf. git diff.c :: get_compact_summary
+my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
 
 # link to line numbers in blobs
 sub diff_hunk ($$$$) {
@@ -55,12 +52,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,21 +86,21 @@ 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);
 
        # 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;
+               $ctx->{-long_path}->{$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>'.
-                       to_html($linkify, $rest);
+                       ascii_html($orig) . '</a>' . $spaces .
+                       $linkify->to_html($rest);
                return 1;
        }
        undef;
@@ -112,7 +109,7 @@ sub anchor0 ($$$$$) {
 sub anchor1 ($$$$$) {
        my ($dst, $ctx, $linkify, $pb, $s) = @_;
        my $attr = to_attr($ctx->{-apfx}.$pb) or return;
-       my $line = to_html($linkify, $s);
+       my $line = $linkify->to_html($s);
 
        my $ok = delete $ctx->{-anchors}->{$attr};
 
@@ -135,6 +132,17 @@ sub anchor1 ($$$$$) {
        undef
 }
 
+sub missing_diff_git_line ($$) {
+       my ($dctx, $pb) = @_;
+       # missing "diff --git ..."
+       $dctx->{path_b} = $pb;
+       $dctx->{Q} = '?b='.uri_escape_utf8($pb, UNSAFE);
+       my $pa = $dctx->{path_a};
+       if (defined($pa) && $pa ne $pb) {
+               $dctx->{Q} .= '&amp;a='. uri_escape_utf8($pa, UNSAFE);
+       }
+}
+
 sub flush_diff ($$$) {
        my ($dst, $ctx, $linkify) = @_;
        my $diff = $ctx->{-diff};
@@ -146,7 +154,7 @@ sub flush_diff ($$$) {
                if ($s =~ /^---$/) {
                        to_state($dst, $state, DSTATE_STAT);
                        $$dst .= $s;
-               } elsif ($s =~ /^ /) {
+               } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) {
                        # works for common cases, but not weird/long filenames
                        if ($state == DSTATE_STAT &&
                                        $s =~ /^ (.+)( +\| .*\z)/s) {
@@ -154,12 +162,12 @@ sub flush_diff ($$$) {
                        } elsif ($state2class[$state]) {
                                to_state($dst, $state, DSTATE_CTX);
                        }
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ /^-- $/) { # email signature begins
                        $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_X) ($PATH_X)$!o) {
                        my ($pa, $pb) = ($1, $2);
                        if ($state != DSTATE_HEAD) {
                                to_state($dst, $state, DSTATE_HEAD);
@@ -174,53 +182,69 @@ sub flush_diff ($$$) {
                                        uri_escape_utf8($pa, UNSAFE);
                        }
                        anchor1($dst, $ctx, $linkify, $pb, $s) and next;
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
                        $$dst .= $1 . oid($dctx, $spfx, $2);
                        $dctx = { Q => '' };
-                       $$dst .= to_html($linkify, $s) ;
+                       $$dst .= $linkify->to_html($s) ;
                } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
                        $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
                        $dctx = { Q => '' };
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
                        $dctx->{oid_a} = $1;
                        $dctx->{oid_b} = $2;
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
                        $$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)! ||
-                        $s =~ m!^\+{3} (?:$PATH_B)!)  {
+                       $$dst .= $linkify->to_html($s);
+               } elsif ($s =~ m!^--- ($PATH_X)!o) {
+                       my $pa = $1;
+                       $pa = (split('/', git_unquote($pa), 2))[1];
+                       if (($dctx->{path_a} // '') ne $pa) {
+                               # missing "diff --git ..." ?
+                               $dctx->{path_a} = $pa;
+                       }
+                       # color only (no oid link) if missing dctx->{oid_*}
+                       $state <= DSTATE_STAT and
+                               to_state($dst, $state, DSTATE_HEAD);
+                       $$dst .= $linkify->to_html($s);
+               } elsif ($s =~ m!^\+{3} ($PATH_X)!o) {
+                       my $pb = $1;
+                       $pb = (split('/', git_unquote($pb), 2))[1];
+                       if (($dctx->{path_b} // '') ne $pb) {
+                               missing_diff_git_line($dctx, $pb);
+                       }
+
                        # color only (no oid link) if missing dctx->{oid_*}
                        $state <= DSTATE_STAT and
                                to_state($dst, $state, DSTATE_HEAD);
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ /^\+/) {
                        if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
                                to_state($dst, $state, DSTATE_ADD);
                        }
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } elsif ($s =~ /^-/) {
                        if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
                                to_state($dst, $state, DSTATE_DEL);
                        }
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                # ignore the following lines in headers:
                } elsif ($s =~ /^(?:dis)similarity index/ ||
                         $s =~ /^(?:old|new) mode/ ||
                         $s =~ /^(?:deleted|new) file mode/ ||
                         $s =~ /^(?:copy|rename) (?:from|to) / ||
                         $s =~ /^(?:dis)?similarity index /) {
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                } else {
                        $state <= DSTATE_STAT or
                                to_state($dst, $state, DSTATE_INIT);
-                       $$dst .= to_html($linkify, $s);
+                       $$dst .= $linkify->to_html($s);
                }
        }
        @$diff = ();