From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6392F1F751 for ; Sun, 19 Apr 2020 23:19:39 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/4] import: init_bare: allow use as method, use in tests Date: Sun, 19 Apr 2020 23:19:34 +0000 Message-Id: <20200419231937.56365-2-e@yhbt.net> In-Reply-To: <20200419231937.56365-1-e@yhbt.net> References: <20200419231937.56365-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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. --- lib/PublicInbox/Import.pm | 3 ++- lib/PublicInbox/V2Writable.pm | 5 ++--- t/admin.t | 3 ++- t/altid.t | 2 +- t/cgi.t | 3 ++- t/config.t | 3 ++- t/convert-compact.t | 3 +-- t/feed.t | 2 +- t/git.t | 5 +++-- t/html_index.t | 2 +- t/import.t | 3 +-- t/mda.t | 3 ++- t/nulsubject.t | 2 +- t/psgi_attach.t | 2 +- t/psgi_mount.t | 2 +- t/psgi_text.t | 2 +- t/search-thr-index.t | 3 ++- t/solver_git.t | 3 ++- t/v1-add-remove-add.t | 2 +- t/v1reindex.t | 2 +- t/watch_maildir.t | 3 ++- t/watch_maildir_v2.t | 3 ++- t/www_listing.t | 5 +++-- 23 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index c72c1e92..351bc660 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -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: diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index 1c78ef24..ca5f4f1b 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -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); diff --git a/t/admin.t b/t/admin.t index 41aebe46..a9d67d25 100644 --- 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'); diff --git a/t/altid.t b/t/altid.t index 6c34cdd6..3134e627 100644 --- 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 97bdebd9..52f80e88 100644 --- 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"; diff --git a/t/config.t b/t/config.t index d08c3b9c..0db12359 100644 --- a/t/config.t +++ b/t/config.t @@ -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"); diff --git a/t/convert-compact.t b/t/convert-compact.t index 70609c7d..af16b701 100644 --- a/t/convert-compact.t +++ b/t/convert-compact.t @@ -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'); diff --git a/t/feed.t b/t/feed.t index cfa09a7c..ffd5ca7e 100644 --- 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 8224969d..0936ac5e 100644 --- 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: $!"; diff --git a/t/html_index.t b/t/html_index.t index 158a7862..fda3962a 100644 --- a/t/html_index.t +++ b/t/html_index.t @@ -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) { diff --git a/t/import.t b/t/import.t index 703aa362..79af9846 100644 --- a/t/import.t +++ b/t/import.t @@ -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 ec09cf69..af5e1931 100644 --- 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 <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/psgi_attach.t b/t/psgi_attach.t index 297e92e1..a47f3754 100644 --- a/t/psgi_attach.t +++ b/t/psgi_attach.t @@ -21,9 +21,9 @@ my $config = PublicInbox::Config->new(\<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"; diff --git a/t/psgi_mount.t b/t/psgi_mount.t index 8cac3502..3afb1fb5 100644 --- a/t/psgi_mount.t +++ b/t/psgi_mount.t @@ -21,9 +21,9 @@ my $config = PublicInbox::Config->new(\<new($maindir); my $im = PublicInbox::Import->new($git, 'test', $addr); +$im->init_bare; { my $mime = Email::MIME->new(< diff --git a/t/psgi_text.t b/t/psgi_text.t index 6d8a518d..77fc9ee2 100644 --- a/t/psgi_text.t +++ b/t/psgi_text.t @@ -21,7 +21,7 @@ my $config = PublicInbox::Config->new(\<new($config); test_psgi(sub { $www->call(@_) }, sub { diff --git a/t/search-thr-index.t b/t/search-thr-index.t index f073304a..6c564b19 100644 --- a/t/search-thr-index.t +++ b/t/search-thr-index.t @@ -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"); diff --git a/t/solver_git.t b/t/solver_git.t index 7f0cd999..b5231d2c 100644 --- a/t/solver_git.t +++ b/t/solver_git.t @@ -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'; diff --git a/t/v1-add-remove-add.t b/t/v1-add-remove-add.t index 736e414e..fdf06a96 100644 --- a/t/v1-add-remove-add.t +++ b/t/v1-add-remove-add.t @@ -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'); diff --git a/t/v1reindex.t b/t/v1reindex.t index 240e28f9..378c8efb 100644 --- a/t/v1reindex.t +++ b/t/v1reindex.t @@ -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"); diff --git a/t/watch_maildir.t b/t/watch_maildir.t index 5fb908fb..9ed04cab 100644 --- a/t/watch_maildir.t +++ b/t/watch_maildir.t @@ -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 = <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"); }