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/;
9 use List::MoreUtils qw/any zip/;
10 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
12 has '+type' => ( default => 'commit' );
13 has '+_gpp_obj' => ( handles => [ 'comment',
25 method patch ( Maybe[NonEmptySimpleStr] $parent? ) {
26 my @args = qw/format-patch --encoding=utf8 --stdout -1/;
27 my $refspec = $self->sha1;
28 if (defined $parent) {
30 $refspec = $parent . '..' . $self->sha1;
32 push @args, '--root', $refspec;
33 my $out = $self->_run_cmd( @args );
37 method diff ( Maybe[Bool] :$patch?,
38 Maybe[NonEmptySimpleStr] :$parent?,
39 Maybe[NonEmptySimpleStr] :$file?
47 ( $file ? ('--', $file) : () ),
50 my @out = $self->_raw_diff(
51 ( $patch ? '--patch-with-raw' : () ),
52 ( $parent ? $parent : () ),
56 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
57 my @difftree = $self->_parse_diff_tree(\@out);
62 # The blank line between the tree and the patch.
65 # XXX And no I'm not happy about having diff return tree + patch.
66 return \@difftree, [$self->_parse_diff(@out)];
70 # gitweb uses the following sort of command for diffing merges:
71 # /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 --
72 # and for regular diffs
73 # /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 --
74 method _raw_diff (@args) {
75 return $self->_run_cmd_list(
76 qw(diff-tree -r -M --no-commit-id --full-index),
81 method _parse_diff_tree ($diff) {
82 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
84 while (@$diff and $diff->[0] =~ /^:\d+/) {
85 my $line = shift @$diff;
86 # see. man git-diff-tree for more info
87 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
88 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
89 my %line = zip @keys, @vals;
90 # Some convenience keys
91 $line{file} = $line{src};
92 $line{sha1} = $line{sha1dst};
93 $line{is_new} = $line{sha1src} =~ /^0+$/
95 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
96 if $line{status} =~ /^R/;
103 method _parse_diff (@diff) {
106 # This regex is a little pathological.
107 if (m{^diff --git (a/(.*?)) (b/\2)}) {
118 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
119 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
123 # XXX Somewhat hacky. Ahem.
124 $ret[@ret ? -1 : 0]{diff} .= "$_\n";