Stop Project->diff() blowing up when no parent is specified, and commit
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
CommitLineData
a8a8f8f9 1use MooseX::Declare;
84f31a44 2use Moose::Autobox;
a8a8f8f9 3
4class 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,
77edf882 13 handles => {
14 _run_cmd => 'run_cmd',
15 _get_gpp_object => 'get_gpp_object',
16 },
50394a3e 17 );
84f31a44 18 has sha1 => ( isa => NonEmptySimpleStr,
54368e9d 19 required => 1,
20 is => 'ro' );
21
84f31a44 22 has $_ => ( isa => NonEmptySimpleStr,
50394a3e 23 required => 1,
24 is => 'ro',
0617cbd0 25 lazy_build => 1 )
483b98b7 26 for qw/type modestr size/;
54368e9d 27
77edf882 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
54368e9d 43 # objects can't determine their mode or filename
84f31a44 44 has file => ( isa => NonEmptySimpleStr,
54368e9d 45 required => 0,
46 is => 'ro' );
47 has mode => ( isa => Int,
48 required => 1,
49 default => 0,
50 is => 'ro' );
51
77edf882 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 }
a8a8f8f9 57
84f31a44 58 foreach my $key (qw/ type size /) {
59 method "_build_$key" {
10af354d 60 my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
61 chomp($v);
62 return $v;
84f31a44 63 }
50394a3e 64 }
54368e9d 65
a8a8f8f9 66 method _build_modestr {
8d953eae 67 my $modestr = mode_to_string($self->mode);
a8a8f8f9 68 return $modestr;
69 }
70
84f31a44 71 method _cat_file_with_flag ($flag) {
77edf882 72 $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
50394a3e 73 }
74
75=head2 contents
76
77Return the contents of a given file.
78
79=cut
a8a8f8f9 80
84f31a44 81 # FIXME - Should be an attribute so it gets cached?
50394a3e 82 method contents {
83 if ( $self->type ne 'blob' ) {
84 die "object $self->sha1 is not a file\n"
85 }
86
84f31a44 87 $self->_cat_file_with_flag('p');
50394a3e 88 }
a8a8f8f9 89
90} # end class