1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # This interface wraps and mimics PublicInbox::Import
5 # Used to write to V2 inboxes (see L<public-inbox-v2-format(5)>).
6 package PublicInbox::V2Writable;
9 use base qw(PublicInbox::Lock);
10 use PublicInbox::SearchIdxPart;
11 use PublicInbox::MIME;
13 use PublicInbox::Import;
14 use PublicInbox::MID qw(mids);
15 use PublicInbox::ContentId qw(content_id content_digest);
16 use PublicInbox::Inbox;
17 use PublicInbox::OverIdx;
18 use PublicInbox::Msgmap;
19 use PublicInbox::Spawn qw(spawn);
20 use PublicInbox::SearchIdx;
23 # an estimate of the post-packed size to the raw uncompressed size
24 my $PACKING_FACTOR = 0.4;
26 # assume 2 cores if GNU nproc(1) is not available
28 my $n = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
29 # subtract for the main process and git-fast-import
34 sub count_partitions ($) {
37 my $xpfx = $self->{xpfx};
39 # always load existing partitions in case core count changes:
40 # Also, partition count may change while -watch is running
43 foreach my $part (<$xpfx/*>) {
44 -d $part && $part =~ m!/\d+\z! or next;
46 Search::Xapian::Database->new($part)->close;
55 my ($class, $v2ibx, $creat) = @_;
56 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
60 File::Path::mkpath($dir);
62 die "$dir does not exist\n";
66 $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
67 $v2ibx->umask_prepare;
69 my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
72 im => undef, # PublicInbox::Import
76 over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
77 lock_path => "$dir/inbox.lock",
78 # limit each git repo (epoch) to 1GB or so
79 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
80 last_commit => [], # git repo -> commit
82 $self->{partitions} = count_partitions($self) || nproc_parts();
87 my ($self, $parallel, $skip) = @_;
88 $self->{parallel} = $parallel;
91 git_dir_latest($self, \$epoch_max);
92 if (defined $skip && $epoch_max == -1) {
95 $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
99 # returns undef on duplicate or spam
100 # mimics Import::add and wraps it for v2
102 my ($self, $mime, $check_cb) = @_;
103 $self->{-inbox}->with_umask(sub {
104 _add($self, $mime, $check_cb)
109 my ($self, $mime, $check_cb) = @_;
113 $mime = $check_cb->($mime) or return;
116 # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
117 # as does SQLite 3.4.1+ (released in 2007-07-20), and
118 # Xapian 1.3.2+ (released 2015-03-15).
119 # For the most part, we can spawn git-fast-import without
120 # leaking FDs to it...
124 my $num = num_for($self, $mime, \$mid0);
125 defined $num or return; # duplicate
126 defined $mid0 or die "BUG: $mid0 undefined\n";
127 my $im = $self->importer;
128 my $cmt = $im->add($mime);
129 $cmt = $im->get_mark($cmt);
130 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
132 my ($oid, $len, $msgref) = @{$im->{last_object}};
133 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
134 my $nparts = $self->{partitions};
135 my $part = $num % $nparts;
136 my $idx = $self->idx_part($part);
137 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
138 my $n = $self->{transact_bytes} += $len;
139 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
147 my ($self, $mime, $mid0) = @_;
148 my $mids = mids($mime->header_obj);
150 my $mid = $mids->[0];
151 my $num = $self->{mm}->mid_insert($mid);
152 if (defined $num) { # common case
157 # crap, Message-ID is already known, hope somebody just resent:
158 foreach my $m (@$mids) {
159 # read-only lookup now safe to do after above barrier
160 my $existing = $self->lookup_content($mime, $m);
161 # easy, don't store duplicates
162 # note: do not add more diagnostic info here since
163 # it gets noisy on public-inbox-watch restarts
167 # AltId may pre-populate article numbers (e.g. X-Mail-Count
168 # or NNTP article number), use that article number if it's
170 my $altid = $self->{-inbox}->{altid};
171 if ($altid && grep(/:file=msgmap\.sqlite3\z/, @$altid)) {
172 my $num = $self->{mm}->num_for($mid);
174 if (defined $num && !$self->{over}->get_art($num)) {
181 warn "<$mid> reused for mismatched content\n";
183 # try the rest of the mids
184 for(my $i = $#$mids; $i >= 1; $i--) {
186 $num = $self->{mm}->mid_insert($m);
188 warn "alternative <$m> for <$mid> found\n";
194 # none of the existing Message-IDs are good, generate a new one:
195 num_for_harder($self, $mime, $mid0);
199 my ($self, $mime, $mid0) = @_;
201 my $hdr = $mime->header_obj;
202 my $dig = content_digest($mime);
203 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
204 my $num = $self->{mm}->mid_insert($$mid0);
205 unless (defined $num) {
206 # it's hard to spoof the last Received: header
207 my @recvd = $hdr->header_raw('Received');
208 $dig->add("Received: $_") foreach (@recvd);
209 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
210 $num = $self->{mm}->mid_insert($$mid0);
212 # fall back to a random Message-ID and give up determinism:
213 until (defined($num)) {
215 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
216 warn "using random Message-ID <$$mid0> as fallback\n";
217 $num = $self->{mm}->mid_insert($$mid0);
220 PublicInbox::Import::append_mid($hdr, $$mid0);
225 my ($self, $part) = @_;
226 $self->{idx_parts}->[$part];
232 return if $self->{idx_parts};
233 my $ibx = $self->{-inbox};
235 # do not leak read-only FDs to child processes, we only have these
236 # FDs for duplicate detection so they should not be
237 # frequently activated.
238 delete $ibx->{$_} foreach (qw(git mm search));
240 my $indexlevel = $ibx->{indexlevel};
241 if ($indexlevel && $indexlevel eq 'basic') {
242 $self->{parallel} = 0;
245 if ($self->{parallel}) {
246 pipe(my ($r, $w)) or die "pipe failed: $!";
247 # pipe for barrier notifications doesn't need to be big,
249 fcntl($w, 1031, 4096) if $^O eq 'linux';
250 $self->{bnote} = [ $r, $w ];
254 my $over = $self->{over};
256 $ibx->with_umask(sub {
260 # -compact can change partition count while -watch is idle
261 my $nparts = count_partitions($self);
262 if ($nparts && $nparts != $self->{partitions}) {
263 $self->{partitions} = $nparts;
266 # need to create all parts before initializing msgmap FD
267 my $max = $self->{partitions} - 1;
269 # idx_parts must be visible to all forked processes
270 my $idx = $self->{idx_parts} = [];
271 for my $i (0..$max) {
272 push @$idx, PublicInbox::SearchIdxPart->new($self, $i);
275 # Now that all subprocesses are up, we can open the FDs
277 my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
278 "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
279 $mm->{dbh}->begin_work;
284 my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
286 my $pfx = "$self->{-inbox}->{mainrepo}/git";
288 foreach my $i (0..$self->{epoch_max}) {
289 my $git_dir = "$pfx/$i.git";
291 my $git = PublicInbox::Git->new($git_dir);
292 my $im = $self->import_init($git, 0, 1);
293 $purges->[$i] = $im->purge_oids($purge);
298 sub content_ids ($) {
300 my @cids = ( content_id($mime) );
302 # Email::MIME->as_string doesn't always round-trip, so we may
303 # use a second content_id
304 my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
305 push @cids, $rt if $cids[0] ne $rt;
309 sub content_matches ($$) {
310 my ($cids, $existing) = @_;
311 my $cid = content_id($existing);
313 return 1 if $_ eq $cid
318 sub remove_internal {
319 my ($self, $mime, $cmt_msg, $purge) = @_;
321 my $im = $self->importer unless $purge;
322 my $over = $self->{over};
323 my $cids = content_ids($mime);
324 my $parts = $self->{idx_parts};
325 my $mm = $self->{mm};
327 my $mids = mids($mime->header_obj);
329 # We avoid introducing new blobs into git since the raw content
330 # can be slightly different, so we do not need the user-supplied
331 # message now that we have the mids and content_id
335 foreach my $mid (@$mids) {
338 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
339 my $msg = get_blob($self, $smsg);
340 if (!defined($msg)) {
341 warn "broken smsg for $mid\n";
345 my $cur = PublicInbox::MIME->new($msg);
346 if (content_matches($cids, $cur)) {
347 $smsg->{mime} = $cur;
348 $gone{$smsg->{num}} = [ $smsg, \$orig ];
351 my $n = scalar keys %gone;
354 warn "BUG: multiple articles linked to <$mid>\n",
355 join(',', sort keys %gone), "\n";
357 foreach my $num (keys %gone) {
358 my ($smsg, $orig) = @{$gone{$num}};
359 $mm->num_delete($num);
360 # $removed should only be set once assuming
361 # no bugs in our deduplication code:
363 my $oid = $smsg->{blob};
367 ($mark, undef) = $im->remove($orig, $cmt_msg);
370 $self->unindex_oid_remote($oid, $mid);
375 my $cmt = $im->get_mark($mark);
376 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
378 if ($purge && scalar keys %$purge) {
379 return purge_oids($self, $purge);
385 my ($self, $mime, $cmt_msg) = @_;
386 $self->{-inbox}->with_umask(sub {
387 remove_internal($self, $mime, $cmt_msg);
392 my ($self, $mime) = @_;
393 my $purges = $self->{-inbox}->with_umask(sub {
394 remove_internal($self, $mime, undef, {});
396 $self->idx_init if @$purges; # ->done is called on purges
397 for my $i (0..$#$purges) {
398 defined(my $cmt = $purges->[$i]) or next;
399 $self->{last_commit}->[$i] = $cmt;
404 sub last_commit_part ($$;$) {
405 my ($self, $i, $cmt) = @_;
406 my $v = PublicInbox::Search::SCHEMA_VERSION();
407 $self->{mm}->last_commit_xap($v, $i, $cmt);
410 sub set_last_commits ($) {
412 defined(my $epoch_max = $self->{epoch_max}) or return;
413 my $last_commit = $self->{last_commit};
414 foreach my $i (0..$epoch_max) {
415 defined(my $cmt = $last_commit->[$i]) or next;
416 $last_commit->[$i] = undef;
417 last_commit_part($self, $i, $cmt);
423 $self->{bnote} or return;
425 my $barrier = { map { $_ => 1 } (0..$n) };
429 my ($self, $barrier) = @_;
430 my $bnote = $self->{bnote} or return;
432 while (scalar keys %$barrier) {
433 defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
434 $l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
435 delete $barrier->{$1} or die "bad part[$1] on barrier wait";
439 sub checkpoint ($;$) {
440 my ($self, $wait) = @_;
442 if (my $im = $self->{im}) {
449 my $parts = $self->{idx_parts};
451 my $dbh = $self->{mm}->{dbh};
453 # SQLite msgmap data is second in importance
456 # SQLite overview is third
457 $self->{over}->commit_lazy;
459 # Now deal with Xapian
461 my $barrier = $self->barrier_init(scalar @$parts);
463 # each partition needs to issue a barrier command
464 $_->remote_barrier for @$parts;
466 # wait for each Xapian partition
467 $self->barrier_wait($barrier);
469 $_->remote_commit for @$parts;
472 # last_commit is special, don't commit these until
473 # remote partitions are done:
475 set_last_commits($self);
480 $self->{transact_bytes} = 0;
483 # issue a write barrier to ensure all data is visible to other processes
484 # and read-only ops. Order of data importance is: git > SQLite > Xapian
485 sub barrier { checkpoint($_[0], 1) };
489 my $im = delete $self->{im};
490 $im->done if $im; # PublicInbox::Import::done
492 my $mm = delete $self->{mm};
493 $mm->{dbh}->commit if $mm;
494 my $parts = delete $self->{idx_parts};
496 $_->remote_close for @$parts;
498 $self->{over}->disconnect;
499 delete $self->{bnote};
500 $self->{transact_bytes} = 0;
501 $self->lock_release if $parts;
505 my ($self, $epoch) = @_;
506 my $pfx = "$self->{-inbox}->{mainrepo}/git";
507 my $git_dir = "$pfx/$epoch.git";
508 my @cmd = (qw(git init --bare -q), $git_dir);
509 PublicInbox::Import::run_die(\@cmd);
511 my $all = "$self->{-inbox}->{mainrepo}/all.git";
513 @cmd = (qw(git init --bare -q), $all);
514 PublicInbox::Import::run_die(\@cmd);
515 @cmd = (qw/git config/, "--file=$all/config",
516 'repack.writeBitmaps', 'true');
517 PublicInbox::Import::run_die(\@cmd);
520 @cmd = (qw/git config/, "--file=$git_dir/config",
521 'include.path', '../../all.git/config');
522 PublicInbox::Import::run_die(\@cmd);
524 my $alt = "$all/objects/info/alternates";
528 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
529 %alts = map { chomp; $_ => 1 } (<$fh>);
531 foreach my $i (0..$epoch) {
532 my $dir = "../../git/$i.git/objects";
533 push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
535 return $git_dir unless @add;
536 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
537 foreach my $dir (@add) {
538 print $fh "$dir\n" or die "print >> $alt: $!\n";
540 close $fh or die "close $alt: $!\n";
545 my ($self, $max) = @_;
547 my $pfx = "$self->{-inbox}->{mainrepo}/git";
548 return unless -d $pfx;
550 opendir my $dh, $pfx or die "opendir $pfx: $!\n";
551 while (defined(my $git_dir = readdir($dh))) {
552 $git_dir =~ m!\A(\d+)\.git\z! or next;
555 $latest = "$pfx/$git_dir";
563 my $im = $self->{im};
565 if ($im->{bytes_added} < $self->{rotate_bytes}) {
572 my $git_dir = $self->git_init(++$self->{epoch_max});
573 my $git = PublicInbox::Git->new($git_dir);
574 return $self->import_init($git, 0);
579 my $latest = git_dir_latest($self, \$max);
580 if (defined $latest) {
581 my $git = PublicInbox::Git->new($latest);
582 my $packed_bytes = $git->packed_bytes;
583 if ($packed_bytes >= $self->{rotate_bytes}) {
586 $self->{epoch_max} = $max;
587 return $self->import_init($git, $packed_bytes);
590 $self->{epoch_max} = $epoch;
591 $latest = $self->git_init($epoch);
592 $self->import_init(PublicInbox::Git->new($latest), 0);
596 my ($self, $git, $packed_bytes, $tmp) = @_;
597 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
598 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
599 $im->{want_object_info} = 1;
600 $im->{lock_path} = undef;
601 $im->{path_type} = 'v2';
602 $self->{im} = $im unless $tmp;
608 my ($mid, $cur, $new) = @_;
609 use File::Temp qw(tempfile);
611 my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
612 print $ah $cur->as_string or die "print: $!";
613 close $ah or die "close: $!";
614 my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
615 PublicInbox::Import::drop_unwanted_headers($new);
616 print $bh $new->as_string or die "print: $!";
617 close $bh or die "close: $!";
618 my $cmd = [ qw(diff -u), $an, $bn ];
619 print STDERR "# MID conflict <$mid>\n";
620 my $pid = spawn($cmd, undef, { 1 => 2 });
621 defined $pid or die "diff failed to spawn $!";
622 waitpid($pid, 0) == $pid or die "diff did not finish";
627 my ($self, $smsg) = @_;
628 if (my $im = $self->{im}) {
629 my $msg = $im->cat_blob($smsg->{blob});
632 # older message, should be in alternates
633 my $ibx = $self->{-inbox};
634 $ibx->msg_by_smsg($smsg);
638 my ($self, $mime, $mid) = @_;
639 my $over = $self->{over};
640 my $cids = content_ids($mime);
642 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
643 my $msg = get_blob($self, $smsg);
644 if (!defined($msg)) {
645 warn "broken smsg for $mid\n";
648 my $cur = PublicInbox::MIME->new($msg);
649 if (content_matches($cids, $cur)) {
650 $smsg->{mime} = $cur;
655 # XXX DEBUG_DIFF is experimental and may be removed
656 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
663 my $fh = delete $self->{reindex_pipe};
665 if (my $parts = $self->{idx_parts}) {
666 $_->atfork_child foreach @$parts;
668 if (my $im = $self->{im}) {
671 die "unexpected mm" if $self->{mm};
672 close $self->{bnote}->[0] or die "close bnote[0]: $!\n";
677 my ($self, $D, $git, $oid) = @_;
678 my $msgref = $git->cat_file($oid);
679 my $mime = PublicInbox::MIME->new($$msgref);
680 my $mids = mids($mime->header_obj);
681 my $cid = content_id($mime);
682 foreach my $mid (@$mids) {
683 $D->{"$mid\0$cid"} = $oid;
688 my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
690 my $msgref = $git->cat_file($oid, \$len);
691 my $mime = PublicInbox::MIME->new($$msgref);
692 my $mids = mids($mime->header_obj);
693 my $cid = content_id($mime);
695 # get the NNTP article number we used before, highest number wins
696 # and gets deleted from mm_tmp;
700 foreach my $mid (@$mids) {
701 $del += delete($D->{"$mid\0$cid"}) ? 1 : 0;
702 my $n = $mm_tmp->num_for($mid);
703 if (defined $n && $n > $num) {
706 $self->{mm}->mid_set($num, $mid0);
709 if (!defined($mid0) && $regen && !$del) {
711 die "BUG: ran out of article numbers\n" if $num <= 0;
712 my $mm = $self->{mm};
713 foreach my $mid (reverse @$mids) {
714 if ($mm->mid_set($num, $mid) == 1) {
719 if (!defined($mid0)) {
720 my $id = '<' . join('> <', @$mids) . '>';
721 warn "Message-ID $id unusable for $num\n";
722 foreach my $mid (@$mids) {
723 defined(my $n = $mm->num_for($mid)) or next;
724 warn "#$n previously mapped for <$mid>\n";
729 if (!defined($mid0) || $del) {
730 if (!defined($mid0) && $del) { # expected for deletes
732 $self->{mm}->num_highwater($num) unless $reindex;
736 my $id = '<' . join('> <', @$mids) . '>';
738 warn "Skipping $id, no article number found\n";
739 if ($del && defined($mid0)) {
740 warn "$id was deleted $del " .
741 "time(s) but mapped to article #$num\n";
746 $mm_tmp->mid_delete($mid0) or
747 die "failed to delete <$mid0> for article #$num\n";
749 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
750 my $nparts = $self->{partitions};
751 my $part = $num % $nparts;
752 my $idx = $self->idx_part($part);
753 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
754 my $n = $self->{transact_bytes} += $len;
755 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
757 $mm_tmp->atfork_prepare;
758 $self->done; # release lock
759 # allow -watch or -mda to write...
760 $self->idx_init; # reacquire lock
761 $mm_tmp->atfork_parent;
765 # only update last_commit for $i on reindex iff newer than current
766 sub update_last_commit {
767 my ($self, $git, $i, $cmt) = @_;
768 my $last = last_commit_part($self, $i);
769 if (defined $last && is_ancestor($git, $last, $cmt)) {
770 my @cmd = (qw(rev-list --count), "$last..$cmt");
771 chomp(my $n = $git->qx(@cmd));
772 return if $n ne '' && $n == 0;
774 last_commit_part($self, $i, $cmt);
777 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
780 my ($self, $epoch_max) = @_;
782 for (my $i = $epoch_max; $i >= 0; $i--) {
783 $heads->[$i] = last_commit_part($self, $i);
788 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
791 my ($self, $opts, $epoch_max, $ranges) = @_;
793 my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
794 for (my $i = $epoch_max; $i >= 0; $i--) {
795 die "already indexing!\n" if $self->{index_pipe};
796 my $git_dir = git_dir_n($self, $i);
797 -d $git_dir or next; # missing parts are fine
798 my $git = PublicInbox::Git->new($git_dir);
799 chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
800 next if $?; # new repo
802 if (defined(my $cur = $ranges->[$i])) {
803 $range = "$cur..$tip";
804 if (is_ancestor($git, $cur, $tip)) { # common case
805 my $n = $git->qx(qw(rev-list --count), $range);
808 $ranges->[$i] = undef;
813 discontiguous range: $range
814 Rewritten history? (in $git_dir)
816 my $base = $git->qx('merge-base', $tip, $cur);
819 $range = "$base..$tip";
820 warn "found merge-base: $base\n"
824 discarding history at $cur
828 reindexing $git_dir starting at
831 $self->{"unindex-range.$i"} = "$base..$cur";
834 $range = $tip; # all of it
836 $ranges->[$i] = $range;
838 # can't use 'rev-list --count' if we use --diff-filter
839 my $fh = $git->popen(qw(log --pretty=tformat:%H
840 --no-notes --no-color --no-renames
841 --diff-filter=AM), $range, '--', 'm');
842 ++$regen_max while <$fh>;
847 sub unindex_oid_remote {
848 my ($self, $oid, $mid) = @_;
849 $_->remote_remove($oid, $mid) foreach @{$self->{idx_parts}};
850 $self->{over}->remove_oid($oid, $mid);
854 my ($self, $git, $oid) = @_;
855 my $msgref = $git->cat_file($oid);
856 my $mime = PublicInbox::MIME->new($msgref);
857 my $mids = mids($mime->header_obj);
858 $mime = $msgref = undef;
859 my $over = $self->{over};
860 foreach my $mid (@$mids) {
863 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
864 $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
867 my $n = scalar keys %gone;
870 warn "BUG: multiple articles linked to $oid\n",
871 join(',',sort keys %gone), "\n";
873 foreach my $num (keys %gone) {
874 $self->{unindexed}->{$_}++;
875 $self->{mm}->num_delete($num);
877 $self->unindex_oid_remote($oid, $mid);
881 my $x40 = qr/[a-f0-9]{40}/;
883 my ($self, $opts, $git, $unindex_range) = @_;
884 my $un = $self->{unindexed} ||= {}; # num => removal count
885 my $before = scalar keys %$un;
886 my @cmd = qw(log --raw -r
887 --no-notes --no-color --no-abbrev --no-renames);
888 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
890 /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
891 $self->unindex_oid($git, $1);
893 delete $self->{reindex_pipe};
896 return unless $opts->{prune};
897 my $after = scalar keys %$un;
898 return if $before == $after;
900 # ensure any blob can not longer be accessed via dumb HTTP
901 PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
902 qw(-c gc.reflogExpire=now gc --prune=all)]);
906 my ($self, $opts) = @_;
909 my $latest = git_dir_latest($self, \$epoch_max);
910 return unless defined $latest;
911 $self->idx_init; # acquire lock
912 my $mm_tmp = $self->{mm}->tmp_clone;
913 my $reindex = $opts->{reindex};
914 my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
916 my $high = $self->{mm}->num_highwater();
917 my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
918 $$regen += $high if $high;
919 my $D = {}; # "$mid\0$cid" => $oid
920 my @cmd = qw(log --raw -r --pretty=tformat:%H
921 --no-notes --no-color --no-abbrev --no-renames);
923 # work backwards through history
924 my $last_commit = [];
925 for (my $i = $epoch_max; $i >= 0; $i--) {
926 my $git_dir = git_dir_n($self, $i);
927 die "already reindexing!\n" if delete $self->{reindex_pipe};
928 -d $git_dir or next; # missing parts are fine
929 my $git = PublicInbox::Git->new($git_dir);
930 my $unindex = delete $self->{"unindex-range.$i"};
931 $self->unindex($opts, $git, $unindex) if $unindex;
932 defined(my $range = $ranges->[$i]) or next;
933 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
936 if (/\A$x40$/o && !defined($cmt)) {
938 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
939 $self->reindex_oid($mm_tmp, $D, $git, $1,
941 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
942 $self->mark_deleted($D, $git, $1);
946 delete $self->{reindex_pipe};
947 $self->update_last_commit($git, $i, $cmt) if defined $cmt;
950 # unindex is required for leftovers if "deletes" affect messages
951 # in a previous fetch+index window:
952 if (scalar keys %$D) {
953 my $git = $self->{-inbox}->git;
954 $self->unindex_oid($git, $_) for values %$D;