Implement '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 List::MoreUtils qw/any zip/;
10         our $SHA1RE = qr/[0-9a-fA-F]{40}/;
11
12         has '+type' => ( default => 'commit' );
13         has '+_gpp_obj' => ( handles => [ 'comment',
14                                           'tree_sha1',
15                                           'committer',
16                                           'committed_time',
17                                           'author',
18                                           'authored_time',
19                                           'parents',
20                                           'parent_sha1',
21                                           'parent_sha1s',
22                                       ],
23                          );
24
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) {
29                 push @args, '-n';
30                 $refspec = $parent . '..' . $self->sha1;
31             }
32             push @args, '--root', $refspec;
33             my $out = $self->_run_cmd( @args );
34             return $out;
35         }
36
37         method diff ( Maybe[Bool] :$patch?,
38                        Maybe[NonEmptySimpleStr] :$parent?,
39                        Maybe[NonEmptySimpleStr] :$file?
40                    ) {
41             $parent = $parent
42                 ? $parent
43                     : $self->parents <= 1
44                         ? $self->parent_sha1
45                             : '-c';
46             my @etc = (
47                 ( $file  ? ('--', $file) : () ),
48             );
49
50             my @out = $self->_raw_diff(
51                 ( $patch ? '--patch-with-raw' : () ),
52                 ( $parent ? $parent : () ),
53                 $self->sha1, @etc,
54             );
55
56             # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
57             my @difftree = $self->_parse_diff_tree(\@out);
58
59             return \@difftree
60                 unless $patch;
61
62             # The blank line between the tree and the patch.
63             shift @out;
64
65             # XXX And no I'm not happy about having diff return tree + patch.
66             return \@difftree, [$self->_parse_diff(@out)];
67         }
68
69         ## Private methods
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),
77                 @args
78             );
79         }
80
81         method _parse_diff_tree ($diff) {
82             my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
83             my @ret;
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+$/
94                     if $line{sha1src};
95                 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
96                     if $line{status} =~ /^R/;
97                 push @ret, \%line;
98             }
99
100             return @ret;
101         }
102
103         method _parse_diff (@diff) {
104             my @ret;
105             for (@diff) {
106                 # This regex is a little pathological.
107                 if (m{^diff --git (a/(.*?)) (b/\2)}) {
108                     push @ret, {
109                         head => $_,
110                         a    => $1,
111                         b    => $3,
112                         file => $2,
113                         diff => '',
114                     };
115                     next;
116                 }
117
118                 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
119                     @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
120                     next
121                 }
122
123                 # XXX Somewhat hacky. Ahem.
124                 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
125             }
126
127             return @ret;
128         }
129
130     }