Handle merges in the diff view.
[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 SimpleStr/;
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 get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
27                            Int $patch_count?) {
28             # assembling the git command to execute...
29             my @cmd = qw/format-patch --encoding=utf8 --stdout/;
30
31             # patch, or patch set?
32             push @cmd,
33                 defined $patch_count
34                 ? "-$patch_count -n" : "-1";
35
36             # refspec
37             if (defined $parent_hash) {
38                 #  if a parent is specified: hp..h
39                 push @cmd, "$parent_hash.." . $self->sha1;
40             } else {
41                 #  if not, but a merge commit: --cc h
42                 #  otherwise: --root h
43                 push @cmd, $self->parents->length > 1
44                     ? '--cc' : '--root';
45                 push @cmd, $self->sha1;
46             }
47             return $self->_run_cmd_fh( @cmd );
48         }
49
50         method diff ( Bool              :$patch?,
51                       NonEmptySimpleStr :$parent?,
52                       NonEmptySimpleStr :$filename?
53                     ) {
54             $parent = $parent
55                 ? $parent
56                     : $self->parents <= 1
57                         ? $self->parent_sha1
58                             : '-c';
59             my @etc = (
60                 ( $filename  ? ('--', $filename) : () ),
61             );
62
63             # If we're not comparing against something and we have multiple
64             # parents then it's a merge commit so show what was merged.
65             my $sha1 = $parent eq '-c' && @{[$self->parents]} > 1
66                  ? sprintf("%s^1..%s^2", ($self->sha1) x 2)
67                       : $self->sha1;
68
69             my @out = $self->_raw_diff(
70                 ( $patch ? '--patch-with-raw' : () ),
71                 ( $parent ? $parent : () ),
72                 $sha1, @etc,
73             );
74
75             # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
76             my @difftree = $self->_parse_diff_tree(\@out);
77
78             return \@difftree
79                 unless $patch;
80
81             # The blank line between the tree and the patch.
82             shift @out;
83
84             # XXX And no I'm not happy about having diff return tree + patch.
85             return \@difftree, [$self->_parse_diff(@out)];
86         }
87
88         ## Private methods
89         # gitweb uses the following sort of command for diffing merges:
90         # /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 --
91         # and for regular diffs
92         # /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 --
93         method _raw_diff (@args) {
94             return $self->_run_cmd_list(
95                 qw(diff-tree -r -M --no-commit-id --full-index),
96                 @args
97             );
98         }
99
100         method _parse_diff_tree ($diff) {
101             my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
102             my @ret;
103             while (@$diff and $diff->[0] =~ /^:\d+/) {
104                 my $line = shift @$diff;
105                 # see. man git-diff-tree for more info
106                 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
107                 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
108                 my %line = zip @keys, @vals;
109                 # Some convenience keys
110                 $line{file}   = $line{src};
111                 $line{sha1}   = $line{sha1dst};
112                 $line{is_new} = $line{sha1src} =~ /^0+$/
113                     if $line{sha1src};
114                 @line{qw/status sim/} = $line{status} =~ /(R)0*(\d+)/
115                     if $line{status} =~ /^R/;
116                 push @ret, \%line;
117             }
118
119             return @ret;
120         }
121
122         method _parse_diff (@diff) {
123             my @ret;
124             for (@diff) {
125                 # This regex is a little pathological.
126                 if (m{^diff --git (a/(.*?)) (b/\2)}) {
127                     push @ret, {
128                         head => $_,
129                         a    => $1,
130                         b    => $3,
131                         file => $2,
132                         diff => '',
133                     };
134                     next;
135                 }
136
137                 if (/^index (\w+)\.\.(\w+)(?: (\d+))?$/) {
138                     @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
139                     next
140                 }
141
142                 # XXX Somewhat hacky. Ahem.
143                 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
144             }
145
146             return @ret;
147         }
148
149
150   # XXX A prime candidate for caching.
151   method blame ( NonEmptySimpleStr $filename, SimpleStr $sha1 ) {
152     my @blameout = $self->_run_cmd_list(
153       blame => '-p', $sha1 ? $sha1 : $self->sha1, '--', $filename
154     );
155
156     my(%commitdata, @filedata);
157     while(defined(local $_ = shift @blameout)) {
158       my ($sha1, $orig_lineno, $lineno, $group_size) =
159         /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
160
161       $commitdata{$sha1} = {}
162         unless exists $commitdata{$sha1};
163
164       my $commit = $commitdata{$sha1};
165       my $line;
166       until(($line = shift @blameout) =~ s/^\t//) {
167         $commit->{$1} = $2
168          if $line =~ /^(\S+) (.*)/;
169       }
170
171       unless(exists $commit->{author_dt}) {
172         for my $t (qw/author committer/) {
173           my $dt = DateTime->from_epoch(epoch => $commit->{"$t-time"});
174           $dt->set_time_zone($commit->{"$t-tz"});
175           $commit->{"$t\_dt"} = $dt;
176         }
177       }
178
179       push @filedata, {
180         line => $line,
181         commit => { sha1 => $sha1, %$commit },
182         meta => {
183           orig_lineno => $orig_lineno,
184           lineno => $lineno,
185           ( $group_size ? (group_size => $group_size) : () ),
186         },
187       };
188     }
189
190     return \@filedata;
191   }
192 }
193
194
195 1;
196
197 __END__
198
199 =head1 NAME
200
201 Gitalist::Git::Object::Commit
202
203 =head1 SYNOPSIS
204
205     my $commit = Repository->get_object($commit_sha1);
206
207 =head1 DESCRIPTION
208
209 Represents a commit object in a git repository.
210 Subclass of C<Gitalist::Git::Object>.
211
212
213 =head1 ATTRIBUTES
214
215 =head2 committer
216
217 =head2 committed_time
218
219 =head2 author
220
221 =head2 authored_time
222
223 =head2 comment
224
225 =head2 tree_sha1
226
227 =head2 parents
228
229 =head2 parent_sha1
230
231 =head2 parent_sha1s
232
233
234 =head1 METHODS
235
236 =head2 get_patch
237
238 =head2 diff
239
240 =head2 blame
241
242 =head1 AUTHORS
243
244 See L<Gitalist> for authors.
245
246 =head1 LICENSE
247
248 See L<Gitalist> for the license.
249
250 =cut