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