]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
overidx: parse_references: less error-prone args
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index cb5549125c4c0fdc0f5ab2b50e55e081ecf692e2..32be9c3ffbb63963f64c1958038f9b4c1d6899ba 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # based on notmuch, but with no concept of folders, files or flags
 #
@@ -19,6 +19,7 @@ use POSIX qw(strftime);
 use PublicInbox::OverIdx;
 use PublicInbox::Spawn qw(spawn);
 use PublicInbox::Git qw(git_unquote);
+use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 my $X = \%PublicInbox::Search::X;
 my ($DB_CREATE_OR_OPEN, $DB_OPEN);
 use constant {
@@ -34,7 +35,7 @@ sub new {
        ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
        my $levels = qr/\A(?:full|medium|basic)\z/;
        my $inboxdir = $ibx->{inboxdir};
-       my $version = $ibx->{version} || 1;
+       my $version = $ibx->version;
        my $indexlevel = 'full';
        my $altid = $ibx->{altid};
        if ($altid) {
@@ -54,7 +55,7 @@ sub new {
                -inbox => $ibx,
                git => $ibx->git,
                -altid => $altid,
-               version => $version,
+               ibx_ver => $version,
                indexlevel => $indexlevel,
        }, $class;
        $ibx->umask_prepare;
@@ -209,12 +210,8 @@ sub index_diff ($$$) {
                        index_diff_inc($self, $_, 'XDFCTX', $xnq);
                } elsif (/^-- $/) { # email signature begins
                        $in_diff = undef;
-               } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
-                       my ($fa, $fb) = ($1, $2);
-                       my $fn = (split('/', git_unquote($fa), 2))[1];
-                       $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
-                       $fn = (split('/', git_unquote($fb), 2))[1];
-                       $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
+               } elsif (m!^diff --git "?[^/]+/.+ "?[^/]+/.+\z!) {
+                       # wait until "---" and "+++" to capture filenames
                        $in_diff = 1;
                # traditional diff:
                } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
@@ -224,12 +221,12 @@ sub index_diff ($$$) {
                        next unless $opt =~ /[uU]/;
                        $in_diff = index_old_diff_fn($self, \%seen, $fa, $fb,
                                                        $xnq);
-               } elsif (m!^--- ("?a/.+)!) {
+               } elsif (m!^--- ("?[^/]+/.+)!) {
                        my $fn = $1;
                        $fn = (split('/', git_unquote($fn), 2))[1];
                        $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
                        $in_diff = 1;
-               } elsif (m!^\+\+\+ ("?b/.+)!)  {
+               } elsif (m!^\+\+\+ ("?[^/]+/.+)!)  {
                        my $fn = $1;
                        $fn = (split('/', git_unquote($fn), 2))[1];
                        $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
@@ -311,11 +308,14 @@ sub index_xapian { # msg_iter callback
 
 sub add_xapian ($$$$$$) {
        my ($self, $mime, $num, $oid, $mids, $mid0) = @_;
-       my $smsg = PublicInbox::SearchMsg->new($mime);
+       my $smsg = PublicInbox::Smsg->new($mime);
+       my $hdr = $mime->header_obj;
+       $smsg->{ds} = msg_datestamp($hdr, $self->{autime});
+       $smsg->{ts} = msg_timestamp($hdr, $self->{cotime});
        my $doc = $X->{Document}->new;
        my $subj = $smsg->subject;
-       add_val($doc, PublicInbox::Search::TS(), $smsg->ts);
-       my @ds = gmtime($smsg->ds);
+       add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
+       my @ds = gmtime($smsg->{ds});
        my $yyyymmdd = strftime('%Y%m%d', @ds);
        add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
        my $dt = strftime('%Y%m%d%H%M%S', @ds);
@@ -339,8 +339,10 @@ sub add_xapian ($$$$$$) {
                }
        }
        $smsg->{to} = $smsg->{cc} = '';
-       PublicInbox::OverIdx::parse_references($smsg, $mid0, $mids);
-       my $data = $smsg->to_doc_data($oid, $mid0);
+       $smsg->{blob} = $oid;
+       $smsg->{mid} = $mid0;
+       PublicInbox::OverIdx::parse_references($smsg, $hdr, $mids);
+       my $data = $smsg->to_doc_data;
        $doc->set_data($data);
        if (my $altid = $self->{-altid}) {
                foreach my $alt (@$altid) {
@@ -358,7 +360,7 @@ sub add_xapian ($$$$$$) {
 
 sub _msgmap_init ($) {
        my ($self) = @_;
-       die "BUG: _msgmap_init is only for v1\n" if $self->{version} != 1;
+       die "BUG: _msgmap_init is only for v1\n" if $self->{ibx_ver} != 1;
        $self->{mm} //= eval {
                require PublicInbox::Msgmap;
                PublicInbox::Msgmap->new($self->{inboxdir}, 1);
@@ -379,7 +381,8 @@ sub add_message {
                        add_xapian($self, $mime, $num, $oid, $mids, $mid0);
                }
                if (my $over = $self->{over}) {
-                       $over->add_overview($mime, $bytes, $num, $oid, $mid0);
+                       $over->add_overview($mime, $bytes, $num, $oid, $mid0,
+                                               $self);
                }
        };
 
@@ -464,7 +467,7 @@ sub remove_by_oid {
        for (; $head != $tail; $head++) {
                my $docid = $head->get_docid;
                my $doc = $db->get_document($docid);
-               my $smsg = PublicInbox::SearchMsg->wrap($mid);
+               my $smsg = PublicInbox::Smsg->wrap($mid);
                $smsg->load_expand($doc);
                if ($smsg->{blob} eq $oid) {
                        push(@delete, $docid);
@@ -600,6 +603,10 @@ sub read_log {
                } elsif ($line =~ /^commit ($h40)/o) {
                        $latest = $1;
                        $newest ||= $latest;
+               } elsif ($line =~ /^author .*? ([0-9]+) [\-\+][0-9]+$/) {
+                       $self->{over}->{autime} = $self->{autime} = $1;
+               } elsif ($line =~ /^committer .*? ([0-9]+) [\-\+][0-9]+$/) {
+                       $self->{over}->{cotime} = $self->{cotime} = $1;
                }
        }
        close($log) or die "git log failed: \$?=$?";
@@ -655,7 +662,7 @@ sub _git_log {
                $self->{regen_down} = $high + $fcount;
        }
 
-       $git->popen(qw/log --no-notes --no-color --no-renames
+       $git->popen(qw/log --pretty=raw --no-notes --no-color --no-renames
                                --raw -r --no-abbrev/, $range);
 }