a7865b742e34cf952c93a403afa3128bf2d495af
[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 IPC::Run qw(run);
7     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
8
9     has project => (
10         isa => 'Gitalist::Git::Project',
11         handles => { gitdir => 'path' },
12         is => 'bare', # No accessor
13         weak_ref => 1, # Weak, you have to hold onto me.
14         predicate => 'has_project',
15     );
16     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
17     sub _build__git {
18         my $git = File::Which::which('git');
19
20         if (!$git) {
21             die <<EOR;
22 Could not find a git executable.
23 Please specify the which git executable to use in gitweb.yml
24 EOR
25         }
26
27         return $git;
28     }
29
30     has _gpp      => (
31         isa => 'Git::PurePerl', is => 'ro', lazy => 1,
32         default => sub {
33             my $self = shift;
34             confess("Cannot get gpp without project")
35                 unless $self->has_project;
36             Git::PurePerl->new(gitdir => $self->gitdir);
37         },
38     );
39
40     method run_cmd (@args) {
41         unshift @args, ( '--git-dir' => $self->gitdir )
42             if $self->has_project;
43
44         run [$self->_git, @args], \my($in, $out, $err);
45
46         return $out;
47     }
48
49     method get_gpp_object (NonEmptySimpleStr $sha1) {
50         return $self->_gpp->get_object($sha1) || undef;
51     }
52
53 } # end class