Implement the 'patch' action.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
1 package Gitalist::Git::Object::Commit;
2 use MooseX::Declare;
3
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 Moose::Autobox;
10         use List::MoreUtils qw/any zip/;
11         our $SHA1RE = qr/[0-9a-fA-F]{40}/;
12
13         has '+type' => ( default => 'commit' );
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                                       ],
24                          );
25
26         method patch ( Maybe[NonEmptySimpleStr] $parent?,
27                        Int $count?) {
28             $count ||= 1;
29             # Assemble the git command.
30             # common args:
31             my @cmd_args = qw/format-patch --encoding=utf8 --stdout/;
32             # single patch, or patch set?
33             if ($count > 1) {
34                 push @cmd_args, "-$count", "-n";
35             } else {
36                 push @cmd_args, "-1";
37             }
38             # if a parent is specified: hp..h
39             # if not, but a merge commit: --cc h
40             # otherwise: --root h
41             if (defined $parent) {
42                 push @cmd_args, "$parent.." . $self->sha1;
43             } else {
44                 push @cmd_args, $self->parents->length > 1
45                     ? '--cc' : '--root';
46                 push @cmd_args, $self->sha1;
47             }
48             my $out = $self->_run_cmd( @cmd_args );
49             return $out;
50         }
51
52         method diff ( Maybe[Bool] :$patch?,
53                        Maybe[NonEmptySimpleStr] :$parent?,
54                        Maybe[NonEmptySimpleStr] :$file?
55                    ) {
56             $parent = $parent
57                 ? $parent
58                     : $self->parents <= 1
59                         ? $self->parent_sha1
60                             : '-c';
61             my @etc = (
62                 ( $file  ? ('--', $file) : () ),
63             );
64
65             my @out = $self->_raw_diff(
66                 ( $patch ? '--patch-with-raw' : () ),
67                 ( $parent ? $parent : () ),
68                 $self->sha1, @etc,
69             );
70
71             # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
72             my @difftree = $self->_parse_diff_tree(\@out);
73
74             return \@difftree
75                 unless $patch;
76
77             # The blank line between the tree and the patch.
78             shift @out;
79
80             # XXX And no I'm not happy about having diff return tree + patch.
81             return \@difftree, [$self->_parse_diff(@out)];
82         }
83
84         ## Private methods
85         # gitweb uses the following sort of command for diffing merges:
86         # /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 --
87         # and for regular diffs
88         # /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 --
89         method _raw_diff (@args) {
90             return $self->_run_cmd_list(
91                 qw(diff-tree -r -M --no-commit-id --full-index),
92                 @args
93             );
94         }
95
96         method _parse_diff_tree ($diff) {
97             my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
98             my @ret;
99             while (@$diff and $diff->[0] =~ /^:\d+/) {
100                 my $line = shift @$diff;
101                 # see. man git-diff-tree for more info
102                 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
103                 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
104                 my %line = zip @keys, @vals;
105                 # Some convenience keys
106                 $line{file}   = $line{src};
107                 $line{sha1}   = $line{sha1dst};
108                 $line{is_new} = $line{sha1src} =~ /^0+$/
109                     if $line{sha1src};
110                 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
111                     if $line{status} =~ /^R/;
112                 push @ret, \%line;
113             }
114
115             return @ret;
116         }
117
118         method _parse_diff (@diff) {
119             my @ret;
120             for (@diff) {
121                 # This regex is a little pathological.
122                 if (m{^diff --git (a/(.*?)) (b/\2)}) {
123                     push @ret, {
124                         head => $_,
125                         a    => $1,
126                         b    => $3,
127                         file => $2,
128                         diff => '',
129                     };
130                     next;
131                 }
132
133                 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
134                     @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
135                     next
136                 }
137
138                 # XXX Somewhat hacky. Ahem.
139                 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
140             }
141
142             return @ret;
143         }
144
145     }