InGit/Server.pm

filter: Statementstmt 87.0% 87 / 100 Branchbran 60.5% 23 / 38 Conditioncond 38.4% 15 / 39 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 100% 24 / 24 Podpod 100% 10 / 10 totaltotal 69.1% 161 / 233 CC 34 SCAR 23.6
CC
34
Cov
71%
serve_author_file
28.5
mount
10.5
archive_tree
10.0
CRAP 10.6
« InGit/MutableTree.pm ↑ Summary
1 package CPAN::InGit::Server;
2 1 our $VERSION = '0.003'; # VERSION
3 # ABSTRACT: A Mojolicious::Controller that serves the ArchiveTrees from a git repo
4  
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util 'refaddr', 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Compress::Gzip qw( gzip $GzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use CPAN::InGit;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 8 ▶sub cpan_repo($c) { $c->stash('cpan_repo') }
Subroutine cpan_repo: called CC=1 SCAR=0.0
Pod documented
19  
20 15 ▶sub archive_tree($c) {
Subroutine archive_tree: called CC=9 SCAR=23.0
Pod documented
21 15 ▶ $c->stash->{archive_tree} //= do {
Condition66%2/3
$c->stash->{"archive_tree"} //= do { my $branch_name = $c->branch_name; my $cache = $c->branch_cache; my $atree = $cache->{$branch_name}; if ($atree) { my $current_tree = $c->cpan_repo->lookup_tree($branch_name); unless (defined $current_tree and $atree->tree->id eq $current_tree->id) { delete $cache->{$branch_name}; $atree = undef; } ; } ; if (not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)) { if ($c->branch_head_only and not defined $atree->branch) { $c->log->debug("Branch '${branch_name}' is not a branch HEAD"); $atree = undef; } else { $cache->{$branch_name} = $atree; } ; } ; $atree }
l!l&&r!l&&!r
960
MC/DC0%0/2
$c->stash->{"archive_tree"} //= do { my $branch_name = $c->branch_name; my $cache = $c->branch_cache; my $atree = $cache->{$branch_name}; if ($atree) { my $current_tree = $c->cpan_repo->lookup_tree($branch_name); unless (defined $current_tree and $atree->tree->id eq $current_tree->id) { delete $cache->{$branch_name}; $atree = undef; } ; } ; if (not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)) { if ($c->branch_head_only and not defined $atree->branch) { $c->log->debug("Branch '${branch_name}' is not a branch HEAD"); $atree = undef; } else { $cache->{$branch_name} = $atree; } ; } ; $atree }
$c->stash->{"archive_tree"} do { my $branch_name = $c->branch_name; my $cache = $c->branch_cache; my $atree = $cache->{$branch_name}; if ($atree) { my $current_tree = $c->cpan_repo->lookup_tree($branch_name); unless (defined $current_tree and $atree->tree->id eq $current_tree->id) { delete $cache->{$branch_name}; $atree = undef; } ; } ; if (not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)) { if ($c->branch_head_only and not defined $atree->branch) { $c->log->debug("Branch '${branch_name}' is not a branch HEAD"); $atree = undef; } else { $cache->{$branch_name} = $atree; } ; } ; $atree }
Truth table66%2/3
A //= B
A= $c->stash->{"archive_tree"}B= do { my $branch_name = $c->branch_name; my $cache = $c->branch_cache; my $atree = $cache->{$branch_name}; if ($atree) { my $current_tree = $c->cpan_repo->lookup_tree($branch_name); unless (defined $current_tree and $atree->tree->id eq $current_tree->id) { delete $cache->{$branch_name}; $atree = undef; } ; } ; if (not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)) { if ($c->branch_head_only and not defined $atree->branch) { $c->log->debug("Branch '${branch_name}' is not a branch HEAD"); $atree = undef; } else { $cache->{$branch_name} = $atree; } ; } ; $atree }
ABresult
1X1
011
000
22 6 my $branch_name= $c->branch_name;
23 6 my $cache= $c->branch_cache;
24 6 my $atree= $cache->{$branch_name};
25 6 ▶ if ($atree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atree) 5 1 6
26 # check whether branch has updated since cached
27 5 my $current_tree= $c->cpan_repo->lookup_tree($branch_name);
28 5 ▶ unless (defined $current_tree and $atree->tree->id eq $current_tree->id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current_tree and $atree->tree->id eq $current_tree->id) 1 4 5
Condition66%2/3
defined $current_tree and $atree->tree->id eq $current_tree->id
!ll&&!rl&&r
014
MC/DC50%1/2
defined $current_tree and $atree->tree->id eq $current_tree->id
defined $current_tree $atree->tree->id eq $current_tree->id
Truth table66%2/3
A and B
A= defined $current_treeB= $atree->tree->id eq $current_tree->id
ABresult
0X0
100
111
29 1 delete $cache->{$branch_name};
30 1 $atree= undef;
31 }
32 }
33 6 ▶ if (!$atree && ($atree= $c->cpan_repo->get_archive_tree($branch_name))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)) 2 4 6
Condition66%2/3
not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)
!ll&&!rl&&r
402
MC/DC50%1/2
not $atree and $atree = $c->cpan_repo->get_archive_tree($branch_name)
not $atree $atree = $c->cpan_repo->get_archive_tree($branch_name)
Truth table66%2/3
not(A) and B
A= not $atreeB= $atree = $c->cpan_repo->get_archive_tree($branch_name)
ABresult
0X0
100
111
34 2 ▶ if ($c->branch_head_only && !defined $atree->branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->branch_head_only and not defined $atree->branch) { } 0 2 2
Condition33%1/3
$c->branch_head_only and not defined $atree->branch
!ll&&!rl&&r
200
MC/DC0%0/2
$c->branch_head_only and not defined $atree->branch
$c->branch_head_only not defined $atree->branch
Truth table33%1/3
A and not(B)
A= $c->branch_head_onlyB= not defined $atree->branch
ABresult
0X0
100
111
35 0 $c->log->debug("Branch '$branch_name' is not a branch HEAD");
36 0 $atree= undef;
37 } else {
38 2 $cache->{$branch_name}= $atree;
39 }
40 }
41 6 $atree;
42 };
43 }
44  
45 6 ▶sub branch_name($c) { $c->stash('branch_name') }
Subroutine branch_name: called CC=1 SCAR=0.0
Pod documented
46  
47 6 ▶sub branch_cache($c) {
Subroutine branch_cache: called CC=2 SCAR=11.6
Pod documented
48 6 ▶ $c->stash->{branch_cache} //= do {
Condition33%1/3
$c->stash->{"branch_cache"} //= do { warn "no branch_cache, creating temporary"; $c->_new_cache }
l!l&&r!l&&!r
600
MC/DC0%0/2
$c->stash->{"branch_cache"} //= do { warn "no branch_cache, creating temporary"; $c->_new_cache }
$c->stash->{"branch_cache"} do { warn "no branch_cache, creating temporary"; $c->_new_cache }
Truth table33%1/3
A //= B
A= $c->stash->{"branch_cache"}B= do { warn "no branch_cache, creating temporary"; $c->_new_cache }
ABresult
1X1
011
000
49 0 warn "no branch_cache, creating temporary";
50 0 $c->_new_cache
51 }
52 }
53  
54 2 ▶sub branch_head_only($c) { $c->stash('branch_head_only') }
Subroutine branch_head_only: called CC=1 SCAR=0.0
Pod documented
55  
56 1 ▶sub _new_cache {
Subroutine _new_cache: called CC=2 SCAR=7.6
57 # the 'recent_limit' feature was added in 0.20
58 1 ▶ state $have_tree_rb_xs= eval 'use Tree::RB::XS 0.20';
Subroutine BEGIN: called CC=1 SCAR=1.2
59 1 my %hash;
60 1 ▶ tie %hash, 'Tree::RB::XS', track_recent => 1, recent_limit => 20
Branch: 1 branch
BranchTrueFalseTotal
if $have_tree_rb_xs 0 1 1
61 if $have_tree_rb_xs;
62 1 \%hash;
63 }
64  
65  
66 1 ▶sub mount($class, $base_route, $cpan_repo, %options) {
Subroutine mount: called CC=6 SCAR=23.5
Pod documented
67 1 my $atree= $options{archive_tree};
68 1 ▶ if ($atree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atree) { } 0 1 1
69 0 ▶ croak "Not an ArchiveTree"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($atree) and $atree->can("get_path") 0 0 0
Condition0%0/3
builtin::blessed($atree) and $atree->can("get_path")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($atree) and $atree->can("get_path")
builtin::blessed($atree) $atree->can("get_path")
Truth table0%0/3
A and B
A= builtin::blessed($atree)B= $atree->can("get_path")
ABresult
0X0
100
111
70 unless blessed($atree) && $atree->can('get_path');
71 } else {
72 # Ensure there is a cache if serving all branches
73 1 ▶ $options{branch_cache} //= $class->_new_cache;
Condition33%1/3
$options{"branch_cache"} //= $class->_new_cache
l!l&&r!l&&!r
010
MC/DC0%0/2
$options{"branch_cache"} //= $class->_new_cache
$options{"branch_cache"} $class->_new_cache
Truth table33%1/3
A //= B
A= $options{"branch_cache"}B= $class->_new_cache
ABresult
1X1
011
000
74 }
75 1 $base_route= $base_route->to(namespace => '', controller => $class, cpan_repo => $cpan_repo, %options);
76 1 ▶ my $tree_route= $atree? $base_route
Branch: 1 branch
BranchTrueFalseTotal
$atree ? : 0 1 1
77 : $base_route->any('/:branch_name');
78 1 $tree_route->get('/modules/02packages.details', [ format => ['txt','txt.gz'] ])
79 ->to(action => 'serve_package_details');
80 1 $tree_route->get('/authors/id/*author_path')->to(action => 'serve_author_file');
81 }
82  
83  
84 6 ▶sub check_branch_exists($c) {
Subroutine check_branch_exists: called CC=2 SCAR=9.2
Pod documented
85 6 ▶ unless ($c->archive_tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->archive_tree) 0 6 6
86 0 $c->render(text => 'Git branch does not exist', status => 404);
87 0 return undef;
88 }
89 6 return 1;
90 }
91  
92  
93 2 ▶sub serve_package_details($c) {
Subroutine serve_package_details: called CC=2 SCAR=7.0
Pod documented
94 2 ▶ return undef unless $c->check_branch_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_branch_exists 0 2 2
95 2 my $blob= $c->archive_tree->package_details_blob;
96 $c->respond_to(
97 1 ▶ txt => sub { $c->render(text => $blob->content) },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 1 ▶ 'txt.gz' => sub { $c->render_gzipped($blob->content, '02packages.details.txt.gz') },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 2 );
100 }
101  
102  
103 4 ▶sub serve_author_file($c) {
Subroutine serve_author_file: called CC=14 SCAR=33.5
Pod documented
104 4 ▶ return undef unless $c->check_branch_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_branch_exists 0 4 4
105 4 my $path= 'authors/id/'.$c->stash->{author_path};
106 # cpanm adds extra '/' when the path name doesn't match the expected A/AU/AUTHOR format
107 4 $path =~ s,//+,/,g;
108 4 my ($basename)= ($path =~ m,([^/]+)\z,);
109 # Does the exact file exist?
110 4 my $ent= $c->archive_tree->get_path($path);
111 4 ▶ $c->log->debug("Path $path is ".($ent? ($ent->[0]->is_blob? 'blob ':'tree ').$ent->[0]->id : '<undef>'));
Branch: 2 branches
BranchTrueFalseTotal
$ent ? : 3 1 4
$ent->[0]->is_blob ? : 3 0 3
112 4 ▶ if ($ent) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ent) { } 3 1 4
elsif ($path =~ /(.*?)\.gz\z/u and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_blob) { } 0 1 1
elsif ($path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta") and $ent->[0]->is_blob and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_tree) { } 1 0 1
113 3 ▶ if ($ent->[0]->is_blob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ent->[0]->is_blob) { } 3 0 3
114 3 $c->res->headers->content_disposition(qq{attachment; filename="$basename"});
115 3 $c->render(data => $ent->[0]->content);
116 } else {
117 0 $c->render(status => 403, text => 'not a file');
118 }
119 }
120 ▶ elsif ($path =~ /(.*?)\.gz\z/
Condition33%2/6
$path =~ /(.*?)\.gz\z/u and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_blob
!ll&&!rl&&r
100
$path =~ /(.*?)\.gz\z/u and $ent = $c->archive_tree->get_path($1)
!ll&&!rl&&r
010
MC/DC0%0/3
$path =~ /(.*?)\.gz\z/u and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_blob
$path =~ /(.*?)\.gz\z/u $ent = $c->archive_tree->get_path($1) $ent->[0]->is_blob
Truth table25%1/4
A and B and C
A= $path =~ /(.*?)\.gz\z/uB= $ent = $c->archive_tree->get_path($1)C= $ent->[0]->is_blob
ABCresult
0XX0
10X0
1100
1111
121 && ($ent= $c->archive_tree->get_path($1)) && $ent->[0]->is_blob
122 ) {
123 0 $c->log->debug("Path $1 is ".$ent->[0]->id.', will gzip it');
124 0 $c->render_gzipped($ent->[0]->content, $basename);
125 }
126 ▶ elsif ($path =~ /(.*?)\.tar\.gz\z/
Condition33%4/12
$path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta") and $ent->[0]->is_blob and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_tree
!ll&&!rl&&r
001
$path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta") and $ent->[0]->is_blob and $ent = $c->archive_tree->get_path($1)
!ll&&!rl&&r
001
$path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta") and $ent->[0]->is_blob
!ll&&!rl&&r
001
$path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta")
!ll&&!rl&&r
001
MC/DC0%0/5
$path =~ /(.*?)\.tar\.gz\z/u and $ent = $c->archive_tree->get_path("$1.meta") and $ent->[0]->is_blob and $ent = $c->archive_tree->get_path($1) and $ent->[0]->is_tree
$path =~ /(.*?)\.tar\.gz\z/u $ent = $c->archive_tree->get_path("$1.meta") $ent->[0]->is_blob $ent = $c->archive_tree->get_path($1) $ent->[0]->is_tree
Truth table16%1/6
A and B and C and D and E
A= $path =~ /(.*?)\.tar\.gz\z/uB= $ent = $c->archive_tree->get_path("$1.meta")C= $ent->[0]->is_blobD= $ent = $c->archive_tree->get_path($1)E= $ent->[0]->is_tree
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
127 # Don't auto-tar trees that don't look like distributions
128 && ($ent= $c->archive_tree->get_path("$1.meta")) && $ent->[0]->is_blob
129 && ($ent= $c->archive_tree->get_path($1)) && $ent->[0]->is_tree
130 ) {
131 1 $c->log->debug("Path $1 is ".$ent->[0]->id.', will tar+gzip it');
132 1 my $tar= Archive::Tar->new;
133 1 $c->cpan_repo->add_git_tree_to_tar($tar, substr($basename, 0, -7), $ent->[0]);
134 1 $c->render_gzipped($tar->write, $basename);
135 }
136 else {
137 0 $c->render(status => 404, text => 'No such path in branch');
138 }
139 }
140  
141  
142 2 ▶sub render_gzipped($c, $data, $filename='') {
Subroutine render_gzipped: called CC=4 SCAR=14.2
Pod documented
143 2 my $gzipped;
144 2 ▶ gzip \$data => \$gzipped
Branch: 1 branch
BranchTrueFalseTotal
gzip(\$data, \$gzipped) or return $c->reply->exception("gzip failed: $CPAN::InGit::Server::GzipError") 2 0 2
145 or return $c->reply->exception("gzip failed: $GzipError");
146 2 $c->res->headers->content_type('application/gzip');
147 2 ▶ $c->res->headers->content_disposition(qq{attachment; filename="$filename"})
Branch: 1 branch
BranchTrueFalseTotal
if length $filename 2 0 2
148 if length $filename;
149 2 return $c->render(data => $gzipped);
150 }
151  
152 1 1;
153  
154 __END__
« InGit/MutableTree.pm ↑ Summary