Refactor somewhat
[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 project => (
8         isa => 'Gitalist::Git::Project',
9         handles => { gitdir => 'project_dir' },
10         is => 'bare', # No accessor
11         weak_ref => 1, # Weak, you have to hold onto me.
12     );
13     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
14     sub _build__git {
15         my $git = File::Which::which('git');
16
17         if (!$git) {
18             die <<EOR;
19 Could not find a git executable.
20 Please specify the which git executable to use in gitweb.yml
21 EOR
22         }
23
24         return $git;
25     }
26
27     has _gpp      => (
28         isa => 'Git::PurePerl', is => 'ro', lazy => 1,
29         default => sub { Git::PurePerl->new(gitdir => shift->gitdir) },
30     );
31
32     method run_cmd (@args) {
33         unshift @args, ( '--git-dir' => $self->gitdir );
34         print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
35
36         open my $fh, '-|', $self->_git, @args
37             or die "failed to run git command";
38         binmode $fh, ':encoding(UTF-8)';
39
40         my $output = do { local $/ = undef; <$fh> };
41         close $fh;
42
43         return $output;
44     }
45 } # end class