about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-01-07 08:02:13 +0000
committerEric Wong <e@80x24.org>2019-01-11 03:55:21 +0000
commit34f4c437bff62a3297b2fcebf31fcdb24638dec9 (patch)
tree23aa33826a32c9a4dc1dc3004db91fcf99231ae7 /lib/PublicInbox
parentb0e5062d43a96372801713ef78a78d6a1bc852bc (diff)
downloadpublic-inbox-34f4c437bff62a3297b2fcebf31fcdb24638dec9.tar.gz
We'll be using it in future admin tools, and making this
easier-to-test.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/Admin.pm44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm
new file mode 100644
index 00000000..d0a8dd00
--- /dev/null
+++ b/lib/PublicInbox/Admin.pm
@@ -0,0 +1,44 @@
+# 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::Admin;
+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, $ver) = @_;
+        my $prefix = defined $cd ? $cd : './';
+        if (-d $prefix && -f "$prefix/inbox.lock") { # v2
+                $$ver = 2 if $ver;
+                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;
+                $$ver = 1 if $ver;
+                return abs_path($cd) if ($dir eq '.' && defined $cd);
+                abs_path($dir);
+        }
+}
+
+1;