From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 1/2] hoist out resolve_repo_dir from -index
Date: Mon, 7 Jan 2019 08:02:13 +0000 [thread overview]
Message-ID: <20190107080214.462-1-e@80x24.org> (raw)
We'll be using it in future tools, and making this
easier-to-test.
---
MANIFEST | 1 +
lib/PublicInbox/Tool.pm | 42 ++++++++++++++++++++++++++++++++++++++++++
script/public-inbox-index | 32 ++------------------------------
3 files changed, 45 insertions(+), 30 deletions(-)
create mode 100644 lib/PublicInbox/Tool.pm
diff --git a/MANIFEST b/MANIFEST
index e4f3df8..dafc77a 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -105,6 +105,7 @@ lib/PublicInbox/Spamcheck.pm
lib/PublicInbox/Spamcheck/Spamc.pm
lib/PublicInbox/Spawn.pm
lib/PublicInbox/SpawnPP.pm
+lib/PublicInbox/Tool.pm
lib/PublicInbox/Unsubscribe.pm
lib/PublicInbox/V2Writable.pm
lib/PublicInbox/View.pm
diff --git a/lib/PublicInbox/Tool.pm b/lib/PublicInbox/Tool.pm
new file mode 100644
index 0000000..ac167be
--- /dev/null
+++ b/lib/PublicInbox/Tool.pm
@@ -0,0 +1,42 @@
+# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# common stuff for administrative command-line tools
+# Unstable internal API
+package PublicInbox::Tool;
+use strict;
+use warnings;
+use Cwd 'abs_path';
+use base qw(Exporter);
+our @EXPORT_OK = qw(resolve_repo_dir);
+
+sub resolve_repo_dir {
+ my ($cd) = @_;
+ my $prefix = defined $cd ? $cd : './';
+ if (-d $prefix && -f "$prefix/inbox.lock") { # v2
+ return abs_path($prefix);
+ }
+
+ my @cmd = qw(git rev-parse --git-dir);
+ my $cmd = join(' ', @cmd);
+ my $pid = open my $fh, '-|';
+ defined $pid or die "forking $cmd failed: $!\n";
+ if ($pid == 0) {
+ if (defined $cd) {
+ chdir $cd or die "chdir $cd failed: $!\n";
+ }
+ exec @cmd;
+ die "Failed to exec $cmd: $!\n";
+ } else {
+ my $dir = eval {
+ local $/;
+ <$fh>;
+ };
+ close $fh or die "error in $cmd: $!\n";
+ chomp $dir;
+ return abs_path($cd) if ($dir eq '.' && defined $cd);
+ abs_path($dir);
+ }
+}
+
+1;
diff --git a/script/public-inbox-index b/script/public-inbox-index
index 73ad9bc..2f10d7e 100755
--- a/script/public-inbox-index
+++ b/script/public-inbox-index
@@ -9,9 +9,10 @@
use strict;
use warnings;
use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
-use Cwd 'abs_path';
my $usage = "public-inbox-index REPO_DIR";
use PublicInbox::Config;
+use PublicInbox::Tool qw(resolve_repo_dir);
+
my $config = eval { PublicInbox::Config->new } || eval {
warn "public-inbox unconfigured for serving, indexing anyways...\n";
{}
@@ -35,35 +36,6 @@ die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
my @dirs;
-sub resolve_repo_dir {
- my ($cd) = @_;
- my $prefix = defined $cd ? $cd : './';
- if (-d $prefix && -f "$prefix/inbox.lock") { # v2
- return abs_path($prefix);
- }
-
- my @cmd = qw(git rev-parse --git-dir);
- my $cmd = join(' ', @cmd);
- my $pid = open my $fh, '-|';
- defined $pid or die "forking $cmd failed: $!\n";
- if ($pid == 0) {
- if (defined $cd) {
- chdir $cd or die "chdir $cd failed: $!\n";
- }
- exec @cmd;
- die "Failed to exec $cmd: $!\n";
- } else {
- my $dir = eval {
- local $/;
- <$fh>;
- };
- close $fh or die "error in $cmd: $!\n";
- chomp $dir;
- return abs_path($cd) if ($dir eq '.' && defined $cd);
- abs_path($dir);
- }
-}
-
if (@ARGV) {
@dirs = map { resolve_repo_dir($_) } @ARGV;
} else {
--
EW
next reply other threads:[~2019-01-07 8:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-07 8:02 Eric Wong [this message]
2019-01-07 8:02 ` [PATCH 2/2] wip Eric Wong
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190107080214.462-1-e@80x24.org \
--to=e@80x24.org \
--cc=spew@80x24.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).