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 '--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 defined($config) and $config->each_inbox(sub {
79 for my $i (0..$#dirs) {
80 next if $dirs[$i] ne $ibx->{mainrepo};
85 foreach my $dir (@dirs) {
86 if (!ref($dir) && -f "$dir/inbox.lock") { # v2
87 my $ibx = { mainrepo => $dir, name => 'unnamed' };
88 $dir = PublicInbox::Inbox->new($ibx);
95 if (!ref $repo && ! -d $repo) {
96 die "$repo does not appear to be an inbox repository\n";
98 if (ref($repo) && ($repo->{version} || 1) == 2) {
99 eval { require PublicInbox::V2Writable };
100 die "v2 requirements not met: $@\n" if $@;
102 $jobs and local $ENV{NPROC} = $jobs;
103 PublicInbox::V2Writable->new($repo);
107 $v2w->{parallel} = 0;
109 my $n = $v2w->{partitions};
110 if ($jobs != ($n + 1)) {
112 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
116 $v2w->index_sync({ reindex => $reindex, prune => $prune });
118 my $s = PublicInbox::SearchIdx->new($repo, 1);
119 $s->index_sync({ reindex => $reindex });