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