Move Repository->hash_by_path to Commit->sha_by_path.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
CommitLineData
e1307124 1package Gitalist::Git::Object::Commit;
2use MooseX::Declare;
3
0250a92d 4class Gitalist::Git::Object::Commit
5 extends Gitalist::Git::Object
6 with Gitalist::Git::Object::HasTree {
9cd610f4 7 use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
61f14672 8 use MooseX::Types::Common::String qw/NonEmptySimpleStr SimpleStr/;
61ba8635 9 use Moose::Autobox;
0250a92d 10 use List::MoreUtils qw/any zip/;
11 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
12
98390bf6 13 has '+type' => ( default => 'commit' );
0250a92d 14 has '+_gpp_obj' => ( handles => [ 'comment',
15 'tree_sha1',
16 'committer',
17 'committed_time',
18 'author',
19 'authored_time',
20 'parents',
21 'parent_sha1',
22 'parent_sha1s',
23 ],
e1307124 24 );
25
9aed017f 26 method sha_by_path ($path) {
27 $path =~ s{/+$}();
28 # FIXME should this really just take the first result?
29 my @paths = $self->repository->run_cmd('ls-tree', $self->sha1, '--', $path)
30 or return;
31 my $line = $paths[0];
32
33 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
34 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
35 my $sha1 = $3;
36 return $sha1;
37 }
38
35eaa65a 39 method get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
40 Int $patch_count?) {
41 # assembling the git command to execute...
42 my @cmd = qw/format-patch --encoding=utf8 --stdout/;
43
44 # patch, or patch set?
45 push @cmd,
46 defined $patch_count
47 ? "-$patch_count -n" : "-1";
48
49 # refspec
50 if (defined $parent_hash) {
51 # if a parent is specified: hp..h
52 push @cmd, "$parent_hash.." . $self->sha1;
61ba8635 53 } else {
35eaa65a 54 # if not, but a merge commit: --cc h
55 # otherwise: --root h
56 push @cmd, $self->parents->length > 1
61ba8635 57 ? '--cc' : '--root';
35eaa65a 58 push @cmd, $self->sha1;
377bf360 59 }
aa7f1f92 60 return $self->_run_cmd_fh( @cmd );
377bf360 61 }
62
586572a7 63 method diff ( Bool :$patch?,
64 NonEmptySimpleStr :$parent?,
65 NonEmptySimpleStr :$filename?
66 ) {
0250a92d 67 $parent = $parent
68 ? $parent
69 : $self->parents <= 1
70 ? $self->parent_sha1
71 : '-c';
72 my @etc = (
7998de12 73 ( $filename ? ('--', $filename) : () ),
0250a92d 74 );
75
faafb966 76 # If we're not comparing against something and we have multiple
77 # parents then it's a merge commit so show what was merged.
78 my $sha1 = $parent && $parent eq '-c' && @{[$self->parents]} > 1
79 ? sprintf("%s^1..%s^2", ($self->sha1) x 2)
80 : $self->sha1;
9c9f54f0 81
0250a92d 82 my @out = $self->_raw_diff(
83 ( $patch ? '--patch-with-raw' : () ),
84 ( $parent ? $parent : () ),
9c9f54f0 85 $sha1, @etc,
0250a92d 86 );
87
88 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
89 my @difftree = $self->_parse_diff_tree(\@out);
90
91 return \@difftree
92 unless $patch;
93
94 # The blank line between the tree and the patch.
95 shift @out;
96
97 # XXX And no I'm not happy about having diff return tree + patch.
98 return \@difftree, [$self->_parse_diff(@out)];
99 }
100
101 ## Private methods
102 # gitweb uses the following sort of command for diffing merges:
103 # /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 --
104 # and for regular diffs
105 # /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 --
106 method _raw_diff (@args) {
107 return $self->_run_cmd_list(
108 qw(diff-tree -r -M --no-commit-id --full-index),
109 @args
110 );
111 }
112
113 method _parse_diff_tree ($diff) {
114 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
115 my @ret;
116 while (@$diff and $diff->[0] =~ /^:\d+/) {
117 my $line = shift @$diff;
118 # see. man git-diff-tree for more info
119 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
120 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
121 my %line = zip @keys, @vals;
122 # Some convenience keys
123 $line{file} = $line{src};
124 $line{sha1} = $line{sha1dst};
125 $line{is_new} = $line{sha1src} =~ /^0+$/
126 if $line{sha1src};
906a2dd2 127 @line{qw/status sim/} = $line{status} =~ /(R)0*(\d+)/
0250a92d 128 if $line{status} =~ /^R/;
129 push @ret, \%line;
130 }
131
132 return @ret;
133 }
134
135 method _parse_diff (@diff) {
136 my @ret;
137 for (@diff) {
138 # This regex is a little pathological.
139 if (m{^diff --git (a/(.*?)) (b/\2)}) {
140 push @ret, {
141 head => $_,
142 a => $1,
143 b => $3,
144 file => $2,
145 diff => '',
146 };
147 next;
148 }
149
3a58f004 150 if (/^index (\w+)\.\.(\w+)(?: (\d+))?$/) {
0250a92d 151 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
152 next
153 }
154
155 # XXX Somewhat hacky. Ahem.
156 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
157 }
158
159 return @ret;
160 }
161
18a8059a 162
6d4fe0d7 163 # XXX A prime candidate for caching.
61f14672 164 method blame ( NonEmptySimpleStr $filename, SimpleStr $sha1 ) {
18a8059a 165 my @blameout = $self->_run_cmd_list(
61f14672 166 blame => '-p', $sha1 ? $sha1 : $self->sha1, '--', $filename
18a8059a 167 );
168
6d4fe0d7 169 my(%commitdata, @filedata);
18a8059a 170 while(defined(local $_ = shift @blameout)) {
171 my ($sha1, $orig_lineno, $lineno, $group_size) =
6d4fe0d7 172 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
18a8059a 173
6d4fe0d7 174 $commitdata{$sha1} = {}
175 unless exists $commitdata{$sha1};
18a8059a 176
6d4fe0d7 177 my $commit = $commitdata{$sha1};
18a8059a 178 my $line;
6d4fe0d7 179 until(($line = shift @blameout) =~ s/^\t//) {
180 $commit->{$1} = $2
181 if $line =~ /^(\S+) (.*)/;
18a8059a 182 }
183
6d4fe0d7 184 unless(exists $commit->{author_dt}) {
185 for my $t (qw/author committer/) {
186 my $dt = DateTime->from_epoch(epoch => $commit->{"$t-time"});
187 $dt->set_time_zone($commit->{"$t-tz"});
188 $commit->{"$t\_dt"} = $dt;
189 }
190 }
18a8059a 191
6d4fe0d7 192 push @filedata, {
193 line => $line,
194 commit => { sha1 => $sha1, %$commit },
195 meta => {
196 orig_lineno => $orig_lineno,
197 lineno => $lineno,
198 ( $group_size ? (group_size => $group_size) : () ),
199 },
200 };
0250a92d 201 }
18a8059a 202
6d4fe0d7 203 return \@filedata;
18a8059a 204 }
205}
775e96e0 206
207
2081;
209
210__END__
211
212=head1 NAME
213
214Gitalist::Git::Object::Commit
215
c19af0d0 216=head1 SYNOPSIS
217
44a9ed75 218 my $commit = Repository->get_object($commit_sha1);
c19af0d0 219
775e96e0 220=head1 DESCRIPTION
221
c19af0d0 222Represents a commit object in a git repository.
223Subclass of C<Gitalist::Git::Object>.
224
225
226=head1 ATTRIBUTES
227
228=head2 committer
229
230=head2 committed_time
231
232=head2 author
233
234=head2 authored_time
235
236=head2 comment
237
238=head2 tree_sha1
239
240=head2 parents
241
242=head2 parent_sha1
243
244=head2 parent_sha1s
245
246
247=head1 METHODS
248
9aed017f 249=head2 sha_by_path ($path)
250
251Returns the tree/file sha1 for a given path in a commit.
252
c19af0d0 253=head2 get_patch
254
255=head2 diff
256
257=head2 blame
775e96e0 258
259=head1 AUTHORS
260
261See L<Gitalist> for authors.
262
263=head1 LICENSE
264
265See L<Gitalist> for the license.
266
267=cut