Move all POD below the code, for ::Repo and ::Project.
[catagits/Gitalist.git] / lib / Gitalist / Git / Util.pm
index d624c4d..53fdb5b 100644 (file)
@@ -3,8 +3,17 @@ 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 gitdir => ( isa => 'Path::Class::Dir', is => 'ro', required => 1 );
+
+    has project => (
+        isa => 'Gitalist::Git::Project',
+        handles => { gitdir => 'path' },
+        is => 'bare', # No accessor
+        weak_ref => 1, # Weak, you have to hold onto me.
+        predicate => 'has_project',
+    );
     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
     sub _build__git {
         my $git = File::Which::which('git');
@@ -19,29 +28,58 @@ EOR
         return $git;
     }
 
-    has _gpp      => ( isa => 'Git::PurePerl',   is => 'rw', lazy_build => 1 );
-    method _build__gpp {
-        my $gpp = Git::PurePerl->new(gitdir => $self->gitdir);
-        return $gpp;
-    }
+    has gpp      => (
+        isa => 'Git::PurePerl', is => 'ro', lazy => 1,
+        default => sub {
+            my $self = shift;
+            confess("Cannot get gpp without project")
+                unless $self->has_project;
+            Git::PurePerl->new(gitdir => $self->gitdir);
+        },
+    );
 
     method run_cmd (@args) {
-        unshift @args, ( '--git-dir' => $self->gitdir );
-        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
-        
-        open my $fh, '-|', $self->_git, @args
-            or die "failed to run git command";
-        binmode $fh, ':encoding(UTF-8)';
+        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);
+
+        return $out;
+    }
+
+    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;
+    }
 
-        my $output = do { local $/ = undef; <$fh> };
-        close $fh;
+    method run_cmd_list (@args) {
+        my $cmdout = $self->run_cmd(@args);
+        return $cmdout ? split(/\n/, $cmdout) : ();
+    }
 
-        return $output;
+    method get_gpp_object (NonEmptySimpleStr $sha1) {
+        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
 
-#
-} # end class