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/;
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',
26 method get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
28 # assembling the git command to execute...
29 my @cmd = qw/format-patch --encoding=utf8 --stdout/;
31 # patch, or patch set?
34 ? "-$patch_count -n" : "-1";
37 if (defined $parent_hash) {
38 # if a parent is specified: hp..h
39 push @cmd, "$parent_hash.." . $self->sha1;
41 # if not, but a merge commit: --cc h
43 push @cmd, $self->parents->length > 1
45 push @cmd, $self->sha1;
47 return $self->_run_cmd_fh( @cmd );
50 method diff ( Maybe[Bool] :$patch?,
51 Maybe[NonEmptySimpleStr] :$parent?,
52 Maybe[NonEmptySimpleStr] :$file?
60 ( $file ? ('--', $file) : () ),
63 my @out = $self->_raw_diff(
64 ( $patch ? '--patch-with-raw' : () ),
65 ( $parent ? $parent : () ),
69 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
70 my @difftree = $self->_parse_diff_tree(\@out);
75 # The blank line between the tree and the patch.
78 # XXX And no I'm not happy about having diff return tree + patch.
79 return \@difftree, [$self->_parse_diff(@out)];
82 # method snapshot ( NonEmptySimpleStr $format ) {
83 # # TODO - only valid formats are 'tar' and 'zip'
84 # my $formats = { tgz => 'tar', zip => 'zip' };
85 # unless ($formats->exists($format)) {
86 # die("No such format: $format");
88 # $format = $formats->{$format};
89 # my $name = $self->project->name;
90 # $name =~ s,([^/])/*\.git$,$1,;
91 # my $filename = to_utf8($name);
92 # $filename .= "-$self->sha1.$format";
93 # $name =~ s/\047/\047\\\047\047/g;
96 # my @cmd = ('archive', "--format=$format", "--prefix=$name/", $self->sha1);
97 # return $self->_run_cmd_fh(@cmd);
98 # # TODO - support compressed archives
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),
113 method _parse_diff_tree ($diff) {
114 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
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+$/
127 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
128 if $line{status} =~ /^R/;
135 method _parse_diff (@diff) {
138 # This regex is a little pathological.
139 if (m{^diff --git (a/(.*?)) (b/\2)}) {
150 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
151 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
155 # XXX Somewhat hacky. Ahem.
156 $ret[@ret ? -1 : 0]{diff} .= "$_\n";