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