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 List::MoreUtils qw/any zip/;
8 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
10 has '+_gpp_obj' => ( handles => [ 'comment',
22 method diff ( Maybe[Bool] :$patch?,
23 Maybe[NonEmptySimpleStr] :$parent?,
24 Maybe[NonEmptySimpleStr] :$file?
26 # method diff (:$patch?, :$parent?, :$file?) {
27 # Use parent if specifed, else take the parent from the commit
28 # if there is only one, otherwise it was a merge commit.
35 ( $file ? ('--', $file) : () ),
38 my @out = $self->_raw_diff(
39 ( $patch ? '--patch-with-raw' : () ),
40 ( $parent ? $parent : () ),
44 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
45 my @difftree = $self->_parse_diff_tree(\@out);
50 # The blank line between the tree and the patch.
53 # XXX And no I'm not happy about having diff return tree + patch.
54 return \@difftree, [$self->_parse_diff(@out)];
58 # gitweb uses the following sort of command for diffing merges:
59 # /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 --
60 # and for regular diffs
61 # /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 --
62 method _raw_diff (@args) {
63 return $self->_run_cmd_list(
64 qw(diff-tree -r -M --no-commit-id --full-index),
69 method _parse_diff_tree ($diff) {
70 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
72 while (@$diff and $diff->[0] =~ /^:\d+/) {
73 my $line = shift @$diff;
74 # see. man git-diff-tree for more info
75 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
76 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
77 my %line = zip @keys, @vals;
78 # Some convenience keys
79 $line{file} = $line{src};
80 $line{sha1} = $line{sha1dst};
81 $line{is_new} = $line{sha1src} =~ /^0+$/
83 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
84 if $line{status} =~ /^R/;
91 method _parse_diff (@diff) {
94 # This regex is a little pathological.
95 if (m{^diff --git (a/(.*?)) (b/\2)}) {
106 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
107 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
111 # XXX Somewhat hacky. Ahem.
112 $ret[@ret ? -1 : 0]{diff} .= "$_\n";