]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
v2writable: support reindexing Xapian
[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 %opts = ( '--reindex' => \$reindex );
27 GetOptions(%opts) or die "bad command-line args\n$usage";
28
29 my @dirs;
30
31 sub resolve_repo_dir {
32         my ($cd) = @_;
33         my $prefix = defined $cd ? $cd : './';
34         if (-d $prefix && -f "$prefix/inbox.lock") { # v2
35                 return abs_path($prefix);
36         }
37
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";
42         if ($pid == 0) {
43                 if (defined $cd) {
44                         chdir $cd or die "chdir $cd failed: $!\n";
45                 }
46                 exec @cmd;
47                 die "Failed to exec $cmd: $!\n";
48         } else {
49                 my $dir = eval {
50                         local $/;
51                         <$fh>;
52                 };
53                 close $fh or die "error in $cmd: $!\n";
54                 chomp $dir;
55                 return abs_path($cd) if ($dir eq '.' && defined $cd);
56                 abs_path($dir);
57         }
58 }
59
60 if (@ARGV) {
61         @dirs = map { resolve_repo_dir($_) } @ARGV;
62 } else {
63         @dirs = (resolve_repo_dir());
64 }
65
66 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
67 usage() unless @dirs;
68
69 foreach my $k (keys %$config) {
70         $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
71         my $name = $1;
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;
77         }
78 }
79
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);
84         }
85         index_dir($dir);
86 }
87
88 sub index_dir {
89         my ($repo) = @_;
90         if (!ref $repo && ! -d $repo) {
91                 die "$repo does not appear to be an inbox repository\n";
92         }
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);
97                 $v2w->reindex;
98                 $v2w->done;
99         } else {
100                 my $s = PublicInbox::SearchIdx->new($repo, 1);
101                 $s->index_sync({ reindex => $reindex });
102         }
103 }