]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Git.pm
fixup Plack-related requires
[public-inbox.git] / lib / PublicInbox / Git.pm
index 5135862e6ad51bc904e880f94175239d59519707..0f92dd9a82f6e70bc5de58661fbfab0bce76bd47 100644 (file)
@@ -11,6 +11,7 @@ use strict;
 use warnings;
 use POSIX qw(dup2);
 require IO::Handle;
+use PublicInbox::Spawn qw(spawn popen_rd);
 
 sub new {
        my ($class, $git_dir) = @_;
@@ -26,15 +27,8 @@ sub _bidi_pipe {
        pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
 
        my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
-       $self->{$pid} = fork;
-       defined $self->{$pid} or fail($self, "fork failed: $!");
-       if ($self->{$pid} == 0) {
-               dup2(fileno($out_r), 0) or die "redirect stdin failed: $!\n";
-               dup2(fileno($in_w), 1) or die "redirect stdout failed: $!\n";
-               exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
-       }
-       close $out_r or fail($self, "close failed: $!");
-       close $in_w or fail($self, "close failed: $!");
+       my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
+       $self->{$pid} = spawn(\@cmd, undef, $redir);
        $out_w->autoflush(1);
        $self->{$out} = $out_w;
        $self->{$in} = $in_r;
@@ -104,13 +98,9 @@ sub check {
 
 sub _destroy {
        my ($self, $in, $out, $pid) = @_;
-       my $p = $self->{$pid} or return;
-       $self->{$pid} = undef;
+       my $p = delete $self->{$pid} or return;
        foreach my $f ($in, $out) {
-               my $fh = $self->{$f};
-               defined $fh or next;
-               close $fh;
-               $self->{$f} = undef;
+               delete $self->{$f};
        }
        waitpid $p, 0;
 }
@@ -123,12 +113,8 @@ sub fail {
 
 sub popen {
        my ($self, @cmd) = @_;
-       my $mode = '-|';
-       $mode = shift @cmd if ($cmd[0] eq '|-');
        @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
-       my $pid = open my $fh, $mode, @cmd or
-               die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
-       $fh;
+       popen_rd(\@cmd);
 }
 
 sub cleanup {