]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Import.pm
v2: support Xapian + SQLite indexing
[public-inbox.git] / lib / PublicInbox / Import.pm
index b8e9dd03f68e7ba405ad3bc15a7e9f8f7a2b2909..1a2698a75d609aea4162075e312a791f1c9041c0 100644 (file)
@@ -12,6 +12,8 @@ use PublicInbox::Spawn qw(spawn);
 use PublicInbox::MID qw(mid_mime mid2path);
 use PublicInbox::Address;
 use PublicInbox::ContentId qw(content_id);
+use Date::Parse qw(str2time);
+use Time::Zone qw(tz_offset);
 
 sub new {
        my ($class, $git, $name, $email, $ibx) = @_;
@@ -29,6 +31,7 @@ sub new {
                inbox => $ibx,
                path_type => '2/38', # or 'v2'
                ssoma_lock => 1, # disable for v2
+               bytes_added => 0,
        }, $class
 }
 
@@ -57,7 +60,7 @@ sub gfi_start {
        chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $self->{ref}));
 
        my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
-                       --quiet --done --date-format=rfc2822));
+                       --quiet --done --date-format=raw));
        my $rdr = { 0 => fileno($out_r), 1 => fileno($in_w) };
        my $pid = spawn(\@cmd, undef, $rdr);
        die "spawn fast-import failed: $!" unless defined $pid;
@@ -74,14 +77,7 @@ sub gfi_start {
 
 sub wfail () { die "write to fast-import failed: $!" }
 
-sub now2822 () {
-       my @t = gmtime(time);
-       my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[$t[6]];
-       my $mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$t[4]];
-
-       sprintf('%s, %2d %s %d %02d:%02d:%02d +0000',
-               $day, $t[3], $mon, $t[5] + 1900, $t[2], $t[1], $t[0]);
-}
+sub now_raw () { time . ' +0000' }
 
 sub norm_body ($) {
        my ($mime) = @_;
@@ -138,9 +134,27 @@ sub check_remove_v1 {
        (undef, $cur);
 }
 
+# used for v2 (maybe)
+sub checkpoint {
+       my ($self) = @_;
+       return unless $self->{pid};
+       print { $self->{out} } "checkpoint\n" or wfail;
+       undef;
+}
+
+# used for v2
+sub get_mark {
+       my ($self, $mark) = @_;
+       die "not active\n" unless $self->{pid};
+       my ($r, $w) = $self->gfi_start;
+       print $w "get-mark $mark\n" or wfail;
+       defined(my $oid = <$r>) or die "get-mark failed, need git 2.6.0+\n";
+       $oid;
+}
+
 # returns undef on non-existent
-# ('MISMATCH', msg) on mismatch
-# (:MARK, msg) on success
+# ('MISMATCH', Email::MIME) on mismatch
+# (:MARK, Email::MIME) on success
 #
 # For v2 inboxes, the content_id is returned instead of the msg
 # v2 callers should check with Xapian before calling this as
@@ -171,7 +185,7 @@ sub remove {
                print $w "reset $ref\n" or wfail;
        }
        my $ident = $self->{ident};
-       my $now = now2822();
+       my $now = now_raw();
        $msg ||= 'rm';
        my $len = length($msg) + 1;
        print $w "commit $ref\nmark :$commit\n",
@@ -188,20 +202,53 @@ sub remove {
        (($self->{tip} = ":$commit"), $cur);
 }
 
+sub parse_date ($) {
+       my ($mime) = @_;
+       my $hdr = $mime->header_obj;
+       my $date = $hdr->header_raw('Date');
+       my ($ts, $zone);
+       my $mid = $hdr->header_raw('Message-ID');
+       if ($date) {
+               $ts = eval { str2time($date) };
+               if ($@) {
+                       warn "bad Date: $date in $mid: $@\n";
+               } elsif ($date =~ /\s+([\+\-]\d+)\s*\z/) {
+                       $zone = $1;
+               }
+       }
+       unless ($ts) {
+               my @recvd = $hdr->header_raw('Received');
+               foreach my $r (@recvd) {
+                       $zone = undef;
+                       $r =~ /\s*(\d+\s+[[:alpha:]]+\s+\d{2,4}\s+
+                               \d+\D\d+(?:\D\d+)\s+([\+\-]\d+))/osx or next;
+                       $zone = $2;
+                       $ts = eval { str2time($1) } and last;
+                       warn "no date in Received: $r\n";
+               }
+       }
+       $zone ||= '+0000';
+       # "-1200" is the furthest westermost zone offset,
+       # but git fast-import is liberal so we use "-1400"
+       if ($zone >= 1400 || $zone <= -1400) {
+               warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
+               $zone = '+0000';
+       }
+       $ts = time unless defined $ts;
+       $ts = 0 if $ts < 0; # git uses unsigned times
+       "$ts $zone";
+}
+
 # returns undef on duplicate
+# returns the :MARK of the most recent commit
 sub add {
        my ($self, $mime, $check_cb) = @_; # mime = Email::MIME
 
        my $from = $mime->header('From');
        my ($email) = PublicInbox::Address::emails($from);
        my ($name) = PublicInbox::Address::names($from);
-       # git gets confused with:
-       #  "'A U Thor <u@example.com>' via foo" <foo@example.com>
-       # ref:
-       # <CAD0k6qSUYANxbjjbE4jTW4EeVwOYgBD=bXkSu=akiYC_CB7Ffw@mail.gmail.com>
-       $name =~ tr/<>//d;
 
-       my $date = $mime->header('Date');
+       my $date_raw = parse_date($mime);
        my $subject = $mime->header('Subject');
        $subject = '(no subject)' unless defined $subject;
        my $path_type = $self->{path_type};
@@ -227,17 +274,18 @@ sub add {
                $mime = $check_cb->($mime) or return;
        }
 
-       $mime = $mime->as_string;
        my $blob = $self->{mark}++;
-       print $w "blob\nmark :$blob\ndata ", length($mime), "\n" or wfail;
-       print $w $mime, "\n" or wfail;
+       my $str = $mime->as_string;
+       my $n = length($str);
+       $self->{bytes_added} += $n;
+       print $w "blob\nmark :$blob\ndata ", $n, "\n" or wfail;
+       print $w $str, "\n" or wfail;
+       $str = undef;
 
        # v2: we need this for Xapian
        if ($self->{want_object_id}) {
-               print $w "get-mark :$blob\n" or wfail;
-               defined(my $object_id = <$r>) or
-                               die "get-mark failed, need git 2.6.0+\n";
-               chomp($self->{last_object_id} = $object_id);
+               chomp($self->{last_object_id} = $self->get_mark(":$blob"));
+               $self->{last_object_size} = $n;
        }
 
        my $ref = $self->{ref};
@@ -248,13 +296,29 @@ sub add {
                print $w "reset $ref\n" or wfail;
        }
 
-       utf8::encode($email);
-       utf8::encode($name);
+       # quiet down wide character warnings with utf8::encode
+       if (defined $email) {
+               utf8::encode($email);
+       } else {
+               $email = '';
+               warn "no email in From: $from\n";
+       }
+
+       # git gets confused with:
+       #  "'A U Thor <u@example.com>' via foo" <foo@example.com>
+       # ref:
+       # <CAD0k6qSUYANxbjjbE4jTW4EeVwOYgBD=bXkSu=akiYC_CB7Ffw@mail.gmail.com>
+       if (defined $name) {
+               $name =~ tr/<>//d;
+               utf8::encode($name);
+       } else {
+               $name = '';
+               warn "no name in From: $from\n";
+       }
        utf8::encode($subject);
-       # quiet down wide character warnings:
        print $w "commit $ref\nmark :$commit\n",
-               "author $name <$email> $date\n",
-               "committer $self->{ident} ", now2822(), "\n" or wfail;
+               "author $name <$email> $date_raw\n",
+               "committer $self->{ident} ", now_raw(), "\n" or wfail;
        print $w "data ", (length($subject) + 1), "\n",
                $subject, "\n\n" or wfail;
        if ($tip ne '') {
@@ -265,7 +329,7 @@ sub add {
        $self->{tip} = ":$commit";
 }
 
-sub run_die ($$) {
+sub run_die ($;$) {
        my ($cmd, $env) = @_;
        my $pid = spawn($cmd, $env, undef);
        defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
@@ -294,7 +358,7 @@ sub done {
        }
        if ($nchg) {
                run_die([@cmd, 'update-server-info'], undef);
-               eval {
+               ($self->{path_type} eq '2/38') and eval {
                        require PublicInbox::SearchIdx;
                        my $inbox = $self->{inbox} || $git_dir;
                        my $s = PublicInbox::SearchIdx->new($inbox);