X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FImport.pm;h=27f36a7e486b84337e1e4e4586ab9ec5251e4c65;hb=2c69f7bc34a2b12dc7f55e2bb24fa28565f24f03;hp=1f52a0cc80f9eddd40a8e16f7b44496a4085b005;hpb=1f89e15a29cd63335f40902f1de7944e2efe91fa;p=public-inbox.git diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index 1f52a0cc..27f36a7e 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -8,7 +8,6 @@ package PublicInbox::Import; use strict; use warnings; use Fcntl qw(:flock :DEFAULT); -use Email::Address; use PublicInbox::Spawn qw(spawn); use PublicInbox::MID qw(mid_mime mid2path); @@ -51,6 +50,7 @@ sub gfi_start { $self->{out} = $out_w; $self->{lockfh} = $lockfh; $self->{pid} = $pid; + $self->{nchg} = 0; ($in_r, $out_w); } @@ -112,8 +112,11 @@ sub remove { defined($n) or die "read final byte of cat-blob failed: $!"; die "bad read on final byte: <$lf>" if $lf ne "\n"; my $cur = Email::MIME->new($buf); - if ($cur->header('Subject') ne $mime->header('Subject') || - norm_body($cur) ne norm_body($mime)) { + my $cur_s = $cur->header('Subject'); + $cur_s = '' unless defined $cur_s; + my $cur_m = $mime->header('Subject'); + $cur_m = '' unless defined $cur_m; + if ($cur_s ne $cur_m || norm_body($cur) ne norm_body($mime)) { return ('MISMATCH', $cur); } @@ -131,29 +134,30 @@ sub remove { "data 3\nrm\n\n", 'from ', ($parent ? $parent : $tip), "\n" or wfail; print $w "D $path\n\n" or wfail; + $self->{nchg}++; (($self->{tip} = ":$commit"), $cur); } # returns undef on duplicate sub add { - my ($self, $mime) = @_; # mime = Email::MIME + my ($self, $mime, $check_cb) = @_; # mime = Email::MIME my $from = $mime->header('From'); - my @from = Email::Address->parse($from); - my $name = $from[0]->name; - my $email = $from[0]->address; + my ($email) = ($from =~ /([^<\s]+\@[^>\s]+)/g); + my $name = $from; + $name =~ s/\s*\S+\@\S+\s*\z//; + # git gets confused with: + # "'A U Thor ' via foo" + # ref: + # + $name =~ tr/<>// and $name = $email; + my $date = $mime->header('Date'); my $subject = $mime->header('Subject'); $subject = '(no subject)' unless defined $subject; my $mid = mid_mime($mime); my $path = mid2path($mid); - # git gets confused with: - # "'A U Thor ' via foo" - # ref: - # - $name =~ s/<([^>]+)>/($1)/g; - my ($r, $w) = $self->gfi_start; my $tip = $self->{tip}; if ($tip ne '') { @@ -166,6 +170,10 @@ sub add { # kill potentially confusing/misleading headers $mime->header_set($_) for qw(bytes lines content-length status); + if ($check_cb) { + $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; @@ -191,6 +199,7 @@ sub add { print $w 'from ', ($parent ? $parent : $tip), "\n" or wfail; } print $w "M 100644 :$blob $path\n\n" or wfail; + $self->{nchg}++; $self->{tip} = ":$commit"; } @@ -202,14 +211,15 @@ sub done { 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: $?"; + my $nchg = delete $self->{nchg}; # for compatibility with existing ssoma installations # we can probably remove this entirely by 2020 my $git_dir = $self->{git}->{git_dir}; - my $index = "$git_dir/ssoma.index"; # XXX: change the following scope to: if (-e $index) # in 2018 or so.. my @cmd = ('git', "--git-dir=$git_dir"); - unless ($ENV{FAST}) { + if ($nchg && !$ENV{FAST}) { + my $index = "$git_dir/ssoma.index"; my $env = { GIT_INDEX_FILE => $index }; my @rt = (@cmd, qw(read-tree -m -v -i), $self->{ref}); $pid = spawn(\@rt, $env, undef); @@ -217,11 +227,18 @@ sub done { waitpid($pid, 0) == $pid or die 'read-tree did not finish'; $? == 0 or die "failed to update $git_dir/ssoma.index: $?\n"; } - - $pid = spawn([@cmd, 'update-server-info'], undef, undef); - defined $pid or die "spawn update-server-info failed: $!\n"; - waitpid($pid, 0) == $pid or die 'update-server-info did not finish'; - $? == 0 or die "failed to update-server-info: $?\n"; + if ($nchg) { + $pid = spawn([@cmd, 'update-server-info'], undef, undef); + defined $pid or die "spawn update-server-info failed: $!\n"; + waitpid($pid, 0) == $pid or + die 'update-server-info did not finish'; + $? == 0 or die "failed to update-server-info: $?\n"; + + eval { + require PublicInbox::SearchIdx; + PublicInbox::SearchIdx->new($git_dir, 2)->index_sync; + }; + } my $lockfh = delete $self->{lockfh} or die "BUG: not locked: $!"; flock($lockfh, LOCK_UN) or die "unlock failed: $!";