Merge branch 'master' of git://github.com/bobtfish/Gitalist
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
index 2d4c6f5..4a5d491 100644 (file)
@@ -1,56 +1,81 @@
 use MooseX::Declare;
 
+=head1 NAME
+
+Gitalist::Git::Repo - Model of a repository directory
+
+=head1 SYNOPSIS
+
+    my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir );
+    my $project_list = $repo->projects;
+    my $first_project = @$project_list[0];
+    my $named_project = $repo->project('Gitalist');
+
+=head1 DESCRIPTION
+
+This class models a Gitalist Repo, which is a collection of
+Projects (git repositories).  It is used for creating Project
+objects to work with.
+
+=cut
+
+
 class Gitalist::Git::Repo {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
-    use Path::Class;
-    use Gitalist::Git::Project;
-    has repo_dir => ( isa => NonEmptySimpleStr,
-                      is => 'ro',
-                      lazy_build => 1 );
-
-    method _build_repo_dir {
-        return Gitalist->config->{repo_dir};
-    }
+    use MooseX::Types::Path::Class qw/Dir/;
+    use MooseX::Types::Moose qw/ArrayRef/;
+    use aliased 'Gitalist::Git::Project';
+
+=head1 ATTRIBUTES
 
-=head2 _is_git_repo
+=head2 repo_dir
 
-Determine whether a given directory (as a L<Path::Class::Dir> object) is a
-C<git> repo.
+L<Path::Class::Dir> for the root of the Repo.
 
 =cut
 
-    method _is_git_repo ($dir) {
-        return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
-    }
+    has repo_dir => (
+        isa => Dir,
+        is => 'ro',
+        required => 1,
+        coerce => 1,
+    );
 
-=head2 project_dir
+=head2 projects
 
-The directory under which the given project will reside i.e C<.git/..>
+An array of L<Gitalist::Git::Project> for each valid git repo
+found in repo_dir.
 
 =cut
 
-    method project_dir ($project) {
-        my $dir = blessed($project) && $project->isa('Path::Class::Dir')
-            ? $project->stringify
-                : $self->dir_from_project_name($project);
+    has projects => (
+        is => 'ro',
+        isa => ArrayRef['Gitalist::Git::Project'],
+        required => 1,
+        lazy_build => 1,
+    );
 
-        $dir .= '/.git'
-            if -f dir($dir)->file('.git/HEAD');
 
-        return $dir;
-    }
+=head1 METHODS
 
-=head2 list_projects
+=head2 project
 
-For the C<repo_dir> specified in the config return an array of projects where
-each item will contain the contents of L</project_info>.
+Returns a L<Gitalist::Git::Project> for the specified project
+name.
 
 =cut
 
-    method list_projects {
-        my $base = dir($self->repo_dir);
-        my @ret;
+    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( $self->repo_dir->subdir($project) );
+    }
+
+
+    method _build_projects {
+        my $base = $self->repo_dir;
         my $dh = $base->open || die "Could not open $base";
+        my @ret;
         while (my $file = $dh->read) {
             next if $file =~ /^.{1,2}$/;
 
@@ -58,34 +83,30 @@ each item will contain the contents of L</project_info>.
             next unless -d $obj;
             next unless $self->_is_git_repo($obj);
 
-            # XXX Leaky abstraction alert!
-            my $is_bare = !-d $obj->subdir('.git');
-
-            my $name = (File::Spec->splitdir($obj))[-1];
-            push @ret, Gitalist::Git::Project->new( name => $name,
-                                     path => $obj,
-                                 );
-            #push @ret, {
-            #    name => ($name . ( $is_bare ? '' : '/.git' )),
-               # $self->get_project_properties(
-               #     $is_bare ? $obj : $obj->subdir('.git')
-               # ),
-            #};
+            push @ret, $self->project($file);
         }
 
-        return [sort { $a->{name} cmp $b->{name} } @ret];
+        return [sort { $a->name cmp $b->name } @ret];
+    }
+
+    # Determine whether a given directory is a git repo.
+    method _is_git_repo ($dir) {
+        return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
     }
+}                               # end class
 
-=head2 dir_from_project_name
+__END__
 
-Get the corresponding directory of a given project.
+=head1 SEE ALSO
 
-=cut
+L<Gitalist::Git::Project>
 
-    method dir_from_project_name (Str $project) {
-        return dir($self->repo_dir)->subdir($project);
-    }
+=head1 AUTHORS
 
+See L<Gitalist> for authors.
 
+=head1 LICENSE
 
-}                               # end class
+See L<Gitalist> for the license.
+
+=cut