Add a HasUtils role, so that ::Repo can have utils also, and use it in ::Project...
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Repo with Gitalist::Git::HasUtils {
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 aliased 'Gitalist::Git::Project';
8
9     # FIXME - this is nasty as we build the Git::Utils thing without a project name
10     #         should refactor or something?
11     method _build__util {
12         Gitalist::Git::Util->new();
13     }
14
15     has repo_dir => (
16         isa => Dir,
17         is => 'ro',
18         required => 1,
19         coerce => 1,
20     );
21
22     method project (NonEmptySimpleStr $project) {
23         return Project->new(
24             name => $project,
25             path => $self->repo_dir->subdir($project),
26         );
27     }
28
29
30 =head2 _is_git_repo
31
32 Determine whether a given directory (as a L<Path::Class::Dir> object) is a
33 C<git> repo.
34
35 =cut
36
37     method _is_git_repo ($dir) {
38         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
39     }
40
41 =head2 list_projects
42
43 For the C<repo_dir> specified in the config return an array of projects where
44 each item will contain the contents of L</project_info>.
45
46 =cut
47
48     has projects => (
49         isa => ArrayRef['Gitalist::Git::Project'],
50         reader => 'list_projects',
51         lazy_build => 1,
52     );
53
54     method _build_projects {
55         my $base = $self->repo_dir;
56         my $dh = $base->open || die "Could not open $base";
57         my @ret;
58         while (my $file = $dh->read) {
59             next if $file =~ /^.{1,2}$/;
60
61             my $obj = $base->subdir($file);
62             next unless -d $obj;
63             next unless $self->_is_git_repo($obj);
64
65             push @ret, $self->project($file);
66         }
67
68         return [sort { $a->name cmp $b->name } @ret];
69     }
70 }                               # end class