Refactor somewhat
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
CommitLineData
7e7f9335 1use MooseX::Declare;
2
3class Gitalist::Git::Repo {
4 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
84f31a44 5 use MooseX::Types::Path::Class qw/Dir/;
6 use MooseX::Types::Moose qw/ArrayRef/;
7e7f9335 7 use Gitalist::Git::Project;
84f31a44 8 has repo_dir => (
9 isa => Dir,
10 is => 'ro',
11 required => 1,
12 coerce => 1,
13 );
7e7f9335 14
ba033c36 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
7e7f9335 24=head2 _is_git_repo
25
26Determine whether a given directory (as a L<Path::Class::Dir> object) is a
27C<git> repo.
28
29=cut
30
31 method _is_git_repo ($dir) {
255ee743 32 return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
7e7f9335 33 }
34
7e7f9335 35=head2 list_projects
36
37For the C<repo_dir> specified in the config return an array of projects where
38each item will contain the contents of L</project_info>.
39
40=cut
41
84f31a44 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;
7e7f9335 50 my $dh = $base->open || die "Could not open $base";
84f31a44 51 my @ret;
7e7f9335 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
84f31a44 59 push @ret, Gitalist::Git::Project->new(
60 name => $file,
61 path => $obj,
62 );
7e7f9335 63 }
64
84f31a44 65 return [sort { $a->name cmp $b->name } @ret];
7e7f9335 66 }
7e7f9335 67} # end class