Big cleanup of ::Repo.
Zachary Stevens [Tue, 10 Nov 2009 01:02:01 +0000 (01:02 +0000)]
 * Reorganised and POD added.
 * list_projects attribute renamed to projects
 * project() method validates input
URIs with invalid project params will 404.

lib/Gitalist/Controller/Root.pm
lib/Gitalist/Git/Repo.pm
t/02git_repo.t

index 3a93e9a..c8f5b58 100644 (file)
@@ -90,7 +90,7 @@ sub index :Path :Args(0) {
     my ( $self, $c ) = @_;
     $c->detach($c->req->param('a')) if $c->req->param('a');
 
-    my $list = $c->model()->list_projects;
+    my $list = $c->model()->projects;
     unless(@$list) {
         die "No projects found in ". $c->model->repo_dir;
     }
@@ -111,6 +111,7 @@ A summary of what's happening in the repo.
 sub summary : Local {
   my ( $self, $c ) = @_;
   my $project = $c->stash->{Project};
+  $c->detach('error_404') unless $project;
   my $commit = $self->_get_commit($c);
   $c->stash(
     commit    => $commit,
@@ -457,7 +458,7 @@ sub header {
 
   $c->stash->{version}     = $Gitalist::VERSION;
   # check git's version by running it on the first project in the list.
-  $c->stash->{git_version} = $c->model()->list_projects->[0]->run_cmd('--version');
+  $c->stash->{git_version} = $c->model()->projects->[0]->run_cmd('--version');
   $c->stash->{title}       = $title;
 
   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
@@ -512,14 +513,19 @@ sub header {
     home_link_str => $c->config->{home_link_str},
     );
 
-  if(defined $project) {
+  if (defined $project) {
+      eval {
+          $c->stash(Project => $c->model('GitRepos')->project($project));
+      };
+      if ($@) {
+          $c->detach('error_404');
+      }
       $c->stash(
           search_text => ( $c->req->param('s') ||
                                $c->req->param('searchtext') || ''),
           search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
                                || $c->req->param('h')  || $c->req->param('hash')
                                    || 'HEAD' ),
-          Project => $c->model('GitRepos')->project($project),
       );
   }
 }
index e31ef3f..3f7798f 100644 (file)
@@ -1,11 +1,39 @@
 use MooseX::Declare;
 
+=head1 NAME
+
+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 MooseX::Types::Path::Class qw/Dir/;
     use MooseX::Types::Moose qw/ArrayRef/;
     use aliased 'Gitalist::Git::Project';
 
+=head1 ATTRIBUTES
+
+=head2 repo_dir
+
+L<Path::Class::Dir> for the root of the Repo.
+
+=cut
+
     has repo_dir => (
         isa => Dir,
         is => 'ro',
@@ -13,37 +41,41 @@ class Gitalist::Git::Repo {
         coerce => 1,
     );
 
-    method project (NonEmptySimpleStr $project) {
-        return Project->new(
-            name => $project,
-            path => $self->repo_dir->subdir($project),
-        );
-    }
+=head2 projects
 
+An array of L<Gitalist::Git::Project> for each valid git repo
+found in repo_dir.
 
-=head2 _is_git_repo
+=cut
 
-Determine whether a given directory (as a L<Path::Class::Dir> object) is a
-C<git> repo.
+    has projects => (
+        is => 'ro',
+        isa => ArrayRef['Gitalist::Git::Project'],
+        required => 1,
+        lazy_build => 1,
+    );
 
-=cut
+    method BUILD { $self->projects() }
 
-    method _is_git_repo ($dir) {
-        return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
-    }
 
-=head2 list_projects
+=head1 METHODS
+
+=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
 
-    has projects => (
-        isa => ArrayRef['Gitalist::Git::Project'],
-        reader => 'list_projects',
-        lazy_build => 1,
-    );
+    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),
+        );
+    }
+
 
     method _build_projects {
         my $base = $self->repo_dir;
@@ -61,4 +93,34 @@ each item will contain the contents of L</project_info>.
 
         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');
+    }
+
+
+=head1 SEE ALSO
+
+L<Gitalist::Git::Project>
+
+=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 8d98e95..4f5758b 100644 (file)
@@ -2,6 +2,7 @@ use strict;
 use warnings;
 use FindBin qw/$Bin/;
 use Test::More qw/no_plan/;
+use Test::Exception;
 
 use Data::Dumper;
 
@@ -13,8 +14,16 @@ isa_ok($repo, 'Gitalist::Git::Repo');
 
 is($repo->repo_dir, $repo_dir, "repo->repo_dir is correct" );
 
-my $project_list = $repo->list_projects;
+my $project_list = $repo->{projects};
 isa_ok(@$project_list[0], 'Gitalist::Git::Project');
 
+dies_ok {
+    my $project = $repo->project('NoSuchProject');
+} 'throws exception for invalid project';
+
+dies_ok {
+    my $project = $repo->project();
+} 'throws exception for no project';
+
 my $project = $repo->project('repo1');
 isa_ok($project, 'Gitalist::Git::Project');