From: "Eric Wong (Contractor, The Linux Foundation)" <e@80x24.org>
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 [thread overview]
Message-ID: <20180329201720.7165-3-e@80x24.org> (raw)
In-Reply-To: <20180329201720.7165-1-e@80x24.org>
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
next prev parent reply other threads:[~2018-03-29 20:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-29 20:17 [PATCH 0/4] tooling updates for v2 Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` [PATCH 1/4] import: run_die supports redirects as spawn does Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` Eric Wong (Contractor, The Linux Foundation) [this message]
2018-03-29 20:17 ` [PATCH 3/4] public-inbox-compact: new tool for driving xapian-compact Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` [PATCH 4/4] mda: support v2 inboxes Eric Wong (Contractor, The Linux Foundation)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180329201720.7165-3-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).