Added commit object serialization.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
CommitLineData
a8a8f8f9 1use MooseX::Declare;
84f31a44 2use Moose::Autobox;
a8a8f8f9 3
2057d9bd 4class Gitalist::Git::Object {
b36b7e0b 5 use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
84f31a44 6 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
1501cb4e 7
2057d9bd 8 with 'Gitalist::Serializeable';
9
82bc0f05 10 # repository and sha1 are required initargs
11 has repository => ( isa => 'Gitalist::Git::Repository',
50394a3e 12 required => 1,
13 is => 'ro',
84f31a44 14 weak_ref => 1,
77edf882 15 handles => {
16 _run_cmd => 'run_cmd',
aa7f1f92 17 _run_cmd_fh => 'run_cmd_fh',
1501cb4e 18 _run_cmd_list => 'run_cmd_list',
77edf882 19 _get_gpp_object => 'get_gpp_object',
20 },
50394a3e 21 );
84f31a44 22 has sha1 => ( isa => NonEmptySimpleStr,
1501cb4e 23 required => 1,
24 is => 'ro' );
54368e9d 25
98390bf6 26 has type => ( isa => NonEmptySimpleStr,
27 is => 'ro',
28 required => 1 );
29
84f31a44 30 has $_ => ( isa => NonEmptySimpleStr,
1501cb4e 31 required => 1,
32 is => 'ro',
33 lazy_build => 1 )
98390bf6 34 for qw/modestr size/;
54368e9d 35
77edf882 36 has _gpp_obj => ( isa => 'Git::PurePerl::Object',
37 required => 1,
38 is => 'ro',
39 lazy_build => 1,
2057d9bd 40 handles => [ 'content' ],
41 traits => [qw/ DoNotSerialize /],
77edf882 42 );
43
54368e9d 44 # objects can't determine their mode or filename
84f31a44 45 has file => ( isa => NonEmptySimpleStr,
54368e9d 46 required => 0,
47 is => 'ro' );
48 has mode => ( isa => Int,
1501cb4e 49 required => 1,
50 default => 0,
51 is => 'ro' );
54368e9d 52
98390bf6 53 method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
77edf882 54
1501cb4e 55## Private methods
1501cb4e 56
57## Builders
98390bf6 58 method _build__gpp_obj {
77edf882 59 return $self->_get_gpp_object($self->sha1)
60 }
a8a8f8f9 61
98390bf6 62 method "_build_size" {
63 my $v = $self->_cat_file_with_flag('s');
64 chomp($v);
65 return $v;
50394a3e 66 }
54368e9d 67
b421837d 68 method _cat_file_with_flag ($flag) {
69 $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
70 }
71
a8a8f8f9 72 method _build_modestr {
b421837d 73 return _mode_str($self->mode);
a8a8f8f9 74 }
75
b421837d 76 # via gitweb.pm circa line 1305
77 use Fcntl ':mode';
78 use constant {
c1a9cd73 79 S_IFINVALID => 0030000,
80 S_IFGITLINK => 0160000,
b421837d 81 };
82
82bc0f05 83 # submodule/subrepository, a commit object reference
b421837d 84 sub S_ISGITLINK($) {
85 return (($_[0] & S_IFMT) == S_IFGITLINK)
86 }
87
88 # convert file mode in octal to symbolic file mode string
89 sub _mode_str {
90 my $mode = shift;
91
92 if (S_ISGITLINK($mode)) {
93 return 'm---------';
94 } elsif (S_ISDIR($mode & S_IFMT)) {
95 return 'drwxr-xr-x';
96 } elsif (S_ISLNK($mode)) {
97 return 'lrwxrwxrwx';
98 } elsif (S_ISREG($mode)) {
99 # git cares only about the executable bit
100 if ($mode & S_IXUSR) {
101 return '-rwxr-xr-x';
102 } else {
103 return '-rw-r--r--';
104 }
105 } else {
106 return '----------';
107 }
50394a3e 108 }
109
a8a8f8f9 110} # end class
775e96e0 111
c19af0d0 112__END__
113
114=head1 NAME
115
116Gitalist::Git::Object - Model of a git object.
117
118=head1 SYNOPSIS
119
44a9ed75 120 my $object = Repository->get_object($sha1);
c19af0d0 121
122=head1 DESCRIPTION
123
124Abstract base class for git objects.
125
126
127=head1 ATTRIBUTES
128
129
130=head1 METHODS
775e96e0 131
132
133=head1 AUTHORS
134
135See L<Gitalist> for authors.
136
137=head1 LICENSE
138
139See L<Gitalist> for the license.
140
141=cut