#!perl -w
# Copyright (C) all contributors
# License: AGPL-3.0+
use v5.12;
use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
my $help = < 1, scan => 1 }; # --no-scan is hidden
GetOptions($opt, qw(quiet|q verbose|v+ reindex jobs|j=i fsync|sync! dangerous
indexlevel|index-level|L=s join:s@
batch_size|batch-size=s max_size|max-size=s
include|I=s@ only=s@ all show:s@
project-list=s exclude=s@ project-root|r=s
git-dir|g=s@
sort-parallel=s sort-compress-program=s sort-buffer-size=s
d=s update|u scan! prune dry-run|n C=s@ help|h))
or die $help;
if ($opt->{help}) { print $help; exit 0 };
die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
require IO::Handle;
STDOUT->autoflush(1);
STDERR->autoflush(1);
$SIG{USR1} = 'IGNORE'; # to be overridden in cidx_sync
$SIG{PIPE} = 'IGNORE';
# require lazily to speed up --help
require PublicInbox::Admin;
PublicInbox::Admin::do_chdir(delete $opt->{C});
my $cfg = $opt->{-pi_cfg} = PublicInbox::Config->new;
my $cidx_dir = $opt->{d};
PublicInbox::Admin::require_or_die('Xapian');
PublicInbox::Admin::progress_prepare($opt);
my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
%ENV = (%ENV, %$env) if $env;
my @git_dirs;
require PublicInbox::CodeSearchIdx; # unstable internal API
if (@ARGV) {
my @g = map { "-g $_" } @ARGV;
die <{'project-list'})) {
my $pfx = $opt->{'project-root'} // die <{'git-dir'} and die <{'git-dir'}})
EOM
open my $fh, '<', $pl or die "open($pl): $!\n";
chomp(@git_dirs = <$fh>);
$pfx .= '/';
$pfx =~ tr!/!/!s;
substr($_, 0, 0, $pfx) for @git_dirs;
} elsif (my $gd = $opt->{'git-dir'}) {
@git_dirs = @$gd;
} elsif (grep defined, @$opt{qw(show update prune)}) {
} else {
warn "No --git-dir= nor --project-list= + --project-root= specified\n";
die $help;
}
$_ = PublicInbox::Admin::resolve_git_dir($_) for @git_dirs;
if (defined $cidx_dir) { # external index
die "`%' is not allowed in $cidx_dir\n" if $cidx_dir =~ /\%/;
my $cidx = PublicInbox::CodeSearchIdx->new($cidx_dir, $opt);
@{$cidx->{git_dirs}} = @git_dirs; # may be empty
$cidx->cidx_run;
} elsif (!@git_dirs) {
die $help
} else {
die <{update};
--update requires `-d EXTDIR'
EOM
for my $gd (@git_dirs) {
my $cd = "$gd/public-inbox-cindex";
my $cidx = PublicInbox::CodeSearchIdx->new($cd, { %$opt });
$cidx->{-cidx_internal} = 1;
@{$cidx->{git_dirs}} = ($gd);
$cidx->cidx_run;
}
}