Introduce ::Object::Commit.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2 use Moose::Autobox;
3
4 class Gitalist::Git::Object {
5     use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use File::Stat::ModeString qw/mode_to_string/;
8     use List::MoreUtils qw/any zip/;
9
10     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
11
12     # project and sha1 are required initargs
13     has project => ( isa => 'Gitalist::Git::Project',
14                      required => 1,
15                      is => 'ro',
16                      weak_ref => 1,
17                      handles => {
18                          _run_cmd => 'run_cmd',
19                          _run_cmd_list => 'run_cmd_list',
20                          _get_gpp_object => 'get_gpp_object',
21                      },
22                  );
23     has sha1 => ( isa => NonEmptySimpleStr,
24                   required => 1,
25                   is => 'ro' );
26
27     has $_ => ( isa => NonEmptySimpleStr,
28                 required => 1,
29                 is => 'ro',
30                 lazy_build => 1 )
31         for qw/type modestr size/;
32
33     has _gpp_obj => ( isa => 'Git::PurePerl::Object',
34                       required => 1,
35                       is => 'ro',
36                       lazy_build => 1,
37                       handles => [ 'parents',
38                                    'author',
39                                    'committer',
40                                ],
41                   );
42
43     # This feels wrong, but current templates assume
44     # these attributes are present on every object.
45     foreach my $key (qw/content/) {
46         has $key => ( isa => Str,
47                       required => 1,
48                       is => 'ro',
49                       lazy_build => 1,
50                   );
51         method "_build_$key" {
52             confess("Object can't " . $key) unless $self->_gpp_obj->can($key);
53             return $self->_gpp_obj->$key;
54         }
55     }
56
57     # objects can't determine their mode or filename
58     has file => ( isa => NonEmptySimpleStr,
59                   required => 0,
60                   is => 'ro' );
61     has mode => ( isa => Int,
62                   required => 1,
63                   default => 0,
64                   is => 'ro' );
65
66     has tree => ( isa => 'ArrayRef[Gitalist::Git::Object]',
67                   required => 0,
68                   is => 'ro',
69                   lazy_build => 1 );
70
71     method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
72
73     method _build_tree {
74         confess("Can't list_tree on a blob object.")
75             if $self->type eq 'blob';
76         my $output = $self->_run_cmd(qw/ls-tree -z/, $self->sha1);
77         return unless defined $output;
78
79         my @ret;
80         for my $line (split /\0/, $output) {
81             my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
82             push @ret, Gitalist::Git::Object->new( mode => oct $mode,
83                                     type => $type,
84                                     sha1 => $object,
85                                     file => $file,
86                                     project => $self->project,
87                                   );
88         }
89         return \@ret;
90     }
91
92     method diff ( Maybe[Bool] :$patch?,
93                   Maybe[NonEmptySimpleStr] :$parent?,
94                   Maybe[NonEmptySimpleStr] :$file?
95               ) {
96         # Use parent if specifed, else take the parent from the commit
97         # if there is only one, otherwise it was a merge commit.
98         $parent = $parent
99             ? $parent
100                 : $self->parents <= 1
101                     ? $self->parent_sha1
102                         : '-c';
103         my @etc = (
104             ( $file  ? ('--', $file) : () ),
105         );
106
107         my @out = $self->_raw_diff(
108             ( $patch ? '--patch-with-raw' : () ),
109             ( $parent ? $parent : () ),
110             $self->sha1, @etc,
111         );
112
113         # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
114         my @difftree = $self->_parse_diff_tree(\@out);
115
116         return \@difftree
117             unless $patch;
118
119         # The blank line between the tree and the patch.
120         shift @out;
121
122         # XXX And no I'm not happy about having diff return tree + patch.
123         return \@difftree, [$self->_parse_diff(@out)];
124     }
125
126 ## Private methods
127     # gitweb uses the following sort of command for diffing merges:
128     # /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 --
129     # and for regular diffs
130     # /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 --
131     method _raw_diff (@args) {
132         return $self->_run_cmd_list(
133             qw(diff-tree -r -M --no-commit-id --full-index),
134             @args
135         );
136     }
137
138     method _parse_diff_tree ($diff) {
139         my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
140         my @ret;
141         while (@$diff and $diff->[0] =~ /^:\d+/) {
142             my $line = shift @$diff;
143             # see. man git-diff-tree for more info
144             # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
145             my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
146             my %line = zip @keys, @vals;
147             # Some convenience keys
148             $line{file}   = $line{src};
149             $line{sha1}   = $line{sha1dst};
150             $line{is_new} = $line{sha1src} =~ /^0+$/
151                 if $line{sha1src};
152             @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
153                 if $line{status} =~ /^R/;
154             push @ret, \%line;
155         }
156
157         return @ret;
158     }
159
160     method _parse_diff (@diff) {
161         my @ret;
162         for (@diff) {
163             # This regex is a little pathological.
164             if (m{^diff --git (a/(.*?)) (b/\2)}) {
165                 push @ret, {
166                     head => $_,
167                     a    => $1,
168                     b    => $3,
169                     file => $2,
170                     diff => '',
171                 };
172                 next;
173             }
174
175             if (/^index (\w+)\.\.(\w+) (\d+)$/) {
176                 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
177                 next
178             }
179
180             # XXX Somewhat hacky. Ahem.
181             $ret[@ret ? -1 : 0]{diff} .= "$_\n";
182         }
183
184         return @ret;
185     }
186
187
188 ## Builders
189 method _build__gpp_obj {
190         return $self->_get_gpp_object($self->sha1)
191     }
192
193     foreach my $key (qw/ type size /) {
194         method "_build_$key" {
195             my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
196             chomp($v);
197             return $v;
198         }
199     }
200
201     method _build_modestr {
202         my $modestr = mode_to_string($self->mode);
203         return $modestr;
204     }
205
206     method _cat_file_with_flag ($flag) {
207         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
208     }
209
210 } # end class