Gave ::Project methods to return Objects, and several methods which
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Object {
4     use MooseX::Types::Moose qw/Str Int/;
5     use File::Stat::ModeString qw/mode_to_string/;
6     # project and sha1 are required initargs
7     has project => ( isa => 'Gitalist::Git::Project',
8                      required => 1,
9                      is => 'ro',
10                      handles => [ 'run_cmd' ],
11                  );
12     has sha1 ( isa => Str,
13                required => 1,
14                is => 'ro' );
15
16     has $_ => ( isa => Str,
17                   required => 1,
18                   is => 'ro',
19                   lazy_build => 1 )
20         for qw/type modestr size/;
21
22     # objects can't determine their mode or filename
23     has file => ( isa => Str,
24                   required => 0,
25                   is => 'ro' );
26     has mode => ( isa => Int,
27                 required => 1,
28                 default => 0,
29                 is => 'ro' );
30
31     method BUILD {
32         $self->$_() for qw/type modestr size/; # Ensure to build early.
33     }
34
35     method _build_type {
36         my $output = $self->run_cmd(qw/cat-file -t/, $self->{sha1});
37         chomp($output);
38         return $output;
39     }
40
41     method _build_modestr {
42         my $modestr = mode_to_string($self->{mode});
43         return $modestr;
44     }
45
46     method _build_size {
47         my $output = $self->run_cmd(qw/cat-file -s/, $self->{sha1});
48         chomp($output);
49         return $output;
50     }
51
52 =head2 contents
53
54 Return the contents of a given file.
55
56 =cut
57
58     method contents {
59         if ( $self->type ne 'blob' ) {
60             die "object $self->sha1 is not a file\n"
61         }
62
63         my $output = $self->run_cmd(qw/cat-file -p/, $self->sha1);
64         return unless $output;
65
66         return $output;
67     }
68
69 } # end class