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 # pipe for barrier notifications doesn't need to be big,
248 fcntl($w, 1031, 4096) if $^O eq 'linux';
249 $self->{bnote} = [ $r, $w ];
253 my $over = $self->{over};
255 $ibx->with_umask(sub {
259 # -compact can change partition count while -watch is idle
260 my $nparts = count_partitions($self);
261 if ($nparts && $nparts != $self->{partitions}) {
262 $self->{partitions} = $nparts;
265 # need to create all parts before initializing msgmap FD
266 my $max = $self->{partitions} - 1;
268 # idx_parts must be visible to all forked processes
269 my $idx = $self->{idx_parts} = [];
270 for my $i (0..$max) {
271 push @$idx, PublicInbox::SearchIdxPart->new($self, $i);
274 # Now that all subprocesses are up, we can open the FDs
276 my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
277 "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
278 $mm->{dbh}->begin_work;
283 my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
285 my $pfx = "$self->{-inbox}->{mainrepo}/git";
287 foreach my $i (0..$self->{epoch_max}) {
288 my $git = PublicInbox::Git->new("$pfx/$i.git");
289 my $im = $self->import_init($git, 0, 1);
290 $purges->[$i] = $im->purge_oids($purge);
295 sub content_ids ($) {
297 my @cids = ( content_id($mime) );
299 # Email::MIME->as_string doesn't always round-trip, so we may
300 # use a second content_id
301 my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
302 push @cids, $rt if $cids[0] ne $rt;
306 sub content_matches ($$) {
307 my ($cids, $existing) = @_;
308 my $cid = content_id($existing);
310 return 1 if $_ eq $cid
315 sub remove_internal {
316 my ($self, $mime, $cmt_msg, $purge) = @_;
318 my $im = $self->importer unless $purge;
319 my $over = $self->{over};
320 my $cids = content_ids($mime);
321 my $parts = $self->{idx_parts};
322 my $mm = $self->{mm};
324 my $mids = mids($mime->header_obj);
326 # We avoid introducing new blobs into git since the raw content
327 # can be slightly different, so we do not need the user-supplied
328 # message now that we have the mids and content_id
332 foreach my $mid (@$mids) {
335 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
336 my $msg = get_blob($self, $smsg);
337 if (!defined($msg)) {
338 warn "broken smsg for $mid\n";
342 my $cur = PublicInbox::MIME->new($msg);
343 if (content_matches($cids, $cur)) {
344 $smsg->{mime} = $cur;
345 $gone{$smsg->{num}} = [ $smsg, \$orig ];
348 my $n = scalar keys %gone;
351 warn "BUG: multiple articles linked to <$mid>\n",
352 join(',', sort keys %gone), "\n";
354 foreach my $num (keys %gone) {
355 my ($smsg, $orig) = @{$gone{$num}};
356 $mm->num_delete($num);
357 # $removed should only be set once assuming
358 # no bugs in our deduplication code:
360 my $oid = $smsg->{blob};
364 ($mark, undef) = $im->remove($orig, $cmt_msg);
367 $self->unindex_oid_remote($oid, $mid);
372 my $cmt = $im->get_mark($mark);
373 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
375 if ($purge && scalar keys %$purge) {
376 return purge_oids($self, $purge);
382 my ($self, $mime, $cmt_msg) = @_;
383 $self->{-inbox}->with_umask(sub {
384 remove_internal($self, $mime, $cmt_msg);
389 my ($self, $mime) = @_;
390 my $purges = $self->{-inbox}->with_umask(sub {
391 remove_internal($self, $mime, undef, {});
393 $self->idx_init if @$purges; # ->done is called on purges
394 for my $i (0..$#$purges) {
395 defined(my $cmt = $purges->[$i]) or next;
396 $self->{last_commit}->[$i] = $cmt;
401 sub last_commit_part ($$;$) {
402 my ($self, $i, $cmt) = @_;
403 my $v = PublicInbox::Search::SCHEMA_VERSION();
404 $self->{mm}->last_commit_xap($v, $i, $cmt);
407 sub set_last_commits ($) {
409 defined(my $epoch_max = $self->{epoch_max}) or return;
410 my $last_commit = $self->{last_commit};
411 foreach my $i (0..$epoch_max) {
412 defined(my $cmt = $last_commit->[$i]) or next;
413 $last_commit->[$i] = undef;
414 last_commit_part($self, $i, $cmt);
420 $self->{bnote} or return;
422 my $barrier = { map { $_ => 1 } (0..$n) };
426 my ($self, $barrier) = @_;
427 my $bnote = $self->{bnote} or return;
429 while (scalar keys %$barrier) {
430 defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
431 $l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
432 delete $barrier->{$1} or die "bad part[$1] on barrier wait";
436 sub checkpoint ($;$) {
437 my ($self, $wait) = @_;
439 if (my $im = $self->{im}) {
446 my $parts = $self->{idx_parts};
448 my $dbh = $self->{mm}->{dbh};
450 # SQLite msgmap data is second in importance
453 # SQLite overview is third
454 $self->{over}->commit_lazy;
456 # Now deal with Xapian
458 my $barrier = $self->barrier_init(scalar @$parts);
460 # each partition needs to issue a barrier command
461 $_->remote_barrier for @$parts;
463 # wait for each Xapian partition
464 $self->barrier_wait($barrier);
466 $_->remote_commit for @$parts;
469 # last_commit is special, don't commit these until
470 # remote partitions are done:
472 set_last_commits($self);
477 $self->{transact_bytes} = 0;
480 # issue a write barrier to ensure all data is visible to other processes
481 # and read-only ops. Order of data importance is: git > SQLite > Xapian
482 sub barrier { checkpoint($_[0], 1) };
486 my $im = delete $self->{im};
487 $im->done if $im; # PublicInbox::Import::done
489 my $mm = delete $self->{mm};
490 $mm->{dbh}->commit if $mm;
491 my $parts = delete $self->{idx_parts};
493 $_->remote_close for @$parts;
495 $self->{over}->disconnect;
496 delete $self->{bnote};
497 $self->{transact_bytes} = 0;
498 $self->lock_release if $parts;
502 my ($self, $epoch) = @_;
503 my $pfx = "$self->{-inbox}->{mainrepo}/git";
504 my $git_dir = "$pfx/$epoch.git";
505 my @cmd = (qw(git init --bare -q), $git_dir);
506 PublicInbox::Import::run_die(\@cmd);
508 my $all = "$self->{-inbox}->{mainrepo}/all.git";
510 @cmd = (qw(git init --bare -q), $all);
511 PublicInbox::Import::run_die(\@cmd);
512 @cmd = (qw/git config/, "--file=$all/config",
513 'repack.writeBitmaps', 'true');
514 PublicInbox::Import::run_die(\@cmd);
517 @cmd = (qw/git config/, "--file=$git_dir/config",
518 'include.path', '../../all.git/config');
519 PublicInbox::Import::run_die(\@cmd);
521 my $alt = "$all/objects/info/alternates";
525 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
526 %alts = map { chomp; $_ => 1 } (<$fh>);
528 foreach my $i (0..$epoch) {
529 my $dir = "../../git/$i.git/objects";
530 push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
532 return $git_dir unless @add;
533 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
534 foreach my $dir (@add) {
535 print $fh "$dir\n" or die "print >> $alt: $!\n";
537 close $fh or die "close $alt: $!\n";
542 my ($self, $max) = @_;
544 my $pfx = "$self->{-inbox}->{mainrepo}/git";
545 return unless -d $pfx;
547 opendir my $dh, $pfx or die "opendir $pfx: $!\n";
548 while (defined(my $git_dir = readdir($dh))) {
549 $git_dir =~ m!\A(\d+)\.git\z! or next;
552 $latest = "$pfx/$git_dir";
560 my $im = $self->{im};
562 if ($im->{bytes_added} < $self->{rotate_bytes}) {
569 my $git_dir = $self->git_init(++$self->{epoch_max});
570 my $git = PublicInbox::Git->new($git_dir);
571 return $self->import_init($git, 0);
576 my $latest = git_dir_latest($self, \$max);
577 if (defined $latest) {
578 my $git = PublicInbox::Git->new($latest);
579 my $packed_bytes = $git->packed_bytes;
580 if ($packed_bytes >= $self->{rotate_bytes}) {
583 $self->{epoch_max} = $max;
584 return $self->import_init($git, $packed_bytes);
587 $self->{epoch_max} = $epoch;
588 $latest = $self->git_init($epoch);
589 $self->import_init(PublicInbox::Git->new($latest), 0);
593 my ($self, $git, $packed_bytes, $tmp) = @_;
594 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
595 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
596 $im->{want_object_info} = 1;
597 $im->{lock_path} = undef;
598 $im->{path_type} = 'v2';
599 $self->{im} = $im unless $tmp;
605 my ($mid, $cur, $new) = @_;
606 use File::Temp qw(tempfile);
608 my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
609 print $ah $cur->as_string or die "print: $!";
610 close $ah or die "close: $!";
611 my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
612 PublicInbox::Import::drop_unwanted_headers($new);
613 print $bh $new->as_string or die "print: $!";
614 close $bh or die "close: $!";
615 my $cmd = [ qw(diff -u), $an, $bn ];
616 print STDERR "# MID conflict <$mid>\n";
617 my $pid = spawn($cmd, undef, { 1 => 2 });
618 defined $pid or die "diff failed to spawn $!";
619 waitpid($pid, 0) == $pid or die "diff did not finish";
624 my ($self, $smsg) = @_;
625 if (my $im = $self->{im}) {
626 my $msg = $im->cat_blob($smsg->{blob});
629 # older message, should be in alternates
630 my $ibx = $self->{-inbox};
631 $ibx->msg_by_smsg($smsg);
635 my ($self, $mime, $mid) = @_;
636 my $over = $self->{over};
637 my $cids = content_ids($mime);
639 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
640 my $msg = get_blob($self, $smsg);
641 if (!defined($msg)) {
642 warn "broken smsg for $mid\n";
645 my $cur = PublicInbox::MIME->new($msg);
646 if (content_matches($cids, $cur)) {
647 $smsg->{mime} = $cur;
652 # XXX DEBUG_DIFF is experimental and may be removed
653 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
660 my $fh = delete $self->{reindex_pipe};
662 if (my $parts = $self->{idx_parts}) {
663 $_->atfork_child foreach @$parts;
665 if (my $im = $self->{im}) {
668 die "unexpected mm" if $self->{mm};
669 close $self->{bnote}->[0] or die "close bnote[0]: $!\n";
674 my ($self, $D, $git, $oid) = @_;
675 my $msgref = $git->cat_file($oid);
676 my $mime = PublicInbox::MIME->new($$msgref);
677 my $mids = mids($mime->header_obj);
678 my $cid = content_id($mime);
679 foreach my $mid (@$mids) {
680 $D->{"$mid\0$cid"} = $oid;
685 my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
687 my $msgref = $git->cat_file($oid, \$len);
688 my $mime = PublicInbox::MIME->new($$msgref);
689 my $mids = mids($mime->header_obj);
690 my $cid = content_id($mime);
692 # get the NNTP article number we used before, highest number wins
693 # and gets deleted from mm_tmp;
697 foreach my $mid (@$mids) {
698 $del += delete($D->{"$mid\0$cid"}) ? 1 : 0;
699 my $n = $mm_tmp->num_for($mid);
700 if (defined $n && $n > $num) {
703 $self->{mm}->mid_set($num, $mid0);
706 if (!defined($mid0) && $regen && !$del) {
708 die "BUG: ran out of article numbers\n" if $num <= 0;
709 my $mm = $self->{mm};
710 foreach my $mid (reverse @$mids) {
711 if ($mm->mid_set($num, $mid) == 1) {
716 if (!defined($mid0)) {
717 my $id = '<' . join('> <', @$mids) . '>';
718 warn "Message-ID $id unusable for $num\n";
719 foreach my $mid (@$mids) {
720 defined(my $n = $mm->num_for($mid)) or next;
721 warn "#$n previously mapped for <$mid>\n";
726 if (!defined($mid0) || $del) {
727 if (!defined($mid0) && $del) { # expected for deletes
729 $self->{mm}->num_highwater($num) unless $reindex;
733 my $id = '<' . join('> <', @$mids) . '>';
735 warn "Skipping $id, no article number found\n";
736 if ($del && defined($mid0)) {
737 warn "$id was deleted $del " .
738 "time(s) but mapped to article #$num\n";
743 $mm_tmp->mid_delete($mid0) or
744 die "failed to delete <$mid0> for article #$num\n";
746 $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
747 my $nparts = $self->{partitions};
748 my $part = $num % $nparts;
749 my $idx = $self->idx_part($part);
750 $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
751 my $n = $self->{transact_bytes} += $len;
752 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
754 $mm_tmp->atfork_prepare;
755 $self->done; # release lock
756 # allow -watch or -mda to write...
757 $self->idx_init; # reacquire lock
758 $mm_tmp->atfork_parent;
762 # only update last_commit for $i on reindex iff newer than current
763 sub update_last_commit {
764 my ($self, $git, $i, $cmt) = @_;
765 my $last = last_commit_part($self, $i);
766 if (defined $last && is_ancestor($git, $last, $cmt)) {
767 my @cmd = (qw(rev-list --count), "$last..$cmt");
768 chomp(my $n = $git->qx(@cmd));
769 return if $n ne '' && $n == 0;
771 last_commit_part($self, $i, $cmt);
774 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
777 my ($self, $epoch_max) = @_;
779 for (my $i = $epoch_max; $i >= 0; $i--) {
780 $heads->[$i] = last_commit_part($self, $i);
785 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
788 my ($self, $opts, $epoch_max, $ranges) = @_;
790 my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
791 for (my $i = $epoch_max; $i >= 0; $i--) {
792 die "already indexing!\n" if $self->{index_pipe};
793 my $git_dir = git_dir_n($self, $i);
794 -d $git_dir or next; # missing parts are fine
795 my $git = PublicInbox::Git->new($git_dir);
796 chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
797 next if $?; # new repo
799 if (defined(my $cur = $ranges->[$i])) {
800 $range = "$cur..$tip";
801 if (is_ancestor($git, $cur, $tip)) { # common case
802 my $n = $git->qx(qw(rev-list --count), $range);
805 $ranges->[$i] = undef;
810 discontiguous range: $range
811 Rewritten history? (in $git_dir)
813 my $base = $git->qx('merge-base', $tip, $cur);
816 $range = "$base..$tip";
817 warn "found merge-base: $base\n"
821 discarding history at $cur
825 reindexing $git_dir starting at
828 $self->{"unindex-range.$i"} = "$base..$cur";
831 $range = $tip; # all of it
833 $ranges->[$i] = $range;
835 # can't use 'rev-list --count' if we use --diff-filter
836 my $fh = $git->popen(qw(log --pretty=tformat:%H
837 --no-notes --no-color --no-renames
838 --diff-filter=AM), $range, '--', 'm');
839 ++$regen_max while <$fh>;
844 sub unindex_oid_remote {
845 my ($self, $oid, $mid) = @_;
846 $_->remote_remove($oid, $mid) foreach @{$self->{idx_parts}};
847 $self->{over}->remove_oid($oid, $mid);
851 my ($self, $git, $oid) = @_;
852 my $msgref = $git->cat_file($oid);
853 my $mime = PublicInbox::MIME->new($msgref);
854 my $mids = mids($mime->header_obj);
855 $mime = $msgref = undef;
856 my $over = $self->{over};
857 foreach my $mid (@$mids) {
860 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
861 $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
864 my $n = scalar keys %gone;
867 warn "BUG: multiple articles linked to $oid\n",
868 join(',',sort keys %gone), "\n";
870 foreach my $num (keys %gone) {
871 $self->{unindexed}->{$_}++;
872 $self->{mm}->num_delete($num);
874 $self->unindex_oid_remote($oid, $mid);
878 my $x40 = qr/[a-f0-9]{40}/;
880 my ($self, $opts, $git, $unindex_range) = @_;
881 my $un = $self->{unindexed} ||= {}; # num => removal count
882 my $before = scalar keys %$un;
883 my @cmd = qw(log --raw -r
884 --no-notes --no-color --no-abbrev --no-renames);
885 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
887 /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
888 $self->unindex_oid($git, $1);
890 delete $self->{reindex_pipe};
893 return unless $opts->{prune};
894 my $after = scalar keys %$un;
895 return if $before == $after;
897 # ensure any blob can not longer be accessed via dumb HTTP
898 PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
899 qw(-c gc.reflogExpire=now gc --prune=all)]);
903 my ($self, $opts) = @_;
906 my $latest = git_dir_latest($self, \$epoch_max);
907 return unless defined $latest;
908 $self->idx_init; # acquire lock
909 my $mm_tmp = $self->{mm}->tmp_clone;
910 my $reindex = $opts->{reindex};
911 my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
913 my $high = $self->{mm}->num_highwater();
914 my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
915 $$regen += $high if $high;
916 my $D = {}; # "$mid\0$cid" => $oid
917 my @cmd = qw(log --raw -r --pretty=tformat:%H
918 --no-notes --no-color --no-abbrev --no-renames);
920 # work backwards through history
921 my $last_commit = [];
922 for (my $i = $epoch_max; $i >= 0; $i--) {
923 my $git_dir = git_dir_n($self, $i);
924 die "already reindexing!\n" if delete $self->{reindex_pipe};
925 -d $git_dir or next; # missing parts are fine
926 my $git = PublicInbox::Git->new($git_dir);
927 my $unindex = delete $self->{"unindex-range.$i"};
928 $self->unindex($opts, $git, $unindex) if $unindex;
929 defined(my $range = $ranges->[$i]) or next;
930 my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
933 if (/\A$x40$/o && !defined($cmt)) {
935 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
936 $self->reindex_oid($mm_tmp, $D, $git, $1,
938 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
939 $self->mark_deleted($D, $git, $1);
943 delete $self->{reindex_pipe};
944 $self->update_last_commit($git, $i, $cmt) if defined $cmt;
947 # unindex is required for leftovers if "deletes" affect messages
948 # in a previous fetch+index window:
949 if (scalar keys %$D) {
950 my $git = $self->{-inbox}->git;
951 $self->unindex_oid($git, $_) for values %$D;