Revert "Merge remote branch 't0m/json' into json"
[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     # objects can't determine their mode or filename
42     has file => ( isa => NonEmptySimpleStr,
43                   required => 0,
44                   is => 'ro' );
45     has mode => ( isa => Int,
46                   required => 1,
47                   default => 0,
48                   is => 'ro' );
49
50     method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
51
52 ## Private methods
53
54 ## Builders
55     method _build__gpp_obj {
56         return $self->_get_gpp_object($self->sha1)
57     }
58
59     method "_build_size" {
60         my $v = $self->_cat_file_with_flag('s');
61         chomp($v);
62         return $v;
63     }
64
65     method _cat_file_with_flag ($flag) {
66         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
67     }
68
69     method _build_modestr {
70         return _mode_str($self->mode);
71     }
72
73     # via gitweb.pm circa line 1305
74     use Fcntl ':mode';
75     use constant {
76         S_IFINVALID => 0030000,
77         S_IFGITLINK => 0160000,
78     };
79
80     # submodule/subrepository, a commit object reference
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         }
105     }
106
107 } # end class
108
109 __END__
110
111 =head1 NAME
112
113 Gitalist::Git::Object - Model of a git object.
114
115 =head1 SYNOPSIS
116
117     my $object = Repository->get_object($sha1);
118
119 =head1 DESCRIPTION
120
121 Abstract base class for git objects.
122
123
124 =head1 ATTRIBUTES
125
126
127 =head1 METHODS
128
129
130 =head1 AUTHORS
131
132 See L<Gitalist> for authors.
133
134 =head1 LICENSE
135
136 See L<Gitalist> for the license.
137
138 =cut