3984dd5907fa880196bf7382a5b5288fa5bb7cbf
[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 aliased 'Gitalist::Git::Project';
8
9     has repo_dir => (
10         isa => Dir,
11         is => 'ro',
12         required => 1,
13         coerce => 1,
14     );
15
16     has projects => (
17         is => 'ro',
18         isa => ArrayRef['Gitalist::Git::Project'],
19         required => 1,
20         lazy_build => 1,
21     );
22
23     ## Public methods
24     method project (NonEmptySimpleStr $project) {
25         my $path = $self->repo_dir->subdir($project)->resolve;
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?
28         die "Directory traversal prohibited" unless $self->repo_dir->contains($path);
29         die "Not a valid Project" unless $self->_is_git_repo($path);
30         return Project->new( $self->repo_dir->subdir($project) );
31     }
32
33     ## Builders
34     method _build_projects {
35         my $base = $self->repo_dir;
36         my $dh = $base->open || die "Could not open $base";
37         my @ret;
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
45             push @ret, $self->project($file);
46         }
47
48         return [sort { $a->name cmp $b->name } @ret];
49     }
50
51     ## Private methods
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     }
56 }                               # end class
57
58 __END__
59
60 =head1 NAME
61
62 Gitalist::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
73 This class models a Gitalist Repo, which is a collection of
74 Projects (git repositories).  It is used for creating Project
75 objects to work with.
76
77
78 =head1 ATTRIBUTES
79
80 =head2 repo_dir (C<Path::Class::Dir>)
81
82 The filesystem root of the Repo.
83
84 =head2 projects
85
86 An array of all Repos found in C<repo_dir>.
87
88
89
90 =head1 METHODS
91
92 =head2 project (Str $project)
93
94 Returns a L<Gitalist::Git::Project> for the specified project
95 name.
96
97
98 =head1 SEE ALSO
99
100 L<Gitalist::Git::Project>
101
102
103 =head1 AUTHORS
104
105 See L<Gitalist> for authors.
106
107 =head1 LICENSE
108
109 See L<Gitalist> for the license.
110
111 =cut