Commit | Line | Data |
a8a8f8f9 |
1 | use MooseX::Declare; |
84f31a44 |
2 | use Moose::Autobox; |
a8a8f8f9 |
3 | |
4 | class Gitalist::Git::Object { |
0617cbd0 |
5 | use MooseX::Types::Moose qw/Str Int/; |
84f31a44 |
6 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
a8a8f8f9 |
7 | use File::Stat::ModeString qw/mode_to_string/; |
54368e9d |
8 | # project and sha1 are required initargs |
50394a3e |
9 | has project => ( isa => 'Gitalist::Git::Project', |
10 | required => 1, |
11 | is => 'ro', |
84f31a44 |
12 | weak_ref => 1, |
50394a3e |
13 | handles => [ 'run_cmd' ], |
14 | ); |
84f31a44 |
15 | has sha1 => ( isa => NonEmptySimpleStr, |
54368e9d |
16 | required => 1, |
17 | is => 'ro' ); |
18 | |
84f31a44 |
19 | has $_ => ( isa => NonEmptySimpleStr, |
50394a3e |
20 | required => 1, |
21 | is => 'ro', |
0617cbd0 |
22 | lazy_build => 1 ) |
483b98b7 |
23 | for qw/type modestr size/; |
54368e9d |
24 | |
25 | # objects can't determine their mode or filename |
84f31a44 |
26 | has file => ( isa => NonEmptySimpleStr, |
54368e9d |
27 | required => 0, |
28 | is => 'ro' ); |
29 | has mode => ( isa => Int, |
30 | required => 1, |
31 | default => 0, |
32 | is => 'ro' ); |
33 | |
84f31a44 |
34 | method BUILD { $self->$_() for qw/type size modestr/ } |
a8a8f8f9 |
35 | |
84f31a44 |
36 | foreach my $key (qw/ type size /) { |
37 | method "_build_$key" { |
38 | $self->_cat_file_with_flag(substr($key, 0, 1))->chomp; |
39 | } |
50394a3e |
40 | } |
54368e9d |
41 | |
a8a8f8f9 |
42 | method _build_modestr { |
8d953eae |
43 | my $modestr = mode_to_string($self->mode); |
a8a8f8f9 |
44 | return $modestr; |
45 | } |
46 | |
84f31a44 |
47 | method _cat_file_with_flag ($flag) { |
48 | $self->run_cmd('cat-file', '-' . $flag, $self->{sha1}) |
50394a3e |
49 | } |
50 | |
51 | =head2 contents |
52 | |
53 | Return the contents of a given file. |
54 | |
55 | =cut |
a8a8f8f9 |
56 | |
84f31a44 |
57 | # FIXME - Should be an attribute so it gets cached? |
50394a3e |
58 | method contents { |
59 | if ( $self->type ne 'blob' ) { |
60 | die "object $self->sha1 is not a file\n" |
61 | } |
62 | |
84f31a44 |
63 | $self->_cat_file_with_flag('p'); |
50394a3e |
64 | } |
a8a8f8f9 |
65 | |
66 | } # end class |