# requires read-only access.
package PublicInbox::Import;
use strict;
-use warnings;
-use base qw(PublicInbox::Lock);
+use parent qw(PublicInbox::Lock);
+use v5.10.1;
use PublicInbox::Spawn qw(spawn popen_rd);
use PublicInbox::MID qw(mids mid2path);
use PublicInbox::Address;
-use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
+use PublicInbox::Smsg;
+use PublicInbox::MsgTime qw(msg_datestamp);
use PublicInbox::ContentHash qw(content_digest);
use PublicInbox::MDA;
use PublicInbox::Eml;
my ($class, $git, $name, $email, $ibx) = @_;
my $ref = 'refs/heads/master';
if ($ibx) {
- $ref = $ibx->{ref_head} || 'refs/heads/master';
- $name ||= $ibx->{name};
- $email ||= $ibx->{-primary_address};
- $git ||= $ibx->git;
+ $ref = $ibx->{ref_head} // 'refs/heads/master';
+ $name //= $ibx->{name};
+ $email //= $ibx->{-primary_address};
+ $git //= $ibx->git;
}
bless {
git => $git,
}
my $ident = $self->{ident};
my $now = now_raw();
- $msg ||= 'rm';
+ $msg //= 'rm';
my $len = length($msg) + 1;
print $w "commit $ref\nmark :$commit\n",
"author $ident $now\n",
(($self->{tip} = ":$commit"), $cur);
}
-sub git_timestamp {
- my ($ts, $zone) = @_;
+sub git_timestamp ($) {
+ my ($ts, $zone) = @{$_[0]};
$ts = 0 if $ts < 0; # git uses unsigned times
"$ts $zone";
}
sub extract_cmt_info ($;$) {
my ($mime, $smsg) = @_;
+ # $mime is PublicInbox::Eml, but remains Email::MIME-compatible
+ $smsg //= bless {}, 'PublicInbox::Smsg';
+
+ my $hdr = $mime->header_obj;
+ $smsg->populate($hdr);
my $sender = '';
- my $from = $mime->header('From');
- $from ||= '';
+ my $from = delete($smsg->{From}) // '';
my ($email) = PublicInbox::Address::emails($from);
my ($name) = PublicInbox::Address::names($from);
if (!defined($name) || !defined($email)) {
- $sender = $mime->header('Sender');
- $sender ||= '';
- if (!defined($name)) {
- ($name) = PublicInbox::Address::names($sender);
- }
- if (!defined($email)) {
- ($email) = PublicInbox::Address::emails($sender);
- }
+ $sender = $hdr->header('Sender') // '';
+ $name //= (PublicInbox::Address::names($sender))[0];
+ $email //= (PublicInbox::Address::emails($sender))[0];
}
if (defined $email) {
# Email::Address::XS may leave quoted '<' in addresses,
warn "no name in From: $from or Sender: $sender\n";
}
- my $hdr = $mime->header_obj;
-
- my $subject = $hdr->header('Subject');
- $subject = '(no subject)' unless defined $subject;
- # Mime decoding can create nulls replace them with spaces to protect git
- $subject =~ tr/\0/ /;
+ my $subject = delete($smsg->{Subject}) // '(no subject)';
utf8::encode($subject);
- my $at = git_timestamp(my @at = msg_datestamp($hdr));
- my $ct = git_timestamp(my @ct = msg_timestamp($hdr));
- if ($smsg) {
- $smsg->{ds} = $at[0];
- $smsg->{ts} = $ct[0];
- }
- ($name, $email, $at, $ct, $subject);
+ my $at = git_timestamp(delete $smsg->{-ds});
+ my $ct = git_timestamp(delete $smsg->{-ts});
+ ("$name <$email>", $at, $ct, $subject);
}
# kill potentially confusing/misleading headers
sub add {
my ($self, $mime, $check_cb, $smsg) = @_;
- my ($name, $email, $at, $ct, $subject) = extract_cmt_info($mime, $smsg);
+ my ($author, $at, $ct, $subject) = extract_cmt_info($mime, $smsg);
my $path_type = $self->{path_type};
my $path;
if ($path_type eq '2/38') {
# v2: we need this for Xapian
if ($smsg) {
$smsg->{blob} = $self->get_mark(":$blob");
- $smsg->{bytes} = $n;
+ $smsg->{raw_bytes} = $n;
$smsg->{-raw_email} = \$raw_email;
}
my $ref = $self->{ref};
}
print $w "commit $ref\nmark :$commit\n",
- "author $name <$email> $at\n",
+ "author $author $at\n",
"committer $self->{ident} $ct\n" or wfail;
print $w "data ", (length($subject) + 1), "\n",
$subject, "\n\n" or wfail;
waitpid($pid, 0) == $pid or die 'fast-import did not finish';
$? == 0 or die "fast-import failed: $?";
- _update_git_info($self, 1) if delete $self->{nchg};
-
- $self->lock_release;
+ my $nchg = delete $self->{nchg};
+ _update_git_info($self, 1) if $nchg;
+ $self->lock_release(!!$nchg);
$self->{git}->cleanup;
}
sub rewrite_commit ($$$$) {
my ($self, $oids, $buf, $mime) = @_;
- my ($name, $email, $at, $ct, $subject);
+ my ($author, $at, $ct, $subject);
if ($mime) {
- ($name, $email, $at, $ct, $subject) = extract_cmt_info($mime);
+ ($author, $at, $ct, $subject) = extract_cmt_info($mime);
} else {
- $name = $email = '';
+ $author = '<>';
$subject = 'purged '.join(' ', @$oids);
}
@$oids = ();
my $l = $buf->[$i];
if ($l =~ /^author .* ([0-9]+ [\+-]?[0-9]+)$/) {
$at //= $1;
- $buf->[$i] = "author $name <$email> $at\n";
+ $buf->[$i] = "author $author $at\n";
} elsif ($l =~ /^committer .* ([0-9]+ [\+-]?[0-9]+)$/) {
$ct //= $1;
$buf->[$i] = "committer $self->{ident} $ct\n";