Skilletor/Spec.pm

filter: Statementstmt 100% 272 / 272 Branchbran 97.1% 171 / 176 Conditioncond 84.5% 104 / 123 MC/DCmcdc 71.4% 50 / 70 Subroutinesub 100% 40 / 40 Podpod 100% 3 / 3 totaltotal 93.5% 640 / 684 CC 134 SCAR 31.6
CC
134
Cov
96%
_forge_link
53.4
_resolve_address
25.0
decode_uri_component
20.7
CRAP 23.6
« Skilletor/Engine.pm ↑ Summary Skilletor/Report.pm »
1 package Skilletor::Spec;
2 # ABSTRACT: Resolve an add shorthand (owner/repo, a forge link, a URL, a path) to a source
3  
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Skilletor::Entry qw( is_source_name );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Skilletor::Error::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Skilletor::JSON qw( js_stringify );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Skilletor::JSString qw( js_lc js_trim js_blank js_ws_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Skilletor::URL qw( parse_url );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 35 our $VERSION = '0.001';
16  
17 35 our @EXPORT_OK = qw( resolve_spec decode_uri_component normalize_name );
18  
19 35 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # The known forges (never probed), by host, with the name their errors use.
22 35 my %FORGES = (
23 'github.com' => 'GitHub', 'gitlab.com' => 'GitLab', 'codeberg.org' => 'Codeberg',
24 'hf.co' => 'Hugging Face', 'huggingface.co' => 'Hugging Face',
25 );
26 35 my $DEFAULT_REPO = 'skills';
27 35 my $FALLBACK_NAME = 'source';
28 35 my $FORMS = 'expected a local path (/path, ./path, ../path, ~/path), scheme://..., transport::address, user@host:path, '
29 .'github:owner[/repo], owner[/repo] or host.tld[/path]';
30 35 my $SCHEME = qr/\A[a-z][a-z0-9+.\-]*:(?=\/\/)/i;
31 35 my $TRANSPORT = qr/\A[A-Za-z][A-Za-z0-9+.\-]*::/;
32 35 my $WS = js_ws_re();
33 35 my $NOT_IN_SEGMENT = qr/(?:$WS|\p{Cc}|[:\@])/;
34  
35 182 ▶sub _fail { Skilletor::Error::Spec->throw( $_[0] ) }
Subroutine _fail: called CC=1 SCAR=0.0
36  
37 450 ▶sub normalize_name {
Subroutine normalize_name: called CC=1 SCAR=0.0
Pod documented
38 450 my ( $raw ) = @_;
39 450 my $s = js_lc($raw);
40 450 $s =~ s/\.git\z//;
41 450 $s =~ s/[^a-z0-9-]+/-/g;
42 450 $s =~ s/\A-+|-+\z//g;
43 450 return $s;
44 }
45  
46 21 ▶sub _basename {
Subroutine _basename: called CC=2 SCAR=6.9
47 21 my ( $path ) = @_;
48 21 my @parts = grep { length } split( WIN32 ? qr{[\\/]} : qr{/}, $path );
49 21 ▶ return @parts ? $parts[-1] : $path;
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 20 1 21
50 }
51  
52 563 ▶sub _is_local {
Subroutine _is_local: called CC=2 SCAR=6.9
53 563 my ( $spec ) = @_;
54 563 ▶ return 1 if $spec =~ m{\A(?:/|\./|\.\./|~)};
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ m[\A(?:/|\./|\.\./|~)] 19 544 563
55 544 return WIN32 && ( $spec =~ m{\A[A-Za-z]:[\\/]} || $spec =~ m{\A\\\\} || $spec =~ m{\A\.\.?\\} ) ? 1 : 0;
56 }
57  
58 544 ▶sub _has_scheme { $_[0] =~ $SCHEME ? 1 : 0 }
Subroutine _has_scheme: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$SCHEME/ ? : 182 362 544
59  
60 180 ▶sub _lower_scheme {
Subroutine _lower_scheme: called CC=1 SCAR=0.0
61 180 my ( $spec ) = @_;
62 180 ( my $s = $spec ) =~ s/($SCHEME)/lc $1/e;
63 180 return $s;
64 }
65  
66 544 ▶sub _helper_address {
Subroutine _helper_address: called CC=3 SCAR=11.0
67 544 my ( $spec ) = @_;
68 544 ▶ return undef unless $spec =~ /($TRANSPORT)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /($TRANSPORT)/ 522 22 544
69 22 my $len = length $1;
70 22 ▶ return length($spec) > $len ? substr( $spec, $len ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $spec > $len ? : 20 2 22
71 }
72  
73 245 ▶sub _check_github {
Subroutine _check_github: called CC=4 SCAR=13.9
74 245 my ( $spec, $owner, $repo ) = @_;
75 245 ▶ unless ( $owner =~ /\A[A-Za-z0-9-]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($owner =~ /\A[A-Za-z0-9-]+\z/) 48 197 245
76 48 _fail( 'cannot resolve "'.$spec.'": GitHub owner "'.$owner.'" may only contain ASCII letters, digits and "-"; '.$FORMS );
77 }
78 197 ▶ if ( defined $repo && $repo !~ /\A[A-Za-z0-9._-]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $repo and not $repo =~ /\A[A-Za-z0-9._-]+\z/) 8 189 197
Condition100%3/3
defined $repo and not $repo =~ /\A[A-Za-z0-9._-]+\z/
!ll&&!rl&&r
871028
MC/DC100%2/2
defined $repo and not $repo =~ /\A[A-Za-z0-9._-]+\z/
defined $repo not $repo =~ /\A[A-Za-z0-9._-]+\z/
Truth table100%3/3
A and not(B)
A= defined $repoB= not $repo =~ /\A[A-Za-z0-9._-]+\z/
ABresult
0X0
100
111
79 8 _fail( 'cannot resolve "'.$spec.'": GitHub repo "'.$repo.'" may only contain ASCII letters, digits, ".", "_" and "-"; '.$FORMS );
80 }
81 }
82  
83 56 ▶sub _check_forge {
Subroutine _check_forge: called CC=3 SCAR=11.0
84 56 my ( $spec, $forge, $owner, $repo ) = @_;
85 56 for my $pair ( [ owner => $owner ], [ repo => $repo ] ) {
86 110 my ( $what, $seg ) = @$pair;
87 110 ▶ if ( $seg =~ $NOT_IN_SEGMENT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg =~ /$NOT_IN_SEGMENT/) 4 106 110
88 4 _fail( 'cannot resolve "'.$spec.'": '.$forge.' '.$what.' "'.$seg
89 .'" must not contain whitespace, control characters, ":" or "@"; '.$FORMS );
90 }
91 }
92 }
93  
94 517 ▶sub _is_scp_like { $_[0] =~ m{\A[^\@/]+\@[^:/]+:} ? 1 : 0 }
Subroutine _is_scp_like: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[\A[^\@/]+\@[^:/]+:] ? : 22 495 517
95  
96 180 ▶sub _is_tarball { $_[0] =~ /\.(?:tar\.gz|tgz)\z/i ? 1 : 0 }
Subroutine _is_tarball: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\.(?:tar\.gz|tgz)\z/i ? : 28 152 180
97  
98 282 ▶sub _repo_name {
Subroutine _repo_name: called CC=4 SCAR=13.9
99 282 my ( $owner, $repo ) = @_;
100 282 ▶ my $name = defined $repo ? normalize_name($repo) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $repo ? : 237 45 282
101 282 ▶ return ( length $name && $name ne $DEFAULT_REPO ) ? $name : normalize_name($owner);
Branch: 1 branch
BranchTrueFalseTotal
length $name && $name ne $DEFAULT_REPO ? : 212 70 282
Condition100%3/3
length $name and $name ne $DEFAULT_REPO
!ll&&!rl&&r
4723212
MC/DC100%2/2
length $name and $name ne $DEFAULT_REPO
length $name $name ne $DEFAULT_REPO
Truth table100%3/3
A and B
A= length $nameB= $name ne $DEFAULT_REPO
ABresult
0X0
100
111
102 }
103  
104 118 ▶sub _name_from_path {
Subroutine _name_from_path: called CC=3 SCAR=11.0
105 118 my ( $path ) = @_;
106 118 my @segs = grep { length } split m{/}, $path;
107 118 ▶ return normalize_name($path) unless @segs;
Branch: 1 branch
BranchTrueFalseTotal
unless @segs 0 118 118
108 118 ▶ return _repo_name( $segs[0], @segs > 1 ? $segs[-1] : undef );
Branch: 1 branch
BranchTrueFalseTotal
@segs > 1 ? : 94 24 118
109 }
110  
111 155 ▶sub _name_from_url {
Subroutine _name_from_url: called CC=7 SCAR=19.5
112 155 my ( $spec, $kind ) = @_;
113 155 ▶ if ( _is_scp_like($spec) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_scp_like($spec)) 11 144 155
114 11 my $path = substr $spec, index( $spec, ':' ) + 1;
115 11 ▶ return _name_from_path($path) if $kind eq 'git';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "git" 9 2 11
116 2 my ($first) = split m{/}, $path, 2;
117 2 ▶ return normalize_name( _basename( $first // '' ) );
Condition50%1/2
$first // ""
l!l
20
MC/DC0%0/1
$first // ""
$first
Truth table50%1/2
A
A= $first
Aresult
11
00
118 }
119 144 my $u = parse_url($spec);
120 144 ▶ return normalize_name($spec) unless $u;
Branch: 1 branch
BranchTrueFalseTotal
unless $u 2 142 144
121 142 my @segs = grep { length } split m{/}, $u->{pathname};
122 142 ▶ return _name_from_path( $u->{pathname} ) if $kind eq 'git' && @segs;
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "git" and @segs 102 40 142
Condition100%3/3
$kind eq "git" and @segs
!ll&&!rl&&r
2515102
MC/DC100%2/2
$kind eq "git" and @segs
$kind eq "git" @segs
Truth table100%3/3
A and B
A= $kind eq "git"B= @segs
ABresult
0X0
100
111
123 40 return normalize_name( $u->{hostname} );
124 }
125  
126 20 ▶sub _name_from_helper {
Subroutine _name_from_helper: called CC=3 SCAR=11.0
127 20 my ( $address ) = @_;
128 20 ▶ return ( _has_scheme($address) || _is_scp_like($address) ) ? _name_from_url( $address, 'git' ) : _name_from_path($address);
Branch: 1 branch
BranchTrueFalseTotal
_has_scheme($address) || _is_scp_like($address) ? : 13 7 20
Condition100%3/3
_has_scheme($address) or _is_scp_like($address)
l!l&&r!l&&!r
1217
MC/DC100%2/2
_has_scheme($address) or _is_scp_like($address)
_has_scheme($address) _is_scp_like($address)
Truth table100%3/3
A or B
A= _has_scheme($address)B= _is_scp_like($address)
ABresult
1X1
011
000
129 }
130  
131 # decodeURIComponent: %XX runs must be UTF-8 of a scalar value; undef where it throws URIError.
132 102 ▶sub decode_uri_component {
Subroutine decode_uri_component: called CC=20 SCAR=30.3
Pod documented
133 102 my ( $s ) = @_;
134 102 my $out = '';
135 102 my $i = 0;
136 102 my $len = length $s;
137 102 ▶ while ( $i < $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 444 91 535
138 444 my $c = substr $s, $i, 1;
139 444 ▶ if ( $c ne '%' ) { $out .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "%") 422 22 444
140 22 my $byte = _hex_at( $s, $i );
141 22 ▶ return undef unless defined $byte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $byte 5 17 22
142 17 $i += 3;
143 17 ▶ if ( $byte < 0x80 ) { $out .= chr $byte; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($byte < 128) 9 8 17
144 8 ▶ my $n = $byte >= 0xC0 && $byte < 0xE0 ? 2 : $byte >= 0xE0 && $byte < 0xF0 ? 3 : $byte >= 0xF0 && $byte < 0xF8 ? 4 : 0;
Branch: 3 branches
BranchTrueFalseTotal
$byte >= 192 && $byte < 224 ? : 5 3 8
$byte >= 224 && $byte < 240 ? : 2 1 3
$byte >= 240 && $byte < 248 ? : 0 1 1
Condition55%5/9
$byte >= 192 and $byte < 224
!ll&&!rl&&r
035
$byte >= 224 and $byte < 240
!ll&&!rl&&r
012
$byte >= 240 and $byte < 248
!ll&&!rl&&r
010
MC/DC50%1/2
$byte >= 192 and $byte < 224
$byte >= 192 $byte < 224
MC/DC50%1/2
$byte >= 224 and $byte < 240
$byte >= 224 $byte < 240
MC/DC0%0/2
$byte >= 240 and $byte < 248
$byte >= 240 $byte < 248
Truth table66%2/3
A and B
A= $byte >= 192B= $byte < 224
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $byte >= 224B= $byte < 240
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $byte >= 240B= $byte < 248
ABresult
0X0
100
111
145 8 ▶ return undef unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 7 8
146 7 my @bytes = ($byte);
147 7 for ( 2 .. $n ) {
148 9 ▶ return undef unless substr( $s, $i, 1 ) eq '%';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($s, $i, 1) eq "%" 1 8 9
149 8 my $cont = _hex_at( $s, $i );
150 8 ▶ return undef unless defined $cont && ( $cont & 0xC0 ) == 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cont and ($cont & 192) == 128 2 6 8
Condition100%3/3
defined $cont and ($cont & 192) == 128
!ll&&!rl&&r
116
MC/DC100%2/2
defined $cont and ($cont & 192) == 128
defined $cont ($cont & 192) == 128
Truth table100%3/3
A and B
A= defined $contB= ($cont & 192) == 128
ABresult
0X0
100
111
151 6 push @bytes, $cont;
152 6 $i += 3;
153 }
154 4 my $copy = pack 'C*', @bytes;
155 4 my $char = eval { Encode::decode( 'UTF-8', $copy, Encode::FB_CROAK() ) };
156 4 ▶ return undef unless defined $char && length $char == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char and length $char == 1 2 2 4
Condition66%2/3
defined $char and length $char == 1
!ll&&!rl&&r
202
MC/DC50%1/2
defined $char and length $char == 1
defined $char length $char == 1
Truth table66%2/3
A and B
A= defined $charB= length $char == 1
ABresult
0X0
100
111
157 2 my $cp = ord $char;
158 2 ▶ return undef if $cp >= 0xD800 && $cp <= 0xDFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $cp >= 55296 and $cp <= 57343 0 2 2
Condition33%1/3
$cp >= 55296 and $cp <= 57343
!ll&&!rl&&r
200
MC/DC0%0/2
$cp >= 55296 and $cp <= 57343
$cp >= 55296 $cp <= 57343
Truth table33%1/3
A and B
A= $cp >= 55296B= $cp <= 57343
ABresult
0X0
100
111
159 2 $out .= $char;
160 }
161 91 return $out;
162 }
163  
164 30 ▶sub _hex_at {
Subroutine _hex_at: called CC=3 SCAR=11.0
165 30 my ( $s, $i ) = @_;
166 30 my $h = substr $s, $i + 1, 2;
167 30 ▶ return undef unless length $h == 2 && $h =~ /\A[0-9A-Fa-f]{2}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless length $h == 2 and $h =~ /\A[0-9A-Fa-f]{2}\z/ 6 24 30
Condition100%3/3
length $h == 2 and $h =~ /\A[0-9A-Fa-f]{2}\z/
!ll&&!rl&&r
4224
MC/DC100%2/2
length $h == 2 and $h =~ /\A[0-9A-Fa-f]{2}\z/
length $h == 2 $h =~ /\A[0-9A-Fa-f]{2}\z/
Truth table100%3/3
A and B
A= length $h == 2B= $h =~ /\A[0-9A-Fa-f]{2}\z/
ABresult
0X0
100
111
168 24 return hex $h;
169 }
170  
171 246 ▶sub _forge_link {
Subroutine _forge_link: called CC=53 SCAR=39.8
172 246 my ( $spec, $written, $rest, $explicit ) = @_;
173 246 my $host = js_lc($written);
174 246 my $forge = $FORGES{$host};
175 246 my ($before) = $rest =~ /\A([^?#]*)/;
176 246 my @segs = grep { length } split m{/}, $before;
177 246 ▶ my $hf = ( $forge eq 'Hugging Face' && @segs && ( $segs[0] eq 'datasets' || $segs[0] eq 'spaces' ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$forge eq 'Hugging Face' && @segs && ($segs[0] eq 'datasets' || $segs[0] eq 'spaces') ? : 8 238 246
Condition88%8/9
$forge eq "Hugging Face" and @segs and $segs[0] eq "datasets" || $segs[0] eq "spaces"
!ll&&!rl&&r
223158
$forge eq "Hugging Face" and @segs
!ll&&!rl&&r
223023
$segs[0] eq "datasets" or $segs[0] eq "spaces"
l!l&&r!l&&!r
6215
MC/DC75%3/4
$forge eq "Hugging Face" and @segs and $segs[0] eq "datasets" || $segs[0] eq "spaces"
$forge eq "Hugging Face" @segs $segs[0] eq "datasets" $segs[0] eq "spaces"
Truth table80%4/5
A and B and C or D
A= $forge eq "Hugging Face"B= @segsC= $segs[0] eq "datasets"D= $segs[0] eq "spaces"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
178 246 ▶ my @prefix = $hf ? ( shift @segs ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$hf ? : 8 238 246
179 246 my $owner = $segs[0];
180 246 ▶ if ( !defined $owner || js_blank($owner) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $owner or js_blank($owner)) 4 242 246
Condition100%3/3
not defined $owner or js_blank($owner)
l!l&&r!l&&!r
22242
MC/DC100%2/2
not defined $owner or js_blank($owner)
not defined $owner js_blank($owner)
Truth table100%3/3
not(A) or B
A= not defined $ownerB= js_blank($owner)
ABresult
1X1
011
000
181 4 ▶ return { kind => 'git', value => $explicit, derivedName => _name_from_url( $explicit, 'git' ) } if defined $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $explicit 0 4 4
182 4 _fail( 'cannot resolve "'.$spec.'": expected '.$written.'/'.join( '', map { $_.'/' } @prefix ).'owner[/repo]' );
183 }
184 242 my @tail = grep { !js_blank($_) } @segs[ 1 .. $#segs ];
185 242 my ( @repo_path, @view );
186 242 ▶ if ( $host eq 'gitlab.com' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host eq 'gitlab.com') { } 35 207 242
187 35 my ($dash) = grep { $tail[$_] eq '-' } 0 .. $#tail;
188 35 ▶ if ( defined $dash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dash) { } 11 24 35
189 11 @repo_path = @tail[ 0 .. $dash - 1 ];
190 11 @view = @tail[ $dash + 1 .. $#tail ];
191 }
192 else {
193 24 @repo_path = @tail;
194 }
195 }
196 else {
197 207 ▶ @repo_path = @tail ? ( $tail[0] ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@tail ? : 187 20 207
198 207 @view = @tail[ 1 .. $#tail ];
199 }
200 242 ▶ my $repo = @repo_path ? $repo_path[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@repo_path ? : 218 24 242
201 242 ▶ if ( defined $explicit && !@view ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit and not @view) 50 192 242
Condition100%3/3
defined $explicit and not @view
!ll&&!rl&&r
1543850
MC/DC100%2/2
defined $explicit and not @view
defined $explicit not @view
Truth table100%3/3
A and not(B)
A= defined $explicitB= not @view
ABresult
0X0
100
111
202 50 return { kind => 'git', value => $explicit, derivedName => _name_from_url( $explicit, 'git' ) };
203 }
204 192 ▶ if ( $host eq 'github.com' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host eq 'github.com') { } 139 53 192
205 139 _check_github( $spec, $owner, $repo );
206 }
207 else {
208 53 ▶ _check_forge( $spec, $written, $owner, $_ ) for ( @repo_path ? @repo_path : ($DEFAULT_REPO) );
Branch: 1 branch
BranchTrueFalseTotal
@repo_path ? : 45 8 53
209 }
210 164 ▶ my $value = 'https://'.$host.'/'.join( '/', @prefix, $owner, ( @repo_path ? @repo_path : ($DEFAULT_REPO) ) );
Branch: 1 branch
BranchTrueFalseTotal
@repo_path ? : 143 21 164
211 164 my %resolved = ( kind => 'git', value => $value, derivedName => _repo_name( $owner, $repo ) );
212 164 ▶ return \%resolved unless @view;
Branch: 1 branch
BranchTrueFalseTotal
unless @view 73 91 164
213  
214 91 ▶ my $fail = sub { _fail( 'cannot resolve "'.$spec.'": '.$_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
215 91 ▶ my $page = sub { $fail->( 'a '.$forge.' '.js_stringify( join '/', @view ).' page is not a source; add '.$value ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
216 93 ▶ my $decode = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
217 93 my ( $seg ) = @_;
218 93 my $d = decode_uri_component($seg);
219 93 ▶ $fail->( js_stringify($seg).' in the link is not a valid %-escape' ) unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 3 90 93
220 90 return $d;
221 91 };
222 54 ▶ my $tree = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
223 54 my ( $at ) = @_;
224 54 ▶ $fail->( 'a '.$forge.' tree link without a ref; add '.$value ) unless defined $view[$at];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $view[$at] 4 50 54
225 50 my %link = ( %resolved, ref => $decode->( $view[$at] ) );
226 48 my @sub = map { $decode->($_) } @view[ $at + 1 .. $#view ];
227 47 ▶ $link{path} = join '/', @sub if @sub;
Branch: 1 branch
BranchTrueFalseTotal
if @sub 22 25 47
228 47 return \%link;
229 91 };
230 15 ▶ my $pin = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
231 15 my ( $at, $what ) = @_;
232 15 ▶ unless ( defined $view[$at] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $view[$at]) 2 13 15
233 2 ▶ $fail->( 'a '.$forge.' '.$what.' link without a '.( $what eq 'release' ? 'tag' : 'commit' ).'; add '.$value );
Branch: 1 branch
BranchTrueFalseTotal
$what eq 'release' ? : 1 1 2
234 }
235 13 ▶ $page->() if @view > $at + 1;
Branch: 1 branch
BranchTrueFalseTotal
if @view > $at + 1 1 12 13
236 12 return { %resolved, ref => $decode->( $view[$at] ) };
237 91 };
238 7 ▶ my $file = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
239 7 my ( $at, $tree_of ) = @_;
240 7 my $ref = $view[$at];
241 7 ▶ my $pinned = defined $ref ? ', or '.$tree_of->($ref).' to pin ref "'.$ref.'"' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ref ? : 6 1 7
242 7 $fail->( 'a '.$forge.' file link; a source is a repository, not a file: add '.$value.$pinned );
243 91 };
244 91 my ( $v0, $v1 ) = @view;
245 91 ▶ $v1 //= undef;
Condition100%2/2
$v1 //= undef
l!l
7714
MC/DC100%1/1
$v1 //= undef
$v1
Truth table100%2/2
A
A= $v1
Aresult
11
00
246 91 ▶ if ( $forge eq 'GitHub' || $forge eq 'Hugging Face' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forge eq "GitHub" or $forge eq "Hugging Face") 69 22 91
Condition100%3/3
$forge eq "GitHub" or $forge eq "Hugging Face"
l!l&&r!l&&!r
63622
MC/DC100%2/2
$forge eq "GitHub" or $forge eq "Hugging Face"
$forge eq "GitHub" $forge eq "Hugging Face"
Truth table100%3/3
A or B
A= $forge eq "GitHub"B= $forge eq "Hugging Face"
ABresult
1X1
011
000
247 69 ▶ return $tree->(1) if $v0 eq 'tree';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "tree" 43 26 69
248 26 ▶ if ( $v0 eq 'blob' || ( $forge eq 'Hugging Face' && ( $v0 eq 'resolve' || $v0 eq 'raw' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0 eq "blob" or $forge eq "Hugging Face" and $v0 eq "resolve" || $v0 eq "raw") 5 21 26
Condition88%8/9
$v0 eq "blob" or $forge eq "Hugging Face" and $v0 eq "resolve" || $v0 eq "raw"
l!l&&r!l&&!r
4121
$forge eq "Hugging Face" and $v0 eq "resolve" || $v0 eq "raw"
!ll&&!rl&&r
1921
$v0 eq "resolve" or $v0 eq "raw"
l!l&&r!l&&!r
102
MC/DC75%3/4
$v0 eq "blob" or $forge eq "Hugging Face" and $v0 eq "resolve" || $v0 eq "raw"
$v0 eq "blob" $forge eq "Hugging Face" $v0 eq "resolve" $v0 eq "raw"
Truth table80%4/5
A or B and C or D
A= $v0 eq "blob"B= $forge eq "Hugging Face"C= $v0 eq "resolve"D= $v0 eq "raw"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
249 5 ▶ $file->( 1, sub { $value.'/tree/'.$_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
250 }
251 21 ▶ return $pin->( 1, 'commit' ) if $v0 eq 'commit';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "commit" 5 16 21
252 16 ▶ return $pin->( 2, 'release' ) if $forge eq 'GitHub' && $v0 eq 'releases' && defined $v1 && $v1 eq 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if $forge eq "GitHub" and $v0 eq "releases" and defined $v1 and $v1 eq "tag" 5 11 16
Condition100%9/9
$forge eq "GitHub" and $v0 eq "releases" and defined $v1 and $v1 eq "tag"
!ll&&!rl&&r
1015
$forge eq "GitHub" and $v0 eq "releases" and defined $v1
!ll&&!rl&&r
916
$forge eq "GitHub" and $v0 eq "releases"
!ll&&!rl&&r
187
MC/DC100%4/4
$forge eq "GitHub" and $v0 eq "releases" and defined $v1 and $v1 eq "tag"
$forge eq "GitHub" $v0 eq "releases" defined $v1 $v1 eq "tag"
Truth table100%5/5
A and B and C and D
A= $forge eq "GitHub"B= $v0 eq "releases"C= defined $v1D= $v1 eq "tag"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
253 11 $page->();
254 }
255 22 ▶ if ( $forge eq 'GitLab' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forge eq "GitLab") 11 11 22
256 11 ▶ return $tree->(1) if $v0 eq 'tree';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "tree" 5 6 11
257 6 ▶ $file->( 1, sub { $value.'/-/tree/'.$_[0] } ) if $v0 eq 'blob' || $v0 eq 'raw';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "blob" or $v0 eq "raw" 1 5 6
Condition66%2/3
$v0 eq "blob" or $v0 eq "raw"
l!l&&r!l&&!r
105
MC/DC50%1/2
$v0 eq "blob" or $v0 eq "raw"
$v0 eq "blob" $v0 eq "raw"
Truth table66%2/3
A or B
A= $v0 eq "blob"B= $v0 eq "raw"
ABresult
1X1
011
000
258 5 ▶ return $pin->( 1, 'commit' ) if $v0 eq 'commit';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "commit" 1 4 5
259 4 ▶ return $pin->( 1, 'release' ) if $v0 eq 'tags' || $v0 eq 'releases';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "tags" or $v0 eq "releases" 2 2 4
Condition100%3/3
$v0 eq "tags" or $v0 eq "releases"
l!l&&r!l&&!r
112
MC/DC100%2/2
$v0 eq "tags" or $v0 eq "releases"
$v0 eq "tags" $v0 eq "releases"
Truth table100%3/3
A or B
A= $v0 eq "tags"B= $v0 eq "releases"
ABresult
1X1
011
000
260 2 $page->();
261 }
262 # Codeberg (Forgejo): src|raw|media/branch|tag|commit/<ref>
263 11 my %kinds = map { $_ => 1 } qw( branch tag commit );
264 11 ▶ return $tree->(2) if $v0 eq 'src' && defined $v1 && $kinds{$v1};
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "src" and defined $v1 and $kinds{$v1} 6 5 11
Condition83%5/6
$v0 eq "src" and defined $v1 and $kinds{$v1}
!ll&&!rl&&r
416
$v0 eq "src" and defined $v1
!ll&&!rl&&r
407
MC/DC66%2/3
$v0 eq "src" and defined $v1 and $kinds{$v1}
$v0 eq "src" defined $v1 $kinds{$v1}
Truth table75%3/4
A and B and C
A= $v0 eq "src"B= defined $v1C= $kinds{$v1}
ABCresult
0XX0
10X0
1100
1111
265 5 ▶ $fail->( 'a '.$forge.' tree link without a ref; add '.$value ) if $v0 eq 'src' && !defined $v1;
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "src" and not defined $v1 0 5 5
Condition66%2/3
$v0 eq "src" and not defined $v1
!ll&&!rl&&r
410
MC/DC0%0/2
$v0 eq "src" and not defined $v1
$v0 eq "src" not defined $v1
Truth table66%2/3
A and not(B)
A= $v0 eq "src"B= not defined $v1
ABresult
0X0
100
111
266 5 ▶ if ( ( $v0 eq 'raw' || $v0 eq 'media' ) && defined $v1 && $kinds{$v1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0 eq "raw" || $v0 eq "media" and defined $v1 and $kinds{$v1}) 1 4 5
Condition66%6/9
$v0 eq "raw" || $v0 eq "media" and defined $v1 and $kinds{$v1}
!ll&&!rl&&r
401
$v0 eq "raw" || $v0 eq "media" and defined $v1
!ll&&!rl&&r
401
$v0 eq "raw" or $v0 eq "media"
l!l&&r!l&&!r
104
MC/DC25%1/4
$v0 eq "raw" || $v0 eq "media" and defined $v1 and $kinds{$v1}
$v0 eq "raw" $v0 eq "media" defined $v1 $kinds{$v1}
Truth table28%2/7
A or B and C and D
A= $v0 eq "raw"B= $v0 eq "media"C= defined $v1D= $kinds{$v1}
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
267 1 ▶ $file->( 2, sub { $value.'/src/'.$v1.'/'.$_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
268 }
269 4 ▶ return $pin->( 1, 'commit' ) if $v0 eq 'commit';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "commit" 1 3 4
270 3 ▶ return $pin->( 2, 'release' ) if $v0 eq 'releases' && defined $v1 && $v1 eq 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if $v0 eq "releases" and defined $v1 and $v1 eq "tag" 1 2 3
Condition66%4/6
$v0 eq "releases" and defined $v1 and $v1 eq "tag"
!ll&&!rl&&r
201
$v0 eq "releases" and defined $v1
!ll&&!rl&&r
201
MC/DC33%1/3
$v0 eq "releases" and defined $v1 and $v1 eq "tag"
$v0 eq "releases" defined $v1 $v1 eq "tag"
Truth table50%2/4
A and B and C
A= $v0 eq "releases"B= defined $v1C= $v1 eq "tag"
ABCresult
0XX0
10X0
1100
1111
271 2 $page->();
272 }
273  
274 575 ▶sub resolve_spec {
Subroutine resolve_spec: called CC=2 SCAR=6.9
Pod documented
275 575 my ( $spec, $probe ) = @_;
276 575 my $r = _resolve_address( $spec, $probe );
277 392 ▶ return is_source_name( $r->{derivedName} ) ? $r : { %$r, derivedName => $FALLBACK_NAME };
Branch: 1 branch
BranchTrueFalseTotal
is_source_name($r->{'derivedName'}) ? : 373 19 392
278 }
279  
280 575 ▶sub _resolve_address {
Subroutine _resolve_address: called CC=25 SCAR=32.2
281 575 my ( $spec, $probe ) = @_;
282 575 my $s = js_trim($spec);
283  
284 # An empty spec would be a bare word; an address kept verbatim must not reach git as an option.
285 575 ▶ _fail( 'cannot resolve "'.$spec.'": empty source; '.$FORMS ) if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 4 571 575
286 571 ▶ _fail( 'cannot resolve "'.$spec.'": a source must not start with "-"' ) if $s =~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A-/ 8 563 571
287  
288 # 1. Local paths.
289 563 ▶ return { kind => 'local', value => $s, derivedName => normalize_name( _basename($s) ) } if _is_local($s);
Branch: 1 branch
BranchTrueFalseTotal
if _is_local($s) 19 544 563
290  
291 # 2. Explicit URLs, scp-like and transport::address forms: kept verbatim, but a lower-case
292 # scheme; an https:// link to a known forge reads as in 4.
293 544 my $address = _helper_address($s);
294 544 ▶ return { kind => 'git', value => $s, derivedName => _name_from_helper($address) } if defined $address;
Branch: 1 branch
BranchTrueFalseTotal
if defined $address 20 524 544
295 524 ▶ if ( _has_scheme($s) || _is_scp_like($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_scheme($s) or _is_scp_like($s)) 180 344 524
Condition100%3/3
_has_scheme($s) or _is_scp_like($s)
l!l&&r!l&&!r
17010344
MC/DC100%2/2
_has_scheme($s) or _is_scp_like($s)
_has_scheme($s) _is_scp_like($s)
Truth table100%3/3
A or B
A= _has_scheme($s)B= _is_scp_like($s)
ABresult
1X1
011
000
296 180 my $value = _lower_scheme($s);
297 180 ▶ my $kind = _is_tarball($value) ? 'url' : 'git';
Branch: 1 branch
BranchTrueFalseTotal
_is_tarball($value) ? : 28 152 180
298 180 ▶ if ( $kind eq 'git' && $value =~ m{\A(https://([^/?#]+)/)} && exists $FORGES{ js_lc($2) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "git" and $value =~ m[\A(https://([^/?#]+)/)] and exists $FORGES{js_lc($2)}) 88 92 180
Condition100%6/6
$kind eq "git" and $value =~ m[\A(https://([^/?#]+)/)] and exists $FORGES{js_lc($2)}
!ll&&!rl&&r
632988
$kind eq "git" and $value =~ m[\A(https://([^/?#]+)/)]
!ll&&!rl&&r
2835117
MC/DC100%3/3
$kind eq "git" and $value =~ m[\A(https://([^/?#]+)/)] and exists $FORGES{js_lc($2)}
$kind eq "git" $value =~ m[\A(https://([^/?#]+)/)] exists $FORGES{js_lc($2)}
Truth table100%4/4
A and B and C
A= $kind eq "git"B= $value =~ m[\A(https://([^/?#]+)/)]C= exists $FORGES{js_lc($2)}
ABCresult
0XX0
10X0
1100
1111
299 88 my ( $whole, $host ) = ( $1, $2 );
300 88 my ($explicit) = $value =~ /\A([^?#]*)/;
301 88 return _forge_link( $spec, $host, substr( $value, length $whole ), $explicit );
302 }
303 92 return { kind => $kind, value => $value, derivedName => _name_from_url( $value, $kind ) };
304 }
305  
306 # 3. github:owner/repo, in any case.
307 344 ▶ if ( $s =~ /\Agithub:/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\Agithub:/i) 33 311 344
308 33 my $path = substr $s, length 'github:';
309 33 my ($first) = $path =~ m{\A([^/?#]*)};
310 33 ▶ _fail( 'cannot resolve "'.$spec.'": expected github:owner[/repo]' ) if js_blank($first);
Branch: 1 branch
BranchTrueFalseTotal
if js_blank($first) 7 26 33
311 26 return _forge_link( $spec, 'github.com', $path );
312 }
313  
314 311 my $slash = index $s, '/';
315 311 ▶ my $first_seg = $slash == -1 ? $s : substr( $s, 0, $slash );
Branch: 1 branch
BranchTrueFalseTotal
$slash == -1 ? : 155 156 311
316 311 ▶ my $rest = $slash == -1 ? '' : substr( $s, $slash + 1 );
Branch: 1 branch
BranchTrueFalseTotal
$slash == -1 ? : 155 156 311
317  
318 # 4. A known forge with an owner (never probed).
319 311 ▶ return _forge_link( $spec, $first_seg, $rest ) if exists $FORGES{ js_lc($first_seg) } && $slash != -1;
Branch: 1 branch
BranchTrueFalseTotal
if exists $FORGES{js_lc($first_seg)} and $slash != -1 87 224 311
Condition100%3/3
exists $FORGES{js_lc($first_seg)} and $slash != -1
!ll&&!rl&&r
218687
MC/DC100%2/2
exists $FORGES{js_lc($first_seg)} and $slash != -1
exists $FORGES{js_lc($first_seg)} $slash != -1
Truth table100%3/3
A and B
A= exists $FORGES{js_lc($first_seg)}B= $slash != -1
ABresult
0X0
100
111
320  
321 # 5. and 6. take a first segment without a dot as a GitHub owner, which has no colon.
322 224 my $colon = index $first_seg, ':';
323 224 ▶ if ( $colon != -1 && index( $first_seg, '.' ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colon != -1 and index($first_seg, ".") == -1) 43 181 224
Condition100%3/3
$colon != -1 and index($first_seg, ".") == -1
!ll&&!rl&&r
172943
MC/DC100%2/2
$colon != -1 and index($first_seg, ".") == -1
$colon != -1 index($first_seg, ".") == -1
Truth table100%3/3
A and B
A= $colon != -1B= index($first_seg, ".") == -1
ABresult
0X0
100
111
324 43 _fail( 'cannot resolve "'.$spec.'": unknown prefix "'.substr( $first_seg, 0, $colon + 1 ).'"; '.$FORMS );
325 }
326  
327 # 5. and 7. take a colon after a dotted host as its port, a number.
328 181 my $host_port = substr $first_seg, rindex( $first_seg, '@' ) + 1;
329 181 my $port_at = index $host_port, ':';
330 181 ▶ if ( $port_at != -1 && substr( $host_port, $port_at + 1 ) !~ /\A[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port_at != -1 and not substr($host_port, $port_at + 1) =~ /\A[0-9]+\z/) 6 175 181
Condition100%3/3
$port_at != -1 and not substr($host_port, $port_at + 1) =~ /\A[0-9]+\z/
!ll&&!rl&&r
17326
MC/DC100%2/2
$port_at != -1 and not substr($host_port, $port_at + 1) =~ /\A[0-9]+\z/
$port_at != -1 not substr($host_port, $port_at + 1) =~ /\A[0-9]+\z/
Truth table100%3/3
A and not(B)
A= $port_at != -1B= not substr($host_port, $port_at + 1) =~ /\A[0-9]+\z/
ABresult
0X0
100
111
331 6 _fail( 'cannot resolve "'.$spec.'": port "'.substr( $host_port, $port_at + 1 ).'" of '
332 .substr( $host_port, 0, $port_at ).' is not a number; '.$FORMS );
333 }
334  
335 # 5. A single token.
336 175 ▶ if ( $slash == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slash == -1) 120 55 175
337 120 ▶ if ( index( $first_seg, '.' ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($first_seg, ".") == -1) 106 14 120
338 106 _check_github( $spec, $first_seg, undef );
339 74 return { kind => 'git', value => 'https://github.com/'.$first_seg.'/'.$DEFAULT_REPO, derivedName => normalize_name($first_seg) };
340 }
341 14 return _probe_generic( 'https://'.$first_seg.'/'.$DEFAULT_REPO, $first_seg, $spec, $probe );
342 }
343  
344 # 6. owner/repo, the first segment without a dot: GitHub, read as a github.com link.
345 55 ▶ return _forge_link( $spec, 'github.com', $s ) if index( $first_seg, '.' ) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($first_seg, ".") == -1 45 10 55
346  
347 # 7. host.tld[/path]: a generic host, probed.
348 10 ( my $path = $rest ) =~ s{/+\z}{};
349 10 ▶ my $base = length $path ? 'https://'.$first_seg.'/'.$path : 'https://'.$first_seg.'/'.$DEFAULT_REPO;
Branch: 1 branch
BranchTrueFalseTotal
length $path ? : 9 1 10
350 10 return _probe_generic( $base, $first_seg, $spec, $probe );
351 }
352  
353 24 ▶sub _probe_generic {
Subroutine _probe_generic: called CC=4 SCAR=13.9
354 24 my ( $base, $host, $original, $probe ) = @_;
355 24 ▶ my $result = $probe->($base) // {};
Condition50%1/2
&$probe($base) // {}
l!l
230
MC/DC0%0/1
&$probe($base) // {}
&$probe($base)
Truth table50%1/2
A
A= &$probe($base)
Aresult
11
00
356 23 ▶ return { kind => 'git', value => $base, derivedName => normalize_name($host) } if $result->{git};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"git"} 3 20 23
357 20 ▶ return { kind => 'url', value => $base.'.tar.gz', derivedName => normalize_name($host) } if $result->{tarball};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"tarball"} 2 18 20
358 18 _fail( 'cannot resolve "'.$original.'": neither '.$base.' (git) nor '.$base.'.tar.gz (tarball) responded' );
359 }
360  
361 35 1;
362  
363 __END__
« Skilletor/Engine.pm ↑ Summary Skilletor/Report.pm »