Migrated commit action to new model.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2 use Moose::Autobox;
3
4 class Gitalist::Git::Object {
5     use MooseX::Types::Moose qw/Str Int/;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use File::Stat::ModeString qw/mode_to_string/;
8     # project and sha1 are required initargs
9     has project => ( isa => 'Gitalist::Git::Project',
10                      required => 1,
11                      is => 'ro',
12                      weak_ref => 1,
13                      handles => {
14                          _run_cmd => 'run_cmd',
15                          _get_gpp_object => 'get_gpp_object',
16                      },
17                  );
18     has sha1 => ( isa => NonEmptySimpleStr,
19                required => 1,
20                is => 'ro' );
21
22     has $_ => ( isa => NonEmptySimpleStr,
23                   required => 1,
24                   is => 'ro',
25                   lazy_build => 1 )
26         for qw/type modestr size/;
27
28     has _gpp_obj => ( isa => 'Git::PurePerl::Object',
29                       required => 1,
30                       is => 'ro',
31                       lazy_build => 1,
32                       handles => [ 'parents',
33                                    'parent_sha1',
34                                    'comment',
35                                    'author',
36                                    'authored_time',
37                                    'committer',
38                                    'committed_time',
39                                    'tree_sha1',
40                                ],
41                   );
42
43     # objects can't determine their mode or filename
44     has file => ( isa => NonEmptySimpleStr,
45                   required => 0,
46                   is => 'ro' );
47     has mode => ( isa => Int,
48                 required => 1,
49                 default => 0,
50                 is => 'ro' );
51
52     method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
53
54     method _build__gpp_obj {
55         return $self->_get_gpp_object($self->sha1)
56     }
57
58     foreach my $key (qw/ type size /) {
59         method "_build_$key" {
60             my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
61             chomp($v);
62             return $v;
63         }
64     }
65
66     method _build_modestr {
67         my $modestr = mode_to_string($self->mode);
68         return $modestr;
69     }
70
71     method _cat_file_with_flag ($flag) {
72         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
73     }
74
75 =head2 contents
76
77 Return the contents of a given file.
78
79 =cut
80
81     # FIXME - Should be an attribute so it gets cached?
82     method contents {
83         if ( $self->type ne 'blob' ) {
84             die "object $self->sha1 is not a file\n"
85         }
86
87         $self->_cat_file_with_flag('p');
88     }
89
90 } # end class