Fix commit 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 '+_gpp_obj' => ( handles => [ 'comment',
13                                           'tree_sha1',
14                                           'committer',
15                                           'committed_time',
16                                           'author',
17                                           'authored_time',
18                                           'parents',
19                                           'parent_sha1',
20                                           'parent_sha1s',
21                                       ],
22                          );
23
24          method diff ( Maybe[Bool] :$patch?,
25                        Maybe[NonEmptySimpleStr] :$parent?,
26                        Maybe[NonEmptySimpleStr] :$file?
27                    ) {
28             $parent = $parent
29                 ? $parent
30                     : $self->parents <= 1
31                         ? $self->parent_sha1
32                             : '-c';
33             my @etc = (
34                 ( $file  ? ('--', $file) : () ),
35             );
36
37             my @out = $self->_raw_diff(
38                 ( $patch ? '--patch-with-raw' : () ),
39                 ( $parent ? $parent : () ),
40                 $self->sha1, @etc,
41             );
42
43             # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
44             my @difftree = $self->_parse_diff_tree(\@out);
45
46             return \@difftree
47                 unless $patch;
48
49             # The blank line between the tree and the patch.
50             shift @out;
51
52             # XXX And no I'm not happy about having diff return tree + patch.
53             return \@difftree, [$self->_parse_diff(@out)];
54         }
55
56         ## Private methods
57         # gitweb uses the following sort of command for diffing merges:
58         # /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 --
59         # and for regular diffs
60         # /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 --
61         method _raw_diff (@args) {
62             return $self->_run_cmd_list(
63                 qw(diff-tree -r -M --no-commit-id --full-index),
64                 @args
65             );
66         }
67
68         method _parse_diff_tree ($diff) {
69             my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
70             my @ret;
71             while (@$diff and $diff->[0] =~ /^:\d+/) {
72                 my $line = shift @$diff;
73                 # see. man git-diff-tree for more info
74                 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
75                 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
76                 my %line = zip @keys, @vals;
77                 # Some convenience keys
78                 $line{file}   = $line{src};
79                 $line{sha1}   = $line{sha1dst};
80                 $line{is_new} = $line{sha1src} =~ /^0+$/
81                     if $line{sha1src};
82                 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
83                     if $line{status} =~ /^R/;
84                 push @ret, \%line;
85             }
86
87             return @ret;
88         }
89
90         method _parse_diff (@diff) {
91             my @ret;
92             for (@diff) {
93                 # This regex is a little pathological.
94                 if (m{^diff --git (a/(.*?)) (b/\2)}) {
95                     push @ret, {
96                         head => $_,
97                         a    => $1,
98                         b    => $3,
99                         file => $2,
100                         diff => '',
101                     };
102                     next;
103                 }
104
105                 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
106                     @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
107                     next
108                 }
109
110                 # XXX Somewhat hacky. Ahem.
111                 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
112             }
113
114             return @ret;
115         }
116
117     }