$self->{idx_parts}->[$part];
}
+# idempotent
sub idx_init {
my ($self) = @_;
return if $self->{idx_parts};
use strict;
use warnings;
my $usage = "public-inbox-init NAME GIT_DIR HTTP_URL ADDRESS [ADDRESS..]";
+use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
use PublicInbox::Config;
use File::Temp qw/tempfile/;
use File::Basename qw/dirname/;
sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
sub usage { print STDERR "Usage: $usage\n"; exit 1 }
-
+my $version = 1;
+my %opts = ( 'V|version=i' => \$version );
+GetOptions(%opts) or usage();
my $name = shift @ARGV or usage();
-my $git_dir = shift @ARGV or usage();
+my $mainrepo = shift @ARGV or usage();
my $http_url = shift @ARGV or usage();
my (@address) = @ARGV;
@address or usage();
my $pi_config = PublicInbox::Config->default_file;
my $dir = dirname($pi_config);
mkpath($dir); # will croak on fatal errors
-my ($fh, $filename) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
+my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
if (-e $pi_config) {
open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
my @st = stat($oh);
exit(1) if $conflict;
}
-close $fh or die "failed to close $filename: $!\n";
+close $fh or die "failed to close $pi_config_tmp: $!\n";
my $pfx = "publicinbox.$name";
-my @x = (qw/git config/, "--file=$filename");
-$git_dir = abs_path($git_dir);
-x(qw(git init -q --bare), $git_dir);
+my @x = (qw/git config/, "--file=$pi_config_tmp");
+
+$mainrepo = abs_path($mainrepo);
-# set a reasonable default:
-x(qw/git config/, "--file=$git_dir/config", 'repack.writeBitmaps', 'true');
+if ($version >= 2) {
+ require PublicInbox::V2Writable;
+ require PublicInbox::Inbox;
+ my $ibx = {
+ mainrepo => $mainrepo,
+ name => $name,
+ version => $version,
+ -primary_address => $address[0],
+ };
+ $ibx = PublicInbox::Inbox->new($ibx);
+ my $v2w = PublicInbox::V2Writable->new($ibx, 1);
+ $v2w->git_init(0);
+ $v2w->idx_init(0);
+ $v2w->done;
+} elsif ($version == 1) {
+ x(qw(git init -q --bare), $mainrepo);
+
+ # set a reasonable default:
+ x(qw/git config/, "--file=$mainrepo/config",
+ 'repack.writeBitmaps', 'true');
+} else {
+ die "Unsupported -V/--version: $version\n";
+}
foreach my $addr (@address) {
next if $seen{lc($addr)};
x(@x, "--add", "$pfx.address", $addr);
}
x(@x, "$pfx.url", $http_url);
-x(@x, "$pfx.mainrepo", $git_dir);
+x(@x, "$pfx.mainrepo", $mainrepo);
-rename $filename, $pi_config or
- die "failed to rename `$filename' to `$pi_config': $!\n";
+rename $pi_config_tmp, $pi_config or
+ die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");
}
+SKIP: {
+ foreach my $mod (qw(DBD::SQLite Search::Xapian::WritableDatabase)) {
+ eval "require $mod";
+ skip "$mod missing for v2", 2 if $@;
+ }
+ local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
+ my $cfgfile = "$ENV{PI_DIR}/config";
+ my @cmd = (pi_init, '-V2', 'v2list', "$tmpdir/v2list",
+ qw(http://example.com/v2list v2list@example.com));
+ is(system(@cmd), 0, 'public-inbox-init -V2 OK');
+ ok(-d "$tmpdir/v2list", 'v2list directory exists');
+ ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
+ ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
+}
+
done_testing();
my $home = "$tmpdir/pi-home";
my $err = "$tmpdir/stderr.log";
my $out = "$tmpdir/stdout.log";
-my $maindir = "$tmpdir/main.git";
+my $mainrepo = "$tmpdir/main.git";
my $group = 'test-nntpd';
my $addr = $group . '@example.com';
my $nntpd = 'blib/script/public-inbox-nntpd';
my $init = 'blib/script/public-inbox-init';
use_ok 'PublicInbox::Import';
+use_ok 'PublicInbox::Inbox';
use_ok 'PublicInbox::Git';
+use_ok 'PublicInbox::V2Writable';
+# XXX FIXME: make it easier to test both versions
+my $version = int($ENV{PI_VERSION} || 1);
my %opts = (
LocalAddr => '127.0.0.1',
ReuseAddr => 1,
my $pid;
my $len;
END { kill 'TERM', $pid if defined $pid };
+
+my $ibx = {
+ mainrepo => $mainrepo,
+ name => $group,
+ version => $version,
+ -primary_address => $addr,
+};
+$ibx = PublicInbox::Inbox->new($ibx);
{
local $ENV{HOME} = $home;
- system($init, $group, $maindir, 'http://example.com/', $addr);
+ my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
+ push @cmd, "-V$version";
+ is(system(@cmd), 0, 'init OK');
is(system(qw(git config), "--file=$home/.public-inbox/config",
"publicinbox.$group.newsgroup", $group),
0, 'enabled newsgroup');
my $len;
+ my $im;
+ if ($version == 2) {
+ $im = PublicInbox::V2Writable->new($ibx);
+ } elsif ($version == 1) {
+ my $git = PublicInbox::Git->new($mainrepo);
+ $im = PublicInbox::Import->new($git, 'test', $addr);
+ } else {
+ die "unsupported version: $version";
+ }
+
# ensure successful message delivery
{
my $mime = Email::MIME->new(<<EOF);
$list_id =~ s/@/./;
$mime->header_set('List-Id', "<$list_id>");
$len = length($mime->as_string);
- my $git = PublicInbox::Git->new($maindir);
- my $im = PublicInbox::Import->new($git, 'test', $addr);
$im->add($mime);
$im->done;
- my $s = PublicInbox::SearchIdx->new($maindir, 1);
- $s->index_sync;
+ if ($version == 1) {
+ my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
+ $s->index_sync;
+ }
}
ok($sock, 'sock created');