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