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