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