]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
use PublicInbox::Config::each_inbox where appropriate
[public-inbox.git] / script / public-inbox-index
1 #!/usr/bin/perl -w
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
8
9 use strict;
10 use warnings;
11 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
12 use Cwd 'abs_path';
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";
17         {}
18 };
19 eval { require PublicInbox::SearchIdx };
20 if ($@) {
21         print STDERR "Search::Xapian required for $0\n";
22         exit 1;
23 }
24
25 my $reindex;
26 my $prune;
27 my $jobs = undef;
28 my %opts = (
29         '--reindex' => \$reindex,
30         '--jobs|j=i' => \$jobs,
31         '--prune' => \$prune,
32 );
33 GetOptions(%opts) or die "bad command-line args\n$usage";
34 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
35
36 my @dirs;
37
38 sub resolve_repo_dir {
39         my ($cd) = @_;
40         my $prefix = defined $cd ? $cd : './';
41         if (-d $prefix && -f "$prefix/inbox.lock") { # v2
42                 return abs_path($prefix);
43         }
44
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";
49         if ($pid == 0) {
50                 if (defined $cd) {
51                         chdir $cd or die "chdir $cd failed: $!\n";
52                 }
53                 exec @cmd;
54                 die "Failed to exec $cmd: $!\n";
55         } else {
56                 my $dir = eval {
57                         local $/;
58                         <$fh>;
59                 };
60                 close $fh or die "error in $cmd: $!\n";
61                 chomp $dir;
62                 return abs_path($cd) if ($dir eq '.' && defined $cd);
63                 abs_path($dir);
64         }
65 }
66
67 if (@ARGV) {
68         @dirs = map { resolve_repo_dir($_) } @ARGV;
69 } else {
70         @dirs = (resolve_repo_dir());
71 }
72
73 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
74 usage() unless @dirs;
75
76 $config->each_inbox(sub {
77         my ($ibx) = @_;
78
79         for my $i (0..$#dirs) {
80                 next if $dirs[$i] ne $ibx->{mainrepo};
81                 $dirs[$i] = $ibx;
82         }
83 });
84
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);
89         }
90         index_dir($dir);
91 }
92
93 sub index_dir {
94         my ($repo) = @_;
95         if (!ref $repo && ! -d $repo) {
96                 die "$repo does not appear to be an inbox repository\n";
97         }
98         if (ref($repo) && ($repo->{version} || 1) == 2) {
99                 eval { require PublicInbox::V2Writable };
100                 die "v2 requirements not met: $@\n" if $@;
101                 my $v2w = eval {
102                         $jobs and local $ENV{NPROC} = $jobs;
103                         PublicInbox::V2Writable->new($repo);
104                 };
105                 if (defined $jobs) {
106                         if ($jobs == 0) {
107                                 $v2w->{parallel} = 0;
108                         } else {
109                                 my $n = $v2w->{partitions};
110                                 if ($jobs != ($n + 1)) {
111                                         warn
112 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
113                                 }
114                         }
115                 }
116                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
117         } else {
118                 my $s = PublicInbox::SearchIdx->new($repo, 1);
119                 $s->index_sync({ reindex => $reindex });
120         }
121 }