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>
5 # Initializes a public-inbox, basically a wrapper for git-init(1)
8 my $usage = "public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]";
9 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
10 use PublicInbox::Admin;
11 PublicInbox::Admin::require_or_die('-base');
12 require PublicInbox::Config;
13 require PublicInbox::InboxWritable;
14 use File::Temp qw/tempfile/;
15 use File::Basename qw/dirname/;
16 use File::Path qw/mkpath/;
19 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
20 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
22 my $indexlevel = undef;
24 my %opts = ( 'V|version=i' => \$version,
25 'L|indexlevel=s' => \$indexlevel,
28 GetOptions(%opts) or usage();
29 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
30 my $name = shift @ARGV or usage();
31 my $mainrepo = shift @ARGV or usage();
32 my $http_url = shift @ARGV or usage();
33 my (@address) = @ARGV;
37 my $pi_config = PublicInbox::Config->default_file;
38 my $dir = dirname($pi_config);
39 mkpath($dir); # will croak on fatal errors
40 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
43 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
46 defined $perm or die "(f)stat failed on $pi_config: $!\n";
47 chmod($perm & 07777, $fh) or
48 die "(f)chmod failed on future $pi_config: $!\n";
54 print $fh $old or die "failed to write: $!\n";
55 close $oh or die "failed to close $pi_config: $!\n";
57 # yes, this conflict checking is racy if multiple instances of this
58 # script are run by the same $PI_DIR
59 my $cfg = PublicInbox::Config->new;
61 foreach my $addr (@address) {
62 my $found = $cfg->lookup($addr);
64 if ($found->{name} ne $name) {
66 "`$addr' already defined for ",
67 "`$found->{name}',\n",
68 "does not match intend `$name'\n";
78 my $ibx = $cfg->lookup_name($name);
80 if (!defined($indexlevel) && $ibx->{indexlevel}) {
81 $indexlevel = $ibx->{indexlevel};
85 close $fh or die "failed to close $pi_config_tmp: $!\n";
87 my $pfx = "publicinbox.$name";
88 my @x = (qw/git config/, "--file=$pi_config_tmp");
90 $mainrepo = abs_path($mainrepo);
91 if (-f "$mainrepo/inbox.lock") {
92 if (!defined $version) {
94 } elsif ($version != 2) {
95 die "$mainrepo is a -V2 repo, -V$version specified\n"
97 } elsif (-d "$mainrepo/objects") {
98 if (!defined $version) {
100 } elsif ($version != 1) {
101 die "$mainrepo is a -V1 repo, -V$version specified\n"
105 $version = 1 unless defined $version;
107 if ($version == 1 && defined $skip) {
108 die "--skip is only supported for -V2 repos\n";
111 my $ibx = PublicInbox::Inbox->new({
112 mainrepo => $mainrepo,
115 -primary_address => $address[0],
116 indexlevel => $indexlevel,
120 PublicInbox::InboxWritable->new($ibx, $creat_opt)->init_inbox(0, $skip);
122 # needed for git prior to v2.1.0
123 umask(0077) if defined $perm;
125 foreach my $addr (@address) {
126 next if $seen{lc($addr)};
127 x(@x, "--add", "$pfx.address", $addr);
129 x(@x, "$pfx.url", $http_url);
130 x(@x, "$pfx.mainrepo", $mainrepo);
132 if (defined($indexlevel)) {
133 x(@x, "$pfx.indexlevel", $indexlevel);
136 # needed for git prior to v2.1.0
138 chmod($perm & 07777, $pi_config_tmp) or
139 die "(f)chmod failed on future $pi_config: $!\n";
142 rename $pi_config_tmp, $pi_config or
143 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";