Part 1 cleanup of ::Project.
[catagits/Gitalist.git] / lib / Gitalist / Git / Util.pm
CommitLineData
56b6dbe6 1use MooseX::Declare;
2
3class Gitalist::Git::Util {
941bb5a1 4 use File::Which;
5 use Git::PurePerl;
6 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
84f31a44 7 has project => (
8 isa => 'Gitalist::Git::Project',
b5b638f7 9 handles => { gitdir => 'path' },
84f31a44 10 is => 'bare', # No accessor
11 weak_ref => 1, # Weak, you have to hold onto me.
38b9e5c8 12 predicate => 'has_project',
84f31a44 13 );
941bb5a1 14 has _git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
15 sub _build__git {
56b6dbe6 16 my $git = File::Which::which('git');
17
18 if (!$git) {
19 die <<EOR;
20Could not find a git executable.
21Please specify the which git executable to use in gitweb.yml
22EOR
23 }
24
25 return $git;
26 }
27
84f31a44 28 has _gpp => (
29 isa => 'Git::PurePerl', is => 'ro', lazy => 1,
38b9e5c8 30 default => sub {
31 my $self = shift;
32 confess("Cannot get gpp without project")
33 unless $self->has_project;
34 Git::PurePerl->new(gitdir => $self->gitdir);
35 },
84f31a44 36 );
941bb5a1 37
38 method run_cmd (@args) {
38b9e5c8 39 unshift @args, ( '--git-dir' => $self->gitdir )
40 if $self->has_project;
4111e151 41# print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
84f31a44 42
941bb5a1 43 open my $fh, '-|', $self->_git, @args
44 or die "failed to run git command";
45 binmode $fh, ':encoding(UTF-8)';
46
47 my $output = do { local $/ = undef; <$fh> };
48 close $fh;
49
50 return $output;
51 }
4111e151 52
53 method get_gpp_object (NonEmptySimpleStr $sha1) {
54 return $self->_gpp->get_object($sha1) || undef;
55 }
56
56b6dbe6 57} # end class