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