1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # git fast-import-based ssoma-mda MDA replacement
5 # This is only ever run by public-inbox-mda and public-inbox-learn,
6 # not the WWW or NNTP code which only requires read-only access.
7 package PublicInbox::Import;
10 use Fcntl qw(:flock :DEFAULT);
12 use PublicInbox::Spawn qw(spawn);
13 use PublicInbox::MID qw(mid_mime mid2path);
16 my ($class, $git, $name, $email) = @_;
19 ident => "$name <$email>",
21 ref => 'refs/heads/master',
25 # idempotent start function
29 return ($self->{in}, $self->{out}) if $self->{pid};
31 my ($in_r, $in_w, $out_r, $out_w);
32 pipe($in_r, $in_w) or die "pipe failed: $!";
33 pipe($out_r, $out_w) or die "pipe failed: $!";
34 my $git = $self->{git};
35 my $git_dir = $git->{git_dir};
36 my $lockpath = "$git_dir/ssoma.lock";
37 sysopen(my $lockfh, $lockpath, O_WRONLY|O_CREAT) or
38 die "failed to open lock $lockpath: $!";
40 # wait for other processes to be done
41 flock($lockfh, LOCK_EX) or die "lock failed: $!\n";
42 chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $self->{ref}));
44 my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
45 --quiet --done --date-format=rfc2822));
46 my $rdr = { 0 => fileno($out_r), 1 => fileno($in_w) };
47 my $pid = spawn(\@cmd, undef, $rdr);
48 die "spawn failed: $!" unless defined $pid;
51 $self->{out} = $out_w;
52 $self->{lockfh} = $lockfh;
57 sub wfail () { die "write to fast-import failed: $!" }
61 my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[$t[6]];
62 my $mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$t[4]];
64 sprintf('%s, %2d %s %d %02d:%02d:%02d +0000',
65 $day, $t[3], $mon, $t[5] + 1900, $t[2], $t[1], $t[0]);
68 # returns undef on non-existent
69 # (-1, msg) on mismatch
70 # (:MARK, msg) on success
72 my ($self, $mime) = @_; # mime = Email::MIME
74 my $mid = mid_mime($mime);
75 my $path = mid2path($mid);
77 my ($r, $w) = $self->gfi_start;
78 my $tip = $self->{tip};
81 print $w "ls $tip $path\n" or wfail;
84 defined $check or die "EOF from fast-import / ls: $!";
85 return if $check =~ /\Amissing /;
86 $check =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $check";
88 print $w "cat-blob $blob\n" or wfail;
90 defined $check or die "EOF from fast-import / cat-blob: $!";
91 $check =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or
92 die "unexpected cat-blob response: $check";
98 $n = read($r, $buf, $left, $offset);
99 defined($n) or die "read cat-blob failed: $!";
100 $n == 0 and die 'fast-export (cat-blob) died';
104 $n = read($r, my $lf, 1);
105 defined($n) or die "read final byte of cat-blob failed: $!";
106 die "bad read on final byte: <$lf>" if $lf ne "\n";
107 my $cur = Email::MIME->new($buf);
108 if ($cur->header('Subject') ne $mime->header('Subject') ||
109 $cur->body ne $mime->body) {
113 my $ref = $self->{ref};
114 my $commit = $self->{mark}++;
115 my $parent = $tip =~ /\A:/ ? $tip : undef;
117 print $w "reset $ref\n" or wfail;
119 my $ident = $self->{ident};
121 print $w "commit $ref\nmark :$commit\n",
122 "author $ident $now\n",
123 "committer $ident $now\n",
125 'from ', ($parent ? $parent : $tip), "\n" or wfail;
126 print $w "D $path\n\n" or wfail;
127 (($self->{tip} = ":$commit"), $cur);
130 # returns undef on duplicate
132 my ($self, $mime) = @_; # mime = Email::MIME
134 my $from = $mime->header('From');
135 my @from = Email::Address->parse($from);
136 my $name = $from[0]->name;
137 my $email = $from[0]->address;
138 my $date = $mime->header('Date');
139 my $subject = $mime->header('Subject');
140 $subject = '(no subject)' unless defined $subject;
141 my $mid = mid_mime($mime);
142 my $path = mid2path($mid);
144 # git gets confused with:
145 # "'A U Thor <u@example.com>' via foo" <foo@example.com>
147 # <CAD0k6qSUYANxbjjbE4jTW4EeVwOYgBD=bXkSu=akiYC_CB7Ffw@mail.gmail.com>
148 $name =~ s/<([^>]+)>/($1)/g;
150 my ($r, $w) = $self->gfi_start;
151 my $tip = $self->{tip};
153 print $w "ls $tip $path\n" or wfail;
156 defined $check or die "EOF from fast-import: $!";
157 return unless $check =~ /\Amissing /;
160 # kill potentially confusing/misleading headers
161 $mime->header_set($_) for qw(bytes lines content-length status);
162 $mime = $mime->as_string;
163 my $blob = $self->{mark}++;
164 print $w "blob\nmark :$blob\ndata ", length($mime), "\n" or wfail;
165 print $w $mime, "\n" or wfail;
166 my $ref = $self->{ref};
167 my $commit = $self->{mark}++;
168 my $parent = $tip =~ /\A:/ ? $tip : undef;
171 print $w "reset $ref\n" or wfail;
174 # quiet down wide character warnings:
175 binmode $w, ':utf8' or die "binmode :utf8 failed: $!";
176 print $w "commit $ref\nmark :$commit\n",
177 "author $name <$email> $date\n",
178 "committer $self->{ident} ", now2822(), "\n",
179 "data ", (bytes::length($subject) + 1), "\n",
180 $subject, "\n\n" or wfail;
181 binmode $w, ':raw' or die "binmode :raw failed: $!";
184 print $w 'from ', ($parent ? $parent : $tip), "\n" or wfail;
186 print $w "M 100644 :$blob $path\n\n" or wfail;
187 $self->{tip} = ":$commit";
192 my $w = delete $self->{out} or return;
193 my $r = delete $self->{in} or die 'BUG: missing {in} when done';
194 print $w "done\n" or wfail;
195 my $pid = delete $self->{pid} or die 'BUG: missing {pid} when done';
196 waitpid($pid, 0) == $pid or die 'fast-import did not finish';
197 $? == 0 or die "fast-import failed: $?";
199 # for compatibility with existing ssoma installations
200 # we can probably remove this entirely by 2020
201 my $git_dir = $self->{git}->{git_dir};
202 my $index = "$git_dir/ssoma.index";
203 # XXX: change the following scope to: if (-e $index) # in 2018 or so..
204 unless ($ENV{FAST}) {
205 local $ENV{GIT_INDEX_FILE} = $index;
206 system('git', "--git-dir=$git_dir", qw(read-tree -m -v -i),
207 $self->{ref}) == 0 or
208 die "failed to update $git_dir/ssoma.index: $?\n";
212 my $lockfh = delete $self->{lockfh} or die "BUG: not locked: $!";
213 flock($lockfh, LOCK_UN) or die "unlock failed: $!";
214 close $lockfh or die "close lock failed: $!";