diff options
author | Eric Wong <e@80x24.org> | 2019-01-31 21:08:48 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2019-01-31 21:08:48 +0000 |
commit | 65323f060a3db731bb9fafa004336eeb4bbb8f00 (patch) | |
tree | ceaff52229c942314524f4bcac650e1db175c773 /lib/PublicInbox/Admin.pm | |
parent | ce4fe8f5144f7555ddd42b6a94ec602e042c6e43 (diff) | |
parent | 440b0feaa209e12e4bcb8ef16a95041fce71e7dc (diff) | |
download | public-inbox-65323f060a3db731bb9fafa004336eeb4bbb8f00.tar.gz |
* origin/purge: implement public-inbox-purge tool v2writable: read epoch on purge v2writable: cleanup processes when done v2writable: purge ignores non-existent git epoch directories v2writable: ->purge returns undef on no-op import: purge: reap fast-export process hoist out resolve_repo_dir from -index
Diffstat (limited to 'lib/PublicInbox/Admin.pm')
-rw-r--r-- | lib/PublicInbox/Admin.pm | 44 |
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; |