]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Import.pm
import: extract_author_info becomes extract_commit_info
[public-inbox.git] / lib / PublicInbox / Import.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # git fast-import-based ssoma-mda MDA replacement
5 # This is only ever run by public-inbox-mda, public-inbox-learn
6 # and public-inbox-watch. Not the WWW or NNTP code which only
7 # requires read-only access.
8 package PublicInbox::Import;
9 use strict;
10 use warnings;
11 use base qw(PublicInbox::Lock);
12 use PublicInbox::Spawn qw(spawn);
13 use PublicInbox::MID qw(mids mid_mime mid2path);
14 use PublicInbox::Address;
15 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
16 use PublicInbox::ContentId qw(content_digest);
17 use PublicInbox::MDA;
18 use POSIX qw(strftime);
19
20 sub new {
21         # we can't change arg order, this is documented in POD
22         # and external projects may rely on it:
23         my ($class, $git, $name, $email, $ibx) = @_;
24         my $ref = 'refs/heads/master';
25         if ($ibx) {
26                 $ref = $ibx->{ref_head} || 'refs/heads/master';
27                 $name ||= $ibx->{name};
28                 $email ||= $ibx->{-primary_address};
29                 $git ||= $ibx->git;
30         }
31         bless {
32                 git => $git,
33                 ident => "$name <$email>",
34                 mark => 1,
35                 ref => $ref,
36                 -inbox => $ibx,
37                 path_type => '2/38', # or 'v2'
38                 lock_path => "$git->{git_dir}/ssoma.lock", # v2 changes this
39                 bytes_added => 0,
40         }, $class
41 }
42
43 # idempotent start function
44 sub gfi_start {
45         my ($self) = @_;
46
47         return ($self->{in}, $self->{out}) if $self->{pid};
48
49         my ($in_r, $in_w, $out_r, $out_w);
50         pipe($in_r, $in_w) or die "pipe failed: $!";
51         pipe($out_r, $out_w) or die "pipe failed: $!";
52         my $git = $self->{git};
53
54         $self->lock_acquire;
55
56         local $/ = "\n";
57         my $ref = $self->{ref};
58         chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $ref));
59         if ($self->{path_type} ne '2/38' && $self->{tip}) {
60                 local $/ = "\0";
61                 my @tree = $git->qx(qw(ls-tree -r -z --name-only), $ref);
62                 chomp @tree;
63                 $self->{-tree} = { map { $_ => 1 } @tree };
64         }
65
66         my $git_dir = $git->{git_dir};
67         my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
68                         --quiet --done --date-format=raw));
69         my $rdr = { 0 => fileno($out_r), 1 => fileno($in_w) };
70         my $pid = spawn(\@cmd, undef, $rdr);
71         die "spawn fast-import failed: $!" unless defined $pid;
72         $out_w->autoflush(1);
73         $self->{in} = $in_r;
74         $self->{out} = $out_w;
75         $self->{pid} = $pid;
76         $self->{nchg} = 0;
77         binmode $out_w, ':raw' or die "binmode :raw failed: $!";
78         binmode $in_r, ':raw' or die "binmode :raw failed: $!";
79         ($in_r, $out_w);
80 }
81
82 sub wfail () { die "write to fast-import failed: $!" }
83
84 sub now_raw () { time . ' +0000' }
85
86 sub norm_body ($) {
87         my ($mime) = @_;
88         my $b = $mime->body_raw;
89         $b =~ s/(\r?\n)+\z//s;
90         $b
91 }
92
93 # only used for v1 (ssoma) inboxes
94 sub _check_path ($$$$) {
95         my ($r, $w, $tip, $path) = @_;
96         return if $tip eq '';
97         print $w "ls $tip $path\n" or wfail;
98         local $/ = "\n";
99         defined(my $info = <$r>) or die "EOF from fast-import: $!";
100         $info =~ /\Amissing / ? undef : $info;
101 }
102
103 sub _cat_blob ($$$) {
104         my ($r, $w, $oid) = @_;
105         print $w "cat-blob $oid\n" or wfail;
106         local $/ = "\n";
107         my $info = <$r>;
108         defined $info or die "EOF from fast-import / cat-blob: $!";
109         $info =~ /\A[a-f0-9]{40} blob ([0-9]+)\n\z/ or return;
110         my $left = $1;
111         my $offset = 0;
112         my $buf = '';
113         my $n;
114         while ($left > 0) {
115                 $n = read($r, $buf, $left, $offset);
116                 defined($n) or die "read cat-blob failed: $!";
117                 $n == 0 and die 'fast-export (cat-blob) died';
118                 $left -= $n;
119                 $offset += $n;
120         }
121         $n = read($r, my $lf, 1);
122         defined($n) or die "read final byte of cat-blob failed: $!";
123         die "bad read on final byte: <$lf>" if $lf ne "\n";
124
125         # fixup some bugginess in old versions:
126         $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
127         \$buf;
128 }
129
130 sub cat_blob {
131         my ($self, $oid) = @_;
132         my ($r, $w) = $self->gfi_start;
133         _cat_blob($r, $w, $oid);
134 }
135
136 sub check_remove_v1 {
137         my ($r, $w, $tip, $path, $mime) = @_;
138
139         my $info = _check_path($r, $w, $tip, $path) or return ('MISSING',undef);
140         $info =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $info";
141         my $oid = $1;
142         my $msg = _cat_blob($r, $w, $oid) or die "BUG: cat-blob $1 failed";
143         my $cur = PublicInbox::MIME->new($msg);
144         my $cur_s = $cur->header('Subject');
145         $cur_s = '' unless defined $cur_s;
146         my $cur_m = $mime->header('Subject');
147         $cur_m = '' unless defined $cur_m;
148         if ($cur_s ne $cur_m || norm_body($cur) ne norm_body($mime)) {
149                 return ('MISMATCH', $cur);
150         }
151         (undef, $cur);
152 }
153
154 sub checkpoint {
155         my ($self) = @_;
156         return unless $self->{pid};
157         print { $self->{out} } "checkpoint\n" or wfail;
158         undef;
159 }
160
161 sub progress {
162         my ($self, $msg) = @_;
163         return unless $self->{pid};
164         print { $self->{out} } "progress $msg\n" or wfail;
165         $self->{in}->getline eq "progress $msg\n" or die
166                 "progress $msg not received\n";
167         undef;
168 }
169
170 sub _update_git_info ($$) {
171         my ($self, $do_gc) = @_;
172         # for compatibility with existing ssoma installations
173         # we can probably remove this entirely by 2020
174         my $git_dir = $self->{git}->{git_dir};
175         my @cmd = ('git', "--git-dir=$git_dir");
176         my $index = "$git_dir/ssoma.index";
177         if (-e $index && !$ENV{FAST}) {
178                 my $env = { GIT_INDEX_FILE => $index };
179                 run_die([@cmd, qw(read-tree -m -v -i), $self->{ref}], $env);
180         }
181         run_die([@cmd, 'update-server-info'], undef);
182         ($self->{path_type} eq '2/38') and eval {
183                 require PublicInbox::SearchIdx;
184                 my $ibx = $self->{-inbox} || $git_dir;
185                 my $s = PublicInbox::SearchIdx->new($ibx);
186                 $s->index_sync({ ref => $self->{ref} });
187         };
188         eval { run_die([@cmd, qw(gc --auto)], undef) } if $do_gc;
189 }
190
191 sub barrier {
192         my ($self) = @_;
193
194         # For safety, we ensure git checkpoint is complete before because
195         # the data in git is still more important than what is in Xapian
196         # in v2.  Performance may be gained by delaying the ->progress
197         # call but we lose safety
198         if ($self->{nchg}) {
199                 $self->checkpoint;
200                 $self->progress('checkpoint');
201                 _update_git_info($self, 0);
202                 $self->{nchg} = 0;
203         }
204 }
205
206 # used for v2
207 sub get_mark {
208         my ($self, $mark) = @_;
209         die "not active\n" unless $self->{pid};
210         my ($r, $w) = $self->gfi_start;
211         print $w "get-mark $mark\n" or wfail;
212         defined(my $oid = <$r>) or die "get-mark failed, need git 2.6.0+\n";
213         chomp($oid);
214         $oid;
215 }
216
217 # returns undef on non-existent
218 # ('MISMATCH', Email::MIME) on mismatch
219 # (:MARK, Email::MIME) on success
220 #
221 # v2 callers should check with Xapian before calling this as
222 # it is not idempotent.
223 sub remove {
224         my ($self, $mime, $msg) = @_; # mime = Email::MIME
225
226         my $path_type = $self->{path_type};
227         my ($path, $err, $cur, $blob);
228
229         my ($r, $w) = $self->gfi_start;
230         my $tip = $self->{tip};
231         if ($path_type eq '2/38') {
232                 $path = mid2path(v1_mid0($mime));
233                 ($err, $cur) = check_remove_v1($r, $w, $tip, $path, $mime);
234                 return ($err, $cur) if $err;
235         } else {
236                 my $sref;
237                 if (ref($mime) eq 'SCALAR') { # optimization used by V2Writable
238                         $sref = $mime;
239                 } else { # XXX should not be necessary:
240                         my $str = $mime->as_string;
241                         $sref = \$str;
242                 }
243                 my $len = length($$sref);
244                 $blob = $self->{mark}++;
245                 print $w "blob\nmark :$blob\ndata $len\n",
246                         $$sref, "\n" or wfail;
247         }
248
249         my $ref = $self->{ref};
250         my $commit = $self->{mark}++;
251         my $parent = $tip =~ /\A:/ ? $tip : undef;
252         unless ($parent) {
253                 print $w "reset $ref\n" or wfail;
254         }
255         my $ident = $self->{ident};
256         my $now = now_raw();
257         $msg ||= 'rm';
258         my $len = length($msg) + 1;
259         print $w "commit $ref\nmark :$commit\n",
260                 "author $ident $now\n",
261                 "committer $ident $now\n",
262                 "data $len\n$msg\n\n",
263                 'from ', ($parent ? $parent : $tip), "\n" or wfail;
264         if (defined $path) {
265                 print $w "D $path\n\n" or wfail;
266         } else {
267                 clean_tree_v2($self, $w, 'd');
268                 print $w "M 100644 :$blob d\n\n" or wfail;
269         }
270         $self->{nchg}++;
271         (($self->{tip} = ":$commit"), $cur);
272 }
273
274 sub git_timestamp {
275         my ($ts, $zone) = @_;
276         $ts = 0 if $ts < 0; # git uses unsigned times
277         "$ts $zone";
278 }
279
280 sub extract_cmt_info ($) {
281         my ($mime) = @_;
282
283         my $sender = '';
284         my $from = $mime->header('From');
285         $from ||= '';
286         my ($email) = PublicInbox::Address::emails($from);
287         my ($name) = PublicInbox::Address::names($from);
288         if (!defined($name) || !defined($email)) {
289                 $sender = $mime->header('Sender');
290                 $sender ||= '';
291                 if (!defined($name)) {
292                         ($name) = PublicInbox::Address::names($sender);
293                 }
294                 if (!defined($email)) {
295                         ($email) = PublicInbox::Address::emails($sender);
296                 }
297         }
298         if (defined $email) {
299                 # quiet down wide character warnings with utf8::encode
300                 utf8::encode($email);
301         } else {
302                 $email = '';
303                 warn "no email in From: $from or Sender: $sender\n";
304         }
305
306         # git gets confused with:
307         #  "'A U Thor <u@example.com>' via foo" <foo@example.com>
308         # ref:
309         # <CAD0k6qSUYANxbjjbE4jTW4EeVwOYgBD=bXkSu=akiYC_CB7Ffw@mail.gmail.com>
310         if (defined $name) {
311                 $name =~ tr/<>//d;
312                 utf8::encode($name);
313         } else {
314                 $name = '';
315                 warn "no name in From: $from or Sender: $sender\n";
316         }
317
318         my $hdr = $mime->header_obj;
319
320         my $subject = $hdr->header('Subject');
321         $subject = '(no subject)' unless defined $subject;
322         # Mime decoding can create nulls replace them with spaces to protect git
323         $subject =~ tr/\0/ /;
324         utf8::encode($subject);
325         my $at = git_timestamp(my @at = msg_datestamp($hdr));
326         my $ct = git_timestamp(my @ct = msg_timestamp($hdr));
327         ($name, $email, $at, $ct, $subject);
328 }
329
330 # kill potentially confusing/misleading headers
331 sub drop_unwanted_headers ($) {
332         my ($mime) = @_;
333
334         $mime->header_set($_) for qw(bytes lines content-length status);
335         $mime->header_set($_) for @PublicInbox::MDA::BAD_HEADERS;
336 }
337
338 # used by V2Writable, too
339 sub append_mid ($$) {
340         my ($hdr, $mid0) = @_;
341         # @cur is likely empty if we need to call this sub, but it could
342         # have random unparseable crap which we'll preserve, too.
343         my @cur = $hdr->header_raw('Message-ID');
344         $hdr->header_set('Message-ID', @cur, "<$mid0>");
345 }
346
347 sub v1_mid0 ($) {
348         my ($mime) = @_;
349         my $hdr = $mime->header_obj;
350         my $mids = mids($hdr);
351
352         if (!scalar(@$mids)) { # spam often has no Message-Id
353                 my $mid0 = digest2mid(content_digest($mime), $hdr);
354                 append_mid($hdr, $mid0);
355                 return $mid0;
356         }
357         $mids->[0];
358 }
359 sub clean_tree_v2 ($$$) {
360         my ($self, $w, $keep) = @_;
361         my $tree = $self->{-tree} or return; #v2 only
362         delete $tree->{$keep};
363         foreach (keys %$tree) {
364                 print $w "D $_\n" or wfail;
365         }
366         %$tree = ($keep => 1);
367 }
368
369 # returns undef on duplicate
370 # returns the :MARK of the most recent commit
371 sub add {
372         my ($self, $mime, $check_cb) = @_; # mime = Email::MIME
373
374         my ($name, $email, $at, $ct, $subject) = extract_cmt_info($mime);
375         my $path_type = $self->{path_type};
376         my $path;
377         if ($path_type eq '2/38') {
378                 $path = mid2path(v1_mid0($mime));
379         } else { # v2 layout, one file:
380                 $path = 'm';
381         }
382
383         my ($r, $w) = $self->gfi_start;
384         my $tip = $self->{tip};
385         if ($path_type eq '2/38') {
386                 _check_path($r, $w, $tip, $path) and return;
387         }
388
389         drop_unwanted_headers($mime);
390
391         # spam check:
392         if ($check_cb) {
393                 $mime = $check_cb->($mime) or return;
394         }
395
396         my $blob = $self->{mark}++;
397         my $str = $mime->as_string;
398         my $n = length($str);
399         $self->{bytes_added} += $n;
400         print $w "blob\nmark :$blob\ndata ", $n, "\n" or wfail;
401         print $w $str, "\n" or wfail;
402
403         # v2: we need this for Xapian
404         if ($self->{want_object_info}) {
405                 my $oid = $self->get_mark(":$blob");
406                 $self->{last_object} = [ $oid, $n, \$str ];
407         }
408         my $ref = $self->{ref};
409         my $commit = $self->{mark}++;
410         my $parent = $tip =~ /\A:/ ? $tip : undef;
411
412         unless ($parent) {
413                 print $w "reset $ref\n" or wfail;
414         }
415
416         print $w "commit $ref\nmark :$commit\n",
417                 "author $name <$email> $at\n",
418                 "committer $self->{ident} $ct\n" or wfail;
419         print $w "data ", (length($subject) + 1), "\n",
420                 $subject, "\n\n" or wfail;
421         if ($tip ne '') {
422                 print $w 'from ', ($parent ? $parent : $tip), "\n" or wfail;
423         }
424         clean_tree_v2($self, $w, $path);
425         print $w "M 100644 :$blob $path\n\n" or wfail;
426         $self->{nchg}++;
427         $self->{tip} = ":$commit";
428 }
429
430 sub run_die ($;$$) {
431         my ($cmd, $env, $rdr) = @_;
432         my $pid = spawn($cmd, $env, $rdr);
433         defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
434         waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
435         $? == 0 or die join(' ', @$cmd) . " failed: $?\n";
436 }
437
438 sub init_bare {
439         my ($dir) = @_;
440         my @cmd = (qw(git init --bare -q), $dir);
441         run_die(\@cmd);
442         # set a reasonable default:
443         @cmd = (qw/git config/, "--file=$dir/config",
444                 'repack.writeBitmaps', 'true');
445         run_die(\@cmd);
446 }
447
448 sub done {
449         my ($self) = @_;
450         my $w = delete $self->{out} or return;
451         my $r = delete $self->{in} or die 'BUG: missing {in} when done';
452         print $w "done\n" or wfail;
453         my $pid = delete $self->{pid} or die 'BUG: missing {pid} when done';
454         waitpid($pid, 0) == $pid or die 'fast-import did not finish';
455         $? == 0 or die "fast-import failed: $?";
456
457         _update_git_info($self, 1) if delete $self->{nchg};
458
459         $self->lock_release;
460
461         $self->{git}->cleanup;
462 }
463
464 sub atfork_child {
465         my ($self) = @_;
466         foreach my $f (qw(in out)) {
467                 next unless defined($self->{$f});
468                 close $self->{$f} or die "failed to close import[$f]: $!\n";
469         }
470 }
471
472 sub digest2mid ($$) {
473         my ($dig, $hdr) = @_;
474         my $b64 = $dig->clone->b64digest;
475         # Make our own URLs nicer:
476         # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648
477         $b64 =~ tr!+/=!-_!d;
478
479         # Add a date prefix to prevent a leading '-' in case that trips
480         # up some tools (e.g. if a Message-ID were a expected as a
481         # command-line arg)
482         my $dt = msg_datestamp($hdr);
483         $dt = POSIX::strftime('%Y%m%d%H%M%S', gmtime($dt));
484         "$dt.$b64" . '@z';
485 }
486
487 sub clean_purge_buffer {
488         my ($oids, $buf) = @_;
489         my $cmt_msg = 'purged '.join(' ',@$oids)."\n";
490         @$oids = ();
491
492         foreach my $i (0..$#$buf) {
493                 my $l = $buf->[$i];
494                 if ($l =~ /^author .* ([0-9]+ [\+-]?[0-9]+)$/) {
495                         $buf->[$i] = "author <> $1\n";
496                 } elsif ($l =~ /^data ([0-9]+)/) {
497                         $buf->[$i++] = "data " . length($cmt_msg) . "\n";
498                         $buf->[$i] = $cmt_msg;
499                         last;
500                 }
501         }
502 }
503
504 sub purge_oids {
505         my ($self, $purge) = @_;
506         my $tmp = "refs/heads/purge-".((keys %$purge)[0]);
507         my $old = $self->{'ref'};
508         my $git = $self->{git};
509         my @export = (qw(fast-export --no-data --use-done-feature), $old);
510         my $rd = $git->popen(@export);
511         my ($r, $w) = $self->gfi_start;
512         my @buf;
513         my $npurge = 0;
514         my @oids;
515         my ($done, $mark);
516         my $tree = $self->{-tree};
517         while (<$rd>) {
518                 if (/^reset (?:.+)/) {
519                         push @buf, "reset $tmp\n";
520                 } elsif (/^commit (?:.+)/) {
521                         if (@buf) {
522                                 $w->print(@buf) or wfail;
523                                 @buf = ();
524                         }
525                         push @buf, "commit $tmp\n";
526                 } elsif (/^data ([0-9]+)/) {
527                         # only commit message, so $len is small:
528                         my $len = $1; # + 1 for trailing "\n"
529                         push @buf, $_;
530                         my $n = read($rd, my $buf, $len) or die "read: $!";
531                         $len == $n or die "short read ($n < $len)";
532                         push @buf, $buf;
533                 } elsif (/^M 100644 ([a-f0-9]+) (\w+)/) {
534                         my ($oid, $path) = ($1, $2);
535                         $tree->{$path} = 1;
536                         if ($purge->{$oid}) {
537                                 push @oids, $oid;
538                                 my $cmd = "M 100644 inline $path\ndata 0\n\n";
539                                 push @buf, $cmd;
540                         } else {
541                                 push @buf, $_;
542                         }
543                 } elsif (/^D (\w+)/) {
544                         my $path = $1;
545                         push @buf, $_ if $tree->{$path};
546                 } elsif ($_ eq "\n") {
547                         if (@oids) {
548                                 my $out = join('', @buf);
549                                 $out =~ s/^/# /sgm;
550                                 warn "purge rewriting\n", $out, "\n";
551                                 clean_purge_buffer(\@oids, \@buf);
552                                 $npurge++;
553                         }
554                         $w->print(@buf, "\n") or wfail;
555                         @buf = ();
556                 } elsif ($_ eq "done\n") {
557                         $done = 1;
558                 } elsif (/^mark :([0-9]+)$/) {
559                         push @buf, $_;
560                         $mark = $1;
561                 } else {
562                         push @buf, $_;
563                 }
564         }
565         close $rd or die "close fast-export failed: $?";
566         if (@buf) {
567                 $w->print(@buf) or wfail;
568         }
569         die 'done\n not seen from fast-export' unless $done;
570         chomp(my $cmt = $self->get_mark(":$mark")) if $npurge;
571         $self->{nchg} = 0; # prevent _update_git_info until update-ref:
572         $self->done;
573         my @git = ('git', "--git-dir=$git->{git_dir}");
574
575         run_die([@git, qw(update-ref), $old, $tmp]) if $npurge;
576
577         run_die([@git, qw(update-ref -d), $tmp]);
578
579         return if $npurge == 0;
580
581         run_die([@git, qw(-c gc.reflogExpire=now gc --prune=all)]);
582         my $err = 0;
583         foreach my $oid (keys %$purge) {
584                 my @info = $git->check($oid);
585                 if (@info) {
586                         warn "$oid not purged\n";
587                         $err++;
588                 }
589         }
590         _update_git_info($self, 0);
591         die "Failed to purge $err object(s)\n" if $err;
592         $cmt;
593 }
594
595 1;
596 __END__
597 =pod
598
599 =head1 NAME
600
601 PublicInbox::Import - message importer for public-inbox v1 inboxes
602
603 =head1 VERSION
604
605 version 1.0
606
607 =head1 SYNOPSYS
608
609         use Email::MIME;
610         use PublicInbox::Git;
611         use PublicInbox::Import;
612
613         chomp(my $git_dir = `git rev-parse --git-dir`);
614         $git_dir or die "GIT_DIR= must be specified\n";
615         my $git = PublicInbox::Git->new($git_dir);
616         my @committer = ('inbox', 'inbox@example.org');
617         my $im = PublicInbox::Import->new($git, @committer);
618
619         # to add a message:
620         my $message = "From: <u\@example.org>\n".
621                 "Subject: test message \n" .
622                 "Date: Thu, 01 Jan 1970 00:00:00 +0000\n" .
623                 "Message-ID: <m\@example.org>\n".
624                 "\ntest message";
625         my $parsed = Email::MIME->new($message);
626         my $ret = $im->add($parsed);
627         if (!defined $ret) {
628                 warn "duplicate: ",
629                         $parsed->header_obj->header_raw('Message-ID'), "\n";
630         } else {
631                 print "imported at mark $ret\n";
632         }
633         $im->done;
634
635         # to remove a message
636         my $junk = Email::MIME->new($message);
637         my ($mark, $orig) = $im->remove($junk);
638         if ($mark eq 'MISSING') {
639                 print "not found\n";
640         } elsif ($mark eq 'MISMATCH') {
641                 print "Message exists but does not match\n\n",
642                         $orig->as_string, "\n",;
643         } else {
644                 print "removed at mark $mark\n\n",
645                         $orig->as_string, "\n";
646         }
647         $im->done;
648
649 =head1 DESCRIPTION
650
651 An importer and remover for public-inboxes which takes L<Email::MIME>
652 messages as input and stores them in a git repository as
653 documented in L<https://public-inbox.org/public-inbox-v1-format.txt>,
654 except it does not allow duplicate Message-IDs.
655
656 It requires L<git(1)> and L<git-fast-import(1)> to be installed.
657
658 =head1 METHODS
659
660 =cut
661
662 =head2 new
663
664         my $im = PublicInbox::Import->new($git, @committer);
665
666 Initialize a new PublicInbox::Import object.
667
668 =head2 add
669
670         my $parsed = Email::MIME->new($message);
671         $im->add($parsed);
672
673 Adds a message to to the git repository.  This will acquire
674 C<$GIT_DIR/ssoma.lock> and start L<git-fast-import(1)> if necessary.
675
676 Messages added will not be visible to other processes until L</done>
677 is called, but L</remove> may be called on them.
678
679 =head2 remove
680
681         my $junk = Email::MIME->new($message);
682         my ($code, $orig) = $im->remove($junk);
683
684 Removes a message from the repository.  On success, it returns
685 a ':'-prefixed numeric code representing the git-fast-import
686 mark and the original messages as an Email::MIME object.
687 If the message could not be found, the code is "MISSING"
688 and the original message is undef.  If there is a mismatch where
689 the "Message-ID" is matched but the subject and body do not match,
690 the returned code is "MISMATCH" and the conflicting message
691 is returned as orig.
692
693 =head2 done
694
695 Finalizes the L<git-fast-import(1)> and unlocks the repository.
696 Calling this is required to finalize changes to a repository.
697
698 =head1 SEE ALSO
699
700 L<Email::MIME>
701
702 =head1 CONTACT
703
704 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
705
706 The mail archives are hosted at L<https://public-inbox.org/meta/>
707
708 =head1 COPYRIGHT
709
710 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
711
712 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>
713
714 =cut