Merge branch 'master' of git://github.com/bobtfish/Gitalist
[catagits/Gitalist.git] / lib / Gitalist / Git / Util.pm
index 8564cb0..53fdb5b 100644 (file)
@@ -3,7 +3,10 @@ use MooseX::Declare;
 class Gitalist::Git::Util {
     use File::Which;
     use Git::PurePerl;
+    use IPC::Run qw(run);
+    use Symbol qw(geniosym);
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+
     has project => (
         isa => 'Gitalist::Git::Project',
         handles => { gitdir => 'path' },
@@ -25,7 +28,7 @@ EOR
         return $git;
     }
 
-    has _gpp      => (
+    has gpp      => (
         isa => 'Git::PurePerl', is => 'ro', lazy => 1,
         default => sub {
             my $self = shift;
@@ -39,19 +42,44 @@ EOR
         unshift @args, ( '--git-dir' => $self->gitdir )
             if $self->has_project;
 #        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
+        run [$self->_git, @args], \my($in, $out, $err);
 
-        open my $fh, '-|', $self->_git, @args
-            or die "failed to run git command";
-        binmode $fh, ':encoding(UTF-8)';
+        return $out;
+    }
 
-        my $output = do { local $/ = undef; <$fh> };
-        close $fh;
+    method run_cmd_fh (@args) {
+        my ($in, $out, $err) = (geniosym, geniosym, geniosym);
+        unshift @args, ('--git-dir' => $self->gitdir)
+            if $self->has_project;
+#        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
+        run [$self->_git, @args],
+            '<pipe', $in,
+            '>pipe', $out,
+            '2>pipe', $err
+                or die "cmd returned *?";
+        return $out;
+    }
 
-        return $output;
+    method run_cmd_list (@args) {
+        my $cmdout = $self->run_cmd(@args);
+        return $cmdout ? split(/\n/, $cmdout) : ();
     }
 
     method get_gpp_object (NonEmptySimpleStr $sha1) {
-        return $self->_gpp->get_object($sha1) || undef;
+        return $self->gpp->get_object($sha1) || undef;
     }
 
 } # end class
+
+__END__
+
+=head1 AUTHORS
+
+See L<Gitalist> for authors.
+
+=head1 LICENSE
+
+See L<Gitalist> for the license.
+
+=cut
+