]> 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 be0c0452d198112599fa593f9aab368097ad1103..ece95f4c2b9b318de5ccc02db8e53bdb27e99bc5 100644 (file)
@@ -38,8 +38,7 @@ 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';
 
 # cf. git diff.c :: get_compact_summary
 my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
@@ -93,8 +92,7 @@ sub anchor0 ($$$$$) {
 
        # 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)) {
@@ -134,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} .= '&a='. uri_escape_utf8($pa, UNSAFE);
+       }
+}
+
 sub flush_diff ($$$) {
        my ($dst, $ctx, $linkify) = @_;
        my $diff = $ctx->{-diff};
@@ -158,7 +167,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)$!o) {
+               } 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);
@@ -193,8 +202,24 @@ sub flush_diff ($$$) {
                        $$dst .= '</span>';
                        $state = DSTATE_CTX;
                        $$dst .= $linkify->to_html($s);
-               } elsif ($s =~ m!^--- (?:$PATH_A)!o ||
-                        $s =~ m!^\+{3} (?:$PATH_B)!o)  {
+               } 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);