Part 1 cleanup of ::Project.
Zachary Stevens [Wed, 11 Nov 2009 01:36:01 +0000 (01:36 +0000)]
 * path now contains project_dir, project_dir goes away.
 * now constructed as Project->new($dir).

lib/Gitalist/Git/Project.pm
lib/Gitalist/Git/Repo.pm
lib/Gitalist/Git/Util.pm
t/02git_object.t
t/02git_project.t
t/02git_util.t

index 7f25e9c..4d114d4 100644 (file)
 use MooseX::Declare;
 
+=head1 NAME
+
+Git::Project - Model of a git repository
+
+=head1 SYNOPSIS
+
+    my $project = Gitalist::Git::Project->new( name => 'Gitalist',
+                                               path => $project_dir );
+    my $project = Gitalist::Git::Project->new($project_dir);
+    my $project_list = $repo->projects;
+    my $first_project = @$project_list[0];
+    my $named_project = $repo->project('Gitalist');
+
+=head1 DESCRIPTION
+
+This class models a git repository, referred to in Gitalist
+as a "Project".
+
+=cut
+
 class Gitalist::Git::Project with Gitalist::Git::HasUtils {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
-    use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
-    use DateTime;
     use MooseX::Types::Path::Class qw/Dir/;
+    use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
     use List::MoreUtils qw/any zip/;
+    use DateTime;
     use aliased 'Gitalist::Git::Object';
 
-    our $SHA1RE = qr/[0-9a-fA-F]{40}/;
+=head1 ATTRIBUTES
+
+=head2 name
+
+=cut
 
     has name => ( isa => NonEmptySimpleStr,
                   is => 'ro', required => 1 );
+
+=head2 path
+
+L<Path::Class:Dir> for the location of the git repository.
+
+=cut
+
     has path => ( isa => Dir,
                   is => 'ro', required => 1);
 
+=head2 description
+
+String containing .git/description
+
+=cut
+
     has description => ( isa => Str,
                          is => 'ro',
                          lazy_build => 1,
                      );
+
+=head2 owner
+
+Owner of the files on disk.
+
+=cut
+
     has owner => ( isa => NonEmptySimpleStr,
                    is => 'ro',
                    lazy_build => 1,
                );
+
+=head2 last_change
+
+L<DateTime> for the time of the last update.
+undef if the repository has never been used.
+
+=cut
+
     has last_change => ( isa => Maybe['DateTime'],
                          is => 'ro',
                          lazy_build => 1,
                      );
 
-    has project_dir => ( isa => Dir,
-        is => 'ro',
-        lazy => 1,
-        default => sub {
-            my $self = shift;
-            $self->is_bare
-                ? $self->path
-                : $self->path->subdir('.git')
-        },
-    );
-    has is_bare => (
-        isa => Bool,
-        is => 'ro',
-        lazy => 1,
-        default => sub {
-            my $self = shift;
-            -f $self->path->file('.git', 'HEAD')
-                ? 0
-                : -f $self->path->file('HEAD')
-                    ? 1
-                    : confess("Cannot find " . $self->path . "/.git/HEAD or "
-                        . $self->path . "/HEAD");
-        },
-    );
+=head2 is_bare
+
+Bool indicating whether this Project is bare.
+
+=cut
+
+    has is_bare => ( isa => Bool,
+                     is => 'ro',
+                     lazy => 1,
+                     default => sub {
+                         -d $_[0]->path->parent->subdir->($_[0]->name)
+                             ? 1 : 0
+                         },
+                     );
 
     method BUILD {
         $self->$_() for qw/last_change owner description/; # Ensure to build early.
     }
 
-    method _project_dir {
-        -f $self->{path}->file('.git', 'HEAD')
-            ? $self->{path}->subdir('.git')
-            : $self->{path};
+    around BUILDARGS (ClassName $class: Dir $dir) {
+        my $name = $dir->dir_list(-1);
+        $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
+        confess("Can't find a git repository at " . $dir)
+            unless ( -f $dir->file('HEAD') );
+        return $class->$orig(name => $name,
+                             path => $dir);
     }
 
     method _build__util {
@@ -69,18 +114,20 @@ class Gitalist::Git::Project with Gitalist::Git::HasUtils {
             project => $self,
         );
     }
-
+    
+    our $SHA1RE = qr/[0-9a-fA-F]{40}/;
+    
     method _build_description {
         my $description = "";
         eval {
-            $description = $self->project_dir->file('description')->slurp;
+            $description = $self->{path}->file('description')->slurp;
             chomp $description;
         };
         return $description;
     }
 
     method _build_owner {
-        my ($gecos, $name) = (getpwuid $self->project_dir->stat->uid)[6,0];
+        my ($gecos, $name) = (getpwuid $self->{path}->stat->uid)[6,0];
         $gecos =~ s/,+$//;
         return length($gecos) ? $gecos : $name;
     }
@@ -447,4 +494,26 @@ be:
         };
     };
 
+=head1 SEE ALSO
+
+L<Gitalist::Git::Util> L<Gitalist::Git::Object>
+
+=head1 AUTHORS AND COPYRIGHT
+
+  Catalyst application:
+    (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
+
+  Original gitweb.cgi from which this was derived:
+    (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
+    (C) 2005, Christian Gierke
+
+=head1 LICENSE
+
+FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
 } # end class
index 3f7798f..e63ef97 100644 (file)
@@ -70,10 +70,7 @@ name.
     method project (NonEmptySimpleStr $project) {
         my $path = $self->repo_dir->subdir($project);
         die "Not a valid Project" unless $self->_is_git_repo($path);
-        return Project->new(
-            name => $project,
-            path => $self->repo_dir->subdir($project),
-        );
+        return Project->new( $self->repo_dir->subdir($project) );
     }
 
 
index 846b567..8564cb0 100644 (file)
@@ -6,7 +6,7 @@ class Gitalist::Git::Util {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     has project => (
         isa => 'Gitalist::Git::Project',
-        handles => { gitdir => 'project_dir' },
+        handles => { gitdir => 'path' },
         is => 'bare', # No accessor
         weak_ref => 1, # Weak, you have to hold onto me.
         predicate => 'has_project',
index 898d8ac..fa09b62 100644 (file)
@@ -8,8 +8,7 @@ use Data::Dumper;
 use Path::Class;
 use Gitalist::Git::Project;
 my $project = Gitalist::Git::Project->new(
-    name => 'repo1',
-    path => dir("$Bin/lib/repositories/repo1"),
+    dir("$Bin/lib/repositories/repo1"),
 );
 
 BEGIN { use_ok 'Gitalist::Git::Object' }
@@ -42,4 +41,4 @@ is($obj2->modestr, '?---------', "modestr is correct" );
 is($obj2->contents, "bar\n", 'obj2 contents is correct');
 is($obj2->size, 4, "size is correct");
 is($obj2->tree_sha1, '', 'tree_sha1 is an empty string');
-is($obj2->comments, '', 'comments is an empty string');
+is($obj2->comment, '', 'comment is an empty string');
index 8829731..9472a9e 100644 (file)
@@ -2,18 +2,19 @@ use strict;
 use warnings;
 use FindBin qw/$Bin/;
 use Test::More qw/no_plan/;
-
+use Test::Exception;
 use Data::Dumper;
 
 BEGIN { use_ok 'Gitalist::Git::Project' }
 
+dies_ok {
+    my $proj = Gitalist::Git::Project->new();
+} 'New project with no args';
+
 use Path::Class;
 my $gitdir = dir("$Bin/lib/repositories/repo1");
 
-my $proj = Gitalist::Git::Project->new(
-    path => $gitdir,
-    name => "repo1",
-);
+my $proj = Gitalist::Git::Project->new($gitdir);
 isa_ok($proj, 'Gitalist::Git::Project');
 is($proj->path, $gitdir, 'repository path is set');
 is($proj->name, qw/repo1/, 'repository name is set');
index 1ba7f8c..685e347 100644 (file)
@@ -13,10 +13,7 @@ BEGIN {
 use Path::Class;
 my $gitdir = dir("$Bin/lib/repositories/repo1");
 
-my $proj = Gitalist::Git::Project->new(
-    path => $gitdir,
-    name => "repo1",
-);
+my $proj = Gitalist::Git::Project->new($gitdir);
 my $util = Gitalist::Git::Util->new(
     project => $proj,
 );