1 package Gitalist::Git::Object::Commit;
4 class Gitalist::Git::Object::Commit
5 extends Gitalist::Git::Object
6 with Gitalist::Git::Object::HasTree {
7 use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
8 use MooseX::Types::Common::String qw/NonEmptySimpleStr SimpleStr/;
10 use List::MoreUtils qw/any zip/;
11 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
13 has '+type' => ( default => 'commit' );
14 has '+_gpp_obj' => ( handles => [ 'comment',
27 return [$self->repository->get_object($self->tree_sha1)];
30 method sha_by_path ($path) {
32 # FIXME should this really just take the first result?
33 my @paths = $self->repository->run_cmd('ls-tree', $self->sha1, '--', $path)
37 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
38 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
40 return $self->repository->get_object($sha1);
43 method get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
45 # assembling the git command to execute...
46 my @cmd = qw/format-patch --encoding=utf8 --stdout/;
48 # patch, or patch set?
51 ? "-$patch_count -n" : "-1";
54 if (defined $parent_hash) {
55 # if a parent is specified: hp..h
56 push @cmd, "$parent_hash.." . $self->sha1;
58 # if not, but a merge commit: --cc h
60 push @cmd, $self->parents->length > 1
62 push @cmd, $self->sha1;
64 return $self->_run_cmd_fh( @cmd );
67 method diff ( Bool :$patch?,
68 NonEmptySimpleStr :$parent?,
69 NonEmptySimpleStr :$filename?
77 ( $filename ? ('--', $filename) : () ),
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)
86 my @out = $self->_raw_diff(
87 ( $patch ? '--patch-with-raw' : () ),
88 ( $parent ? $parent : () ),
92 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
93 my @difftree = $self->_parse_diff_tree(\@out);
98 # The blank line between the tree and the patch.
101 # XXX And no I'm not happy about having diff return tree + patch.
102 return \@difftree, [$self->_parse_diff(@out)];
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),
117 method _parse_diff_tree ($diff) {
118 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
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+$/
131 @line{qw/status sim/} = $line{status} =~ /(R)0*(\d+)/
132 if $line{status} =~ /^R/;
139 method _parse_diff (@diff) {
142 # This regex is a little pathological.
143 if (m{^diff --git (a/(.*?)) (b/\2)}) {
154 if (/^index (\w+)\.\.(\w+)(?: (\d+))?$/) {
155 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
159 # XXX Somewhat hacky. Ahem.
160 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
167 # XXX A prime candidate for caching.
168 method blame ( NonEmptySimpleStr $filename, SimpleStr $sha1 ) {
169 my @blameout = $self->_run_cmd_list(
170 blame => '-p', $sha1 ? $sha1 : $self->sha1, '--', $filename
173 my(%commitdata, @filedata);
174 while(defined(local $_ = shift @blameout)) {
175 my ($sha1, $orig_lineno, $lineno, $group_size) =
176 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
178 $commitdata{$sha1} = {}
179 unless exists $commitdata{$sha1};
181 my $commit = $commitdata{$sha1};
183 until(($line = shift @blameout) =~ s/^\t//) {
185 if $line =~ /^(\S+) (.*)/;
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;
198 commit => { sha1 => $sha1, %$commit },
200 orig_lineno => $orig_lineno,
202 ( $group_size ? (group_size => $group_size) : () ),
218 Gitalist::Git::Object::Commit
222 my $commit = Repository->get_object($commit_sha1);
226 Represents a commit object in a git repository.
227 Subclass of C<Gitalist::Git::Object>.
234 =head2 committed_time
253 =head2 sha_by_path ($path)
255 Returns the tree/file sha1 for a given path in a commit.
265 See L<Gitalist> for authors.
269 See L<Gitalist> for the license.