Align Vhost code with reality.
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfRepositories.pm
1 use MooseX::Declare;
2
3 role Gitalist::Git::CollectionOfRepositories
4      with Gitalist::Git::Serializable
5      with Gitalist::Git::CollectionOfRepositories::Role::Context {
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use MooseX::Types::Moose qw/ArrayRef/;
8     use Moose::Autobox;
9     use aliased 'Gitalist::Git::Repository';
10
11     requires 'debug_string';
12
13     has repositories => (
14         is         => 'ro',
15         isa        => ArrayRef['Gitalist::Git::Repository'],
16         required   => 1,
17         lazy_build => 1,
18     );
19
20     has export_ok => (
21         is => 'ro',
22         isa => 'Str',
23     );
24
25     method get_repository (NonEmptySimpleStr $name) {
26         my $repo = $self->_get_repo_from_name($name);
27         confess("Couldn't get_repository '$name' - not a valid git repository.")
28             unless $self->_is_git_repo($repo->path);
29         return $repo;
30     }
31
32     # Determine whether a given directory is a git repo.
33     # http://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html
34     method _is_git_repo ($dir) {
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;
42     }
43     requires qw/
44         _build_repositories
45         _get_repo_from_name
46     /;
47
48     around _build_repositories {
49         [sort { $a->name cmp $b->name } $self->$orig->flatten];
50     }
51 }
52
53 1;
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