Project->list_tree implementation becomes an access to Object->tree.
[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/;
a8a8f8f9 7 use File::Stat::ModeString qw/mode_to_string/;
1501cb4e 8 use List::MoreUtils qw/any zip/;
9
10 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
11
54368e9d 12 # project and sha1 are required initargs
50394a3e 13 has project => ( isa => 'Gitalist::Git::Project',
14 required => 1,
15 is => 'ro',
84f31a44 16 weak_ref => 1,
77edf882 17 handles => {
18 _run_cmd => 'run_cmd',
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
84f31a44 27 has $_ => ( isa => NonEmptySimpleStr,
1501cb4e 28 required => 1,
29 is => 'ro',
30 lazy_build => 1 )
483b98b7 31 for qw/type modestr size/;
54368e9d 32
77edf882 33 has _gpp_obj => ( isa => 'Git::PurePerl::Object',
34 required => 1,
35 is => 'ro',
36 lazy_build => 1,
37 handles => [ 'parents',
38 'parent_sha1',
77edf882 39 'author',
40 'authored_time',
41 'committer',
42 'committed_time',
77edf882 43 ],
44 );
45
e86f08d0 46 # This feels wrong, but current templates assume
47 # these attributes are present on every object.
77b5d22c 48 foreach my $key (qw/tree_sha1 comment content/) {
e86f08d0 49 has $key => ( isa => Str,
50 required => 1,
51 is => 'ro',
52 lazy_build => 1,
53 );
54 method "_build_$key" {
77b5d22c 55 confess("Object can't " . $key) unless $self->_gpp_obj->can($key);
e86f08d0 56 return $self->_gpp_obj->$key;
57 }
58 }
59
54368e9d 60 # objects can't determine their mode or filename
84f31a44 61 has file => ( isa => NonEmptySimpleStr,
54368e9d 62 required => 0,
63 is => 'ro' );
64 has mode => ( isa => Int,
1501cb4e 65 required => 1,
66 default => 0,
67 is => 'ro' );
54368e9d 68
b36b7e0b 69 has tree => ( isa => 'ArrayRef[Gitalist::Git::Object]',
70 required => 0,
71 is => 'ro',
72 lazy_build => 1 );
73
77edf882 74 method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
75
b36b7e0b 76 method _build_tree {
77 confess("Can't list_tree on a blob object.")
78 if $self->type eq 'blob';
79 my $output = $self->_run_cmd(qw/ls-tree -z/, $self->sha1);
80 return unless defined $output;
81
82 my @ret;
83 for my $line (split /\0/, $output) {
84 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
85 push @ret, Gitalist::Git::Object->new( mode => oct $mode,
86 type => $type,
87 sha1 => $object,
88 file => $file,
89 project => $self->project,
90 );
91 }
92 return \@ret;
93 }
1501cb4e 94
95 method diff ( Maybe[Bool] :$patch?,
96 Maybe[NonEmptySimpleStr] :$parent?,
97 Maybe[NonEmptySimpleStr] :$file?
98 ) {
99 # Use parent if specifed, else take the parent from the commit
100 # if there is only one, otherwise it was a merge commit.
101 $parent = $parent
102 ? $parent
103 : $self->parents <= 1
104 ? $self->parent_sha1
105 : '-c';
106 my @etc = (
107 ( $file ? ('--', $file) : () ),
108 );
109
110 my @out = $self->_raw_diff(
111 ( $patch ? '--patch-with-raw' : () ),
112 ( $parent ? $parent : () ),
113 $self->sha1, @etc,
114 );
115
116 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
117 my @difftree = $self->_parse_diff_tree(\@out);
118
119 return \@difftree
120 unless $patch;
121
122 # The blank line between the tree and the patch.
123 shift @out;
124
125 # XXX And no I'm not happy about having diff return tree + patch.
126 return \@difftree, [$self->_parse_diff(@out)];
127 }
128
129## Private methods
130 # gitweb uses the following sort of command for diffing merges:
131 # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index --cc 316cf158df3f6207afbae7270bcc5ba0 --
132 # and for regular diffs
133 # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 --
134 method _raw_diff (@args) {
135 return $self->_run_cmd_list(
136 qw(diff-tree -r -M --no-commit-id --full-index),
137 @args
138 );
139 }
140
141 method _parse_diff_tree ($diff) {
142 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
143 my @ret;
144 while (@$diff and $diff->[0] =~ /^:\d+/) {
145 my $line = shift @$diff;
146 # see. man git-diff-tree for more info
147 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
148 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
149 my %line = zip @keys, @vals;
150 # Some convenience keys
151 $line{file} = $line{src};
152 $line{sha1} = $line{sha1dst};
153 $line{is_new} = $line{sha1src} =~ /^0+$/
154 if $line{sha1src};
155 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
156 if $line{status} =~ /^R/;
157 push @ret, \%line;
158 }
159
160 return @ret;
161 }
162
163 method _parse_diff (@diff) {
164 my @ret;
165 for (@diff) {
166 # This regex is a little pathological.
167 if (m{^diff --git (a/(.*?)) (b/\2)}) {
168 push @ret, {
169 head => $_,
170 a => $1,
171 b => $3,
172 file => $2,
173 diff => '',
174 };
175 next;
176 }
177
178 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
179 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
180 next
181 }
182
183 # XXX Somewhat hacky. Ahem.
184 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
185 }
186
187 return @ret;
188 }
189
190
191## Builders
192method _build__gpp_obj {
77edf882 193 return $self->_get_gpp_object($self->sha1)
194 }
a8a8f8f9 195
84f31a44 196 foreach my $key (qw/ type size /) {
197 method "_build_$key" {
10af354d 198 my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
199 chomp($v);
200 return $v;
84f31a44 201 }
50394a3e 202 }
54368e9d 203
a8a8f8f9 204 method _build_modestr {
8d953eae 205 my $modestr = mode_to_string($self->mode);
a8a8f8f9 206 return $modestr;
207 }
208
84f31a44 209 method _cat_file_with_flag ($flag) {
77edf882 210 $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
50394a3e 211 }
212
a8a8f8f9 213} # end class