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