Squash warning in Path::Class::Dir from contains() call.
[catagits/Gitalist.git] / lib / Gitalist / Git / CollectionOfRepositories / FromDirectoryRecursive.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive
4     with Gitalist::Git::CollectionOfRepositories {
5       use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
6       use MooseX::Types::Path::Class qw/Dir/;
7
8       has repo_dir => (
9         isa => Dir,
10         is => 'ro',
11         required => 1,
12         coerce => 1,
13       );
14
15     method BUILD {
16       # Make sure repo_dir is an absolute path so that
17       # ->contains() works correctly.
18       $self->repo_dir->resolve;
19     }
20
21     method _get_path_for_repository_name (NonEmptySimpleStr $name) {
22       my $path;
23       $self->repo_dir->recurse( 
24         callback => sub {
25           my ( $thing ) = @_;
26           return unless ( $thing->is_dir );
27           $path = $thing if ( $thing->dir_list(-1) eq $name 
28                                   && $self->_is_git_repo( $thing ) );
29         }
30       );
31       $path->resolve if $path;
32       die "Directory traversal prohibited: ".( $path || 'path undefined' )
33           unless $path and $self->repo_dir->contains($path);
34       return $path;
35     }
36
37     ## Builders
38     method _build_repositories {
39       my @ret;
40       $self->repo_dir->recurse( 
41         callback => sub {
42           my ( $dir ) = @_;
43           if ( $dir->is_dir ) {
44             for my $repo ( @ret ) {
45               # no need to go further if parent is git dir
46               # never have a git repo in a git repo?
47               my $check_dir = $repo->path;
48               # go up one and ignore all in that path
49               # if in hidden .git directory
50               $check_dir = $check_dir->parent 
51                   if ( -f $check_dir->parent->file('.git', 'HEAD') );
52               return if ( $check_dir->contains( $dir ) );
53             }
54             eval {
55               # pass directory name as string
56               my @list = $dir->dir_list();
57               my $p = $self->get_repository($list[$#list]);
58               push @ret, $p;
59             };
60           }
61           return;
62         }
63       );
64       return \@ret;
65     }
66 }                         # end class
67
68 __END__
69
70 =head1 NAME
71
72 Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive - Model of recursive directories containing git repositories
73
74 =head1 SYNOPSIS
75
76     my $repo = Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive->new( repo_dir => $Dir );
77     my $repository_list = $repo->repositories;
78     my $first_repository = $repository_list->[0];
79     my $named_repository = $repo->get_repository('Gitalist');
80
81 =head1 DESCRIPTION
82
83 This class provides a list of Repositories recursively found in the given directory.
84
85 =head1 ATTRIBUTES
86
87 =head2 repo_dir (C<Path::Class::Dir>)
88
89 The filesystem root of the C<Repo>.
90
91 =head1 SEE ALSO
92
93 L<Gitalist::Git::CollectionOfRepositories>, L<Gitalist::Git::Repository>
94
95 =head1 AUTHORS
96
97 See L<Gitalist> for authors.
98
99 =head1 LICENSE
100
101 See L<Gitalist> for the license.
102
103 =cut