Remove the HasUtils role from Repo.
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
CommitLineData
7e7f9335 1use MooseX::Declare;
2
a54e6cb0 3class Gitalist::Git::Repo {
7e7f9335 4 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
84f31a44 5 use MooseX::Types::Path::Class qw/Dir/;
6 use MooseX::Types::Moose qw/ArrayRef/;
38b9e5c8 7 use aliased 'Gitalist::Git::Project';
8
84f31a44 9 has repo_dir => (
10 isa => Dir,
11 is => 'ro',
12 required => 1,
13 coerce => 1,
14 );
7e7f9335 15
ba033c36 16 method project (NonEmptySimpleStr $project) {
38b9e5c8 17 return Project->new(
ba033c36 18 name => $project,
3742082a 19 path => $self->repo_dir->subdir($project),
ba033c36 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
38b9e5c8 59 push @ret, $self->project($file);
7e7f9335 60 }
61
84f31a44 62 return [sort { $a->name cmp $b->name } @ret];
7e7f9335 63 }
7e7f9335 64} # end class