Migrate search and reflog to new model.
[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         return Gitalist::Git::Project->new(
17             name => $project,
18             path => $self->repo_dir->subdir($project),
19         );
20     }
21
22
23 =head2 _is_git_repo
24
25 Determine whether a given directory (as a L<Path::Class::Dir> object) is a
26 C<git> repo.
27
28 =cut
29
30     method _is_git_repo ($dir) {
31         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
32     }
33
34 =head2 list_projects
35
36 For the C<repo_dir> specified in the config return an array of projects where
37 each item will contain the contents of L</project_info>.
38
39 =cut
40
41     has projects => (
42         isa => ArrayRef['Gitalist::Git::Project'],
43         reader => 'list_projects',
44         lazy_build => 1,
45     );
46
47     method _build_projects {
48         my $base = $self->repo_dir;
49         my $dh = $base->open || die "Could not open $base";
50         my @ret;
51         while (my $file = $dh->read) {
52             next if $file =~ /^.{1,2}$/;
53
54             my $obj = $base->subdir($file);
55             next unless -d $obj;
56             next unless $self->_is_git_repo($obj);
57
58             push @ret, Gitalist::Git::Project->new(
59                 name => $file,
60                 path => $obj,
61             );
62         }
63
64         return [sort { $a->name cmp $b->name } @ret];
65     }
66 }                               # end class