]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-init
lock: show failure path
[public-inbox.git] / script / public-inbox-init
index 34c93b47882c3b52623a4de583197cc3c90f57cd..b8d71f3512d2997736274a23921fe8f1ec91c04d 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Initializes a public-inbox, basically a wrapper for git-init(1)
@@ -16,6 +16,7 @@ use PublicInbox::Admin;
 PublicInbox::Admin::require_or_die('-base');
 use PublicInbox::Config;
 use PublicInbox::InboxWritable;
+use PublicInbox::Import;
 use File::Temp qw/tempfile/;
 use PublicInbox::Lock;
 use File::Basename qw/dirname/;
@@ -23,14 +24,13 @@ use File::Path qw/mkpath/;
 use Fcntl qw(:DEFAULT);
 use Cwd qw/abs_path/;
 
-sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
-my $version = undef;
-my $indexlevel = undef;
-my $skip_epoch;
+my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs);
 my %opts = (
        'V|version=i' => \$version,
        'L|indexlevel=s' => \$indexlevel,
        'S|skip|skip-epoch=i' => \$skip_epoch,
+       'N|skip-artnum=i' => \$skip_artnum,
+       'j|jobs=i' => \$jobs,
 );
 GetOptions(%opts) or usage();
 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
@@ -53,6 +53,7 @@ PublicInbox::Lock::lock_acquire($lock_obj);
 
 # git-config will operate on this (and rename on success):
 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
+my $cfg_tmp = UnlinkMe->new($pi_config_tmp);
 
 # Now, we grab another lock to use git-config(1) locking, so it won't
 # wait on the lock, unlike some of our internal flock()-based locks.
@@ -115,24 +116,25 @@ my $pfx = "publicinbox.$name";
 my @x = (qw/git config/, "--file=$pi_config_tmp");
 
 $inboxdir = abs_path($inboxdir);
+die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
 if (-f "$inboxdir/inbox.lock") {
        if (!defined $version) {
                $version = 2;
        } elsif ($version != 2) {
-               die "$inboxdir is a -V2 repo, -V$version specified\n"
+               die "$inboxdir is a -V2 inbox, -V$version specified\n"
        }
 } elsif (-d "$inboxdir/objects") {
        if (!defined $version) {
                $version = 1;
        } elsif ($version != 1) {
-               die "$inboxdir is a -V1 repo, -V$version specified\n"
+               die "$inboxdir is a -V1 inbox, -V$version specified\n"
        }
 }
 
 $version = 1 unless defined $version;
 
 if ($version == 1 && defined $skip_epoch) {
-       die "--skip-epoch is only supported for -V2 repos\n";
+       die "--skip-epoch is only supported for -V2 inboxes\n";
 }
 
 my $ibx = PublicInbox::Inbox->new({
@@ -144,20 +146,27 @@ my $ibx = PublicInbox::Inbox->new({
 });
 
 my $creat_opt = {};
-PublicInbox::InboxWritable->new($ibx, $creat_opt)->init_inbox(0, $skip_epoch);
+if (defined $jobs) {
+       die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
+       die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
+       $creat_opt->{nproc} = $jobs;
+}
+
+$ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
+$ibx->init_inbox(0, $skip_epoch, $skip_artnum);
 
 # needed for git prior to v2.1.0
 umask(0077) if defined $perm;
 
 foreach my $addr (@address) {
        next if $seen{lc($addr)};
-       x(@x, "--add", "$pfx.address", $addr);
+       PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
 }
-x(@x, "$pfx.url", $http_url);
-x(@x, "$pfx.inboxdir", $inboxdir);
+PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
+PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
 
 if (defined($indexlevel)) {
-       x(@x, "$pfx.indexlevel", $indexlevel);
+       PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
 }
 
 # needed for git prior to v2.1.0
@@ -168,6 +177,7 @@ if (defined $perm) {
 
 rename $pi_config_tmp, $pi_config or
        die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
+delete $cfg_tmp->{file};
 $auto_unlink->DESTROY;
 
 package UnlinkMe;