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 package PublicInbox::V2Writable;
8 use base qw(PublicInbox::Lock);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::MIME;
12 use PublicInbox::Import;
13 use PublicInbox::MID qw(mids);
14 use PublicInbox::ContentId qw(content_id content_digest);
15 use PublicInbox::Inbox;
16 use PublicInbox::OverIdx;
17 use PublicInbox::Msgmap;
18 use PublicInbox::Spawn qw(spawn);
19 use PublicInbox::SearchIdx;
22 # an estimate of the post-packed size to the raw uncompressed size
23 my $PACKING_FACTOR = 0.4;
25 # assume 2 cores if GNU nproc(1) is not available
27 my $n = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
28 # subtract for the main process and git-fast-import
33 sub count_partitions ($) {
36 my $xpfx = $self->{xpfx};
38 # always load existing partitions in case core count changes:
39 # Also, partition count may change while -watch is running
42 foreach my $part (<$xpfx/*>) {
43 -d $part && $part =~ m!/\d+\z! or next;
45 Search::Xapian::Database->new($part)->close;
54 my ($class, $v2ibx, $creat) = @_;
55 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
59 File::Path::mkpath($dir);
61 die "$dir does not exist\n";
65 $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
66 $v2ibx->umask_prepare;
68 my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
71 im => undef, # PublicInbox::Import
75 over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
76 lock_path => "$dir/inbox.lock",
77 # limit each git repo (epoch) to 1GB or so
78 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
79 last_commit => [], # git repo -> commit
81 $self->{partitions} = count_partitions($self) || nproc_parts();
86 my ($self, $parallel, $skip) = @_;
87 $self->{parallel} = $parallel;
90 git_dir_latest($self, \$epoch_max);
91 if (defined $skip && $epoch_max == -1) {
94 $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
98 # returns undef on duplicate or spam
99 # mimics Import::add and wraps it for v2
101 my ($self, $mime, $check_cb) = @_;
102 $self->{-inbox}->with_umask(sub {
103 _add($self, $mime, $check_cb)
108 my ($self, $mime, $check_cb) = @_;
112 $mime = $check_cb->($mime) or return;
115 # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
116 # as does SQLite 3.4.1+ (released in 2007-07-20), and
117 # Xapian 1.3.2+ (released 2015-03-15).
118 # For the most part, we can spawn git-fast-import without
119 # leaking FDs to it...
123 my $num = num_for($self, $mime, \$mid0);
124 defined $num or return; # duplicate
125 defined $mid0 or die "BUG: $mid0 undefined\n";
126 my $im = $self->importer;
127 my $cmt = $im->add($mime);
128 $cmt = $im->get_mark($cmt);
129 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
131 my ($oid, $len, $msgref) = @{$im->{last_object}};
132 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
133 my $nparts = $self->{partitions};
134 my $part = $num % $nparts;
135 my $idx = $self->idx_part($part);
136 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
137 my $n = $self->{transact_bytes} += $len;
138 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
146 my ($self, $mime, $mid0) = @_;
147 my $mids = mids($mime->header_obj);
149 my $mid = $mids->[0];
150 my $num = $self->{mm}->mid_insert($mid);
151 if (defined $num) { # common case
156 # crap, Message-ID is already known, hope somebody just resent:
157 foreach my $m (@$mids) {
158 # read-only lookup now safe to do after above barrier
159 my $existing = $self->lookup_content($mime, $m);
160 # easy, don't store duplicates
161 # note: do not add more diagnostic info here since
162 # it gets noisy on public-inbox-watch restarts
166 # AltId may pre-populate article numbers (e.g. X-Mail-Count
167 # or NNTP article number), use that article number if it's
169 my $altid = $self->{-inbox}->{altid};
170 if ($altid && grep(/:file=msgmap\.sqlite3\z/, @$altid)) {
171 my $num = $self->{mm}->num_for($mid);
173 if (defined $num && !$self->{over}->get_art($num)) {
180 warn "<$mid> reused for mismatched content\n";
182 # try the rest of the mids
183 for(my $i = $#$mids; $i >= 1; $i--) {
185 $num = $self->{mm}->mid_insert($m);
187 warn "alternative <$m> for <$mid> found\n";
193 # none of the existing Message-IDs are good, generate a new one:
194 num_for_harder($self, $mime, $mid0);
198 my ($self, $mime, $mid0) = @_;
200 my $hdr = $mime->header_obj;
201 my $dig = content_digest($mime);
202 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
203 my $num = $self->{mm}->mid_insert($$mid0);
204 unless (defined $num) {
205 # it's hard to spoof the last Received: header
206 my @recvd = $hdr->header_raw('Received');
207 $dig->add("Received: $_") foreach (@recvd);
208 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
209 $num = $self->{mm}->mid_insert($$mid0);
211 # fall back to a random Message-ID and give up determinism:
212 until (defined($num)) {
214 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
215 warn "using random Message-ID <$$mid0> as fallback\n";
216 $num = $self->{mm}->mid_insert($$mid0);
219 PublicInbox::Import::append_mid($hdr, $$mid0);
224 my ($self, $part) = @_;
225 $self->{idx_parts}->[$part];
231 return if $self->{idx_parts};
232 my $ibx = $self->{-inbox};
234 # do not leak read-only FDs to child processes, we only have these
235 # FDs for duplicate detection so they should not be
236 # frequently activated.
237 delete $ibx->{$_} foreach (qw(git mm search));
239 my $indexlevel = $ibx->{indexlevel};
240 if ($indexlevel && $indexlevel eq 'basic') {
241 $self->{parallel} = 0;
244 if ($self->{parallel}) {
245 pipe(my ($r, $w)) or die "pipe failed: $!";
246 $self->{bnote} = [ $r, $w ];
250 my $over = $self->{over};
252 $ibx->with_umask(sub {
256 # -compact can change partition count while -watch is idle
257 my $nparts = count_partitions($self);
258 if ($nparts && $nparts != $self->{partitions}) {
259 $self->{partitions} = $nparts;
262 # need to create all parts before initializing msgmap FD
263 my $max = $self->{partitions} - 1;
265 # idx_parts must be visible to all forked processes
266 my $idx = $self->{idx_parts} = [];
267 for my $i (0..$max) {
268 push @$idx, PublicInbox::SearchIdxPart->new($self, $i);
271 # Now that all subprocesses are up, we can open the FDs
273 my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
274 "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
275 $mm->{dbh}->begin_work;
280 my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
282 my $pfx = "$self->{-inbox}->{mainrepo}/git";
284 foreach my $i (0..$self->{epoch_max}) {
285 my $git = PublicInbox::Git->new("$pfx/$i.git");
286 my $im = $self->import_init($git, 0, 1);
287 $purges->[$i] = $im->purge_oids($purge);
292 sub content_ids ($) {
294 my @cids = ( content_id($mime) );
296 # Email::MIME->as_string doesn't always round-trip, so we may
297 # use a second content_id
298 my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
299 push @cids, $rt if $cids[0] ne $rt;
303 sub content_matches ($$) {
304 my ($cids, $existing) = @_;
305 my $cid = content_id($existing);
307 return 1 if $_ eq $cid
312 sub remove_internal {
313 my ($self, $mime, $cmt_msg, $purge) = @_;
315 my $im = $self->importer unless $purge;
316 my $over = $self->{over};
317 my $cids = content_ids($mime);
318 my $parts = $self->{idx_parts};
319 my $mm = $self->{mm};
321 my $mids = mids($mime->header_obj);
323 # We avoid introducing new blobs into git since the raw content
324 # can be slightly different, so we do not need the user-supplied
325 # message now that we have the mids and content_id
329 foreach my $mid (@$mids) {
332 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
333 my $msg = get_blob($self, $smsg);
334 if (!defined($msg)) {
335 warn "broken smsg for $mid\n";
339 my $cur = PublicInbox::MIME->new($msg);
340 if (content_matches($cids, $cur)) {
341 $smsg->{mime} = $cur;
342 $gone{$smsg->{num}} = [ $smsg, \$orig ];
345 my $n = scalar keys %gone;
348 warn "BUG: multiple articles linked to <$mid>\n",
349 join(',', sort keys %gone), "\n";
351 foreach my $num (keys %gone) {
352 my ($smsg, $orig) = @{$gone{$num}};
353 $mm->num_delete($num);
354 # $removed should only be set once assuming
355 # no bugs in our deduplication code:
357 my $oid = $smsg->{blob};
361 ($mark, undef) = $im->remove($orig, $cmt_msg);
364 $self->unindex_oid_remote($oid, $mid);
369 my $cmt = $im->get_mark($mark);
370 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
372 if ($purge && scalar keys %$purge) {
373 return purge_oids($self, $purge);
379 my ($self, $mime, $cmt_msg) = @_;
380 $self->{-inbox}->with_umask(sub {
381 remove_internal($self, $mime, $cmt_msg);
386 my ($self, $mime) = @_;
387 my $purges = $self->{-inbox}->with_umask(sub {
388 remove_internal($self, $mime, undef, {});
390 $self->idx_init if @$purges; # ->done is called on purges
391 for my $i (0..$#$purges) {
392 defined(my $cmt = $purges->[$i]) or next;
393 $self->{last_commit}->[$i] = $cmt;
398 sub last_commit_part ($$;$) {
399 my ($self, $i, $cmt) = @_;
400 my $v = PublicInbox::Search::SCHEMA_VERSION();
401 $self->{mm}->last_commit_xap($v, $i, $cmt);
404 sub set_last_commits ($) {
406 defined(my $epoch_max = $self->{epoch_max}) or return;
407 my $last_commit = $self->{last_commit};
408 foreach my $i (0..$epoch_max) {
409 defined(my $cmt = $last_commit->[$i]) or next;
410 $last_commit->[$i] = undef;
411 last_commit_part($self, $i, $cmt);
417 $self->{bnote} or return;
419 my $barrier = { map { $_ => 1 } (0..$n) };
423 my ($self, $barrier) = @_;
424 my $bnote = $self->{bnote} or return;
426 while (scalar keys %$barrier) {
427 defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
428 $l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
429 delete $barrier->{$1} or die "bad part[$1] on barrier wait";
433 sub checkpoint ($;$) {
434 my ($self, $wait) = @_;
436 if (my $im = $self->{im}) {
443 my $parts = $self->{idx_parts};
445 my $dbh = $self->{mm}->{dbh};
447 # SQLite msgmap data is second in importance
450 # SQLite overview is third
451 $self->{over}->commit_lazy;
453 # Now deal with Xapian
455 my $barrier = $self->barrier_init(scalar @$parts);
457 # each partition needs to issue a barrier command
458 $_->remote_barrier for @$parts;
460 # wait for each Xapian partition
461 $self->barrier_wait($barrier);
463 $_->remote_commit for @$parts;
466 # last_commit is special, don't commit these until
467 # remote partitions are done:
469 set_last_commits($self);
474 $self->{transact_bytes} = 0;
477 # issue a write barrier to ensure all data is visible to other processes
478 # and read-only ops. Order of data importance is: git > SQLite > Xapian
479 sub barrier { checkpoint($_[0], 1) };
483 my $im = delete $self->{im};
484 $im->done if $im; # PublicInbox::Import::done
486 my $mm = delete $self->{mm};
487 $mm->{dbh}->commit if $mm;
488 my $parts = delete $self->{idx_parts};
490 $_->remote_close for @$parts;
492 $self->{over}->disconnect;
493 delete $self->{bnote};
494 $self->{transact_bytes} = 0;
495 $self->lock_release if $parts;
499 my ($self, $epoch) = @_;
500 my $pfx = "$self->{-inbox}->{mainrepo}/git";
501 my $git_dir = "$pfx/$epoch.git";
502 my @cmd = (qw(git init --bare -q), $git_dir);
503 PublicInbox::Import::run_die(\@cmd);
505 my $all = "$self->{-inbox}->{mainrepo}/all.git";
507 @cmd = (qw(git init --bare -q), $all);
508 PublicInbox::Import::run_die(\@cmd);
509 @cmd = (qw/git config/, "--file=$all/config",
510 'repack.writeBitmaps', 'true');
511 PublicInbox::Import::run_die(\@cmd);
514 @cmd = (qw/git config/, "--file=$git_dir/config",
515 'include.path', '../../all.git/config');
516 PublicInbox::Import::run_die(\@cmd);
518 my $alt = "$all/objects/info/alternates";
522 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
523 %alts = map { chomp; $_ => 1 } (<$fh>);
525 foreach my $i (0..$epoch) {
526 my $dir = "../../git/$i.git/objects";
527 push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
529 return $git_dir unless @add;
530 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
531 foreach my $dir (@add) {
532 print $fh "$dir\n" or die "print >> $alt: $!\n";
534 close $fh or die "close $alt: $!\n";
539 my ($self, $max) = @_;
541 my $pfx = "$self->{-inbox}->{mainrepo}/git";
542 return unless -d $pfx;
544 opendir my $dh, $pfx or die "opendir $pfx: $!\n";
545 while (defined(my $git_dir = readdir($dh))) {
546 $git_dir =~ m!\A(\d+)\.git\z! or next;
549 $latest = "$pfx/$git_dir";
557 my $im = $self->{im};
559 if ($im->{bytes_added} < $self->{rotate_bytes}) {
566 my $git_dir = $self->git_init(++$self->{epoch_max});
567 my $git = PublicInbox::Git->new($git_dir);
568 return $self->import_init($git, 0);
573 my $latest = git_dir_latest($self, \$max);
574 if (defined $latest) {
575 my $git = PublicInbox::Git->new($latest);
576 my $packed_bytes = $git->packed_bytes;
577 if ($packed_bytes >= $self->{rotate_bytes}) {
580 $self->{epoch_max} = $max;
581 return $self->import_init($git, $packed_bytes);
584 $self->{epoch_max} = $epoch;
585 $latest = $self->git_init($epoch);
586 $self->import_init(PublicInbox::Git->new($latest), 0);
590 my ($self, $git, $packed_bytes, $tmp) = @_;
591 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
592 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
593 $im->{want_object_info} = 1;
594 $im->{lock_path} = undef;
595 $im->{path_type} = 'v2';
596 $self->{im} = $im unless $tmp;
602 my ($mid, $cur, $new) = @_;
603 use File::Temp qw(tempfile);
605 my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
606 print $ah $cur->as_string or die "print: $!";
607 close $ah or die "close: $!";
608 my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
609 PublicInbox::Import::drop_unwanted_headers($new);
610 print $bh $new->as_string or die "print: $!";
611 close $bh or die "close: $!";
612 my $cmd = [ qw(diff -u), $an, $bn ];
613 print STDERR "# MID conflict <$mid>\n";
614 my $pid = spawn($cmd, undef, { 1 => 2 });
615 defined $pid or die "diff failed to spawn $!";
616 waitpid($pid, 0) == $pid or die "diff did not finish";
621 my ($self, $smsg) = @_;
622 if (my $im = $self->{im}) {
623 my $msg = $im->cat_blob($smsg->{blob});
626 # older message, should be in alternates
627 my $ibx = $self->{-inbox};
628 $ibx->msg_by_smsg($smsg);
632 my ($self, $mime, $mid) = @_;
633 my $over = $self->{over};
634 my $cids = content_ids($mime);
636 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
637 my $msg = get_blob($self, $smsg);
638 if (!defined($msg)) {
639 warn "broken smsg for $mid\n";
642 my $cur = PublicInbox::MIME->new($msg);
643 if (content_matches($cids, $cur)) {
644 $smsg->{mime} = $cur;
649 # XXX DEBUG_DIFF is experimental and may be removed
650 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
657 my $fh = delete $self->{reindex_pipe};
659 if (my $parts = $self->{idx_parts}) {
660 $_->atfork_child foreach @$parts;
662 if (my $im = $self->{im}) {
665 die "unexpected mm" if $self->{mm};
666 close $self->{bnote}->[0] or die "close bnote[0]: $!\n";
671 my ($self, $D, $git, $oid) = @_;
672 my $msgref = $git->cat_file($oid);
673 my $mime = PublicInbox::MIME->new($$msgref);
674 my $mids = mids($mime->header_obj);
675 my $cid = content_id($mime);
676 foreach my $mid (@$mids) {
677 $D->{"$mid\0$cid"} = $oid;
682 my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
684 my $msgref = $git->cat_file($oid, \$len);
685 my $mime = PublicInbox::MIME->new($$msgref);
686 my $mids = mids($mime->header_obj);
687 my $cid = content_id($mime);
689 # get the NNTP article number we used before, highest number wins
690 # and gets deleted from mm_tmp;
694 foreach my $mid (@$mids) {
695 $del += delete($D->{"$mid\0$cid"}) ? 1 : 0;
696 my $n = $mm_tmp->num_for($mid);
697 if (defined $n && $n > $num) {
700 $self->{mm}->mid_set($num, $mid0);
703 if (!defined($mid0) && $regen && !$del) {
705 die "BUG: ran out of article numbers\n" if $num <= 0;
706 my $mm = $self->{mm};
707 foreach my $mid (reverse @$mids) {
708 if ($mm->mid_set($num, $mid) == 1) {
713 if (!defined($mid0)) {
714 my $id = '<' . join('> <', @$mids) . '>';
715 warn "Message-ID $id unusable for $num\n";
716 foreach my $mid (@$mids) {
717 defined(my $n = $mm->num_for($mid)) or next;
718 warn "#$n previously mapped for <$mid>\n";
723 if (!defined($mid0) || $del) {
724 if (!defined($mid0) && $del) { # expected for deletes
726 $self->{mm}->num_highwater($num) unless $reindex;
730 my $id = '<' . join('> <', @$mids) . '>';
732 warn "Skipping $id, no article number found\n";
733 if ($del && defined($mid0)) {
734 warn "$id was deleted $del " .
735 "time(s) but mapped to article #$num\n";
740 $mm_tmp->mid_delete($mid0) or
741 die "failed to delete <$mid0> for article #$num\n";
743 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
744 my $nparts = $self->{partitions};
745 my $part = $num % $nparts;
746 my $idx = $self->idx_part($part);
747 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
748 my $n = $self->{transact_bytes} += $len;
749 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
751 $mm_tmp->atfork_prepare;
752 $self->done; # release lock
753 # allow -watch or -mda to write...
754 $self->idx_init; # reacquire lock
755 $mm_tmp->atfork_parent;
759 # only update last_commit for $i on reindex iff newer than current
760 sub update_last_commit {
761 my ($self, $git, $i, $cmt) = @_;
762 my $last = last_commit_part($self, $i);
763 if (defined $last && is_ancestor($git, $last, $cmt)) {
764 my @cmd = (qw(rev-list --count), "$last..$cmt");
765 chomp(my $n = $git->qx(@cmd));
766 return if $n ne '' && $n == 0;
768 last_commit_part($self, $i, $cmt);
771 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
774 my ($self, $epoch_max) = @_;
776 for (my $i = $epoch_max; $i >= 0; $i--) {
777 $heads->[$i] = last_commit_part($self, $i);
782 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
785 my ($self, $opts, $epoch_max, $ranges) = @_;
787 my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
788 for (my $i = $epoch_max; $i >= 0; $i--) {
789 die "already indexing!\n" if $self->{index_pipe};
790 my $git_dir = git_dir_n($self, $i);
791 -d $git_dir or next; # missing parts are fine
792 my $git = PublicInbox::Git->new($git_dir);
793 chomp(my $tip = $git->qx('rev-parse', $head));
795 if (defined(my $cur = $ranges->[$i])) {
796 $range = "$cur..$tip";
797 if (is_ancestor($git, $cur, $tip)) { # common case
798 my $n = $git->qx(qw(rev-list --count), $range);
801 $ranges->[$i] = undef;
806 discontiguous range: $range
807 Rewritten history? (in $git_dir)
809 my $base = $git->qx('merge-base', $tip, $cur);
812 $range = "$base..$tip";
813 warn "found merge-base: $base\n"
817 discarding history at $cur
821 reindexing $git_dir starting at
824 $self->{"unindex-range.$i"} = "$base..$cur";
827 $range = $tip; # all of it
829 $ranges->[$i] = $range;
831 # can't use 'rev-list --count' if we use --diff-filter
832 my $fh = $git->popen(qw(log --pretty=tformat:%H
833 --no-notes --no-color --no-renames
834 --diff-filter=AM), $range, '--', 'm');
835 ++$regen_max while <$fh>;
840 sub unindex_oid_remote {
841 my ($self, $oid, $mid) = @_;
842 $_->remote_remove($oid, $mid) foreach @{$self->{idx_parts}};
843 $self->{over}->remove_oid($oid, $mid);
847 my ($self, $git, $oid) = @_;
848 my $msgref = $git->cat_file($oid);
849 my $mime = PublicInbox::MIME->new($msgref);
850 my $mids = mids($mime->header_obj);
851 $mime = $msgref = undef;
852 my $over = $self->{over};
853 foreach my $mid (@$mids) {
856 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
857 $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
860 my $n = scalar keys %gone;
863 warn "BUG: multiple articles linked to $oid\n",
864 join(',',sort keys %gone), "\n";
866 foreach my $num (keys %gone) {
867 $self->{unindexed}->{$_}++;
868 $self->{mm}->num_delete($num);
870 $self->unindex_oid_remote($oid, $mid);
874 my $x40 = qr/[a-f0-9]{40}/;
876 my ($self, $opts, $git, $unindex_range) = @_;
877 my $un = $self->{unindexed} ||= {}; # num => removal count
878 my $before = scalar keys %$un;
879 my @cmd = qw(log --raw -r
880 --no-notes --no-color --no-abbrev --no-renames);
881 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
883 /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
884 $self->unindex_oid($git, $1);
886 delete $self->{reindex_pipe};
889 return unless $opts->{prune};
890 my $after = scalar keys %$un;
891 return if $before == $after;
893 # ensure any blob can not longer be accessed via dumb HTTP
894 PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
895 qw(-c gc.reflogExpire=now gc --prune=all)]);
899 my ($self, $opts) = @_;
902 my $latest = git_dir_latest($self, \$epoch_max);
903 return unless defined $latest;
904 $self->idx_init; # acquire lock
905 my $mm_tmp = $self->{mm}->tmp_clone;
906 my $reindex = $opts->{reindex};
907 my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
909 my $high = $self->{mm}->num_highwater();
910 my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
911 $$regen += $high if $high;
912 my $D = {}; # "$mid\0$cid" => $oid
913 my @cmd = qw(log --raw -r --pretty=tformat:%H
914 --no-notes --no-color --no-abbrev --no-renames);
916 # work backwards through history
917 my $last_commit = [];
918 for (my $i = $epoch_max; $i >= 0; $i--) {
919 my $git_dir = git_dir_n($self, $i);
920 die "already reindexing!\n" if delete $self->{reindex_pipe};
921 -d $git_dir or next; # missing parts are fine
922 my $git = PublicInbox::Git->new($git_dir);
923 my $unindex = delete $self->{"unindex-range.$i"};
924 $self->unindex($opts, $git, $unindex) if $unindex;
925 defined(my $range = $ranges->[$i]) or next;
926 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
929 if (/\A$x40$/o && !defined($cmt)) {
931 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
932 $self->reindex_oid($mm_tmp, $D, $git, $1,
934 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
935 $self->mark_deleted($D, $git, $1);
939 delete $self->{reindex_pipe};
940 $self->update_last_commit($git, $i, $cmt) if defined $cmt;
943 # unindex is required for leftovers if "deletes" affect messages
944 # in a previous fetch+index window:
945 if (scalar keys %$D) {
946 my $git = $self->{-inbox}->git;
947 $self->unindex_oid($git, $_) for values %$D;