blib/lib/Yukki/Model/Repository.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 292 CC 25 SCAR 43.6
CC
25
Cov
0%
make_tree
240.0
diff_blobs
20.0
list_files
12.0
CRAP 78.0
1 package Yukki::Model::Repository;
2 {
3 $Yukki::Model::Repository::VERSION = '0.140290';
4 }
5 use 5.12.1;
6 use Moose;
7  
8 extends 'Yukki::Model';
9  
10 use Yukki::Error qw( http_throw );
11 use Yukki::Model::File;
12  
13 use DateTime::Format::Mail;
14 use Git::Repository v1.18;
15 use MooseX::Types::Path::Class;
16  
17 # ABSTRACT: model for accessing objects in a git repository
18  
19  
20 has name => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 );
25  
26  
27 has repository_settings => (
28 is => 'ro',
29 isa => 'Yukki::Settings::Repository',
30 required => 1,
31 lazy => 1,
32 default => sub {
33 my $self = shift;
34 $self->app->settings->repositories->{$self->name};
35 },
36 handles => {
37 'title' => 'name',
38 'branch' => 'site_branch',
39 },
40 );
41  
42  
43 has repository_path => (
44 is => 'ro',
45 isa => 'Path::Class::Dir',
46 coerce => 1,
47 required => 1,
48 lazy_build => 1,
49 );
50  
51 sub _build_repository_path {
52 my $self = shift;
53
54 my $repo_settings = $self->repository_settings;
55 return $self->locate_dir('repository_path', $repo_settings->repository);
56 }
57  
58  
59 has git => (
60 is => 'ro',
61 isa => 'Git::Repository',
62 required => 1,
63 lazy_build => 1,
64 );
65  
66 sub _build_git {
67 my $self = shift;
68 return Git::Repository->new( git_dir => $self->repository_path );
69 }
70  
71  
72 sub author_name { shift->app->settings->anonymous->author_name }
73  
74  
75 sub author_email { shift->app->settings->anonymous->author_email }
76  
77  
78 sub make_tree {
79 my $self = shift;
80 my $base = shift;
81 my $path = shift;
82  
83 my (@new_path, @old_path, $blob);
84  
85 # This is a rename
86 if (ref $_[0]) {
87 my $new_path = shift;
88 $blob = shift;
89 @new_path = @$new_path;
90 @old_path = @$path;
91 }
92  
93 # Otherwise it's a store or delete
94 else {
95 $blob = shift;
96  
97 # Defined $blob -> Store
98 if (defined $blob) {
99 @new_path = @$path;
100 }
101  
102 # Undefined $blob -> delete
103 else {
104 @old_path = @$path;
105 }
106 }
107  
108 my ($new_mode, $new_type, $new_name, $old_name, $remove_here);
109  
110 # Parts to add or update
111 if (@new_path) {
112 $new_name = shift @new_path;
113  
114 # Create the file here?
115 if (@new_path == 0) {
116 $new_mode = '100644';
117 $new_type = 'blob';
118 }
119  
120 # Or we're still hunting down the tree
121 else {
122 $new_mode = '040000';
123 $new_type = 'tree';
124 }
125 }
126  
127 # Parts to remove
128 if (@old_path) {
129 $old_name = shift @old_path;
130 $remove_here = (@old_path == 0);
131 }
132  
133 my $git = $self->git;
134  
135 my $overwrite;
136 my @new_tree;
137 if (defined $base) {
138 my @old_tree = $git->run('ls-tree', $base);
139 for my $line (@old_tree) {
140 my ($old_mode, $old_type, $old_object_id, $old_file) = split /\s+/, $line, 4;
141  
142 if (defined $new_name and $old_file eq $new_name) {
143  
144 # The file already exists, we are doing an update
145 $overwrite++;
146  
147 # Cannot overwrite a file with a dir or a dir with a file
148 http_throw("cannot replace $old_type $new_name with $new_type")
149 if $old_type ne $new_type;
150  
151  
152 # Add the updated file to the tree
153 if ($new_type eq 'blob') {
154 push @new_tree, "$new_mode $new_type $blob\t$new_name";
155 }
156  
157 # The child tree contains both sides of the rename
158 elsif ($old_name eq $new_name) {
159 my $tree_id = $self->make_tree($old_object_id, \@old_path, \@new_path, $blob);
160 push @new_tree, "$new_mode $new_type $tree_id\t$new_name";
161 }
162  
163 # Add the updated tree contains only the rename/add
164 else {
165 my $tree_id = $self->make_tree($old_object_id, \@new_path, $blob);
166 push @new_tree, "$new_mode $new_type $tree_id\t$new_name";
167 }
168 }
169  
170 # If $old_name != $new_name and it matches this file
171 elsif (defined $old_name and $old_file eq $old_name) {
172  
173 # if ($remove_here) { ... do nothing ... }. The file will be
174 # omitted. \o/
175  
176 # Not yet removed, but we need to hunt it down and remove it
177 unless ($remove_here) {
178 my $tree_id = $self->make_tree($old_object_id, \@old_path);
179 push @new_tree, "040000 tree $tree_id\t$old_name";
180 }
181 }
182  
183 # It's something else, leave it be.
184 else {
185 push @new_tree, $line;
186 }
187 }
188 }
189
190 # If the file or tree we want to create was never encountered, add it
191 if ($new_name and not $overwrite) {
192  
193 # ...as a file
194 if ($new_type eq 'blob') {
195 push @new_tree, "$new_mode $new_type $blob\t$new_name";
196 }
197  
198 # ...as a tree
199 else {
200 my $tree_id = $self->make_tree(undef, \@new_path, $blob);
201 push @new_tree, "$new_mode $new_type $tree_id\t$new_name";
202 }
203 }
204  
205 # Now, build this new tree from the input we've generated
206 return $git->run('mktree', { input => join "\n", @new_tree });
207 }
208  
209  
210 sub make_blob {
211 my ($self, $name, $content) = @_;
212  
213 return $self->git->run('hash-object', '-t', 'blob', '-w', '--stdin', '--path', $name,
214 { input => $content });
215 }
216  
217  
218 sub make_blob_from_file {
219 my ($self, $name, $filename) = @_;
220  
221 return $self->git->run('hash-object', '-t', 'blob', '-w', '--path', $name, $filename);
222 }
223  
224  
225 sub find_root {
226 my ($self) = @_;
227  
228 my $old_tree_id;
229 my @ref_info = $self->git->run('show-ref', $self->branch);
230 REF: for my $line (@ref_info) {
231 my ($object_id, $name) = split /\s+/, $line, 2;
232  
233 if ($name eq $self->branch) {
234 $old_tree_id = $object_id;
235 last REF;
236 }
237 }
238  
239 return $old_tree_id;
240 }
241  
242  
243 sub commit_tree {
244 my ($self, $old_tree_id, $new_tree_id, $comment) = @_;
245  
246 return $self->git->run(
247 'commit-tree', $new_tree_id, '-p', $old_tree_id, {
248 input => $comment,
249 env => {
250 GIT_AUTHOR_NAME => $self->author_name,
251 GIT_AUTHOR_EMAIL => $self->author_email,
252 },
253 },
254 );
255 }
256  
257  
258 sub update_root {
259 my ($self, $old_commit_id, $new_commit_id) = @_;
260 $self->git->command('update-ref', $self->branch, $new_commit_id, $old_commit_id);
261 }
262  
263  
264 sub find_path {
265 my ($self, $path) = @_;
266  
267 my $object_id;
268 my @files = $self->git->run('ls-tree', $self->branch, $path);
269 FILE: for my $line (@files) {
270 my ($mode, $type, $id, $name) = split /\s+/, $line, 4;
271  
272 if ($name eq $path) {
273 $object_id = $id;
274 last FILE;
275 }
276 }
277  
278 return $object_id;
279 }
280  
281  
282 sub show {
283 my ($self, $object_id) = @_;
284 return $self->git->run('show', $object_id);
285 }
286  
287  
288 sub fetch_size {
289 my ($self, $path) = @_;
290  
291 my @files = $self->git->run('ls-tree', '-l', $self->branch, $path);
292 FILE: for my $line (@files) {
293 my ($mode, $type, $id, $size, $name) = split /\s+/, $line, 5;
294 return $size if $name eq $path;
295 }
296  
297 return;
298 }
299  
300  
301 sub list_files {
302 my ($self, $path) = @_;
303 my @files;
304  
305 my @tree_files = $self->git->run('ls-tree', $self->branch, $path . '/');
306 FILE: for my $line (@tree_files) {
307 my ($mode, $type, $id, $name) = split /\s+/, $line, 4;
308  
309 next unless $type eq 'blob';
310  
311 my $filetype;
312 if ($name =~ s/\.(?<filetype>[a-z0-9]+)$//) {
313 $filetype = $+{filetype};
314 }
315  
316 push @files, $self->file({ path => $name, filetype => $filetype });
317 }
318  
319 return @files;
320 }
321  
322  
323 sub file {
324 my ($self, $params) = @_;
325  
326 Yukki::Model::File->new(
327 %$params,
328 app => $self->app,
329 repository => $self,
330 );
331 }
332  
333  
334 sub default_file {
335 my $self = shift;
336  
337 return Yukki::Model::File->new(
338 full_path => $self->repository_settings->default_page,
339 app => $self->app,
340 repository => $self,
341 );
342 }
343  
344  
345 sub log {
346 my ($self, $full_path) = @_;
347  
348 my @lines = $self->git->run(
349 'log', $self->branch, '--pretty=format:%H~%an~%aD~%ar~%s', '--numstat',
350 '--', $full_path
351 );
352  
353 my @revisions;
354 my $current_revision;
355  
356 my $mode = 'log';
357 for my $line (@lines) {
358 given ($mode) {
359
360 # First line is the log line
361 when ('log') {
362 $current_revision = {};
363  
364 @{ $current_revision }{qw( object_id author_name date time_ago comment )}
365 = split /~/, $line, 5;
366  
367 $current_revision->{date} = DateTime::Format::Mail->parse_datetime(
368 $current_revision->{date}
369 );
370  
371 $mode = 'stat';
372 }
373  
374 # Remaining lines are the numstat
375 when ('stat') {
376 my ($added, $removed, $path) = split /\s+/, $line, 3;
377 if ($path eq $full_path) {
378 $current_revision->{lines_added} = $added;
379 $current_revision->{lines_removed} = $removed;
380 }
381  
382 $mode = 'skip';
383 }
384  
385 # Once we know the numstat, search for the blank and start over
386 when ('skip') {
387 push @revisions, $current_revision;
388 $mode = 'log' if $line !~ /\S/;
389 }
390  
391 default {
392 http_throw("invalid parse mode '$mode'");
393 }
394 }
395 }
396  
397 return @revisions;
398 }
399  
400  
401 sub diff_blobs {
402 my ($self, $path, $object_id_1, $object_id_2) = @_;
403  
404 my @lines = $self->git->run(
405 'diff', '--word-diff=porcelain', '--unified=10000000', '--patience',
406 $object_id_1, $object_id_2, '--', $path,
407 );
408  
409 my @chunks;
410 my $last_chunk_type = '';
411  
412 my $i = 0;
413 LINE: for my $line (@lines) {
414 next if $i++ < 5;
415  
416 my ($type, $detail) = $line =~ /^(.)(.*)$/;
417 given ($type) {
418 when ([ '~', ' ', '+', '-' ]) {
419 if ($last_chunk_type eq $type) {
420 $chunks[-1][1] .= $detail;
421 }
422 elsif ($type eq '~') {
423 $chunks[-1][1] .= "\n";
424 }
425 else {
426 push @chunks, [ $type, $detail ];
427 $last_chunk_type = $type;
428 }
429 }
430  
431 when ('\\') { }
432 default { warn "unknown diff line type $type" }
433 }
434 }
435  
436 return @chunks;
437 }
438  
439 1;
440  
441 __END__