Refactor somewhat
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Repo {
4     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5     use MooseX::Types::Path::Class qw/Dir/;
6     use MooseX::Types::Moose qw/ArrayRef/;
7     use Gitalist::Git::Project;
8     has repo_dir => (
9         isa => Dir,
10         is => 'ro',
11         required => 1,
12         coerce => 1,
13     );
14
15     method project (NonEmptySimpleStr $project) {
16         my $pd = $self->dir_from_project_name($project);
17         return Gitalist::Git::Project->new(
18             name => $project,
19             path => $pd,
20         );
21     }
22
23
24 =head2 _is_git_repo
25
26 Determine whether a given directory (as a L<Path::Class::Dir> object) is a
27 C<git> repo.
28
29 =cut
30
31     method _is_git_repo ($dir) {
32         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
33     }
34
35 =head2 list_projects
36
37 For the C<repo_dir> specified in the config return an array of projects where
38 each item will contain the contents of L</project_info>.
39
40 =cut
41
42     has projects => (
43         isa => ArrayRef['Gitalist::Git::Project'],
44         reader => 'list_projects',
45         lazy_build => 1,
46     );
47
48     method _build_projects {
49         my $base = $self->repo_dir;
50         my $dh = $base->open || die "Could not open $base";
51         my @ret;
52         while (my $file = $dh->read) {
53             next if $file =~ /^.{1,2}$/;
54
55             my $obj = $base->subdir($file);
56             next unless -d $obj;
57             next unless $self->_is_git_repo($obj);
58
59             push @ret, Gitalist::Git::Project->new(
60                 name => $file,
61                 path => $obj,
62             );
63         }
64
65         return [sort { $a->name cmp $b->name } @ret];
66     }
67 }                               # end class