]> Sergey Matveev's repositories - public-inbox.git/commitdiff
v1writable: new wrapper which is closer to v2writable
authorEric Wong <e@80x24.org>
Tue, 14 May 2019 02:04:41 +0000 (02:04 +0000)
committerEric Wong <e@80x24.org>
Tue, 14 May 2019 02:05:55 +0000 (02:05 +0000)
Import initialization is a little strange from history, but we
also can't change it too much because it's technically a public
API which external code may rely on...

And we may need to support v1 repos indefinitely.  This should
make it easier to write tests for both formats.

MANIFEST
lib/PublicInbox/Import.pm
lib/PublicInbox/V1Writable.pm [new file with mode: 0644]
lib/PublicInbox/V2Writable.pm
script/public-inbox-init

index 29cea10ab9367037a9a6c6912221d3baf8dad1b7..28300e0c315291519ed46591e1238de69bb0aaa1 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -125,6 +125,7 @@ lib/PublicInbox/SpawnPP.pm
 lib/PublicInbox/Syscall.pm
 lib/PublicInbox/Unsubscribe.pm
 lib/PublicInbox/UserContent.pm
+lib/PublicInbox/V1Writable.pm
 lib/PublicInbox/V2Writable.pm
 lib/PublicInbox/View.pm
 lib/PublicInbox/ViewDiff.pm
index c775575597722224d322649b82f85b6d9522e533..12abf39995ffbcd1677f94be807828449e06c96c 100644 (file)
@@ -18,12 +18,15 @@ use PublicInbox::MDA;
 use POSIX qw(strftime);
 
 sub new {
+       # we can't change arg order, this is documented in POD
+       # and external projects may rely on it:
        my ($class, $git, $name, $email, $ibx) = @_;
        my $ref = 'refs/heads/master';
        if ($ibx) {
                $ref = $ibx->{ref_head} || 'refs/heads/master';
                $name ||= $ibx->{name};
                $email ||= $ibx->{-primary_address};
+               $git ||= $ibx->git;
        }
        bless {
                git => $git,
@@ -433,6 +436,16 @@ sub run_die ($;$$) {
        $? == 0 or die join(' ', @$cmd) . " failed: $?\n";
 }
 
+sub init_bare {
+       my ($dir) = @_;
+       my @cmd = (qw(git init --bare -q), $dir);
+       run_die(\@cmd);
+       # set a reasonable default:
+       @cmd = (qw/git config/, "--file=$dir/config",
+               'repack.writeBitmaps', 'true');
+       run_die(\@cmd);
+}
+
 sub done {
        my ($self) = @_;
        my $w = delete $self->{out} or return;
@@ -586,7 +599,7 @@ __END__
 
 =head1 NAME
 
-PublicInbox::Import - message importer for public-inbox
+PublicInbox::Import - message importer for public-inbox v1 inboxes
 
 =head1 VERSION
 
diff --git a/lib/PublicInbox/V1Writable.pm b/lib/PublicInbox/V1Writable.pm
new file mode 100644 (file)
index 0000000..6ca5db4
--- /dev/null
@@ -0,0 +1,34 @@
+# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# This interface wraps PublicInbox::Import and makes it closer
+# to V2Writable
+# Used to write to V1 inboxes (see L<public-inbox-v1-format(5)>).
+package PublicInbox::V1Writable;
+use strict;
+use warnings;
+use base qw(PublicInbox::Import);
+use PublicInbox::InboxWritable;
+
+sub new {
+       my ($class, $ibx, $creat) = @_;
+       my $dir = $ibx->{mainrepo} or die "no mainrepo in inbox\n";
+       unless (-d $dir) {
+               if ($creat) {
+                       PublicInbox::Import::init_bare($dir);
+               } else {
+                       die "$dir does not exist\n";
+               }
+       }
+       $ibx = PublicInbox::InboxWritable->new($ibx);
+       $class->SUPER::new(undef, undef, undef, $ibx);
+}
+
+sub init_inbox {
+       my ($self, $partitions, $skip_epoch, $skip_artnum) = @_;
+       # TODO: honor skip_artnum
+       my $dir = $self->{-inbox}->{mainrepo} or die "no mainrepo in inbox\n";
+       PublicInbox::Import::init_bare($dir);
+}
+
+1;
index 87e8f3eb581a97076b5fd22ea5af917bb623306e..b92d8d247b8d0b689e67159911d736b3c2a49289 100644 (file)
@@ -517,11 +517,7 @@ sub fill_alternates ($$) {
        my $all = "$self->{-inbox}->{mainrepo}/all.git";
        my @cmd;
        unless (-d $all) {
-               @cmd = (qw(git init --bare -q), $all);
-               PublicInbox::Import::run_die(\@cmd);
-               @cmd = (qw/git config/, "--file=$all/config",
-                               'repack.writeBitmaps', 'true');
-               PublicInbox::Import::run_die(\@cmd);
+               PublicInbox::Import::init_bare($all);
        }
        @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
                        'include.path', '../../all.git/config');
index 39f7497f3bf1fe6d239e7b2ad5a0067ea8d756ef..8bb7845114feadb3164dc1796e489a7b096febae 100755 (executable)
@@ -8,6 +8,7 @@ use warnings;
 my $usage = "public-inbox-init NAME REPO_DIR HTTP_URL ADDRESS [ADDRESS..]";
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
 use PublicInbox::Config;
+use PublicInbox::Inbox;
 use File::Temp qw/tempfile/;
 use File::Basename qw/dirname/;
 use File::Path qw/mkpath/;
@@ -103,23 +104,19 @@ if ($version == 1 && defined $skip) {
        die "--skip is only supported for -V2 repos\n";
 }
 
+my $ibx = PublicInbox::Inbox->new({
+       mainrepo => $mainrepo,
+       name => $name,
+       version => $version,
+       -primary_address => $address[0],
+});
+
 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);
        PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0, $skip);
 } elsif ($version == 1) {
-       x(qw(git init -q --bare), $mainrepo);
-
-       # set a reasonable default:
-       x(qw/git config/, "--file=$mainrepo/config",
-               'repack.writeBitmaps', 'true');
+       require PublicInbox::V1Writable;
+       PublicInbox::V1Writable->new($ibx, 1)->init_inbox(0, $skip);
 } else {
        die "Unsupported -V/--version: $version\n";
 }