blib/lib/CPAN/InGit/Server.pm

filter: Statementstmt 96.4% 218 / 226 Branchbran 82.5% 109 / 132 Conditioncond 71.9% 77 / 107 MC/DCmcdc 54.1% 33 / 61 Subroutinesub 100% 39 / 39 Podpod 100% 15 / 15 totaltotal 84.6% 491 / 580 CC 109 SCAR 23.6
CC
109
Cov
87%
serve_author_file
28.5
__ANON__
18.0
_module_page
16.0
CRAP 10.6
« blib/lib/CPAN/InGit/MutableTree.pm ↑ Summary bin/cpangit-add »
1 package CPAN::InGit::Server;
2 3 our $VERSION = '0.006'; # VERSION
3 # ABSTRACT: A Mojolicious::Controller that serves the ArchiveTrees from a git repo
4  
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util 'refaddr', 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IO::Compress::Gzip qw( gzip $GzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use CPAN::InGit;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Mojolicious::Static;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 263 ▶sub cpan_repo($c) { $c->stash('cpan_repo') }
Subroutine cpan_repo: called CC=1 SCAR=0.0
Pod documented
22  
23 # Resolve each namespace directly, without falling back to another namespace.
24 111 ▶sub _tree_object($class, $repo, $tree_name, $branch_head_only=0) {
Subroutine _tree_object: called CC=9 SCAR=23.5
25 111 ▶ return undef unless defined $tree_name
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree_name and $tree_name =~ m[\A~(branch|tag|commit)/([^/]+)\z]u 0 111 111
Condition33%1/3
defined $tree_name and $tree_name =~ m[\A~(branch|tag|commit)/([^/]+)\z]u
!ll&&!rl&&r
00111
MC/DC0%0/2
defined $tree_name and $tree_name =~ m[\A~(branch|tag|commit)/([^/]+)\z]u
defined $tree_name $tree_name =~ m[\A~(branch|tag|commit)/([^/]+)\z]u
Truth table33%1/3
A and B
A= defined $tree_nameB= $tree_name =~ m[\A~(branch|tag|commit)/([^/]+)\z]u
ABresult
0X0
100
111
26 && $tree_name =~ m{\A~(branch|tag|commit)/([^/]+)\z};
27 111 my ($kind, $name)= ($1, $2);
28 111 ▶ return undef if $branch_head_only && $kind ne 'branch';
Branch: 1 branch
BranchTrueFalseTotal
if $branch_head_only and $kind ne "branch" 0 111 111
Condition66%2/3
$branch_head_only and $kind ne "branch"
!ll&&!rl&&r
10920
MC/DC0%0/2
$branch_head_only and $kind ne "branch"
$branch_head_only $kind ne "branch"
Truth table66%2/3
A and B
A= $branch_head_onlyB= $kind ne "branch"
ABresult
0X0
100
111
29 111 $name =~ tr{~}{/};
30 111 return eval {
31 111 ▶ $kind eq 'branch'? Git::Raw::Branch->lookup($repo->git_repo, $name, 1)
Branch: 3 branches
BranchTrueFalseTotal
$kind eq 'branch' ? : 96 15 111
$kind eq 'tag' ? : 8 7 15
$name =~ /\A[0-9a-fA-F]{40}\z/u ? : 7 0 7
32 : $kind eq 'tag'? Git::Raw::Reference->lookup("refs/tags/$name", $repo->git_repo)
33 : $name =~ /\A[0-9a-fA-F]{40}\z/? Git::Raw::Commit->lookup($repo->git_repo, $name)
34 : undef;
35 };
36 }
37  
38 437 ▶sub archive_tree($c) {
Subroutine archive_tree: called CC=9 SCAR=22.0
Pod documented
39 437 ▶ $c->stash->{archive_tree} //= do {
Condition100%3/3
$c->stash->{"archive_tree"} //= do { my $tree_name = $c->tree_name; my $cache = $c->tree_cache; my $object = $c->_tree_object($c->cpan_repo, $tree_name, $c->branch_head_only); my $current_tree = $object && eval { do { $c->cpan_repo->lookup_tree($object) } }; my $atree = $cache->{$tree_name}; if ($atree and !$current_tree || $atree->tree->id ne $current_tree->id) { delete $cache->{$tree_name}; $atree = undef; } ; if (not $atree and $current_tree) { $atree = $c->cpan_repo->get_archive_tree($object); if ($atree) { (my $name = $tree_name) =~ s[^~[^/]+/][]u; $name =~ tr[~][/]; $atree->name($name); $cache->{$tree_name} = $atree; } ; } ; $atree }
l!l&&r!l&&!r
3291044
MC/DC100%2/2
$c->stash->{"archive_tree"} //= do { my $tree_name = $c->tree_name; my $cache = $c->tree_cache; my $object = $c->_tree_object($c->cpan_repo, $tree_name, $c->branch_head_only); my $current_tree = $object && eval { do { $c->cpan_repo->lookup_tree($object) } }; my $atree = $cache->{$tree_name}; if ($atree and !$current_tree || $atree->tree->id ne $current_tree->id) { delete $cache->{$tree_name}; $atree = undef; } ; if (not $atree and $current_tree) { $atree = $c->cpan_repo->get_archive_tree($object); if ($atree) { (my $name = $tree_name) =~ s[^~[^/]+/][]u; $name =~ tr[~][/]; $atree->name($name); $cache->{$tree_name} = $atree; } ; } ; $atree }
$c->stash->{"archive_tree"} do { my $tree_name = $c->tree_name; my $cache = $c->tree_cache; my $object = $c->_tree_object($c->cpan_repo, $tree_name, $c->branch_head_only); my $current_tree = $object && eval { do { $c->cpan_repo->lookup_tree($object) } }; my $atree = $cache->{$tree_name}; if ($atree and !$current_tree || $atree->tree->id ne $current_tree->id) { delete $cache->{$tree_name}; $atree = undef; } ; if (not $atree and $current_tree) { $atree = $c->cpan_repo->get_archive_tree($object); if ($atree) { (my $name = $tree_name) =~ s[^~[^/]+/][]u; $name =~ tr[~][/]; $atree->name($name); $cache->{$tree_name} = $atree; } ; } ; $atree }
Truth table100%3/3
A //= B
A= $c->stash->{"archive_tree"}B= do { my $tree_name = $c->tree_name; my $cache = $c->tree_cache; my $object = $c->_tree_object($c->cpan_repo, $tree_name, $c->branch_head_only); my $current_tree = $object && eval { do { $c->cpan_repo->lookup_tree($object) } }; my $atree = $cache->{$tree_name}; if ($atree and !$current_tree || $atree->tree->id ne $current_tree->id) { delete $cache->{$tree_name}; $atree = undef; } ; if (not $atree and $current_tree) { $atree = $c->cpan_repo->get_archive_tree($object); if ($atree) { (my $name = $tree_name) =~ s[^~[^/]+/][]u; $name =~ tr[~][/]; $atree->name($name); $cache->{$tree_name} = $atree; } ; } ; $atree }
ABresult
1X1
011
000
40 108 my $tree_name= $c->tree_name;
41 108 my $cache= $c->tree_cache;
42 108 my $object= $c->_tree_object($c->cpan_repo, $tree_name, $c->branch_head_only);
43 108 ▶ my $current_tree= $object && eval { $c->cpan_repo->lookup_tree($object) };
Condition66%2/3
$object && eval { do { $c->cpan_repo->lookup_tree($object) } }
!ll&&!rl&&r
40104
MC/DC50%1/2
$object && eval { do { $c->cpan_repo->lookup_tree($object) } }
$object eval { do { $c->cpan_repo->lookup_tree($object) } }
Truth table66%2/3
A && B
A= $objectB= eval { do { $c->cpan_repo->lookup_tree($object) } }
ABresult
0X0
100
111
44 108 my $atree= $cache->{$tree_name};
45 108 ▶ if ($atree && (!$current_tree || $atree->tree->id ne $current_tree->id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atree and !$current_tree || $atree->tree->id ne $current_tree->id) 4 104 108
Condition100%6/6
$atree and !$current_tree || $atree->tree->id ne $current_tree->id
!ll&&!rl&&r
41634
not $current_tree or $atree->tree->id ne $current_tree->id
l!l&&r!l&&!r
1363
MC/DC100%3/3
$atree and !$current_tree || $atree->tree->id ne $current_tree->id
$atree not $current_tree $atree->tree->id ne $current_tree->id
Truth table100%4/4
A and not(B) or C
A= $atreeB= not $current_treeC= $atree->tree->id ne $current_tree->id
ABCresult
0XX0
1000
11X1
1011
46 4 delete $cache->{$tree_name};
47 4 $atree= undef;
48 }
49 108 ▶ if (!$atree && $current_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $atree and $current_tree) 41 67 108
Condition100%3/3
not $atree and $current_tree
!ll&&!rl&&r
63441
MC/DC100%2/2
not $atree and $current_tree
not $atree $current_tree
Truth table100%3/3
not(A) and B
A= not $atreeB= $current_tree
ABresult
0X0
100
111
50 41 $atree= $c->cpan_repo->get_archive_tree($object);
51 41 ▶ if ($atree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atree) 41 0 41
52 # reconstruct actual name for ArchiveTree->name attribute
53 41 (my $name= $tree_name) =~ s{^~[^/]+/}{};
54 41 $name =~ tr{~}{/};
55 41 $atree->name($name);
56 41 $cache->{$tree_name}= $atree;
57 }
58 }
59 108 $atree;
60 };
61 }
62  
63 115 ▶sub tree_name($c) {
Subroutine tree_name: called CC=3 SCAR=11.0
Pod documented
64 115 my $n= $c->stash('tree_name');
65 115 ▶ return $n if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 114 1 115
66 # back-compat: check "branch_name"
67 1 $n= $c->stash('branch_name');
68 1 ▶ return "~branch/".($n =~ tr{/}{~}r) if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 1 1
69 1 return undef;
70 }
71  
72 108 ▶sub tree_cache($c) {
Subroutine tree_cache: called CC=3 SCAR=17.3
Pod documented
73 # back-compat: check "branch_cache"
74 108 ▶ $c->stash->{tree_cache} //= $c->stash->{branch_cache} // do {
Condition33%2/6
$c->stash->{"tree_cache"} //= $c->stash->{"branch_cache"} // do { warn "no tree_cache or branch_cache, creating temporary"; $c->_new_cache }
l!l&&r!l&&!r
01080
$c->stash->{"branch_cache"} // do { warn "no tree_cache or branch_cache, creating temporary"; $c->_new_cache }
l!l&&r!l&&!r
10800
MC/DC0%0/3
$c->stash->{"tree_cache"} //= $c->stash->{"branch_cache"} // do { warn "no tree_cache or branch_cache, creating temporary"; $c->_new_cache }
$c->stash->{"tree_cache"} $c->stash->{"branch_cache"} do { warn "no tree_cache or branch_cache, creating temporary"; $c->_new_cache }
Truth table0%0/4
A //= B // C
A= $c->stash->{"tree_cache"}B= $c->stash->{"branch_cache"}C= do { warn "no tree_cache or branch_cache, creating temporary"; $c->_new_cache }
ABCresult
1XX1
01X1
0011
0000
75 0 warn "no tree_cache or branch_cache, creating temporary";
76 0 $c->_new_cache
77 }
78 }
79  
80 108 ▶sub branch_head_only($c) { $c->stash('branch_head_only') }
Subroutine branch_head_only: called CC=1 SCAR=0.0
Pod documented
81 9 ▶sub index_branches($c) { $c->stash('index_branches') }
Subroutine index_branches: called CC=1 SCAR=0.0
Pod documented
82 107 ▶sub module_index_max_rows($c) { $c->stash('module_index_max_rows') // 5000 }
Subroutine module_index_max_rows: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$c->stash("module_index_max_rows") // 5000
l!l
1295
MC/DC100%1/1
$c->stash("module_index_max_rows") // 5000
$c->stash("module_index_max_rows")
Truth table100%2/2
A
A= $c->stash("module_index_max_rows")
Aresult
11
00
83  
84 94 ▶sub _share_dir {
Subroutine _share_dir: called CC=2 SCAR=7.0
85 94 state $dir= do {
86 3 my $relative= path(__FILE__)->sibling('..', '..', '..', 'share');
87 3 ▶ -f $relative->child('archive_tree_index.html.ep')? $relative->realpath
Branch: 1 branch
BranchTrueFalseTotal
-f $relative->child('archive_tree_index.html.ep') ? : 0 3 3
88 : path(File::ShareDir::dist_dir('CPAN-InGit'));
89 };
90 94 return $dir;
91 }
92  
93 22 ▶sub _new_cache {
Subroutine _new_cache: called CC=2 SCAR=7.6
94 # the 'recent_limit' feature was added in 0.20
95 0 ▶ state $have_tree_rb_xs= eval 'use Tree::RB::XS 0.20';
Subroutine BEGIN: called CC=1 SCAR=1.2
96 22 my %hash;
97 22 ▶ tie %hash, 'Tree::RB::XS', track_recent => 1, recent_limit => 20
Branch: 1 branch
BranchTrueFalseTotal
if $have_tree_rb_xs 0 22 22
98 if $have_tree_rb_xs;
99 22 \%hash;
100 }
101  
102  
103 37 ▶sub mount($class, $base_route, $cpan_repo, %options) {
Subroutine mount: called CC=8 SCAR=21.0
Pod documented
104 37 my $atree= $options{archive_tree};
105 37 my $app= delete $options{app};
106 # Ensure there is a cache if serving all branches
107 37 ▶ $options{branch_cache} //= $class->_new_cache
Branch: 1 branch
BranchTrueFalseTotal
unless defined $atree 23 14 37
Condition66%2/3
$options{"branch_cache"} //= $class->_new_cache
l!l&&r!l&&!r
1220
MC/DC0%0/2
$options{"branch_cache"} //= $class->_new_cache
$options{"branch_cache"} $class->_new_cache
Truth table66%2/3
A //= B
A= $options{"branch_cache"}B= $class->_new_cache
ABresult
1X1
011
000
108 unless defined $atree;
109 # This function could be called multiple times to mount various repos at different paths,
110 # so each instance needs a different route namespace.
111 # The route_ns is added as a stash attribute and used by actions when generating URLs.
112 37 my $route_ns= 'cpan_ingit_'.++$base_route->root->{_cpan_ingit_mount_count};
113 37 my $configured_route= $base_route->to(
114 namespace => '',
115 controller => $class,
116 cpan_ingit_route_ns => $route_ns,
117 cpan_repo => $cpan_repo,
118 %options
119 );
120 # Register static before the catch-all branch index, and serve only the captured path
121 # so static lookup does not depend on how deeply the application is mounted.
122 37 my $static= Mojolicious::Static->new(paths => [$class->_share_dir->child('public')->to_string]);
123 45 ▶ $configured_route->get('/~static/*asset')->to(cb => sub($c) {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 45 local $c->stash->{path}= $c->stash('asset');
125 45 ▶ $static->dispatch($c) || $c->reply->not_found;
Branch: 1 branch
BranchTrueFalseTotal
$static->dispatch($c) or $c->reply->not_found 44 1 45
126 37 })->name("$route_ns.assets");
127 # If serving all branches, capture the branch from however many path components match,
128 # then differentiate the tree route from the base route.
129 37 my $tree_route= $configured_route;
130 37 ▶ if ($atree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atree) { } 14 23 37
131 14 ▶ croak "Not an ArchiveTree"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($atree) and $atree->can("get_path") 0 14 14
Condition33%1/3
builtin::blessed($atree) and $atree->can("get_path")
!ll&&!rl&&r
0014
MC/DC0%0/2
builtin::blessed($atree) and $atree->can("get_path")
builtin::blessed($atree) $atree->can("get_path")
Truth table33%1/3
A and B
A= builtin::blessed($atree)B= $atree->can("get_path")
ABresult
0X0
100
111
132 unless blessed($atree) && $atree->can('get_path');
133 } else {
134 23 $configured_route->get('/')->to(action => 'serve_repo_index');
135 23 ▶ $app or croak 'Multi-tree mounts require the app => $app option';
Branch: 1 branch
BranchTrueFalseTotal
$app or croak("Multi-tree mounts require the app => \$app option") 23 0 23
136 23 $class->_install_tree_rewrite($app, $base_route, $cpan_repo, $options{branch_head_only});
137 $tree_route= $configured_route->any('/*tree_name',
138 23 ▶ [tree_name => $options{branch_head_only}? qr{~branch/[^/]+} : qr{~(?:branch|tag|commit)/[^/]+} ]);
Branch: 1 branch
BranchTrueFalseTotal
$options{'branch_head_only'} ? : 1 22 23
139 }
140 37 $tree_route->get('/modules/02packages.details', [ format => ['txt','txt.gz'] ])
141 ->to(action => 'serve_package_details');
142 37 $tree_route->get('/authors/id/*author_path')->to(action => 'serve_author_file')->name("$route_ns.authors");
143 37 $tree_route->get('/modules/')->to(action => 'serve_module_list')->name("$route_ns.modules");
144 37 $tree_route->get('/', [ format => 0 ])
145 ->to(action => 'serve_index')->name("$route_ns.index");
146 }
147  
148 # Resolve friendly paths before Mojo computes its routing-cache key. Stop at
149 # this app's routes even when the app is embedded in another application.
150 23 ▶sub _install_tree_rewrite($class, $app, $base_route, $repo, $branch_head_only) {
Subroutine _install_tree_rewrite: called CC=3 SCAR=11.0
151 23 my @patterns;
152 23 for (my $route= $base_route; ; $route= $route->parent) {
153 46 ▶ croak 'Mount route does not belong to app' unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 0 46 46
154 46 ▶ last if refaddr($route) == refaddr($app->routes);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($route) == builtin::refaddr($app->routes) 23 23 46
155 23 unshift @patterns, $route->pattern;
156 }
157 159 ▶ $app->hook(before_routes => sub($c) {
Subroutine __ANON__: called CC=18 SCAR=28.9
158 159 ▶ my $original= $c->stash->{path} // $c->req->url->path->to_route;
Condition66%2/3
$c->stash->{"path"} // $c->req->url->path->to_route
l!l&&r!l&&!r
211380
MC/DC0%0/2
$c->stash->{"path"} // $c->req->url->path->to_route
$c->stash->{"path"} $c->req->url->path->to_route
Truth table66%2/3
A // B
A= $c->stash->{"path"}B= $c->req->url->path->to_route
ABresult
1X1
011
000
159 159 ▶ $original= "/$original" unless $original =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $original =~ m[^/]u 0 159 159
160 159 my $remaining= $original;
161 159 for my $pattern (@patterns) {
162 159 ▶ return unless $pattern->match_partial(\$remaining, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->match_partial(\$remaining, 0) 7 152 159
163 }
164 152 ▶ return if $remaining eq '' || $remaining eq '/' || $remaining =~ m{^/~};
Branch: 1 branch
BranchTrueFalseTotal
if $remaining eq "" or $remaining eq "/" or $remaining =~ m[^/~]u 69 83 152
Condition100%6/6
$remaining eq "" or $remaining eq "/" or $remaining =~ m[^/~]u
l!l&&r!l&&!r
96083
$remaining eq "" or $remaining eq "/"
l!l&&r!l&&!r
18143
MC/DC100%3/3
$remaining eq "" or $remaining eq "/" or $remaining =~ m[^/~]u
$remaining eq "" $remaining eq "/" $remaining =~ m[^/~]u
Truth table100%4/4
A or B or C
A= $remaining eq ""B= $remaining eq "/"C= $remaining =~ m[^/~]u
ABCresult
1XX1
01X1
0011
0000
165 83 my $prefix= substr($original, 0, length($original)-length($remaining));
166 83 my @parts;
167 83 ▶ while ($remaining =~ m{\G/([^/]+)}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining =~ m[\G/([^/]+)]cgu) 119 5 124
168 119 push @parts, $1;
169 119 my $name= join '/', @parts;
170 119 my $canonical;
171 119 ▶ if (eval { Git::Raw::Branch->lookup($repo->git_repo, $name, 1) }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { 'Git::Raw::Branch'->lookup($repo->git_repo, $name, 1) } }) { } 72 47 119
elsif (not $branch_head_only) { } 44 3 47
172 72 $canonical= '~branch/'.join('~',@parts);
173 } elsif (!$branch_head_only) {
174 44 ▶ if (eval { Git::Raw::Reference->lookup("refs/tags/$name", $repo->git_repo) }) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { 'Git::Raw::Reference'->lookup("refs/tags/$name", $repo->git_repo) } }) { } 2 42 44
elsif (@parts == 1 and $name =~ /\A[0-9a-fA-F]{6,40}\z/u and my $commit = eval { do { 'Git::Raw::Commit'->lookup($repo->git_repo, $name) } }) { } 2 40 42
elsif ($name eq 'HEAD') { } 3 37 40
175 2 $canonical= '~tag/'.join('~',@parts);
176 }
177 ▶ elsif (@parts == 1 && $name =~ /\A[0-9a-fA-F]{6,40}\z/
Condition83%5/6
@parts == 1 and $name =~ /\A[0-9a-fA-F]{6,40}\z/u and my $commit = eval { do { "Git::Raw::Commit"->lookup($repo->git_repo, $name) } }
!ll&&!rl&&r
4002
@parts == 1 and $name =~ /\A[0-9a-fA-F]{6,40}\z/u
!ll&&!rl&&r
11292
MC/DC66%2/3
@parts == 1 and $name =~ /\A[0-9a-fA-F]{6,40}\z/u and my $commit = eval { do { "Git::Raw::Commit"->lookup($repo->git_repo, $name) } }
@parts == 1 $name =~ /\A[0-9a-fA-F]{6,40}\z/u my $commit = eval { do { "Git::Raw::Commit"->lookup($repo->git_repo, $name) } }
Truth table75%3/4
A and B and C
A= @parts == 1B= $name =~ /\A[0-9a-fA-F]{6,40}\z/uC= my $commit = eval { do { "Git::Raw::Commit"->lookup($repo->git_repo, $name) } }
ABCresult
0XX0
10X0
1100
1111
178 2 && (my $commit= eval { Git::Raw::Commit->lookup($repo->git_repo, $name) })
179 ) {
180 2 $canonical= '~commit/'.$commit->id;
181 }
182 # HEAD is a friendly alias, not an explicit branch name.
183 elsif ($name eq 'HEAD') {
184 3 my $commit= eval { $repo->git_repo->head->peel('commit') };
185 3 ▶ $canonical= '~commit/'.$commit->id if $commit;
Branch: 1 branch
BranchTrueFalseTotal
if $commit 2 1 3
186 }
187 }
188 119 ▶ next unless $canonical;
Branch: 1 branch
BranchTrueFalseTotal
unless $canonical 41 78 119
189 78 $c->stash->{path}= "$prefix/$canonical".substr($remaining, pos($remaining));
190 78 last;
191 }
192 23 });
193 }
194  
195  
196 9 ▶sub serve_repo_index($c) {
Subroutine serve_repo_index: called CC=6 SCAR=17.9
Pod documented
197 9 my @branches;
198 9 ▶ my $index_branches= $c->index_branches // 0;
Condition100%2/2
$c->index_branches // 0
l!l
54
MC/DC100%1/1
$c->index_branches // 0
$c->index_branches
Truth table100%2/2
A
A= $c->index_branches
Aresult
11
00
199 9 ▶ if ($index_branches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_branches) 5 4 9
200 5 my $route_ns= $c->stash('cpan_ingit_route_ns');
201 5 for my $branch (sort { $a->shorthand cmp $b->shorthand } $c->cpan_repo->git_repo->branches('local')) {
202 10 my $tree= $branch->peel('tree');
203 10 ▶ next unless $tree->entry_bypath('cpan_ingit.json')
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->entry_bypath("cpan_ingit.json") and $tree->entry_bypath("modules/02packages.details.txt") 2 8 10
Condition66%2/3
$tree->entry_bypath("cpan_ingit.json") and $tree->entry_bypath("modules/02packages.details.txt")
!ll&&!rl&&r
208
MC/DC50%1/2
$tree->entry_bypath("cpan_ingit.json") and $tree->entry_bypath("modules/02packages.details.txt")
$tree->entry_bypath("cpan_ingit.json") $tree->entry_bypath("modules/02packages.details.txt")
Truth table66%2/3
A and B
A= $tree->entry_bypath("cpan_ingit.json")B= $tree->entry_bypath("modules/02packages.details.txt")
ABresult
0X0
100
111
204 && $tree->entry_bypath('modules/02packages.details.txt');
205 8 my $name= $branch->shorthand;
206 8 push @branches, { name => $name, url => $c->url_for("$route_ns.index", tree_name => '~branch/'.($name =~ tr{/}{~}r))->path->trailing_slash(1) };
207 }
208 }
209 9 $c->render(inline => $c->_share_dir->child('repo_index.html.ep')->slurp('UTF-8'), handler => 'ep', format => 'html',
210 index_branches => $index_branches, branches => \@branches);
211 }
212  
213  
214 109 ▶sub _module_index($c) {
Subroutine _module_index: called CC=7 SCAR=19.6
215 109 my $atree= $c->archive_tree;
216 109 my $blob= $atree->package_details_blob;
217 109 ▶ return undef unless $blob;
Branch: 1 branch
BranchTrueFalseTotal
unless $blob 2 107 109
218 107 my $index= $atree->{_server_module_index};
219 107 ▶ unless ($index && $index->{blob_id} eq $blob->id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index and $index->{"blob_id"} eq $blob->id) 53 54 107
Condition66%2/3
$index and $index->{"blob_id"} eq $blob->id
!ll&&!rl&&r
53054
MC/DC50%1/2
$index and $index->{"blob_id"} eq $blob->id
$index $index->{"blob_id"} eq $blob->id
Truth table66%2/3
A and B
A= $indexB= $index->{"blob_id"} eq $blob->id
ABresult
0X0
100
111
220 53 my ($headers, $body)= split /\r?\n\r?\n/, $blob->content, 2;
221 53 my ($updated)= $headers =~ /^Last-Updated:\s*([^\r\n]*)/m;
222 53 my @rows;
223 53 ▶ for my $line (split /\r?\n/, $body // '') {
Condition50%1/2
$body // ""
l!l
530
MC/DC0%0/1
$body // ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
224 3791 ▶ next unless $line =~ /^(\S+)\s+(\S+)\s+(\S+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^(\S+)\s+(\S+)\s+(\S+)\s*$/u 0 3791 3791
225 3791 push @rows, { name => $1, version => $2, path => $3, text => $line };
226 }
227 53 @rows= sort { $a->{name} cmp $b->{name} } @rows;
228 53 $index= $atree->{_server_module_index}= { blob_id => $blob->id, updated => $updated, rows => \@rows };
229 }
230 107 return $index;
231 }
232  
233 107 ▶sub _module_page($c, $index) {
Subroutine _module_page: called CC=16 SCAR=27.7
234 107 ▶ my $filter= $c->param('filter') // '';
Condition100%2/2
$c->param("filter") // ""
l!l
3077
MC/DC100%1/1
$c->param("filter") // ""
$c->param("filter")
Truth table100%2/2
A
A= $c->param("filter")
Aresult
11
00
235 107 my $needle= lc $filter;
236 107 my @matches= grep index(lc $_->{text}, $needle) >= 0, $index->{rows}->@*;
237 107 my ($after, $before)= map $c->param($_), qw(after before);
238 107 my ($start, $end)= (0, scalar @matches);
239 107 my $max= $c->module_index_max_rows;
240 107 ▶ if (defined $before) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $before) { } 3 104 107
241 3 ▶ --$end while $end && $matches[$end-1]{name} ge $before;
Branch: 1 branch
BranchTrueFalseTotal
$end and $matches[$end - 1]{"name"} ge $before and do { --$end } 1503 3 1506
Condition100%3/3
$end and $matches[$end - 1]{"name"} ge $before
!ll&&!rl&&r
121503
MC/DC100%2/2
$end and $matches[$end - 1]{"name"} ge $before
$end $matches[$end - 1]{"name"} ge $before
Truth table100%3/3
A and B
A= $endB= $matches[$end - 1]{"name"} ge $before
ABresult
0X0
100
111
242 3 ▶ $start= $end > $max? $end-$max : 0;
Branch: 1 branch
BranchTrueFalseTotal
$end > $max ? : 1 2 3
243 } else {
244 104 ▶ ++$start while $start < $end && defined $after && $matches[$start]{name} le $after;
Branch: 1 branch
BranchTrueFalseTotal
$start < $end and defined $after and $matches[$start]{"name"} le $after and do { ++$start } 2505 104 2609
Condition100%6/6
$start < $end and defined $after and $matches[$start]{"name"} le $after
!ll&&!rl&&r
10132505
$start < $end and defined $after
!ll&&!rl&&r
13882508
MC/DC100%3/3
$start < $end and defined $after and $matches[$start]{"name"} le $after
$start < $end defined $after $matches[$start]{"name"} le $after
Truth table100%4/4
A and B and C
A= $start < $endB= defined $afterC= $matches[$start]{"name"} le $after
ABCresult
0XX0
10X0
1100
1111
245 104 ▶ $end= $start+$max if $end > $start+$max;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $start + $max 4 100 104
246 }
247 107 my @rows= @matches[$start .. $end-1];
248 return {
249 modules => \@rows,
250 filter => $filter,
251 tree => $c->archive_tree->tree->id,
252 complete => $start == 0 && $end == @matches? JSON::PP::true : JSON::PP::false,
253 next_after => @rows && $end < @matches? $rows[-1]{name} : undef,
254 previous_before => @rows && $start > 0? $rows[0]{name} : undef,
255 107 ▶ };
Branch: 3 branches
BranchTrueFalseTotal
$start == 0 && $end == @matches ? : 97 10 107
@rows && $end < @matches ? : 6 101 107
@rows && $start > 0 ? : 4 103 107
Condition100%9/9
$start == 0 and $end == @matches
!ll&&!rl&&r
5597
@rows and $end < @matches
!ll&&!rl&&r
14876
@rows and $start > 0
!ll&&!rl&&r
14894
MC/DC100%2/2
$start == 0 and $end == @matches
$start == 0 $end == @matches
MC/DC100%2/2
@rows and $end < @matches
@rows $end < @matches
MC/DC100%2/2
@rows and $start > 0
@rows $start > 0
Truth table100%3/3
A and B
A= $start == 0B= $end == @matches
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= @rowsB= $end < @matches
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= @rowsB= $start > 0
ABresult
0X0
100
111
256 }
257  
258 62 ▶sub serve_module_list($c) {
Subroutine serve_module_list: called CC=5 SCAR=16.1
Pod documented
259 62 ▶ return undef unless $c->check_tree_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_tree_exists 1 61 62
260 61 ▶ return $c->render(status => 400, json => { error => 'Supply only one of after and before' })
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->param("after") and defined $c->param("before") 1 60 61
Condition100%3/3
defined $c->param("after") and defined $c->param("before")
!ll&&!rl&&r
5641
MC/DC100%2/2
defined $c->param("after") and defined $c->param("before")
defined $c->param("after") defined $c->param("before")
Truth table100%3/3
A and B
A= defined $c->param("after")B= defined $c->param("before")
ABresult
0X0
100
111
261 if defined $c->param('after') && defined $c->param('before');
262 60 ▶ my $index= $c->_module_index
Branch: 1 branch
BranchTrueFalseTotal
my $index = $c->_module_index or return $c->render("status", 404, "json", {"error", "No package index in this tree"}) 59 1 60
263 or return $c->render(status => 404, json => { error => 'No package index in this tree' });
264 59 $c->render(json => $c->_module_page($index));
265 }
266  
267 52 ▶sub serve_index($c) {
Subroutine serve_index: called CC=11 SCAR=24.4
Pod documented
268 52 ▶ return undef unless $c->check_tree_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_tree_exists 3 49 52
269 49 ▶ my $index= $c->_module_index
Branch: 1 branch
BranchTrueFalseTotal
my $index = $c->_module_index or return $c->render("status", 404, "text", "No package index in this tree") 48 1 49
270 or return $c->render(status => 404, text => 'No package index in this tree');
271 48 my $atree= $c->archive_tree;
272 48 my $origin= $atree->branch;
273 48 ▶ $origin //= $c->tree_name? $c->_tree_object($c->cpan_repo, $c->tree_name) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->tree_name ? : 3 1 4
Condition100%3/3
$origin //= $c->tree_name ? $c->_tree_object($c->cpan_repo, $c->tree_name) : undef
l!l&&r!l&&!r
4431
MC/DC100%2/2
$origin //= $c->tree_name ? $c->_tree_object($c->cpan_repo, $c->tree_name) : undef
$origin $c->tree_name ? $c->_tree_object($c->cpan_repo, $c->tree_name) : undef
Truth table100%3/3
A //= B
A= $originB= $c->tree_name ? $c->_tree_object($c->cpan_repo, $c->tree_name) : undef
ABresult
1X1
011
000
274 48 ▶ (undef, $origin)= $c->cpan_repo->lookup_tree($atree->name) unless $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless $origin 1 47 48
275 48 ▶ $origin= $origin->target while $origin && $origin->isa('Git::Raw::Tag');
Branch: 1 branch
BranchTrueFalseTotal
$origin and $origin->isa("Git::Raw::Tag") and do { $origin = $origin->target } 0 48 48
Condition33%1/3
$origin and $origin->isa("Git::Raw::Tag")
!ll&&!rl&&r
0480
MC/DC0%0/2
$origin and $origin->isa("Git::Raw::Tag")
$origin $origin->isa("Git::Raw::Tag")
Truth table33%1/3
A and B
A= $originB= $origin->isa("Git::Raw::Tag")
ABresult
0X0
100
111
276 48 ▶ my $commit= !$origin? undef
Branch: 2 branches
BranchTrueFalseTotal
!$origin ? : 0 48 48
$origin->isa('Git::Raw::Commit') ? : 3 45 48
277 : $origin->isa('Git::Raw::Commit')? $origin : $origin->peel('commit');
278 48 my $route_ns= $c->stash('cpan_ingit_route_ns');
279 $c->render(inline => $c->_share_dir->child('archive_tree_index.html.ep')->slurp('UTF-8'), handler => 'ep', format => 'html',
280 module_page => $c->_module_page($index), index_updated => $index->{updated},
281 48 ▶ commit_date => $commit? gmtime($commit->time)->strftime('%a, %d %b %Y %H:%M:%S GMT') : undef,
Branch: 1 branch
BranchTrueFalseTotal
$commit ? : 48 0 48
282 tree_label => $atree->name,
283 json_url => $c->url_for("$route_ns.modules")->path->trailing_slash(1),
284 script_url => $c->url_for("$route_ns.assets", asset => 'archive_tree_index.js'),
285 cve_script_url => $c->url_for("$route_ns.assets", asset => 'cpan_cves.js'),
286 authors_url => $c->url_for("$route_ns.authors", author_path => ''));
287 }
288  
289  
290 142 ▶sub check_tree_exists($c) {
Subroutine check_tree_exists: called CC=2 SCAR=6.9
Pod documented
291 142 ▶ unless ($c->archive_tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->archive_tree) 4 138 142
292 4 $c->render(text => 'Git tree does not exist', status => 404);
293 4 return undef;
294 }
295 138 return 1;
296 }
297  
298  
299 4 ▶sub serve_package_details($c) {
Subroutine serve_package_details: called CC=2 SCAR=7.0
Pod documented
300 4 ▶ return undef unless $c->check_tree_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_tree_exists 0 4 4
301 4 my $blob= $c->archive_tree->package_details_blob;
302 $c->respond_to(
303 3 ▶ txt => sub { $c->render(text => $blob->content) },
Subroutine __ANON__: called CC=1 SCAR=0.0
304 1 ▶ 'txt.gz' => sub { $c->render_gzipped($blob->content, '02packages.details.txt.gz') },
Subroutine __ANON__: called CC=1 SCAR=0.0
305 4 );
306 }
307  
308  
309 24 ▶sub serve_author_file($c) {
Subroutine serve_author_file: called CC=14 SCAR=33.5
Pod documented
310 24 ▶ return undef unless $c->check_tree_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_tree_exists 0 24 24
311 24 my $path= 'authors/id/'.$c->stash->{author_path};
312 # cpanm adds extra '/' when the path name doesn't match the expected A/AU/AUTHOR format
313 24 $path =~ s,//+,/,g;
314 24 my ($basename)= ($path =~ m,([^/]+)\z,);
315 # Does the exact file exist?
316 24 my $ent= $c->archive_tree->get_path($path);
317 24 ▶ $c->log->debug("Path $path is ".($ent? ($ent->[0]->is_blob? 'blob ':'tree ').$ent->[0]->id : '<undef>'));
Branch: 2 branches
BranchTrueFalseTotal
$ent ? : 23 1 24
$ent->[0]->is_blob ? : 23 0 23
318 24 ▶ if ($ent) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ent) { } 23 1 24
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
319 23 ▶ if ($ent->[0]->is_blob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ent->[0]->is_blob) { } 23 0 23
320 23 $c->res->headers->content_disposition(qq{attachment; filename="$basename"});
321 23 $c->render(data => $ent->[0]->content);
322 } else {
323 0 $c->render(status => 403, text => 'not a file');
324 }
325 }
326 ▶ 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
327 && ($ent= $c->archive_tree->get_path($1)) && $ent->[0]->is_blob
328 ) {
329 0 $c->log->debug("Path $1 is ".$ent->[0]->id.', will gzip it');
330 0 $c->render_gzipped($ent->[0]->content, $basename);
331 }
332 ▶ 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
333 # Don't auto-tar trees that don't look like distributions
334 && ($ent= $c->archive_tree->get_path("$1.meta")) && $ent->[0]->is_blob
335 && ($ent= $c->archive_tree->get_path($1)) && $ent->[0]->is_tree
336 ) {
337 1 $c->log->debug("Path $1 is ".$ent->[0]->id.', will tar+gzip it');
338 1 my $tar= Archive::Tar->new;
339 1 $c->cpan_repo->add_git_tree_to_tar($tar, substr($basename, 0, -7), $ent->[0]);
340 1 $c->render_gzipped($tar->write, $basename);
341 }
342 else {
343 0 $c->render(status => 404, text => 'No such path in branch');
344 }
345 }
346  
347  
348 2 ▶sub render_gzipped($c, $data, $filename='') {
Subroutine render_gzipped: called CC=4 SCAR=14.2
Pod documented
349 2 my $gzipped;
350 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
351 or return $c->reply->exception("gzip failed: $GzipError");
352 2 $c->res->headers->content_type('application/gzip');
353 2 ▶ $c->res->headers->content_disposition(qq{attachment; filename="$filename"})
Branch: 1 branch
BranchTrueFalseTotal
if length $filename 2 0 2
354 if length $filename;
355 2 return $c->render(data => $gzipped);
356 }
357  
358 3 1;
359  
360 __END__
« blib/lib/CPAN/InGit/MutableTree.pm ↑ Summary bin/cpangit-add »