Start work splitting out Gitalist::Model::Git into smaller pieces.
[catagits/Gitalist.git] / lib / Gitalist / Git / Repo.pm
1 use MooseX::Declare;
2
3 class 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',
9                       lazy_build => 1 );
10
11     method _build_repo_dir {
12         return Gitalist->config->{repo_dir};
13     }
14
15 =head2 _is_git_repo
16
17 Determine whether a given directory (as a L<Path::Class::Dir> object) is a
18 C<git> repo.
19
20 =cut
21
22     method _is_git_repo ($dir) {
23         return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
24     }
25
26 =head2 project_dir
27
28 The directory under which the given project will reside i.e C<.git/..>
29
30 =cut
31
32     method project_dir ($project) {
33         my $dir = blessed($project) && $project->isa('Path::Class::Dir')
34             ? $project->stringify
35                 : $self->dir_from_project_name($project);
36
37         $dir .= '/.git'
38             if -f dir($dir)->file('.git/HEAD');
39
40         return $dir;
41     }
42
43 =head2 list_projects
44
45 For the C<repo_dir> specified in the config return an array of projects where
46 each item will contain the contents of L</project_info>.
47
48 =cut
49
50     method list_projects {
51         my $base = dir($self->repo_dir);
52         my @ret;
53         my $dh = $base->open || die "Could not open $base";
54         while (my $file = $dh->read) {
55             next if $file =~ /^.{1,2}$/;
56
57             my $obj = $base->subdir($file);
58             next unless -d $obj;
59             next unless $self->_is_git_repo($obj);
60
61             # XXX Leaky abstraction alert!
62             my $is_bare = !-d $obj->subdir('.git');
63
64             my $name = (File::Spec->splitdir($obj))[-1];
65             push @ret, Gitalist::Git::Project->new( name => $name,
66                                      path => $obj,
67                                  );
68             #push @ret, {
69             #    name => ($name . ( $is_bare ? '' : '/.git' )),
70                # $self->get_project_properties(
71                #     $is_bare ? $obj : $obj->subdir('.git')
72                # ),
73             #};
74         }
75
76         return [sort { $a->{name} cmp $b->{name} } @ret];
77     }
78
79 =head2 dir_from_project_name
80
81 Get the corresponding directory of a given project.
82
83 =cut
84
85     method dir_from_project_name (Str $project) {
86         return dir($self->repo_dir)->subdir($project);
87     }
88
89
90
91 }                               # end class