From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 7A4511FAE2 for ; Thu, 29 Mar 2018 20:17:21 +0000 (UTC) From: "Eric Wong (Contractor, The Linux Foundation)" To: meta@public-inbox.org Subject: [PATCH 2/4] v2writable: initializing an existing inbox is idempotent Date: Thu, 29 Mar 2018 20:17:18 +0000 Message-Id: <20180329201720.7165-3-e@80x24.org> In-Reply-To: <20180329201720.7165-1-e@80x24.org> References: <20180329201720.7165-1-e@80x24.org> List-Id: And we do not want to start making confused repos if somebody leaves out "-V2" the second time around. --- lib/PublicInbox/V2Writable.pm | 1 - script/public-inbox-init | 17 ++++++++++++++++- t/init.t | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index b516278..4e7d6de 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -360,7 +360,6 @@ sub git_init { my ($self, $new) = @_; my $pfx = "$self->{-inbox}->{mainrepo}/git"; my $git_dir = "$pfx/$new.git"; - die "$git_dir exists\n" if -e $git_dir; my @cmd = (qw(git init --bare -q), $git_dir); PublicInbox::Import::run_die(\@cmd); diff --git a/script/public-inbox-init b/script/public-inbox-init index 86cf8b5..d6a6482 100755 --- a/script/public-inbox-init +++ b/script/public-inbox-init @@ -15,7 +15,7 @@ use Cwd qw/abs_path/; sub x { system(@_) and die join(' ', @_). " failed: $?\n" } sub usage { print STDERR "Usage: $usage\n"; exit 1 } -my $version = 1; +my $version = undef; my %opts = ( 'V|version=i' => \$version ); GetOptions(%opts) or usage(); my $name = shift @ARGV or usage(); @@ -71,6 +71,21 @@ my $pfx = "publicinbox.$name"; my @x = (qw/git config/, "--file=$pi_config_tmp"); $mainrepo = abs_path($mainrepo); +if (-f "$mainrepo/inbox.lock") { + if (!defined $version) { + $version = 2; + } elsif ($version != 2) { + die "$mainrepo is a -V2 repo, -V$version specified\n" + } +} elsif (-d "$mainrepo/objects") { + if (!defined $version) { + $version = 1; + } elsif ($version != 1) { + die "$mainrepo is a -V1 repo, -V$version specified\n" + } +} + +$version = 1 unless defined $version; if ($version >= 2) { require PublicInbox::V2Writable; diff --git a/t/init.t b/t/init.t index 6ae608e..59f5481 100644 --- a/t/init.t +++ b/t/init.t @@ -7,6 +7,23 @@ use PublicInbox::Config; use File::Temp qw/tempdir/; my $tmpdir = tempdir('pi-init-XXXXXX', TMPDIR => 1, CLEANUP => 1); use constant pi_init => 'blib/script/public-inbox-init'; +use PublicInbox::Import; +use File::Basename; +open my $null, '>>', '/dev/null'; +my $rdr = { 2 => fileno($null) }; +sub quiet_fail { + my ($cmd, $msg) = @_; + # run_die doesn't take absolute paths: + my $path = $ENV{PATH}; + if (index($cmd->[0], '/') >= 0) { + my ($dir, $base) = ($cmd->[0] =~ m!\A(.+)/([^/]+)\z!); + $path = "$dir:$path"; + $cmd->[0] = $base; + } + local $ENV{PATH} = $path; + eval { PublicInbox::Import::run_die($cmd, undef, $rdr) }; + isnt($@, '', $msg); +} { local $ENV{PI_DIR} = "$tmpdir/.public-inbox/"; @@ -23,6 +40,10 @@ use constant pi_init => 'blib/script/public-inbox-init'; qw(http://example.com/clist clist@example.com)); is(system(@cmd), 0, 'public-inbox-init clist OK'); is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved"); + + @cmd = (pi_init, 'clist', '-V2', "$tmpdir/clist", + qw(http://example.com/clist clist@example.com)); + quiet_fail(\@cmd, 'attempting to init V2 from V1 fails'); } SKIP: { @@ -38,8 +59,15 @@ SKIP: { 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'); - @cmd = (qw(git config), "--file=$tmpdir/v2list/all.git/config", - qw(core.sharedRepository 0644)); + @cmd = (pi_init, 'v2list', "$tmpdir/v2list", + qw(http://example.com/v2list v2list@example.com)); + is(system(@cmd), 0, 'public-inbox-init is idempotent'); + ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects", + 'idempotent invocation w/o -V2 does not make inbox v1'); + + @cmd = (pi_init, 'v2list', "-V1", "$tmpdir/v2list", + qw(http://example.com/v2list v2list@example.com)); + quiet_fail(\@cmd, 'initializing V2 as V1 fails'); } done_testing(); -- EW