+ chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
+
+ next if $?; # new repo
+ my $range = log_range($self, $sync, $git, $i, $tip) or next;
+ $sync->{ranges}->[$i] = $range;
+
+ # can't use 'rev-list --count' if we use --diff-filter
+ $pr->("$i.git counting $range ... ") if $pr;
+ my $n = 0;
+ my $fh = $git->popen(qw(log --pretty=tformat:%H
+ --no-notes --no-color --no-renames
+ --diff-filter=AM), $range, '--', 'm');
+ ++$n while <$fh>;
+ $pr->("$n\n") if $pr;
+ $regen_max += $n;
+ }
+
+ return 0 if (!$regen_max && !keys(%{$self->{unindex_range}}));
+
+ # reindex should NOT see new commits anymore, if we do,
+ # it's a problem and we need to notice it via die()
+ my $pad = length($regen_max) + 1;
+ $sync->{-regen_fmt} = "% ${pad}u/$regen_max\n";
+ $sync->{nr} = 0;
+ return -1 if $sync->{reindex};
+ $regen_max + $self->{mm}->num_highwater() || 0;
+}
+
+sub unindex_oid_remote ($$$) {
+ my ($self, $oid, $mid) = @_;
+ $_->remote_remove($oid, $mid) foreach @{$self->{idx_shards}};
+ $self->{over}->remove_oid($oid, $mid);
+}
+
+sub unindex_oid ($$$) {
+ my ($self, $git, $oid) = @_;
+ my $msgref = $git->cat_file($oid);
+ my $mime = PublicInbox::MIME->new($msgref);
+ my $mids = mids($mime->header_obj);
+ $mime = $msgref = undef;
+ my $over = $self->{over};
+ foreach my $mid (@$mids) {
+ my %gone;
+ my ($id, $prev);
+ while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
+ $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
+ 1; # continue
+ }
+ my $n = scalar keys %gone;
+ next unless $n;
+ if ($n > 1) {
+ warn "BUG: multiple articles linked to $oid\n",
+ join(',',sort keys %gone), "\n";
+ }
+ foreach my $num (keys %gone) {
+ $self->{unindexed}->{$num}++;
+ $self->{mm}->num_delete($num);
+ }
+ unindex_oid_remote($self, $oid, $mid);
+ }
+}
+
+my $x40 = qr/[a-f0-9]{40}/;
+sub unindex ($$$$) {
+ my ($self, $sync, $git, $unindex_range) = @_;
+ my $un = $self->{unindexed} ||= {}; # num => removal count
+ my $before = scalar keys %$un;
+ my @cmd = qw(log --raw -r
+ --no-notes --no-color --no-abbrev --no-renames);
+ my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
+ while (<$fh>) {
+ /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
+ unindex_oid($self, $git, $1);