Commit | Line | Data |
6b3c0b76 |
1 | use MooseX::Declare; |
2 | |
cd169152 |
3 | role Gitalist::Git::CollectionOfRepositories { |
6b3c0b76 |
4 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
5 | use MooseX::Types::Moose qw/ArrayRef/; |
6 | use Moose::Autobox; |
44a9ed75 |
7 | use aliased 'Gitalist::Git::Repository'; |
6b3c0b76 |
8 | |
82bc0f05 |
9 | has repositories => ( |
6b3c0b76 |
10 | is => 'ro', |
44a9ed75 |
11 | isa => ArrayRef['Gitalist::Git::Repository'], |
6b3c0b76 |
12 | required => 1, |
13 | lazy_build => 1, |
14 | ); |
b5ce0e6a |
15 | method get_repository (NonEmptySimpleStr $name) { |
82bc0f05 |
16 | my $path = $self->_get_path_for_repository_name($name); |
309cee4f |
17 | die "Couldn't get_repository '$name' - not a valid git repository." |
6b3c0b76 |
18 | unless $self->_is_git_repo($path); |
44a9ed75 |
19 | return Repository->new( $path ); |
6b3c0b76 |
20 | } |
21 | # Determine whether a given directory is a git repo. |
309cee4f |
22 | # http://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html |
6b3c0b76 |
23 | method _is_git_repo ($dir) { |
24 | return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD'); |
25 | } |
26 | requires qw/ |
82bc0f05 |
27 | _build_repositories |
28 | _get_path_for_repository_name |
6b3c0b76 |
29 | /; |
30 | |
82bc0f05 |
31 | around _build_repositories { |
6b3c0b76 |
32 | [sort { $a->name cmp $b->name } $self->$orig->flatten]; |
33 | } |
34 | } |
35 | |
36 | 1; |
271f6b46 |
37 | |
38 | =head1 NAME |
39 | |
40 | Gitalist::Git::CollectionOfRepositories - Interface and partial implementation of a collection of git repositories |
41 | |
42 | =head1 SYNOPSIS |
43 | |
44 | package My::Example::CollectionOfRepositories; |
45 | use Moose::Role; |
46 | use namespace::autoclean; |
47 | |
48 | with 'Gitalist::Git::CollectionOfRepositories'; |
49 | |
50 | sub _build_repositories { |
51 | my $self = shift; |
52 | [ $self->get_repository('Gitalist') ]; |
53 | } |
54 | sub _get_path_for_repository_name { |
55 | my ($self, $name) = @_; |
56 | '/var/example/' . $name . '.git'; |
57 | } |
58 | |
59 | my $collection = My::Example::CollectionOfRepositories->new |
60 | my $repository_list = $collection->repositories; |
61 | my $only_repository = $repository_list->[0]; |
62 | my $named_repository = $repo->get_repository('Gitalist'); |
63 | |
64 | =head1 DESCRIPTION |
65 | |
66 | This role provides an abstraction for a list of Repository directories. |
67 | |
68 | =head1 ATTRIBUTES |
69 | |
70 | =head2 repositories |
71 | |
72 | An array of all L<Gitalist::Git::Repository>s. |
73 | |
74 | =head1 METHODS |
75 | |
76 | =head2 get_repository (Str $name) |
77 | |
78 | Returns a L<Gitalist::Git::Repository> for the given name. |
79 | If C<$name> is not a valid git repository an exception will be thrown. |
80 | |
81 | =head1 SEE ALSO |
82 | |
83 | L<Gitalist::Git::CollectionOfRepositories::FromListOfDirectories>, |
84 | L<Gitalist::Git::CollectionOfRepositories::FromDirectory>, |
85 | L<Gitalist::Git::Repository>. |
86 | |
87 | =head1 AUTHORS |
88 | |
89 | See L<Gitalist> for authors. |
90 | |
91 | =head1 LICENSE |
92 | |
93 | See L<Gitalist> for the license. |
94 | |
95 | =cut |
96 | |