]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
public-inbox-init: Initialize indexlevel
[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 REPO_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 = undef;
19 my $indexlevel = undef;
20 my %opts = ( 'V|version=i' => \$version,
21              'L|indexlevel=s' => \$indexlevel,
22 );
23 GetOptions(%opts) or usage();
24 my $name = shift @ARGV or usage();
25 my $mainrepo = shift @ARGV or usage();
26 my $http_url = shift @ARGV or usage();
27 my (@address) = @ARGV;
28 @address or usage();
29 my %seen;
30
31 my $pi_config = PublicInbox::Config->default_file;
32 my $dir = dirname($pi_config);
33 mkpath($dir); # will croak on fatal errors
34 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
35 if (-e $pi_config) {
36         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
37         my @st = stat($oh);
38         my $perm = $st[2];
39         defined $perm or die "(f)stat failed on $pi_config: $!\n";
40         chmod($perm & 07777, $fh) or
41                 die "(f)chmod failed on future $pi_config: $!\n";
42         my $old;
43         {
44                 local $/;
45                 $old = <$oh>;
46         }
47         print $fh $old or die "failed to write: $!\n";
48         close $oh or die "failed to close $pi_config: $!\n";
49
50         # yes, this conflict checking is racy if multiple instances of this
51         # script are run by the same $PI_DIR
52         my $cfg = PublicInbox::Config->new;
53         my $conflict;
54         foreach my $addr (@address) {
55                 my $found = $cfg->lookup($addr);
56                 if ($found) {
57                         if ($found->{name} ne $name) {
58                                 print STDERR
59                                         "`$addr' already defined for ",
60                                         "`$found->{name}',\n",
61                                         "does not match intend `$name'\n";
62                                 $conflict = 1;
63                         } else {
64                                 $seen{lc($addr)} = 1;
65                         }
66                 }
67         }
68
69         exit(1) if $conflict;
70
71         my $ibx = $cfg->lookup_name($name);
72         if ($ibx) {
73                 if (!defined($indexlevel) && $ibx->{indexlevel}) {
74                         $indexlevel = $ibx->{indexlevel};
75                 }
76         }
77 }
78 close $fh or die "failed to close $pi_config_tmp: $!\n";
79 $indexlevel ||= 'full';
80
81 my $pfx = "publicinbox.$name";
82 my @x = (qw/git config/, "--file=$pi_config_tmp");
83
84 $mainrepo = abs_path($mainrepo);
85 if (-f "$mainrepo/inbox.lock") {
86         if (!defined $version) {
87                 $version = 2;
88         } elsif ($version != 2) {
89                 die "$mainrepo is a -V2 repo, -V$version specified\n"
90         }
91 } elsif (-d "$mainrepo/objects") {
92         if (!defined $version) {
93                 $version = 1;
94         } elsif ($version != 1) {
95                 die "$mainrepo is a -V1 repo, -V$version specified\n"
96         }
97 }
98
99 $version = 1 unless defined $version;
100
101 if ($version >= 2) {
102         require PublicInbox::V2Writable;
103         require PublicInbox::Inbox;
104         my $ibx = {
105                 mainrepo => $mainrepo,
106                 name => $name,
107                 version => $version,
108                 -primary_address => $address[0],
109         };
110         $ibx = PublicInbox::Inbox->new($ibx);
111         PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0);
112 } elsif ($version == 1) {
113         x(qw(git init -q --bare), $mainrepo);
114
115         # set a reasonable default:
116         x(qw/git config/, "--file=$mainrepo/config",
117                 'repack.writeBitmaps', 'true');
118 } else {
119         die "Unsupported -V/--version: $version\n";
120 }
121
122 foreach my $addr (@address) {
123         next if $seen{lc($addr)};
124         x(@x, "--add", "$pfx.address", $addr);
125 }
126 x(@x, "$pfx.url", $http_url);
127 x(@x, "$pfx.mainrepo", $mainrepo);
128 x(@x, "$pfx.indexlevel", $indexlevel);
129
130 rename $pi_config_tmp, $pi_config or
131         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";