c236301e312c7cd0db2ead500ef610d898e31d30
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
1 use MooseX::Declare;
2
3 =head1 NAME
4
5 Gitalist::Git::Repo - Model of a repository directory
6
7 =head1 SYNOPSIS
8
9     my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir );
10     my $project_list = $repo->projects;
11     my $first_project = @$project_list[0];
12     my $named_project = $repo->project('Gitalist');
13
14 =head1 DESCRIPTION
15
16 This class models a Gitalist Repo, which is a collection of
17 Projects (git repositories).  It is used for creating Project
18 objects to work with.
19
20 =cut
21
22
23 class Gitalist::Git::Repo {
24     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
25     use MooseX::Types::Path::Class qw/Dir/;
26     use MooseX::Types::Moose qw/ArrayRef/;
27     use aliased 'Gitalist::Git::Project';
28
29 =head1 ATTRIBUTES
30
31 =head2 repo_dir
32
33 L<Path::Class::Dir> for the root of the Repo.
34
35 =cut
36
37     has repo_dir => (
38         isa => Dir,
39         is => 'ro',
40         required => 1,
41         coerce => 1,
42     );
43
44 =head2 projects
45
46 An array of L<Gitalist::Git::Project> for each valid git repo
47 found in repo_dir.
48
49 =cut
50
51     has projects => (
52         is => 'ro',
53         isa => ArrayRef['Gitalist::Git::Project'],
54         required => 1,
55         lazy_build => 1,
56     );
57
58
59 =head1 METHODS
60
61 =head2 project
62
63 Returns a L<Gitalist::Git::Project> for the specified project
64 name.
65
66 =cut
67
68     method project (NonEmptySimpleStr $project) {
69         my $path = $self->repo_dir->subdir($project);
70         die "Not a valid Project" unless $self->_is_git_repo($path);
71         return Project->new( $self->repo_dir->subdir($project) );
72     }
73
74
75     method _build_projects {
76         my $base = $self->repo_dir;
77         my $dh = $base->open || die "Could not open $base";
78         my @ret;
79         while (my $file = $dh->read) {
80             next if $file =~ /^.{1,2}$/;
81
82             my $obj = $base->subdir($file);
83             next unless -d $obj;
84             next unless $self->_is_git_repo($obj);
85
86             push @ret, $self->project($file);
87         }
88
89         return [sort { $a->name cmp $b->name } @ret];
90     }
91
92     # Determine whether a given directory is a git repo.
93     method _is_git_repo ($dir) {
94         return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
95     }
96
97
98 =head1 SEE ALSO
99
100 L<Gitalist::Git::Project>
101
102 =head1 AUTHORS AND COPYRIGHT
103
104   Catalyst application:
105     (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
106
107   Original gitweb.cgi from which this was derived:
108     (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
109     (C) 2005, Christian Gierke
110
111 =head1 LICENSE
112
113 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
114
115 This library is free software. You can redistribute it and/or modify
116 it under the same terms as Perl itself.
117
118 =cut
119
120
121 }                               # end class