X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FGitalist%2FGit%2FUtil.pm;h=9638b8c04fa68065ea00e5868ea4b67f8345d0e0;hb=10f3d646dc0d4c48a2d48259a3f81ba840351707;hp=06b6d4ddd93ca67ab5285d0a49adbd321f93cb28;hpb=0617cbd0f82afd2766574793034615eebe16b9ca;p=catagits%2FGitalist.git diff --git a/lib/Gitalist/Git/Util.pm b/lib/Gitalist/Git/Util.pm index 06b6d4d..9638b8c 100644 --- a/lib/Gitalist/Git/Util.pm +++ b/lib/Gitalist/Git/Util.pm @@ -3,9 +3,17 @@ use MooseX::Declare; class Gitalist::Git::Util { use File::Which; use Git::PurePerl; + use IPC::Run qw(run start); + use Symbol qw(geniosym); use MooseX::Types::Common::String qw/NonEmptySimpleStr/; - use MooseX::Types::Moose qw/Str/; - has gitdir => ( isa => Str, is => 'ro', required => 1 ); + + has repository => ( + isa => 'Gitalist::Git::Repository', + handles => { gitdir => 'path' }, + is => 'bare', # No accessor + weak_ref => 1, # Weak, you have to hold onto me. + predicate => 'has_repository', + ); has _git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); sub _build__git { my $git = File::Which::which('git'); @@ -20,29 +28,66 @@ EOR return $git; } - has _gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 ); - method _build__gpp { - my $gpp = Git::PurePerl->new(gitdir => $self->gitdir); - return $gpp; - } + has gpp => ( + isa => 'Git::PurePerl', is => 'ro', lazy => 1, + default => sub { + my $self = shift; + confess("Cannot get gpp without repository") + unless $self->has_repository; + Git::PurePerl->new(gitdir => $self->gitdir); + }, + ); method run_cmd (@args) { - unshift @args, ( '--git-dir' => $self->gitdir ); - print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/; - - open my $fh, '-|', $self->_git, @args - or die "failed to run git command"; - binmode $fh, ':encoding(UTF-8)'; + unshift @args, ( '--git-dir' => $self->gitdir ) + if $self->has_repository; +# print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/; + run [$self->_git, @args], \my($in, $out, $err); + + return $out; + } - my $output = do { local $/ = undef; <$fh> }; - close $fh; + method run_cmd_fh (@args) { + my ($in, $out, $err) = (geniosym, geniosym, geniosym); + unshift @args, ('--git-dir' => $self->gitdir) + if $self->has_repository; +# print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/; + start [$self->_git, @args], + 'pipe', $out, + '2>pipe', $err + or die "cmd returned *?"; + return $out; + } - return $output; + method run_cmd_list (@args) { + my $cmdout = $self->run_cmd(@args); + return $cmdout ? split(/\n/, $cmdout) : (); } + method get_gpp_object (NonEmptySimpleStr $sha1) { + return $self->gpp->get_object($sha1) || undef; + } +} # end class +__END__ +=head1 NAME + +Gitalist::Git::Util - Class for utilities to run git or deal with Git::PurePerl + +=head1 SEE ALSO + +L. + +=head1 AUTHORS + +See L for authors. + +=head1 LICENSE + +See L for the license. + +=cut -# -} # end class