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