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) = @_;
87 $self->{parallel} = $parallel;
90 git_dir_latest($self, \$epoch_max);
91 $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
95 # returns undef on duplicate or spam
96 # mimics Import::add and wraps it for v2
98 my ($self, $mime, $check_cb) = @_;
99 $self->{-inbox}->with_umask(sub {
100 _add($self, $mime, $check_cb)
105 my ($self, $mime, $check_cb) = @_;
109 $mime = $check_cb->($mime) or return;
112 # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
113 # as does SQLite 3.4.1+ (released in 2007-07-20), and
114 # Xapian 1.3.2+ (released 2015-03-15).
115 # For the most part, we can spawn git-fast-import without
116 # leaking FDs to it...
120 my $num = num_for($self, $mime, \$mid0);
121 defined $num or return; # duplicate
122 defined $mid0 or die "BUG: $mid0 undefined\n";
123 my $im = $self->importer;
124 my $cmt = $im->add($mime);
125 $cmt = $im->get_mark($cmt);
126 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
128 my ($oid, $len, $msgref) = @{$im->{last_object}};
129 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
130 my $nparts = $self->{partitions};
131 my $part = $num % $nparts;
132 my $idx = $self->idx_part($part);
133 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
134 my $n = $self->{transact_bytes} += $len;
135 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
143 my ($self, $mime, $mid0) = @_;
144 my $mids = mids($mime->header_obj);
146 my $mid = $mids->[0];
147 my $num = $self->{mm}->mid_insert($mid);
148 if (defined $num) { # common case
153 # crap, Message-ID is already known, hope somebody just resent:
154 foreach my $m (@$mids) {
155 # read-only lookup now safe to do after above barrier
156 my $existing = $self->lookup_content($mime, $m);
157 # easy, don't store duplicates
158 # note: do not add more diagnostic info here since
159 # it gets noisy on public-inbox-watch restarts
164 warn "<$mid> reused for mismatched content\n";
166 # try the rest of the mids
167 for(my $i = $#$mids; $i >= 1; $i--) {
169 $num = $self->{mm}->mid_insert($m);
171 warn "alternative <$m> for <$mid> found\n";
177 # none of the existing Message-IDs are good, generate a new one:
178 num_for_harder($self, $mime, $mid0);
182 my ($self, $mime, $mid0) = @_;
184 my $hdr = $mime->header_obj;
185 my $dig = content_digest($mime);
186 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
187 my $num = $self->{mm}->mid_insert($$mid0);
188 unless (defined $num) {
189 # it's hard to spoof the last Received: header
190 my @recvd = $hdr->header_raw('Received');
191 $dig->add("Received: $_") foreach (@recvd);
192 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
193 $num = $self->{mm}->mid_insert($$mid0);
195 # fall back to a random Message-ID and give up determinism:
196 until (defined($num)) {
198 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
199 warn "using random Message-ID <$$mid0> as fallback\n";
200 $num = $self->{mm}->mid_insert($$mid0);
203 PublicInbox::Import::append_mid($hdr, $$mid0);
208 my ($self, $part) = @_;
209 $self->{idx_parts}->[$part];
215 return if $self->{idx_parts};
216 my $ibx = $self->{-inbox};
218 # do not leak read-only FDs to child processes, we only have these
219 # FDs for duplicate detection so they should not be
220 # frequently activated.
221 delete $ibx->{$_} foreach (qw(git mm search));
223 if ($self->{parallel}) {
224 pipe(my ($r, $w)) or die "pipe failed: $!";
225 $self->{bnote} = [ $r, $w ];
229 my $over = $self->{over};
231 $ibx->with_umask(sub {
235 # -compact can change partition count while -watch is idle
236 my $nparts = count_partitions($self);
237 if ($nparts && $nparts != $self->{partitions}) {
238 $self->{partitions} = $nparts;
241 # need to create all parts before initializing msgmap FD
242 my $max = $self->{partitions} - 1;
244 # idx_parts must be visible to all forked processes
245 my $idx = $self->{idx_parts} = [];
246 for my $i (0..$max) {
247 push @$idx, PublicInbox::SearchIdxPart->new($self, $i);
250 # Now that all subprocesses are up, we can open the FDs
252 my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
253 "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
254 $mm->{dbh}->begin_work;
259 my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
261 my $pfx = "$self->{-inbox}->{mainrepo}/git";
263 foreach my $i (0..$self->{epoch_max}) {
264 my $git = PublicInbox::Git->new("$pfx/$i.git");
265 my $im = $self->import_init($git, 0, 1);
266 $purges->[$i] = $im->purge_oids($purge);
271 sub content_ids ($) {
273 my @cids = ( content_id($mime) );
275 # Email::MIME->as_string doesn't always round-trip, so we may
276 # use a second content_id
277 my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
278 push @cids, $rt if $cids[0] ne $rt;
282 sub content_matches ($$) {
283 my ($cids, $existing) = @_;
284 my $cid = content_id($existing);
286 return 1 if $_ eq $cid
291 sub remove_internal {
292 my ($self, $mime, $cmt_msg, $purge) = @_;
294 my $im = $self->importer unless $purge;
295 my $over = $self->{over};
296 my $cids = content_ids($mime);
297 my $parts = $self->{idx_parts};
298 my $mm = $self->{mm};
300 my $mids = mids($mime->header_obj);
302 # We avoid introducing new blobs into git since the raw content
303 # can be slightly different, so we do not need the user-supplied
304 # message now that we have the mids and content_id
308 foreach my $mid (@$mids) {
311 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
312 my $msg = get_blob($self, $smsg);
313 if (!defined($msg)) {
314 warn "broken smsg for $mid\n";
318 my $cur = PublicInbox::MIME->new($msg);
319 if (content_matches($cids, $cur)) {
320 $smsg->{mime} = $cur;
321 $gone{$smsg->{num}} = [ $smsg, \$orig ];
324 my $n = scalar keys %gone;
327 warn "BUG: multiple articles linked to <$mid>\n",
328 join(',', sort keys %gone), "\n";
330 foreach my $num (keys %gone) {
331 my ($smsg, $orig) = @{$gone{$num}};
332 $mm->num_delete($num);
333 # $removed should only be set once assuming
334 # no bugs in our deduplication code:
336 my $oid = $smsg->{blob};
340 ($mark, undef) = $im->remove($orig, $cmt_msg);
343 $self->unindex_oid_remote($oid, $mid);
348 my $cmt = $im->get_mark($mark);
349 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
351 if ($purge && scalar keys %$purge) {
352 return purge_oids($self, $purge);
358 my ($self, $mime, $cmt_msg) = @_;
359 $self->{-inbox}->with_umask(sub {
360 remove_internal($self, $mime, $cmt_msg);
365 my ($self, $mime) = @_;
366 my $purges = $self->{-inbox}->with_umask(sub {
367 remove_internal($self, $mime, undef, {});
369 $self->idx_init if @$purges; # ->done is called on purges
370 for my $i (0..$#$purges) {
371 defined(my $cmt = $purges->[$i]) or next;
372 $self->{last_commit}->[$i] = $cmt;
377 sub last_commit_part ($$;$) {
378 my ($self, $i, $cmt) = @_;
379 my $v = PublicInbox::Search::SCHEMA_VERSION();
380 $self->{mm}->last_commit_xap($v, $i, $cmt);
383 sub set_last_commits ($) {
385 defined(my $epoch_max = $self->{epoch_max}) or return;
386 my $last_commit = $self->{last_commit};
387 foreach my $i (0..$epoch_max) {
388 defined(my $cmt = $last_commit->[$i]) or next;
389 $last_commit->[$i] = undef;
390 last_commit_part($self, $i, $cmt);
396 $self->{bnote} or return;
398 my $barrier = { map { $_ => 1 } (0..$n) };
402 my ($self, $barrier) = @_;
403 my $bnote = $self->{bnote} or return;
405 while (scalar keys %$barrier) {
406 defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
407 $l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
408 delete $barrier->{$1} or die "bad part[$1] on barrier wait";
412 sub checkpoint ($;$) {
413 my ($self, $wait) = @_;
415 if (my $im = $self->{im}) {
422 my $parts = $self->{idx_parts};
424 my $dbh = $self->{mm}->{dbh};
426 # SQLite msgmap data is second in importance
429 # SQLite overview is third
430 $self->{over}->commit_lazy;
432 # Now deal with Xapian
434 my $barrier = $self->barrier_init(scalar @$parts);
436 # each partition needs to issue a barrier command
437 $_->remote_barrier for @$parts;
439 # wait for each Xapian partition
440 $self->barrier_wait($barrier);
442 $_->remote_commit for @$parts;
445 # last_commit is special, don't commit these until
446 # remote partitions are done:
448 set_last_commits($self);
453 $self->{transact_bytes} = 0;
456 # issue a write barrier to ensure all data is visible to other processes
457 # and read-only ops. Order of data importance is: git > SQLite > Xapian
458 sub barrier { checkpoint($_[0], 1) };
462 my $im = delete $self->{im};
463 $im->done if $im; # PublicInbox::Import::done
465 my $mm = delete $self->{mm};
466 $mm->{dbh}->commit if $mm;
467 my $parts = delete $self->{idx_parts};
469 $_->remote_close for @$parts;
471 $self->{over}->disconnect;
472 delete $self->{bnote};
473 $self->{transact_bytes} = 0;
474 $self->lock_release if $parts;
478 my ($self, $epoch) = @_;
479 my $pfx = "$self->{-inbox}->{mainrepo}/git";
480 my $git_dir = "$pfx/$epoch.git";
481 my @cmd = (qw(git init --bare -q), $git_dir);
482 PublicInbox::Import::run_die(\@cmd);
484 my $all = "$self->{-inbox}->{mainrepo}/all.git";
486 @cmd = (qw(git init --bare -q), $all);
487 PublicInbox::Import::run_die(\@cmd);
488 @cmd = (qw/git config/, "--file=$all/config",
489 'repack.writeBitmaps', 'true');
490 PublicInbox::Import::run_die(\@cmd);
493 @cmd = (qw/git config/, "--file=$git_dir/config",
494 'include.path', '../../all.git/config');
495 PublicInbox::Import::run_die(\@cmd);
497 my $alt = "$all/objects/info/alternates";
498 my $new_obj_dir = "../../git/$epoch.git/objects";
501 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
502 %alts = map { chomp; $_ => 1 } (<$fh>);
504 return $git_dir if $alts{$new_obj_dir};
505 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
506 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
507 close $fh or die "close $alt: $!\n";
512 my ($self, $max) = @_;
514 my $pfx = "$self->{-inbox}->{mainrepo}/git";
515 return unless -d $pfx;
517 opendir my $dh, $pfx or die "opendir $pfx: $!\n";
518 while (defined(my $git_dir = readdir($dh))) {
519 $git_dir =~ m!\A(\d+)\.git\z! or next;
522 $latest = "$pfx/$git_dir";
530 my $im = $self->{im};
532 if ($im->{bytes_added} < $self->{rotate_bytes}) {
539 my $git_dir = $self->git_init(++$self->{epoch_max});
540 my $git = PublicInbox::Git->new($git_dir);
541 return $self->import_init($git, 0);
546 my $latest = git_dir_latest($self, \$max);
547 if (defined $latest) {
548 my $git = PublicInbox::Git->new($latest);
549 my $packed_bytes = $git->packed_bytes;
550 if ($packed_bytes >= $self->{rotate_bytes}) {
553 $self->{epoch_max} = $max;
554 return $self->import_init($git, $packed_bytes);
557 $self->{epoch_max} = $epoch;
558 $latest = $self->git_init($epoch);
559 $self->import_init(PublicInbox::Git->new($latest), 0);
563 my ($self, $git, $packed_bytes, $tmp) = @_;
564 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
565 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
566 $im->{want_object_info} = 1;
567 $im->{lock_path} = undef;
568 $im->{path_type} = 'v2';
569 $self->{im} = $im unless $tmp;
575 my ($mid, $cur, $new) = @_;
576 use File::Temp qw(tempfile);
578 my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
579 print $ah $cur->as_string or die "print: $!";
580 close $ah or die "close: $!";
581 my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
582 PublicInbox::Import::drop_unwanted_headers($new);
583 print $bh $new->as_string or die "print: $!";
584 close $bh or die "close: $!";
585 my $cmd = [ qw(diff -u), $an, $bn ];
586 print STDERR "# MID conflict <$mid>\n";
587 my $pid = spawn($cmd, undef, { 1 => 2 });
588 defined $pid or die "diff failed to spawn $!";
589 waitpid($pid, 0) == $pid or die "diff did not finish";
594 my ($self, $smsg) = @_;
595 if (my $im = $self->{im}) {
596 my $msg = $im->cat_blob($smsg->{blob});
599 # older message, should be in alternates
600 my $ibx = $self->{-inbox};
601 $ibx->msg_by_smsg($smsg);
605 my ($self, $mime, $mid) = @_;
606 my $over = $self->{over};
607 my $cids = content_ids($mime);
609 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
610 my $msg = get_blob($self, $smsg);
611 if (!defined($msg)) {
612 warn "broken smsg for $mid\n";
615 my $cur = PublicInbox::MIME->new($msg);
616 if (content_matches($cids, $cur)) {
617 $smsg->{mime} = $cur;
622 # XXX DEBUG_DIFF is experimental and may be removed
623 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
630 my $fh = delete $self->{reindex_pipe};
632 if (my $parts = $self->{idx_parts}) {
633 $_->atfork_child foreach @$parts;
635 if (my $im = $self->{im}) {
638 die "unexpected mm" if $self->{mm};
639 close $self->{bnote}->[0] or die "close bnote[0]: $!\n";
644 my ($self, $D, $git, $oid) = @_;
645 my $msgref = $git->cat_file($oid);
646 my $mime = PublicInbox::MIME->new($$msgref);
647 my $mids = mids($mime->header_obj);
648 my $cid = content_id($mime);
649 foreach my $mid (@$mids) {
650 $D->{"$mid\0$cid"} = 1;
655 my ($self, $mm_tmp, $D, $git, $oid, $regen) = @_;
657 my $msgref = $git->cat_file($oid, \$len);
658 my $mime = PublicInbox::MIME->new($$msgref);
659 my $mids = mids($mime->header_obj);
660 my $cid = content_id($mime);
662 # get the NNTP article number we used before, highest number wins
663 # and gets deleted from mm_tmp;
667 foreach my $mid (@$mids) {
668 $del += (delete $D->{"$mid\0$cid"} || 0);
669 my $n = $mm_tmp->num_for($mid);
670 if (defined $n && $n > $num) {
675 if (!defined($mid0) && $regen && !$del) {
677 die "BUG: ran out of article numbers\n" if $num <= 0;
678 my $mm = $self->{mm};
679 foreach my $mid (reverse @$mids) {
680 if ($mm->mid_set($num, $mid) == 1) {
685 if (!defined($mid0)) {
686 my $id = '<' . join('> <', @$mids) . '>';
687 warn "Message-ID $id unusable for $num\n";
688 foreach my $mid (@$mids) {
689 defined(my $n = $mm->num_for($mid)) or next;
690 warn "#$n previously mapped for <$mid>\n";
695 if (!defined($mid0) || $del) {
696 if (!defined($mid0) && $del) { # expected for deletes
701 my $id = '<' . join('> <', @$mids) . '>';
703 warn "Skipping $id, no article number found\n";
704 if ($del && defined($mid0)) {
705 warn "$id was deleted $del " .
706 "time(s) but mapped to article #$num\n";
711 $mm_tmp->mid_delete($mid0) or
712 die "failed to delete <$mid0> for article #$num\n";
714 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
715 my $nparts = $self->{partitions};
716 my $part = $num % $nparts;
717 my $idx = $self->idx_part($part);
718 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
719 my $n = $self->{transact_bytes} += $len;
720 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
722 $mm_tmp->atfork_prepare;
723 $self->done; # release lock
724 # allow -watch or -mda to write...
725 $self->idx_init; # reacquire lock
726 $mm_tmp->atfork_parent;
730 # only update last_commit for $i on reindex iff newer than current
731 sub update_last_commit {
732 my ($self, $git, $i, $cmt) = @_;
733 my $last = last_commit_part($self, $i);
734 if (defined $last && is_ancestor($git, $last, $cmt)) {
735 my @cmd = (qw(rev-list --count), "$last..$cmt");
736 chomp(my $n = $git->qx(@cmd));
737 return if $n ne '' && $n == 0;
739 last_commit_part($self, $i, $cmt);
742 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
745 my ($self, $epoch_max) = @_;
747 for (my $i = $epoch_max; $i >= 0; $i--) {
748 $heads->[$i] = last_commit_part($self, $i);
753 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
756 my ($self, $opts, $epoch_max, $ranges) = @_;
758 my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
759 for (my $i = $epoch_max; $i >= 0; $i--) {
760 die "already indexing!\n" if $self->{index_pipe};
761 my $git_dir = git_dir_n($self, $i);
762 -d $git_dir or next; # missing parts are fine
763 my $git = PublicInbox::Git->new($git_dir);
764 chomp(my $tip = $git->qx('rev-parse', $head));
766 if (defined(my $cur = $ranges->[$i])) {
767 $range = "$cur..$tip";
768 if (is_ancestor($git, $cur, $tip)) { # common case
769 my $n = $git->qx(qw(rev-list --count), $range);
772 $ranges->[$i] = undef;
777 discontiguous range: $range
778 Rewritten history? (in $git_dir)
780 my $base = $git->qx('merge-base', $tip, $cur);
783 $range = "$base..$tip";
784 warn "found merge-base: $base\n"
788 discarding history at $cur
792 reindexing $git_dir starting at
795 $self->{"unindex-range.$i"} = "$base..$cur";
798 $range = $tip; # all of it
800 $ranges->[$i] = $range;
802 # can't use 'rev-list --count' if we use --diff-filter
803 my $fh = $git->popen(qw(log --pretty=tformat:%H
804 --no-notes --no-color --no-renames
805 --diff-filter=AM), $range, '--', 'm');
806 ++$regen_max while <$fh>;
811 sub unindex_oid_remote {
812 my ($self, $oid, $mid) = @_;
813 $_->remote_remove($oid, $mid) foreach @{$self->{idx_parts}};
814 $self->{over}->remove_oid($oid, $mid);
818 my ($self, $git, $oid) = @_;
819 my $msgref = $git->cat_file($oid);
820 my $mime = PublicInbox::MIME->new($msgref);
821 my $mids = mids($mime->header_obj);
822 $mime = $msgref = undef;
823 my $over = $self->{over};
824 foreach my $mid (@$mids) {
827 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
828 $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
831 my $n = scalar keys %gone;
834 warn "BUG: multiple articles linked to $oid\n",
835 join(',',sort keys %gone), "\n";
837 $self->{unindexed}->{$_}++ foreach keys %gone;
838 $self->unindex_oid_remote($oid, $mid);
842 my $x40 = qr/[a-f0-9]{40}/;
844 my ($self, $opts, $git, $unindex_range) = @_;
845 my $un = $self->{unindexed} ||= {}; # num => removal count
846 my $before = scalar keys %$un;
847 my @cmd = qw(log --raw -r
848 --no-notes --no-color --no-abbrev --no-renames);
849 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
851 /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
852 $self->unindex_oid($git, $1);
854 delete $self->{reindex_pipe};
857 return unless $opts->{prune};
858 my $after = scalar keys %$un;
859 return if $before == $after;
861 # ensure any blob can not longer be accessed via dumb HTTP
862 PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
863 qw(-c gc.reflogExpire=now gc --prune=all)]);
867 my ($self, $opts) = @_;
870 my $latest = git_dir_latest($self, \$epoch_max);
871 return unless defined $latest;
872 $self->idx_init; # acquire lock
873 my $mm_tmp = $self->{mm}->tmp_clone;
874 my $ranges = $opts->{reindex} ? [] : $self->last_commits($epoch_max);
876 my ($min, $max) = $mm_tmp->minmax;
877 my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
878 $$regen += $max if $max;
880 my @cmd = qw(log --raw -r --pretty=tformat:%H
881 --no-notes --no-color --no-abbrev --no-renames);
883 # work backwards through history
884 my $last_commit = [];
885 for (my $i = $epoch_max; $i >= 0; $i--) {
886 my $git_dir = git_dir_n($self, $i);
887 die "already reindexing!\n" if delete $self->{reindex_pipe};
888 -d $git_dir or next; # missing parts are fine
889 my $git = PublicInbox::Git->new($git_dir);
890 my $unindex = delete $self->{"unindex-range.$i"};
891 $self->unindex($opts, $git, $unindex) if $unindex;
892 defined(my $range = $ranges->[$i]) or next;
893 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
896 if (/\A$x40$/o && !defined($cmt)) {
898 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
899 $self->reindex_oid($mm_tmp, $D, $git, $1,
901 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
902 $self->mark_deleted($D, $git, $1);
906 delete $self->{reindex_pipe};
907 $self->update_last_commit($git, $i, $cmt) if defined $cmt;
909 my @d = sort keys %$D;
911 warn "BUG: ", scalar(@d)," unseen deleted messages marked\n";
913 my ($mid, undef) = split(/\0/, $_, 2);