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";
26 my %opts = ( '--reindex' => \$reindex );
27 GetOptions(%opts) or die "bad command-line args\n$usage";
31 sub resolve_repo_dir {
33 my $prefix = defined $cd ? $cd : './';
34 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
35 return abs_path($prefix);
38 my @cmd = qw(git rev-parse --git-dir);
39 my $cmd = join(' ', @cmd);
40 my $pid = open my $fh, '-|';
41 defined $pid or die "forking $cmd failed: $!\n";
44 chdir $cd or die "chdir $cd failed: $!\n";
47 die "Failed to exec $cmd: $!\n";
53 close $fh or die "error in $cmd: $!\n";
55 return abs_path($cd) if ($dir eq '.' && defined $cd);
61 @dirs = map { resolve_repo_dir($_) } @ARGV;
63 @dirs = (resolve_repo_dir());
66 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
69 foreach my $k (keys %$config) {
70 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
72 my $v = $config->{$k};
73 for my $i (0..$#dirs) {
74 next if $dirs[$i] ne $v;
75 my $ibx = $config->lookup_name($name);
76 $dirs[$i] = $ibx if $ibx;
80 foreach my $dir (@dirs) {
81 if (!ref($dir) && -f "$dir/inbox.lock") { # v2
82 my $ibx = { mainrepo => $dir, name => 'unnamed' };
83 $dir = PublicInbox::Inbox->new($ibx);
90 if (!ref $repo && ! -d $repo) {
91 die "$repo does not appear to be an inbox repository\n";
93 if (ref($repo) && ($repo->{version} || 1) == 2) {
94 eval { require PublicInbox::V2Writable };
95 die "v2 requirements not met: $@\n" if $@;
96 my $v2w = PublicInbox::V2Writable->new($repo);
100 my $s = PublicInbox::SearchIdx->new($repo, 1);
101 $s->index_sync({ reindex => $reindex });