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