Commit | Line | Data |
7e7f9335 |
1 | use MooseX::Declare; |
2 | |
a54e6cb0 |
3 | class Gitalist::Git::Repo { |
7e7f9335 |
4 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
84f31a44 |
5 | use MooseX::Types::Path::Class qw/Dir/; |
6 | use MooseX::Types::Moose qw/ArrayRef/; |
38b9e5c8 |
7 | use aliased 'Gitalist::Git::Project'; |
8 | |
84f31a44 |
9 | has repo_dir => ( |
10 | isa => Dir, |
11 | is => 'ro', |
12 | required => 1, |
13 | coerce => 1, |
14 | ); |
7e7f9335 |
15 | |
3bbb1202 |
16 | has projects => ( |
17 | is => 'ro', |
18 | isa => ArrayRef['Gitalist::Git::Project'], |
19 | required => 1, |
20 | lazy_build => 1, |
21 | ); |
7e7f9335 |
22 | |
bba40bd5 |
23 | ## Public methods |
3bbb1202 |
24 | method project (NonEmptySimpleStr $project) { |
5232dbdd |
25 | my $path = $self->repo_dir->subdir($project)->resolve; |
26 | die "Directory traversal prohibited" unless $self->repo_dir->contains($path); |
3bbb1202 |
27 | die "Not a valid Project" unless $self->_is_git_repo($path); |
b5b638f7 |
28 | return Project->new( $self->repo_dir->subdir($project) ); |
3bbb1202 |
29 | } |
30 | |
bba40bd5 |
31 | ## Builders |
84f31a44 |
32 | method _build_projects { |
33 | my $base = $self->repo_dir; |
7e7f9335 |
34 | my $dh = $base->open || die "Could not open $base"; |
84f31a44 |
35 | my @ret; |
7e7f9335 |
36 | while (my $file = $dh->read) { |
37 | next if $file =~ /^.{1,2}$/; |
38 | |
39 | my $obj = $base->subdir($file); |
40 | next unless -d $obj; |
41 | next unless $self->_is_git_repo($obj); |
42 | |
38b9e5c8 |
43 | push @ret, $self->project($file); |
7e7f9335 |
44 | } |
45 | |
84f31a44 |
46 | return [sort { $a->name cmp $b->name } @ret]; |
7e7f9335 |
47 | } |
3bbb1202 |
48 | |
bba40bd5 |
49 | ## Private methods |
3bbb1202 |
50 | # Determine whether a given directory is a git repo. |
51 | method _is_git_repo ($dir) { |
52 | return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD'); |
53 | } |
775e96e0 |
54 | } # end class |
3bbb1202 |
55 | |
775e96e0 |
56 | __END__ |
3bbb1202 |
57 | |
bba40bd5 |
58 | =head1 NAME |
59 | |
60 | Gitalist::Git::Repo - Model of a repository directory |
61 | |
62 | =head1 SYNOPSIS |
63 | |
64 | my $repo = Gitalist::Git::Repo->new( repo_dir => $Dir ); |
65 | my $project_list = $repo->projects; |
66 | my $first_project = @$project_list[0]; |
67 | my $named_project = $repo->project('Gitalist'); |
68 | |
69 | =head1 DESCRIPTION |
70 | |
71 | This class models a Gitalist Repo, which is a collection of |
72 | Projects (git repositories). It is used for creating Project |
73 | objects to work with. |
74 | |
75 | =cut |
76 | |
77 | =head1 ATTRIBUTES |
78 | |
79 | =head2 repo_dir |
80 | |
81 | L<Path::Class::Dir> for the root of the Repo. |
82 | |
83 | =cut |
84 | |
85 | =head2 projects |
86 | |
87 | An array of L<Gitalist::Git::Project> for each valid git repo |
88 | found in repo_dir. |
89 | |
90 | =cut |
91 | |
92 | |
93 | =head1 METHODS |
94 | |
95 | =head2 project (NonEmptySimpleStr $project) |
96 | |
97 | Returns a L<Gitalist::Git::Project> for the specified project |
98 | name. |
99 | |
100 | =cut |
101 | |
3bbb1202 |
102 | =head1 SEE ALSO |
103 | |
104 | L<Gitalist::Git::Project> |
105 | |
775e96e0 |
106 | =head1 AUTHORS |
3bbb1202 |
107 | |
775e96e0 |
108 | See L<Gitalist> for authors. |
3bbb1202 |
109 | |
110 | =head1 LICENSE |
111 | |
775e96e0 |
112 | See L<Gitalist> for the license. |
3bbb1202 |
113 | |
114 | =cut |