Move nuts and bolts of git access out of the Project class.
Zachary Stevens [Sun, 1 Nov 2009 11:29:39 +0000 (11:29 +0000)]
lib/Gitalist/Git/Project.pm
lib/Gitalist/Git/Util.pm
t/git/project.t
t/git/util.t [new file with mode: 0644]

index 5465177..d5671b6 100644 (file)
@@ -5,6 +5,7 @@ class Gitalist::Git::Project {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use DateTime;
     use Path::Class;
+    use Gitalist::Git::Util;
 
     has name => ( isa => NonEmptySimpleStr,
                   is => 'ro' );
@@ -23,7 +24,18 @@ class Gitalist::Git::Project {
                          is => 'ro',
                          lazy_build => 1,
                      );
+    has _util => ( isa => 'Gitalist::Git::Util',
+                   is => 'ro',
+                   lazy_build => 1,
+                   handles => [ 'run_cmd' ],
+               );
 
+    method _build__util {
+        my $util = Gitalist::Git::Util->new(
+            gitdir => $self->path,
+        );
+        return $util;
+    }
     
     method _build_description {
         my $description = $self->path->file('description')->slurp;
@@ -51,48 +63,6 @@ class Gitalist::Git::Project {
         return $last_change;
     }
 
-
-=head2 run_cmd
-
-Call out to the C<git> binary and return a string consisting of the output.
-
-=cut
-
-        method run_cmd (@args) {
-            unshift @args, ( '--git-dir' => $self->path );
-            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)';
-
-            my $output = do { local $/ = undef; <$fh> };
-            close $fh;
-
-            return $output;
-        }
-
-    has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
-    use File::Which;
-    method _build__git {
-        my $git = File::Which::which('git');
-
-        if (!$git) {
-            die <<EOR;
-Could not find a git executable.
-Please specify the which git executable to use in gitweb.yml
-EOR
-        }
-
-        return $git;
-    }
-    has _gpp      => ( isa => 'Git::PurePerl',   is => 'rw', lazy_build => 1 );
-    use Git::PurePerl;
-    method _build__gpp {
-        my $gpp = Git::PurePerl->new(gitdir => $self->path);
-        return $gpp;
-    }
-
     method project_dir (Path::Class::Dir $project) {
         my $dir = $project->stringify;
         $dir .= '/.git'
@@ -100,7 +70,6 @@ EOR
         return $dir;
     }
 
-    
     # Compatibility
 
 =head2 project_info
index 8f7dd0f..c3970fb 100644 (file)
@@ -1,8 +1,12 @@
 use MooseX::Declare;
 
 class Gitalist::Git::Util {
-    has git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
-    sub _build_git {
+    use File::Which;
+    use Git::PurePerl;
+    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    has gitdir => ( isa => "Path::Class::Dir", is => 'ro' );
+    has _git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
+    sub _build__git {
         my $git = File::Which::which('git');
 
         if (!$git) {
@@ -15,7 +19,25 @@ 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;
+    }
+
+    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)';
+
+        my $output = do { local $/ = undef; <$fh> };
+        close $fh;
+
+        return $output;
+    }
 
 
 
index ccb0913..c4c581d 100644 (file)
@@ -14,8 +14,6 @@ my $proj = Gitalist::Git::Project->new(
 );
 isa_ok($proj, 'Gitalist::Git::Project');
 
-like( $proj->_git, qr#/git$#, 'git binary found');
-isa_ok($proj->_gpp, 'Git::PurePerl', 'gpp instance created');
 like($proj->path, qr#/repositories/repo1#, 'repository path is set');
 is($proj->name, qw/repo1/, 'repository name is set');
 is($proj->description, qq/some test repository/, 'repository description loaded');
diff --git a/t/git/util.t b/t/git/util.t
new file mode 100644 (file)
index 0000000..35c6fea
--- /dev/null
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use FindBin qw/$Bin/;
+use Test::More qw/no_plan/;
+
+use Data::Dumper;
+
+BEGIN { use_ok 'Gitalist::Git::Util' }
+
+use Path::Class;
+my $proj = Gitalist::Git::Util->new(
+    gitdir => dir("$Bin/../lib/repositories/repo1"),
+);
+isa_ok($proj, 'Gitalist::Git::Util');
+
+like( $proj->_git, qr#/git$#, 'git binary found');
+isa_ok($proj->_gpp, 'Git::PurePerl', 'gpp instance created');
+like($proj->gitdir, qr#/repositories/repo1#, 'repository path is set');
+
+
+