Lose some code by not converting to/from path::class objects, and also by flattening...
[catagits/Gitalist.git] / lib / Gitalist / Git / Util.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Util {
4     use File::Which;
5     use Git::PurePerl;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     has gitdir => ( isa => 'Path::Class::Dir', is => 'ro', required => 1 );
8     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
9     sub _build__git {
10         my $git = File::Which::which('git');
11
12         if (!$git) {
13             die <<EOR;
14 Could not find a git executable.
15 Please specify the which git executable to use in gitweb.yml
16 EOR
17         }
18
19         return $git;
20     }
21
22     has _gpp      => ( isa => 'Git::PurePerl',   is => 'rw', lazy_build => 1 );
23     method _build__gpp {
24         my $gpp = Git::PurePerl->new(gitdir => $self->gitdir);
25         return $gpp;
26     }
27
28     method run_cmd (@args) {
29         unshift @args, ( '--git-dir' => $self->gitdir );
30         print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
31         
32         open my $fh, '-|', $self->_git, @args
33             or die "failed to run git command";
34         binmode $fh, ':encoding(UTF-8)';
35
36         my $output = do { local $/ = undef; <$fh> };
37         close $fh;
38
39         return $output;
40     }
41
42
43
44
45
46 #
47 } # end class