Switch to chained dispatch
[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
3bbb1202 16 has projects => (
17 is => 'ro',
18 isa => ArrayRef['Gitalist::Git::Project'],
19 required => 1,
20 lazy_build => 1,
21 );
7e7f9335 22
bba40bd5 23 ## Public methods
3bbb1202 24 method project (NonEmptySimpleStr $project) {
5232dbdd 25 my $path = $self->repo_dir->subdir($project)->resolve;
4866fca1 26 $self->repo_dir->resolve; # FIXME - This needs to be called, or if repo_dir contains .., it'll explode below!
27 # This is a Path::Class::Dir bug, right?
5232dbdd 28 die "Directory traversal prohibited" unless $self->repo_dir->contains($path);
3bbb1202 29 die "Not a valid Project" unless $self->_is_git_repo($path);
b5b638f7 30 return Project->new( $self->repo_dir->subdir($project) );
3bbb1202 31 }
32
bba40bd5 33 ## Builders
84f31a44 34 method _build_projects {
35 my $base = $self->repo_dir;
7e7f9335 36 my $dh = $base->open || die "Could not open $base";
84f31a44 37 my @ret;
7e7f9335 38 while (my $file = $dh->read) {
39 next if $file =~ /^.{1,2}$/;
40
41 my $obj = $base->subdir($file);
42 next unless -d $obj;
43 next unless $self->_is_git_repo($obj);
44
38b9e5c8 45 push @ret, $self->project($file);
7e7f9335 46 }
47
84f31a44 48 return [sort { $a->name cmp $b->name } @ret];
7e7f9335 49 }
3bbb1202 50
bba40bd5 51 ## Private methods
3bbb1202 52 # Determine whether a given directory is a git repo.
53 method _is_git_repo ($dir) {
54 return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
55 }
775e96e0 56} # end class
3bbb1202 57
775e96e0 58__END__
3bbb1202 59
bba40bd5 60=head1 NAME
61
62Gitalist::Git::Repo - Model of a repository directory
63
64=head1 SYNOPSIS
65
66 my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir );
67 my $project_list = $repo->projects;
68 my $first_project = @$project_list[0];
69 my $named_project = $repo->project('Gitalist');
70
71=head1 DESCRIPTION
72
73This class models a Gitalist Repo, which is a collection of
74Projects (git repositories). It is used for creating Project
75objects to work with.
76
bba40bd5 77
78=head1 ATTRIBUTES
79
8ba87261 80=head2 repo_dir (C<Path::Class::Dir>)
bba40bd5 81
8ba87261 82The filesystem root of the Repo.
bba40bd5 83
84=head2 projects
85
8ba87261 86An array of all Repos found in C<repo_dir>.
bba40bd5 87
bba40bd5 88
89
90=head1 METHODS
91
8ba87261 92=head2 project (Str $project)
bba40bd5 93
94Returns a L<Gitalist::Git::Project> for the specified project
95name.
96
bba40bd5 97
3bbb1202 98=head1 SEE ALSO
99
100L<Gitalist::Git::Project>
101
8ba87261 102
775e96e0 103=head1 AUTHORS
3bbb1202 104
775e96e0 105See L<Gitalist> for authors.
3bbb1202 106
107=head1 LICENSE
108
775e96e0 109See L<Gitalist> for the license.
3bbb1202 110
111=cut