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