# idempotent
sub idx_init {
- my ($self) = @_;
+ my ($self, $opt) = @_;
return if $self->{idx_parts};
my $ibx = $self->{-inbox};
my $over = $self->{over};
$ibx->umask_prepare;
$ibx->with_umask(sub {
- $self->lock_acquire;
+ $self->lock_acquire unless ($opt && $opt->{-skip_lock});
$over->create;
# -compact can change partition count while -watch is idle
$git->cleanup;
$mm_tmp->atfork_prepare;
$self->done; # release lock
+
+ # TODO: print progress info, here
+
# allow -watch or -mda to write...
$self->idx_init; # reacquire lock
$mm_tmp->atfork_parent;
*is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
+# returns a revision range for git-log(1)
+sub log_range ($$$$$) {
+ my ($self, $git, $ranges, $i, $tip) = @_;
+ my $cur = $ranges->[$i] or return $tip; # all of it
+ my $range = "$cur..$tip";
+ if (is_ancestor($git, $cur, $tip)) { # common case
+ my $n = $git->qx(qw(rev-list --count), $range);
+ chomp($n);
+ if ($n == 0) {
+ $ranges->[$i] = undef;
+ return; # nothing to do
+ }
+ } else {
+ warn <<"";
+discontiguous range: $range
+Rewritten history? (in $git->{git_dir})
+
+ chomp(my $base = $git->qx('merge-base', $tip, $cur));
+ if ($base) {
+ $range = "$base..$tip";
+ warn "found merge-base: $base\n"
+ } else {
+ $range = $tip;
+ warn "discarding history at $cur\n";
+ }
+ warn <<"";
+reindexing $git->{git_dir} starting at
+$range
+
+ $self->{"unindex-range.$i"} = "$base..$cur";
+ }
+ $range;
+}
+
sub index_prepare {
my ($self, $opts, $epoch_max, $ranges) = @_;
+ my $pr = $opts->{-progress};
my $regen_max = 0;
my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
for (my $i = $epoch_max; $i >= 0; $i--) {
-d $git_dir or next; # missing parts are fine
my $git = PublicInbox::Git->new($git_dir);
chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
- next if $?; # new repo
- my $range;
- if (defined(my $cur = $ranges->[$i])) {
- $range = "$cur..$tip";
- if (is_ancestor($git, $cur, $tip)) { # common case
- my $n = $git->qx(qw(rev-list --count), $range);
- chomp($n);
- if ($n == 0) {
- $ranges->[$i] = undef;
- next;
- }
- } else {
- warn <<"";
-discontiguous range: $range
-Rewritten history? (in $git_dir)
-
- my $base = $git->qx('merge-base', $tip, $cur);
- chomp $base;
- if ($base) {
- $range = "$base..$tip";
- warn "found merge-base: $base\n"
- } else {
- $range = $tip;
- warn <<"";
-discarding history at $cur
-
- }
- warn <<"";
-reindexing $git_dir starting at
-$range
- $self->{"unindex-range.$i"} = "$base..$cur";
- }
- } else {
- $range = $tip; # all of it
- }
+ next if $?; # new repo
+ my $range = log_range($self, $git, $ranges, $i, $tip) or next;
$ranges->[$i] = $range;
# can't use 'rev-list --count' if we use --diff-filter
+ $pr->("$i.git counting changes\n\t$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');
- ++$regen_max while <$fh>;
+ ++$n while <$fh>;
+ $pr->("$n\n") if $pr;
+ $regen_max += $n;
}
\$regen_max;
}
qw(-c gc.reflogExpire=now gc --prune=all)]);
}
+sub index_ranges ($$$) {
+ my ($self, $reindex, $epoch_max) = @_;
+ 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;
+}
+
+# called for public-inbox-index
sub index_sync {
my ($self, $opts) = @_;
$opts ||= {};
my $epoch_max;
my $latest = git_dir_latest($self, \$epoch_max);
return unless defined $latest;
- $self->idx_init; # acquire lock
+ $self->idx_init($opts); # acquire lock
my $mm_tmp = $self->{mm}->tmp_clone;
my $reindex = $opts->{reindex};
- my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
+ my $ranges = index_ranges($self, $reindex, $epoch_max);
my $high = $self->{mm}->num_highwater();
my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
--no-notes --no-color --no-abbrev --no-renames);
# work backwards through history
- my $last_commit = [];
for (my $i = $epoch_max; $i >= 0; $i--) {
my $git_dir = git_dir_n($self, $i);
die "already reindexing!\n" if delete $self->{reindex_pipe};