Use anon symbols for safety / reentrancy, no idea if this works :)
[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 Symbol qw(geniosym);
8     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
9
10     has project => (
11         isa => 'Gitalist::Git::Project',
12         handles => { gitdir => 'path' },
13         is => 'bare', # No accessor
14         weak_ref => 1, # Weak, you have to hold onto me.
15         predicate => 'has_project',
16     );
17     has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
18     sub _build__git {
19         my $git = File::Which::which('git');
20
21         if (!$git) {
22             die <<EOR;
23 Could not find a git executable.
24 Please specify the which git executable to use in gitweb.yml
25 EOR
26         }
27
28         return $git;
29     }
30
31     has gpp      => (
32         isa => 'Git::PurePerl', is => 'ro', lazy => 1,
33         default => sub {
34             my $self = shift;
35             confess("Cannot get gpp without project")
36                 unless $self->has_project;
37             Git::PurePerl->new(gitdir => $self->gitdir);
38         },
39     );
40
41     method run_cmd (@args) {
42         unshift @args, ( '--git-dir' => $self->gitdir )
43             if $self->has_project;
44 #        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
45         run [$self->_git, @args], \my($in, $out, $err);
46
47         return $out;
48     }
49
50     method run_cmd_fh (@args) {
51         my ($out, $err) = (geniosym, geniosym);
52         unshift @args, ('--git-dir' => $self->gitdir)
53             if $self->has_project;
54         run [$self->_git, @args],
55             undef,
56             '>pipe', $out,
57             '2>pipe', $err
58                 or die "cmd returned *?";
59         return ($out, $err);
60     }
61
62     method run_cmd_list (@args) {
63         my $cmdout = $self->run_cmd(@args);
64         return $cmdout ? split(/\n/, $cmdout) : ();
65     }
66
67     method get_gpp_object (NonEmptySimpleStr $sha1) {
68         return $self->gpp->get_object($sha1) || undef;
69     }
70
71 } # end class