X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FImport.pm;h=60ce7b66419b3ee0e9ad2dca4b93644497539378;hb=23af251dd607c4e75ab1e68063f2c885c48cc035;hp=60cff9c248e1bf068af3fe5cacd4bf9c39f1c401;hpb=46632639a56dfca600d103f548b561316fc8b326;p=public-inbox.git diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index 60cff9c2..60ce7b66 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2020 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ # # git fast-import-based ssoma-mda MDA replacement @@ -9,7 +9,7 @@ package PublicInbox::Import; use strict; use parent qw(PublicInbox::Lock); use v5.10.1; -use PublicInbox::Spawn qw(spawn popen_rd); +use PublicInbox::Spawn qw(run_die popen_rd); use PublicInbox::MID qw(mids mid2path); use PublicInbox::Address; use PublicInbox::Smsg; @@ -21,9 +21,11 @@ use POSIX qw(strftime); sub default_branch () { state $default_branch = do { - my $r = popen_rd([qw(git config --global init.defaultBranch)]); + my $r = popen_rd([qw(git config --global init.defaultBranch)], + { GIT_CONFIG => undef }); chomp(my $h = <$r> // ''); - $h eq '' ? 'refs/heads/master' : $h; + close $r; + $h eq '' ? 'refs/heads/master' : "refs/heads/$h"; } } @@ -54,9 +56,9 @@ sub new { sub gfi_start { my ($self) = @_; - return ($self->{in}, $self->{out}) if $self->{pid}; + return ($self->{in}, $self->{out}) if $self->{in}; - my ($in_r, $pid, $out_r, $out_w); + my ($in_r, $out_r, $out_w); pipe($out_r, $out_w) or die "pipe failed: $!"; $self->lock_acquire; @@ -66,19 +68,15 @@ sub gfi_start { chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $ref)); die "fatal: rev-parse --revs-only $ref: \$?=$?" if $?; if ($self->{path_type} ne '2/38' && $self->{tip}) { - local $/ = "\0"; - my @t = $git->qx(qw(ls-tree -r -z --name-only), $ref); + my $t = $git->qx(qw(ls-tree -r -z --name-only), $ref); die "fatal: ls-tree -r -z --name-only $ref: \$?=$?" if $?; - chomp @t; - $self->{-tree} = { map { $_ => 1 } @t }; + $self->{-tree} = { map { $_ => 1 } split(/\0/, $t) }; } - my @cmd = ('git', "--git-dir=$git->{git_dir}", - qw(fast-import --quiet --done --date-format=raw)); - ($in_r, $pid) = popen_rd(\@cmd, undef, { 0 => $out_r }); + $in_r = $self->{in} = $git->popen(qw(fast-import + --quiet --done --date-format=raw), + undef, { 0 => $out_r }); $out_w->autoflush(1); - $self->{in} = $in_r; $self->{out} = $out_w; - $self->{pid} = $pid; $self->{nchg} = 0; }; if ($@) { @@ -162,14 +160,14 @@ sub check_remove_v1 { sub checkpoint { my ($self) = @_; - return unless $self->{pid}; + return unless $self->{in}; print { $self->{out} } "checkpoint\n" or wfail; undef; } sub progress { my ($self, $msg) = @_; - return unless $self->{pid}; + return unless $self->{in}; print { $self->{out} } "progress $msg\n" or wfail; readline($self->{in}) eq "progress $msg\n" or die "progress $msg not received\n"; @@ -218,7 +216,7 @@ sub barrier { # used for v2 sub get_mark { my ($self, $mark) = @_; - die "not active\n" unless $self->{pid}; + die "not active\n" unless $self->{in}; 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"; @@ -413,12 +411,22 @@ sub add { # v2: we need this for Xapian if ($smsg) { $smsg->{blob} = $self->get_mark(":$blob"); - $smsg->{raw_bytes} = $n; + $smsg->set_bytes($raw_email, $n); if (my $oidx = delete $smsg->{-oidx}) { # used by LeiStore - return if $oidx->blob_exists($smsg->{blob}); + my $eidx_git = delete $smsg->{-eidx_git}; + + # we need this sharedkv to dedupe blobs added in the + # same fast-import transaction + my $u = $self->{uniq_skv} //= do { + require PublicInbox::SharedKV; + my $x = PublicInbox::SharedKV->new; + $x->dbh; + $x; + }; + return if !$u->set_maybe($smsg->oidbin, 1); + return if (!$oidx->vivify_xvmd($smsg) && + $eidx_git->check($smsg->{blob})); } - # XXX do we need this? it's in git at this point - $smsg->{-raw_email} = \$raw_email; } my $ref = $self->{ref}; my $commit = $self->{mark}++; @@ -442,17 +450,7 @@ sub add { $self->{tip} = ":$commit"; } -sub run_die ($;$$) { - my ($cmd, $env, $rdr) = @_; - my $pid = spawn($cmd, $env, $rdr); - waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish'; - $? == 0 or die join(' ', @$cmd) . " failed: $?\n"; -} - my @INIT_FILES = ('HEAD' => undef, # filled in at runtime - 'description' => < <{in} or die 'BUG: missing {in} when done'; print $w "done\n" or wfail; - my $pid = delete $self->{pid} or - die 'BUG: missing {pid} when done'; - waitpid($pid, 0) == $pid or die 'fast-import did not finish'; - $? == 0 or die "fast-import failed: $?"; + close $r or die "fast-import failed: $?"; # ProcessPipe::CLOSE }; my $wait_err = $@; my $nchg = delete $self->{nchg}; @@ -512,8 +507,8 @@ sub atfork_child { } } -sub digest2mid ($$) { - my ($dig, $hdr) = @_; +sub digest2mid ($$;$) { + my ($dig, $hdr, $fallback_time) = @_; my $b64 = $dig->clone->b64digest; # Make our own URLs nicer: # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648 @@ -522,7 +517,7 @@ sub digest2mid ($$) { # Add a date prefix to prevent a leading '-' in case that trips # up some tools (e.g. if a Message-ID were a expected as a # command-line arg) - my $dt = msg_datestamp($hdr); + my $dt = msg_datestamp($hdr, $fallback_time); $dt = POSIX::strftime('%Y%m%d%H%M%S', gmtime($dt)); "$dt.$b64" . '@z'; }