Rename ::Project to ::Repository
[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;
839da3d7 6 use IPC::Run qw(run);
cdcdec92 7 use Symbol qw(geniosym);
941bb5a1 8 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
839da3d7 9
84f31a44 10 has project => (
44a9ed75 11 isa => 'Gitalist::Git::Repository',
b5b638f7 12 handles => { gitdir => 'path' },
84f31a44 13 is => 'bare', # No accessor
14 weak_ref => 1, # Weak, you have to hold onto me.
38b9e5c8 15 predicate => 'has_project',
84f31a44 16 );
941bb5a1 17 has _git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
18 sub _build__git {
56b6dbe6 19 my $git = File::Which::which('git');
20
21 if (!$git) {
22 die <<EOR;
23Could not find a git executable.
24Please specify the which git executable to use in gitweb.yml
25EOR
26 }
27
28 return $git;
29 }
30
defb0050 31 has gpp => (
84f31a44 32 isa => 'Git::PurePerl', is => 'ro', lazy => 1,
38b9e5c8 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 },
84f31a44 39 );
941bb5a1 40
41 method run_cmd (@args) {
38b9e5c8 42 unshift @args, ( '--git-dir' => $self->gitdir )
43 if $self->has_project;
32a371cc 44# print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
839da3d7 45 run [$self->_git, @args], \my($in, $out, $err);
941bb5a1 46
839da3d7 47 return $out;
941bb5a1 48 }
4111e151 49
aa7f1f92 50 method run_cmd_fh (@args) {
30db8f5b 51 my ($in, $out, $err) = (geniosym, geniosym, geniosym);
aa7f1f92 52 unshift @args, ('--git-dir' => $self->gitdir)
53 if $self->has_project;
30db8f5b 54# print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
aa7f1f92 55 run [$self->_git, @args],
30db8f5b 56 '<pipe', $in,
cdcdec92 57 '>pipe', $out,
58 '2>pipe', $err
aa7f1f92 59 or die "cmd returned *?";
5156786b 60 return $out;
aa7f1f92 61 }
62
32a371cc 63 method run_cmd_list (@args) {
64 my $cmdout = $self->run_cmd(@args);
65 return $cmdout ? split(/\n/, $cmdout) : ();
66 }
67
4111e151 68 method get_gpp_object (NonEmptySimpleStr $sha1) {
defb0050 69 return $self->gpp->get_object($sha1) || undef;
4111e151 70 }
71
56b6dbe6 72} # end class
775e96e0 73
74__END__
75
76=head1 AUTHORS
77
78See L<Gitalist> for authors.
79
80=head1 LICENSE
81
82See L<Gitalist> for the license.
83
84=cut
85