Change other uses of 'project' to 'repository'.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2 use Moose::Autobox;
3
4 class Gitalist::Git::Object {
5     use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7
8     # repository and sha1 are required initargs
9     has repository => ( isa => 'Gitalist::Git::Repository',
10                      required => 1,
11                      is => 'ro',
12                      weak_ref => 1,
13                      handles => {
14                          _run_cmd => 'run_cmd',
15                          _run_cmd_fh => 'run_cmd_fh',
16                          _run_cmd_list => 'run_cmd_list',
17                          _get_gpp_object => 'get_gpp_object',
18                      },
19                  );
20     has sha1 => ( isa => NonEmptySimpleStr,
21                   required => 1,
22                   is => 'ro' );
23
24     has type => ( isa => NonEmptySimpleStr,
25                   is => 'ro',
26                   required => 1 );
27
28     has $_ => ( isa => NonEmptySimpleStr,
29                 required => 1,
30                 is => 'ro',
31                 lazy_build => 1 )
32         for qw/modestr size/;
33
34     has _gpp_obj => ( isa => 'Git::PurePerl::Object',
35                       required => 1,
36                       is => 'ro',
37                       lazy_build => 1,
38                       handles => [ 'content',
39                                ],
40                   );
41
42     # objects can't determine their mode or filename
43     has file => ( isa => NonEmptySimpleStr,
44                   required => 0,
45                   is => 'ro' );
46     has mode => ( isa => Int,
47                   required => 1,
48                   default => 0,
49                   is => 'ro' );
50
51     method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
52
53 ## Private methods
54
55 ## Builders
56     method _build__gpp_obj {
57         return $self->_get_gpp_object($self->sha1)
58     }
59
60     method "_build_size" {
61         my $v = $self->_cat_file_with_flag('s');
62         chomp($v);
63         return $v;
64     }
65
66     method _cat_file_with_flag ($flag) {
67         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
68     }
69
70     method _build_modestr {
71         return _mode_str($self->mode);
72     }
73
74     # via gitweb.pm circa line 1305
75     use Fcntl ':mode';
76     use constant {
77         S_IFINVALID => 0030000,
78         S_IFGITLINK => 0160000,
79     };
80
81     # submodule/subrepository, 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         }
106     }
107
108 } # end class
109
110 __END__
111
112 =head1 NAME
113
114 Gitalist::Git::Object - Model of a git object.
115
116 =head1 SYNOPSIS
117
118     my $object = Repository->get_object($sha1);
119
120 =head1 DESCRIPTION
121
122 Abstract base class for git objects.
123
124
125 =head1 ATTRIBUTES
126
127
128 =head1 METHODS
129
130
131 =head1 AUTHORS
132
133 See L<Gitalist> for authors.
134
135 =head1 LICENSE
136
137 See L<Gitalist> for the license.
138
139 =cut