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