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);
13 my $usage = "public-inbox-index REPO_DIR";
14 use PublicInbox::Config;
15 my $config = eval { PublicInbox::Config->new } || eval {
16 warn "public-inbox unconfigured for serving, indexing anyways...\n";
19 eval { require PublicInbox::SearchIdx };
21 print STDERR "Search::Xapian required for $0\n";
29 '--reindex' => \$reindex,
30 '--regenerate' => \$regen,
31 '--jobs|j=i' => \$jobs,
33 GetOptions(%opts) or die "bad command-line args\n$usage";
34 die "--jobs must be positive\n" if defined $jobs && $jobs <= 0;
38 sub resolve_repo_dir {
40 my $prefix = defined $cd ? $cd : './';
41 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
42 return abs_path($prefix);
45 my @cmd = qw(git rev-parse --git-dir);
46 my $cmd = join(' ', @cmd);
47 my $pid = open my $fh, '-|';
48 defined $pid or die "forking $cmd failed: $!\n";
51 chdir $cd or die "chdir $cd failed: $!\n";
54 die "Failed to exec $cmd: $!\n";
60 close $fh or die "error in $cmd: $!\n";
62 return abs_path($cd) if ($dir eq '.' && defined $cd);
68 @dirs = map { resolve_repo_dir($_) } @ARGV;
70 @dirs = (resolve_repo_dir());
73 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
76 foreach my $k (keys %$config) {
77 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
79 my $v = $config->{$k};
80 for my $i (0..$#dirs) {
81 next if $dirs[$i] ne $v;
82 my $ibx = $config->lookup_name($name);
83 $dirs[$i] = $ibx if $ibx;
87 foreach my $dir (@dirs) {
88 if (!ref($dir) && -f "$dir/inbox.lock") { # v2
89 my $ibx = { mainrepo => $dir, name => 'unnamed' };
90 $dir = PublicInbox::Inbox->new($ibx);
97 if (!ref $repo && ! -d $repo) {
98 die "$repo does not appear to be an inbox repository\n";
100 if (ref($repo) && ($repo->{version} || 1) == 2) {
101 eval { require PublicInbox::V2Writable };
102 die "v2 requirements not met: $@\n" if $@;
104 local $ENV{NPROC} = $jobs;
105 PublicInbox::V2Writable->new($repo);
109 $v2w->{parallel} = 0;
111 my $n = $v2w->{partitions};
114 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
119 my (undef, $max) = $mm->minmax if $mm;
120 if (defined($max) && !$reindex && !$regen) {
122 "v2 inboxes may only use --reindex and/or --regenerate once\n".
123 "msgmap.sqlite3 is initialized\n";
126 $v2w->reindex($regen);
129 my $s = PublicInbox::SearchIdx->new($repo, 1);
130 $s->index_sync({ reindex => $reindex });