]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
v2writable: allow disabling parallelization
[public-inbox.git] / script / public-inbox-init
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Initializes a public-inbox, basically a wrapper for git-init(1)
6 use strict;
7 use warnings;
8 my $usage = "public-inbox-init NAME GIT_DIR HTTP_URL ADDRESS [ADDRESS..]";
9 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
10 use PublicInbox::Config;
11 use File::Temp qw/tempfile/;
12 use File::Basename qw/dirname/;
13 use File::Path qw/mkpath/;
14 use Cwd qw/abs_path/;
15
16 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
17 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
18 my $version = 1;
19 my %opts = ( 'V|version=i' => \$version );
20 GetOptions(%opts) or usage();
21 my $name = shift @ARGV or usage();
22 my $mainrepo = shift @ARGV or usage();
23 my $http_url = shift @ARGV or usage();
24 my (@address) = @ARGV;
25 @address or usage();
26 my %seen;
27
28 my $pi_config = PublicInbox::Config->default_file;
29 my $dir = dirname($pi_config);
30 mkpath($dir); # will croak on fatal errors
31 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
32 if (-e $pi_config) {
33         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
34         my @st = stat($oh);
35         my $perm = $st[2];
36         defined $perm or die "(f)stat failed on $pi_config: $!\n";
37         chmod($perm & 07777, $fh) or
38                 die "(f)chmod failed on future $pi_config: $!\n";
39         my $old;
40         {
41                 local $/;
42                 $old = <$oh>;
43         }
44         print $fh $old or die "failed to write: $!\n";
45         close $oh or die "failed to close $pi_config: $!\n";
46
47         # yes, this conflict checking is racy if multiple instances of this
48         # script are run by the same $PI_DIR
49         my $cfg = PublicInbox::Config->new;
50         my $conflict;
51         foreach my $addr (@address) {
52                 my $found = $cfg->lookup($addr);
53                 if ($found) {
54                         if ($found->{name} ne $name) {
55                                 print STDERR
56                                         "`$addr' already defined for ",
57                                         "`$found->{name}',\n",
58                                         "does not match intend `$name'\n";
59                                 $conflict = 1;
60                         } else {
61                                 $seen{lc($addr)} = 1;
62                         }
63                 }
64         }
65
66         exit(1) if $conflict;
67 }
68 close $fh or die "failed to close $pi_config_tmp: $!\n";
69
70 my $pfx = "publicinbox.$name";
71 my @x = (qw/git config/, "--file=$pi_config_tmp");
72
73 $mainrepo = abs_path($mainrepo);
74
75 if ($version >= 2) {
76         require PublicInbox::V2Writable;
77         require PublicInbox::Inbox;
78         my $ibx = {
79                 mainrepo => $mainrepo,
80                 name => $name,
81                 version => $version,
82                 -primary_address => $address[0],
83         };
84         $ibx = PublicInbox::Inbox->new($ibx);
85         my $v2w = PublicInbox::V2Writable->new($ibx, 1);
86         $v2w->{parallel} = 0;
87         $v2w->idx_init;
88         $v2w->git_init(0);
89         $v2w->done;
90 } elsif ($version == 1) {
91         x(qw(git init -q --bare), $mainrepo);
92
93         # set a reasonable default:
94         x(qw/git config/, "--file=$mainrepo/config",
95                 'repack.writeBitmaps', 'true');
96 } else {
97         die "Unsupported -V/--version: $version\n";
98 }
99
100 foreach my $addr (@address) {
101         next if $seen{lc($addr)};
102         x(@x, "--add", "$pfx.address", $addr);
103 }
104 x(@x, "$pfx.url", $http_url);
105 x(@x, "$pfx.mainrepo", $mainrepo);
106
107 rename $pi_config_tmp, $pi_config or
108         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";