]> Sergey Matveev's repositories - public-inbox.git/commitdiff
import: init_bare: allow use as method, use in tests
authorEric Wong <e@yhbt.net>
Sun, 19 Apr 2020 23:19:34 +0000 (23:19 +0000)
committerEric Wong <e@yhbt.net>
Mon, 20 Apr 2020 20:18:16 +0000 (20:18 +0000)
Allowing ->init_bare to be used as a method saves some
keystrokes, and we can save a little bit of time on systems with
our vfork(2)-enabled spawn().

This also sets us up for future improvements where we can
avoid spawning a process at all.

23 files changed:
lib/PublicInbox/Import.pm
lib/PublicInbox/V2Writable.pm
t/admin.t
t/altid.t
t/cgi.t
t/config.t
t/convert-compact.t
t/feed.t
t/git.t
t/html_index.t
t/import.t
t/mda.t
t/nulsubject.t
t/psgi_attach.t
t/psgi_mount.t
t/psgi_text.t
t/search-thr-index.t
t/solver_git.t
t/v1-add-remove-add.t
t/v1reindex.t
t/watch_maildir.t
t/watch_maildir_v2.t
t/www_listing.t

index c72c1e9224990047e9f4c3a8b88850655a5348fe..351bc660e19b862424e474de545beba50d26719b 100644 (file)
@@ -441,7 +441,8 @@ sub run_die ($;$$) {
 }
 
 sub init_bare {
-       my ($dir) = @_;
+       my ($dir) = @_; # or self
+       $dir = $dir->{git}->{git_dir} if ref($dir);
        my @cmd = (qw(git init --bare -q), $dir);
        run_die(\@cmd);
        # set a reasonable default:
index 1c78ef24f2116ff1e7bc0135df5b0dbe70a84738..ca5f4f1bb5d32167c2f7b3e0f8d90a33887488e0 100644 (file)
@@ -730,9 +730,8 @@ sub fill_alternates ($$) {
 sub git_init {
        my ($self, $epoch) = @_;
        my $git_dir = "$self->{-inbox}->{inboxdir}/git/$epoch.git";
-       my @cmd = (qw(git init --bare -q), $git_dir);
-       PublicInbox::Import::run_die(\@cmd);
-       @cmd = (qw/git config/, "--file=$git_dir/config",
+       PublicInbox::Import::init_bare($git_dir);
+       my @cmd = (qw/git config/, "--file=$git_dir/config",
                        'include.path', '../../all.git/config');
        PublicInbox::Import::run_die(\@cmd);
        fill_alternates($self, $epoch);
index 41aebe46b3d61448f6a11e0b98f2dd4db26340d7..a9d67d258ed2f9a81590e35ad7b97aa9da6283b3 100644 (file)
--- a/t/admin.t
+++ b/t/admin.t
@@ -4,13 +4,14 @@ use strict;
 use warnings;
 use Test::More;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 use_ok 'PublicInbox::Admin', qw(resolve_repo_dir);
 my ($tmpdir, $for_destroy) = tmpdir();
 my $git_dir = "$tmpdir/v1";
 my $v2_dir = "$tmpdir/v2";
 my ($res, $err, $v);
 
-is(0, system(qw(git init -q --bare), $git_dir), 'git init v1');
+PublicInbox::Import::init_bare($git_dir);
 
 # v1
 is(resolve_repo_dir($git_dir), $git_dir, 'top-level GIT_DIR resolved');
index 6c34cdd65bbddcdd7b22722159c00541be2419b1..3134e6276b54fea21cd943c7210e03e482ae2118 100644 (file)
--- a/t/altid.t
+++ b/t/altid.t
@@ -23,9 +23,9 @@ my $ibx;
 }
 
 {
-       is(system(qw(git init -q --bare), $git_dir), 0, 'git init ok');
        my $git = PublicInbox::Git->new($git_dir);
        my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
+       $im->init_bare;
        $im->add(Email::MIME->create(
                header => [
                        From => 'a@example.com',
diff --git a/t/cgi.t b/t/cgi.t
index 97bdebd982a191e5cd96ec83620f097af7f721ae..52f80e88e2fbd1545ed105b806eb6a5ec4fa72e1 100644 (file)
--- a/t/cgi.t
+++ b/t/cgi.t
@@ -7,6 +7,7 @@ use warnings;
 use Test::More;
 use Email::MIME;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 require_mods(qw(Plack::Handler::CGI Plack::Util));
 my ($tmpdir, $for_destroy) = tmpdir();
 my $home = "$tmpdir/pi-home";
@@ -18,7 +19,7 @@ my $addr = 'test-public@example.com';
 {
        is(1, mkdir($home, 0755), "setup ~/ for testing");
        is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
-       is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
+       PublicInbox::Import::init_bare($maindir);
 
        open my $fh, '>', "$maindir/description" or die "open: $!\n";
        print $fh "test for public-inbox\n";
index d08c3b9c03454bf6251ecab45083a3c4722842df..0db12359b5a29c7370ec190fc2413fa441531d2f 100644 (file)
@@ -5,10 +5,11 @@ use warnings;
 use Test::More;
 use PublicInbox::Config;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 my ($tmpdir, $for_destroy) = tmpdir();
 
 {
-       is(system(qw(git init -q --bare), $tmpdir), 0, "git init successful");
+       PublicInbox::Import::init_bare($tmpdir);
        my @cmd = ('git', "--git-dir=$tmpdir", qw(config foo.bar), "hi\nhi");
        is(system(@cmd), 0, "set config");
 
index 70609c7d49743d206d6c1c899e47e794b80fe494..af16b70143244cba7bf2b1eb2f642d19ef5271e3 100644 (file)
@@ -20,8 +20,7 @@ my $ibx = {
        -primary_address => 'test@example.com',
 };
 
-ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{inboxdir}]),
-       'initialized v1 repo');
+PublicInbox::Import::init_bare($ibx->{inboxdir});
 ok(umask(077), 'set restrictive umask');
 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{inboxdir}",
        qw(config core.sharedRepository 0644)]), 'set sharedRepository');
index cfa09a7c68eae9549668c40b07589d8b20d6c3c4..ffd5ca7e71f86dc9a37ba5d1865dc38e11619f5a 100644 (file)
--- a/t/feed.t
+++ b/t/feed.t
@@ -34,7 +34,7 @@ my $git = $ibx->git;
 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
 
 {
-       is(0, system(qw(git init -q --bare), $git_dir), "git init");
+       $im->init_bare;
        local $ENV{GIT_DIR} = $git_dir;
 
        foreach my $i (1..6) {
diff --git a/t/git.t b/t/git.t
index 8224969d0661fb7d6707a39ae779613ca38cf5b1..0936ac5ea8d1420641afd00c4586cd95ceaddc0a 100644 (file)
--- a/t/git.t
+++ b/t/git.t
@@ -6,11 +6,12 @@ use Test::More;
 use PublicInbox::TestCommon;
 my ($dir, $for_destroy) = tmpdir();
 use PublicInbox::Spawn qw(popen_rd);
+use PublicInbox::Import;
 
 use_ok 'PublicInbox::Git';
 
 {
-       is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
+       PublicInbox::Import::init_bare($dir);
        my $fi_data = './t/git.fast-import-data';
        ok(-r $fi_data, "fast-import data readable (or run test at top level)");
        local $ENV{GIT_DIR} = $dir;
@@ -90,7 +91,7 @@ if (1) {
 if ('alternates reloaded') {
        my ($alt, $alt_obj) = tmpdir();
        my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
-       is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
+       PublicInbox::Import::init_bare($alt);
        open my $fh, '<', "$alt/config" or die "open failed: $!\n";
        my $rd = popen_rd(\@cmd, {}, { 0 => $fh } );
        close $fh or die "close failed: $!";
index 158a7862cfdd07abc9828ff2fe6e60d7bc5c0657..fda3962af646dfd52587eca56296e925637ac291 100644 (file)
@@ -22,7 +22,7 @@ my $im = PublicInbox::Import->new($git, 'tester', 'test@example');
 
 # setup
 {
-       is(0, system(qw(git init -q --bare), $git_dir), "git init");
+       $im->init_bare;
        my $prev = "";
 
        foreach my $i (1..6) {
index 703aa36286a2726fef33e3ecaada88b0b59c2139..79af9846dce1eaed486b124fd03211615841073d 100644 (file)
@@ -12,10 +12,9 @@ use File::Temp qw/tempfile/;
 use PublicInbox::TestCommon;
 my ($dir, $for_destroy) = tmpdir();
 
-is(system(qw(git init -q --bare), $dir), 0, 'git init successful');
 my $git = PublicInbox::Git->new($dir);
-
 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
+$im->init_bare;
 my $mime = PublicInbox::MIME->create(
        header => [
                From => 'a@example.com',
diff --git a/t/mda.t b/t/mda.t
index ec09cf69bb6a6e1a229b19aed2e15f3449d781d0..af5e19319ed82cbbc30b687a0a6b92036db261eb 100644 (file)
--- a/t/mda.t
+++ b/t/mda.t
@@ -9,6 +9,7 @@ use PublicInbox::MID qw(mid2path);
 use PublicInbox::Git;
 use PublicInbox::InboxWritable;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $home = "$tmpdir/pi-home";
 my $pi_home = "$home/.public-inbox";
@@ -48,7 +49,7 @@ my $fail_bad_header = sub ($$$) {
                "spamc mock found (run in top of source tree");
        is(1, mkdir($home, 0755), "setup ~/ for testing");
        is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
-       is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
+       PublicInbox::Import::init_bare($maindir);
 
        open my $fh, '>>', $pi_config or die;
        print $fh <<EOF or die;
index fec6c1aee441ab5ee278c9b6ab5939856b78385b..b21507c2ec1be4d16053c4550dac126c9c8e6e0e 100644 (file)
@@ -11,9 +11,9 @@ my ($tmpdir, $for_destroy) = tmpdir();
 my $git_dir = "$tmpdir/a.git";
 
 {
-       is(system(qw(git init -q --bare), $git_dir), 0, 'git init ok');
        my $git = PublicInbox::Git->new($git_dir);
        my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
+       $im->init_bare;
        $im->add(Email::MIME->create(
                header => [
                        From => 'a@example.com',
index 297e92e1a4fb2107affc425dedf667cbc7fef9c3..a47f37540215a8efabca2d9254a114df5442f016 100644 (file)
@@ -21,9 +21,9 @@ my $config = PublicInbox::Config->new(\<<EOF);
 $cfgpfx.address=$addr
 $cfgpfx.inboxdir=$maindir
 EOF
-is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
 my $git = PublicInbox::Git->new($maindir);
 my $im = PublicInbox::Import->new($git, 'test', $addr);
+$im->init_bare;
 
 {
        open my $fh, '<', '/dev/urandom' or die "unable to open urandom: $!\n";
index 8cac3502310c75a78930bdb910e2286f39faed14..3afb1fb59e7251850570729a9c9e9b601983ea6b 100644 (file)
@@ -21,9 +21,9 @@ my $config = PublicInbox::Config->new(\<<EOF);
 $cfgpfx.address=$addr
 $cfgpfx.inboxdir=$maindir
 EOF
-is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
 my $git = PublicInbox::Git->new($maindir);
 my $im = PublicInbox::Import->new($git, 'test', $addr);
+$im->init_bare;
 {
        my $mime = Email::MIME->new(<<EOF);
 From: Me <me\@example.com>
index 6d8a518df9003fa21fd8a0ff06b556e2d1f25ad9..77fc9ee2dac45dd4b1782f962d803d345876e0cf 100644 (file)
@@ -21,7 +21,7 @@ my $config = PublicInbox::Config->new(\<<EOF);
 $cfgpfx.address=$addr
 $cfgpfx.inboxdir=$maindir
 EOF
-is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
+PublicInbox::Import::init_bare($maindir);
 my $www = PublicInbox::WWW->new($config);
 
 test_psgi(sub { $www->call(@_) }, sub {
index f073304a2a5bc1869255af9f180095e500a49ef4..6c564b19c0905b07687b8072c586652f5cd78b2f 100644 (file)
@@ -11,10 +11,11 @@ require_mods(qw(DBD::SQLite Search::Xapian));
 require PublicInbox::SearchIdx;
 require PublicInbox::Smsg;
 require PublicInbox::Inbox;
+use PublicInbox::Import;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $git_dir = "$tmpdir/a.git";
 
-is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
+PublicInbox::Import::init_bare($git_dir);
 my $ibx = PublicInbox::Inbox->new({inboxdir => $git_dir});
 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
 ok($rw, "search indexer created");
index 7f0cd999344dc52da63d00c86a4b1d2d27a48c46..b5231d2cbc30749e55726bbaff647ef42be6507d 100644 (file)
@@ -109,7 +109,8 @@ SKIP: {
        require_mods(@psgi, 7 + scalar(@psgi));
        use_ok($_) for @psgi;
        my $binfoo = "$inboxdir/binfoo.git";
-       system(qw(git init --bare -q), $binfoo) == 0 or die "git init: $?";
+       require PublicInbox::Import;
+       PublicInbox::Import::init_bare($binfoo);
        require_ok 'PublicInbox::ViewVCS';
        my $big_size = do {
                no warnings 'once';
index 736e414e354045b09bf97a5c1d106e6b052d4c9f..fdf06a96e764dc98c828dc4aef33da0de0a75ea7 100644 (file)
@@ -9,7 +9,6 @@ use PublicInbox::TestCommon;
 require_mods(qw(DBD::SQLite Search::Xapian));
 require PublicInbox::SearchIdx;
 my ($inboxdir, $for_destroy) = tmpdir();
-is(system(qw(git init --bare -q), $inboxdir), 0);
 my $ibx = {
        inboxdir => $inboxdir,
        name => 'test-add-remove-add',
@@ -27,6 +26,7 @@ my $mime = PublicInbox::MIME->create(
        body => "hello world\n",
 );
 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
+$im->init_bare;
 ok($im->add($mime), 'message added');
 ok($im->remove($mime), 'message removed');
 ok($im->add($mime), 'message added again');
index 240e28f92d6cd16c486bd731cf98c5cae10ac8be..378c8efb469baa852f34e7b58eef1be67424237e 100644 (file)
@@ -12,7 +12,6 @@ require_mods(qw(DBD::SQLite Search::Xapian));
 use_ok 'PublicInbox::SearchIdx';
 use_ok 'PublicInbox::Import';
 my ($inboxdir, $for_destroy) = tmpdir();
-is(system(qw(git init -q --bare), $inboxdir), 0);
 my $ibx_config = {
        inboxdir => $inboxdir,
        name => 'test-v1reindex',
@@ -35,6 +34,7 @@ my ($mark1, $mark2, $mark3, $mark4);
        my %config = %$ibx_config;
        my $ibx = PublicInbox::Inbox->new(\%config);
        my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
+       $im->init_bare;
        foreach my $i (1..10) {
                $mime->header_set('Message-Id', "<$i\@example.com>");
                ok($im->add($mime), "message $i added");
index 5fb908fb619bd6eb64ef58d5e63ecaaac07988e8..9ed04cab14138ca258e9b6bb325ef0e810f94a44 100644 (file)
@@ -6,6 +6,7 @@ use Email::MIME;
 use Cwd;
 use PublicInbox::Config;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 require_mods(qw(Filesys::Notify::Simple));
 my ($tmpdir, $for_destroy) = tmpdir();
 my $git_dir = "$tmpdir/test.git";
@@ -15,7 +16,7 @@ use_ok 'PublicInbox::WatchMaildir';
 use_ok 'PublicInbox::Emergency';
 my $cfgpfx = "publicinbox.test";
 my $addr = 'test-public@example.com';
-is(system(qw(git init -q --bare), $git_dir), 0, 'initialized git dir');
+PublicInbox::Import::init_bare($git_dir);
 
 my $msg = <<EOF;
 From: user\@example.com
index 685cd6edc2644c50e276fd46ed2714d3f3b01575..dd5030ea0e89bfc4c0311f03e42212c43bfa8763 100644 (file)
@@ -6,6 +6,7 @@ use PublicInbox::MIME;
 use Cwd;
 use PublicInbox::Config;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 require_git(2.6);
 require_mods(qw(Search::Xapian DBD::SQLite Filesys::Notify::Simple));
 require PublicInbox::V2Writable;
@@ -141,7 +142,7 @@ More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
        my $v1repo = "$tmpdir/v1";
        my $v1pfx = "publicinbox.v1";
        my $v1addr = 'v1-public@example.com';
-       is(system(qw(git init -q --bare), $v1repo), 0, 'v1 init OK');
+       PublicInbox::Import::init_bare($v1repo);
        my $cfg2 = <<EOF;
 $orig$v1pfx.address=$v1addr
 $v1pfx.inboxdir=$v1repo
index 9230329ccd79650c4165d6623351c99142efd638..c132edbc00b3bf1d34c9ff0ee05998efbf8fed37 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 use Test::More;
 use PublicInbox::Spawn qw(which);
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 require_mods(qw(URI::Escape Plack::Builder Digest::SHA
                IO::Compress::Gzip IO::Uncompress::Gunzip HTTP::Tiny));
 require PublicInbox::WwwListing;
@@ -18,7 +19,7 @@ use_ok 'PublicInbox::Git';
 
 my ($tmpdir, $for_destroy) = tmpdir();
 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
-is(system(qw(git init -q --bare), $bare->{git_dir}), 0, 'git init --bare');
+PublicInbox::Import::init_bare($bare->{git_dir});
 is(PublicInbox::WwwListing::fingerprint($bare), undef,
        'empty repo has no fingerprint');
 {
@@ -77,7 +78,7 @@ SKIP: {
        my @clone = qw(git clone -q -s --bare);
        is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
 
-       system(qw(git init --bare -q), "$v2/all.git") == 0 or die;
+       PublicInbox::Import::init_bare("$v2/all.git");
        for my $i (0..2) {
                is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
        }