]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
73f88ac6bee72fd0eabc3497881cf42e3392cc97
[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 foreach my $k (keys %$config) {
77         $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
78         my $name = $1;
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;
84         }
85 }
86
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);
91         }
92         index_dir($dir);
93 }
94
95 sub index_dir {
96         my ($repo) = @_;
97         if (!ref $repo && ! -d $repo) {
98                 die "$repo does not appear to be an inbox repository\n";
99         }
100         if (ref($repo) && ($repo->{version} || 1) == 2) {
101                 eval { require PublicInbox::V2Writable };
102                 die "v2 requirements not met: $@\n" if $@;
103                 my $v2w = eval {
104                         local $ENV{NPROC} = $jobs;
105                         PublicInbox::V2Writable->new($repo);
106                 };
107                 if (defined $jobs) {
108                         if ($jobs == 1) {
109                                 $v2w->{parallel} = 0;
110                         } else {
111                                 my $n = $v2w->{partitions};
112                                 if ($jobs != $n) {
113                                         warn
114 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
115                                 }
116                         }
117                 }
118                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
119         } else {
120                 my $s = PublicInbox::SearchIdx->new($repo, 1);
121                 $s->index_sync({ reindex => $reindex });
122         }
123 }