2 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Basic tool to create a Xapian search index for a git repository
5 # configured for public-inbox.
6 # Usage with libeatmydata <https://www.flamingspork.com/projects/libeatmydata/>
7 # highly recommended: eatmydata public-inbox-index REPO_DIR
11 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
12 my $usage = "public-inbox-index REPO_DIR";
13 use PublicInbox::Admin qw(resolve_repo_dir);
14 PublicInbox::Admin::require_or_die('-index');
15 require PublicInbox::Config;
17 my $config = eval { PublicInbox::Config->new } || eval {
18 warn "public-inbox unconfigured for serving, indexing anyways...\n";
27 '--reindex' => \$reindex,
28 '--jobs|j=i' => \$jobs,
30 'L|indexlevel=s' => \$indexlevel,
32 GetOptions(%opts) or die "bad command-line args\n$usage";
33 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
38 @dirs = map { resolve_repo_dir($_) } @ARGV;
40 @dirs = (resolve_repo_dir());
43 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
46 defined($config) and $config->each_inbox(sub {
49 for my $i (0..$#dirs) {
50 next if $dirs[$i] ne $ibx->{mainrepo};
58 foreach my $dir (@dirs) {
62 die "$dir does not appear to be an inbox repository\n";
64 $ibx = PublicInbox::Inbox->new({
67 indexlevel => $indexlevel,
68 version => -f "$dir/inbox.lock" ? 2 : 1,
70 } elsif (defined $indexlevel && !defined($ibx->{indexlevel})) {
71 # XXX: users can shoot themselves in the foot, with this...
72 $ibx->{indexlevel} = $indexlevel;
75 PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
78 PublicInbox::Admin::require_or_die(keys %$mods);
80 require PublicInbox::SearchIdx;
81 index_inbox($_) for @inboxes;
85 if (ref($repo) && ($repo->{version} || 1) == 2) {
86 eval { require PublicInbox::V2Writable };
87 die "v2 requirements not met: $@\n" if $@;
89 PublicInbox::V2Writable->new($repo, {nproc=>$jobs});
95 my $n = $v2w->{partitions};
96 if ($jobs != ($n + 1)) {
98 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
102 local $SIG{__WARN__} = sub {
103 print STDERR $v2w->{current_info}, ': ', @_;
105 $v2w->index_sync({ reindex => $reindex, prune => $prune });
107 my $s = PublicInbox::SearchIdx->new($repo, 1);
108 $s->index_sync({ reindex => $reindex });