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