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