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