Lose some code by not converting to/from path::class objects, and also by flattening...
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
CommitLineData
7e7f9335 1use MooseX::Declare;
2
3class Gitalist::Git::Repo {
4 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5 use Path::Class;
6 use Gitalist::Git::Project;
7 has repo_dir => ( isa => NonEmptySimpleStr,
8 is => 'ro',
e3d34232 9 required => 1 );
7e7f9335 10
11=head2 _is_git_repo
12
13Determine whether a given directory (as a L<Path::Class::Dir> object) is a
14C<git> repo.
15
16=cut
17
18 method _is_git_repo ($dir) {
255ee743 19 return -f $dir->file('HEAD') || -f $dir->file('.git', 'HEAD');
7e7f9335 20 }
21
22=head2 project_dir
23
24The directory under which the given project will reside i.e C<.git/..>
25
26=cut
27
28 method project_dir ($project) {
255ee743 29 -f $project->file('.git', 'HEAD')
30 ? $project->subdir('.git')
31 : $project;
7e7f9335 32 }
33
34=head2 list_projects
35
36For the C<repo_dir> specified in the config return an array of projects where
37each item will contain the contents of L</project_info>.
38
39=cut
40
41 method list_projects {
42 my $base = dir($self->repo_dir);
43 my @ret;
44 my $dh = $base->open || die "Could not open $base";
45 while (my $file = $dh->read) {
46 next if $file =~ /^.{1,2}$/;
47
48 my $obj = $base->subdir($file);
49 next unless -d $obj;
50 next unless $self->_is_git_repo($obj);
51
255ee743 52 # FIXME - Is resolving project_dir here sane?
53 # I think not, just pass $obj down, and
54 # resolve $project->path and $project->is_bare
55 # in BUILDARGS
56 push @ret, Gitalist::Git::Project->new( name => $file,
57 path => $self->project_dir($obj),
7e7f9335 58 );
7e7f9335 59 }
60
61 return [sort { $a->{name} cmp $b->{name} } @ret];
62 }
63
64=head2 dir_from_project_name
65
66Get the corresponding directory of a given project.
67
68=cut
69
70 method dir_from_project_name (Str $project) {
71 return dir($self->repo_dir)->subdir($project);
72 }
73
74
75
76} # end class