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