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