Unfuck last commit
[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
ca6e3675 23 method BUILD {
24 # Make sure repo_dir is an absolute path so that
25 # ->contains() works correctly.
26 $self->repo_dir->resolve;
27 }
28
bba40bd5 29 ## Public methods
ca6e3675 30 method get_project (NonEmptySimpleStr $name) {
31 my $path = $self->repo_dir->subdir($name)->resolve;
32 die "Directory traversal prohibited"
33 unless $self->repo_dir->contains($path);
34 die "Not a valid Project"
35 unless $self->_is_git_repo($path);
36 return Project->new( $path );
3bbb1202 37 }
38
bba40bd5 39 ## Builders
84f31a44 40 method _build_projects {
cea99b3a 41 my $dh = $self->repo_dir->open || die "Could not open repo_dir";
84f31a44 42 my @ret;
cea99b3a 43 while (my $dir_entry = $dh->read) {
44 # try to get a project for each entry in repo_dir
45 eval {
46 my $p = $self->get_project($dir_entry);
47 push @ret, $p;
48 };
49 }
7e7f9335 50
84f31a44 51 return [sort { $a->name cmp $b->name } @ret];
7e7f9335 52 }
3bbb1202 53
bba40bd5 54 ## Private methods
3bbb1202 55 # Determine whether a given directory is a git repo.
56 method _is_git_repo ($dir) {
57 return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
58 }
775e96e0 59} # end class
3bbb1202 60
775e96e0 61__END__
3bbb1202 62
bba40bd5 63=head1 NAME
64
65Gitalist::Git::Repo - Model of a repository directory
66
67=head1 SYNOPSIS
68
69 my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir );
70 my $project_list = $repo->projects;
b90f633a 71 my $first_project = $project_list->[0];
ca6e3675 72 my $named_project = $repo->get_project('Gitalist');
bba40bd5 73
74=head1 DESCRIPTION
75
76This class models a Gitalist Repo, which is a collection of
77Projects (git repositories). It is used for creating Project
78objects to work with.
79
bba40bd5 80
81=head1 ATTRIBUTES
82
8ba87261 83=head2 repo_dir (C<Path::Class::Dir>)
bba40bd5 84
b90f633a 85The filesystem root of the C<Repo>.
bba40bd5 86
87=head2 projects
88
b90f633a 89An array of all L<Gitalist::Git::Project>s found in C<repo_dir>.
bba40bd5 90
bba40bd5 91
92
93=head1 METHODS
94
ca6e3675 95=head2 get_project (Str $name)
bba40bd5 96
b90f633a 97Returns a L<Gitalist::Git::Project> for the given name.
98If C<$name> is not a valid git repository under C<$repo_dir>, an exception
99will be thrown.
100
bba40bd5 101
bba40bd5 102
3bbb1202 103=head1 SEE ALSO
104
105L<Gitalist::Git::Project>
106
8ba87261 107
775e96e0 108=head1 AUTHORS
3bbb1202 109
775e96e0 110See L<Gitalist> for authors.
3bbb1202 111
112=head1 LICENSE
113
775e96e0 114See L<Gitalist> for the license.
3bbb1202 115
116=cut