+ 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_parts}};
+ $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}->{$_}++;
+ $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);
+ }
+ delete $self->{reindex_pipe};
+ $fh = undef;
+
+ return unless $sync->{-opt}->{prune};
+ my $after = scalar keys %$un;
+ return if $before == $after;
+
+ # ensure any blob can not longer be accessed via dumb HTTP
+ PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
+ qw(-c gc.reflogExpire=now gc --prune=all)]);
+}
+
+sub sync_ranges ($$$) {
+ my ($self, $sync, $epoch_max) = @_;
+ my $reindex = $sync->{reindex};
+
+ return last_commits($self, $epoch_max) unless $reindex;
+ return [] if ref($reindex) ne 'HASH';
+
+ my $ranges = $reindex->{from}; # arrayref;
+ if (ref($ranges) ne 'ARRAY') {
+ die 'BUG: $reindex->{from} not an ARRAY';
+ }
+ $ranges;
+}
+
+sub index_epoch ($$$) {
+ my ($self, $sync, $i) = @_;
+
+ my $git_dir = git_dir_n($self, $i);
+ die 'BUG: already reindexing!' if $self->{reindex_pipe};
+ -d $git_dir or return; # missing parts are fine
+ fill_alternates($self, $i);
+ my $git = PublicInbox::Git->new($git_dir);
+ if (my $unindex_range = delete $sync->{unindex_range}->{$i}) {
+ unindex($self, $sync, $git, $unindex_range);
+ }
+ defined(my $range = $sync->{ranges}->[$i]) or return;
+ if (my $pr = $sync->{-opt}->{-progress}) {
+ $pr->("$i.git indexing $range\n");
+ }
+
+ my @cmd = qw(log --raw -r --pretty=tformat:%H
+ --no-notes --no-color --no-abbrev --no-renames);
+ my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
+ my $cmt;
+ while (<$fh>) {
+ chomp;
+ $self->{current_info} = "$i.git $_";
+ if (/\A$x40$/o && !defined($cmt)) {
+ $cmt = $_;
+ } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
+ reindex_oid($self, $sync, $git, $1);
+ } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
+ mark_deleted($self, $sync, $git, $1);
+ }
+ }
+ $fh = undef;
+ delete $self->{reindex_pipe};
+ update_last_commit($self, $git, $i, $cmt) if defined $cmt;
+}
+
+# public, called by public-inbox-index
+sub index_sync {
+ my ($self, $opt) = @_;
+ $opt ||= {};
+ my $pr = $opt->{-progress};
+ my $epoch_max;
+ my $latest = git_dir_latest($self, \$epoch_max);
+ return unless defined $latest;
+ $self->idx_init($opt); # acquire lock
+ my $sync = {
+ D => {}, # "$mid\0$cid" => $oid
+ unindex_range => {}, # EPOCH => oid_old..oid_new
+ reindex => $opt->{reindex},
+ -opt => $opt
+ };
+ $sync->{ranges} = sync_ranges($self, $sync, $epoch_max);
+ $sync->{regen} = sync_prepare($self, $sync, $epoch_max);
+
+ if ($sync->{regen}) {
+ # tmp_clone seems to fail if inside a transaction, so
+ # we rollback here (because we opened {mm} for reading)
+ # Note: we do NOT rely on DBI transactions for atomicity;
+ # only for batch performance.
+ $self->{mm}->{dbh}->rollback;
+ $self->{mm}->{dbh}->begin_work;
+ $sync->{mm_tmp} = $self->{mm}->tmp_clone;
+ }
+
+ # work backwards through history
+ for (my $i = $epoch_max; $i >= 0; $i--) {
+ index_epoch($self, $sync, $i);
+ }
+
+ # unindex is required for leftovers if "deletes" affect messages
+ # in a previous fetch+index window:
+ if (my @leftovers = values %{delete $sync->{D}}) {
+ my $git = $self->{-inbox}->git;
+ unindex_oid($self, $git, $_) for @leftovers;
+ $git->cleanup;
+ }
+ $self->done;
+
+ if (my $nr = $sync->{nr}) {
+ my $pr = $sync->{-opt}->{-progress};
+ $pr->('all.git '.sprintf($sync->{-regen_fmt}, $nr)) if $pr;
+ }
+
+ # reindex does not pick up new changes, so we rerun w/o it:
+ if ($opt->{reindex}) {
+ my %again = %$opt;
+ $sync = undef;
+ delete @again{qw(reindex -skip_lock)};
+ index_sync($self, \%again);