blib/lib/Open/This.pm

filter: Statementstmt 98.6% 223 / 226 Branchbran 93.1% 136 / 146 Conditioncond 82.8% 53 / 64 MC/DCmcdc 72.9% 27 / 37 Subroutinesub 96.0% 24 / 25 Podpod 100% 4 / 4 totaltotal 93.0% 467 / 502 CC 102 SCAR 25.1
CC
102
Cov
94%
parse_text
23.1
_maybe_extract_file_from_url
18.0
editor_args_from_parsed_text
16.0
CRAP 12.4
↑ Summary script/ot »
1 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Open::This;
5  
6 33 our $VERSION = '0.000036';
7  
8 33 our @ISA = qw(Exporter);
9 33 our @EXPORT_OK = qw(
10 maybe_get_url_from_parsed_text
11 editor_args_from_parsed_text
12 parse_text
13 to_editor_args
14 );
15  
16 33 ▶use Module::Runtime qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 is_module_name
18 module_notional_filename
19 require_module
20 33 );
21 33 ▶use Module::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 33 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 33 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 33 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ## no critic (Subroutines::ProhibitExplicitReturnUndef)
27  
28 # host => [ segments before branch, segments after branch, line anchor prefix ]
29 33 my %FORGE_URL_PARTS = (
30 'bitbucket.org' => [ ['src'], [], 'lines-' ],
31 'codeberg.org' => [ [ 'src', 'branch' ], [], 'L' ],
32 'git.sr.ht' => [ ['tree'], ['item'], 'L' ],
33 'gitlab.com' => [ [ '-', 'blob' ], [], 'L' ],
34 );
35 33 my $DEFAULT_URL_PARTS = [ ['blob'], [], 'L' ];
36  
37 122 ▶sub parse_text {
Subroutine parse_text: called CC=23 SCAR=31.4
Pod documented
38 122 my $text = join q{ }, @_;
39  
40 122 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 122 0 122
41 122 $text =~ s/^\s+|\s+$//g;
42 }
43  
44 # Don't fail on a trailing colon which was accidentally pasted.
45 122 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 122 0 122
46 122 $text =~ s{:\z}{};
47 }
48  
49 122 ▶ return undef if !$text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 122 122
50 122 my %parsed = ( original_text => $text );
51  
52 # A URL authority can hold a port or password, which look like line numbers
53 122 ▶ if ( $text =~ m{\Ahttps?://}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m[\Ahttps?://]i) 54 68 122
54 54 ▶ my $authority = URI->new($text)->authority // q{};
Condition50%1/2
"URI"->new($text)->authority // ""
l!l
540
MC/DC0%0/1
"URI"->new($text)->authority // ""
"URI"->new($text)->authority
Truth table50%1/2
A
A= "URI"->new($text)->authority
Aresult
11
00
55 54 $text =~ s{//\Q$authority\E}{//};
56 }
57  
58 122 my ( $line, $col ) = _maybe_extract_line_number( \$text );
59 122 $parsed{line_number} = $line;
60 122 ▶ $parsed{column_number} = $col if $col;
Branch: 1 branch
BranchTrueFalseTotal
if $col 11 111 122
61 122 $parsed{sub_name} = _maybe_extract_subroutine_name( \$text );
62  
63 122 _maybe_filter_text( \$text );
64  
65 # Is this an actual file.
66 122 ▶ if ( -e path($text) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e path($text)) { } 43 79 122
elsif (my $file_name = _maybe_git_diff_path($text)) { } 1 78 79
elsif ($text =~ m[^[^/]+\.go$]) { } 2 76 78
67 43 $parsed{file_name} = $text;
68 }
69 elsif ( my $file_name = _maybe_git_diff_path($text) ) {
70 1 $parsed{file_name} = $file_name;
71 }
72 elsif ( $text =~ m{^[^/]+\.go$} ) {
73 2 my $file_name = _find_go_files($text);
74 2 ▶ $parsed{file_name} = $file_name if $file_name;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name 1 1 2
75 }
76  
77 122 ▶ if ( !exists $parsed{file_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parsed{"file_name"}) 77 45 122
78 77 ▶ if ( my $bin = _which($text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bin = _which($text)) 1 76 77
79 1 $parsed{file_name} = $bin;
80 1 $text = $bin;
81 }
82 }
83  
84 122 $parsed{is_module_name} = is_module_name($text);
85  
86 122 ▶ if ( !$parsed{file_name} && $text =~ m{\Ahttps?://}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed{"file_name"} and $text =~ m[\Ahttps?://]i) 54 68 122
Condition100%3/3
not $parsed{"file_name"} and $text =~ m[\Ahttps?://]i
!ll&&!rl&&r
462254
MC/DC100%2/2
not $parsed{"file_name"} and $text =~ m[\Ahttps?://]i
not $parsed{"file_name"} $text =~ m[\Ahttps?://]i
Truth table100%3/3
not(A) and B
A= not $parsed{"file_name"}B= $text =~ m[\Ahttps?://]i
ABresult
0X0
100
111
87 54 $parsed{file_name} = _maybe_extract_file_from_url( \$text );
88 }
89  
90 122 ▶ if ( !$parsed{file_name} && $parsed{is_module_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed{"file_name"} and $parsed{"is_module_name"}) 16 106 122
Condition100%3/3
not $parsed{"file_name"} and $parsed{"is_module_name"}
!ll&&!rl&&r
881816
MC/DC100%2/2
not $parsed{"file_name"} and $parsed{"is_module_name"}
not $parsed{"file_name"} $parsed{"is_module_name"}
Truth table100%3/3
not(A) and B
A= not $parsed{"file_name"}B= $parsed{"is_module_name"}
ABresult
0X0
100
111
91 16 $parsed{file_name} = _maybe_find_local_file($text);
92 }
93  
94 # This is a loadable module. Have this come after the local module checks
95 # so that we don't default to installed modules.
96 122 ▶ if ( !$parsed{file_name} && $parsed{is_module_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed{"file_name"} and $parsed{"is_module_name"}) 4 118 122
Condition100%3/3
not $parsed{"file_name"} and $parsed{"is_module_name"}
!ll&&!rl&&r
100184
MC/DC100%2/2
not $parsed{"file_name"} and $parsed{"is_module_name"}
not $parsed{"file_name"} $parsed{"is_module_name"}
Truth table100%3/3
not(A) and B
A= not $parsed{"file_name"}B= $parsed{"is_module_name"}
ABresult
0X0
100
111
97 4 my $found = Module::Util::find_installed($text);
98 4 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 2 2 4
99 2 $parsed{file_name} = $found;
100 }
101 }
102  
103 122 ▶ if ( !$parsed{line_number} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed{"line_number"}) 55 67 122
104 $parsed{line_number} = _maybe_extract_line_number_via_sub_name(
105 $parsed{file_name},
106 $parsed{sub_name}
107 55 );
108 }
109  
110 328 my %return = map { $_ => $parsed{$_} }
111 122 ▶ grep { defined $parsed{$_} && $parsed{$_} ne q{} } keys %parsed;
Condition100%3/3
defined $parsed{$_} && $parsed{$_} ne ''
!ll&&!rl&&r
181106328
MC/DC100%2/2
defined $parsed{$_} && $parsed{$_} ne ''
defined $parsed{$_} $parsed{$_} ne ''
Truth table100%3/3
A && B
A= defined $parsed{$_}B= $parsed{$_} ne ''
ABresult
0X0
100
111
112  
113 122 ▶ return $return{file_name} ? \%return : undef;
Branch: 1 branch
BranchTrueFalseTotal
$return{'file_name'} ? : 102 20 122
114 }
115  
116 30 ▶sub maybe_get_url_from_parsed_text {
Subroutine maybe_get_url_from_parsed_text: called CC=13 SCAR=26.1
Pod documented
117 30 my $err;
118 30 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
119 require_module($_)
120 30 for qw( Git::Helpers Git::Sub File::pushd Capture::Tiny );
121 }
122 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 $err = $_;
124 30 };
125  
126 30 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 30 30
127 0 warn 'This feature requires Git::Helpers to be installed';
128 0 return;
129 }
130  
131 30 my $parsed = shift;
132 return undef
133 30 ▶ unless $parsed && $parsed->{file_name} && -f $parsed->{file_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $parsed->{"file_name"} and -f $parsed->{"file_name"} 1 29 30
Condition50%3/6
$parsed and $parsed->{"file_name"} and -f $parsed->{"file_name"}
!ll&&!rl&&r
0129
$parsed and $parsed->{"file_name"}
!ll&&!rl&&r
0030
MC/DC33%1/3
$parsed and $parsed->{"file_name"} and -f $parsed->{"file_name"}
$parsed $parsed->{"file_name"} -f $parsed->{"file_name"}
Truth table50%2/4
A and B and C
A= $parsedB= $parsed->{"file_name"}C= -f $parsed->{"file_name"}
ABCresult
0XX0
10X0
1100
1111
134  
135 # Ask the file's own checkout, which need not be the cwd's
136 29 my $file = path( $parsed->{file_name} )->absolute;
137 29 my $guard = File::pushd::pushd( $file->parent );
138  
139 # enable used remote to be configurable via environment variable
140 29 my $url;
141 REMOTE:
142 29 ▶ for my $remote (
Condition100%2/2
$ENV{'OPEN_THIS_REMOTES'} || ''
l!l
425
MC/DC100%1/1
$ENV{'OPEN_THIS_REMOTES'} || ''
$ENV{'OPEN_THIS_REMOTES'}
Truth table100%2/2
A
A= $ENV{'OPEN_THIS_REMOTES'}
Aresult
11
00
143 split( m{,}, $ENV{OPEN_THIS_REMOTES} || q{} ),
144 q{origin}
145 ) {
146 31 $url = Git::Helpers::https_remote_url($remote);
147  
148 31 ▶ last REMOTE if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 28 3 31
149 }
150  
151 29 ▶ return undef unless $url && $url->can('host');
Branch: 1 branch
BranchTrueFalseTotal
unless $url and $url->can("host") 1 28 29
Condition66%2/3
$url and $url->can("host")
!ll&&!rl&&r
1028
MC/DC50%1/2
$url and $url->can("host")
$url $url->can("host")
Truth table66%2/3
A and B
A= $urlB= $url->can("host")
ABresult
0X0
100
111
152  
153 28 my ( $branch, @repo_path );
154 Capture::Tiny::capture_stderr(
155 28 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
156 28 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 28 $branch = Git::Helpers::current_branch_name();
158  
159 # ls-tree, unlike ls-files, runs no repo-configured fsmonitor
160 28 local $/ = "\0";
161 28 @repo_path = git::ls_tree(
162 qw( -z --full-name --name-only HEAD -- ),
163 ':(literal)' . $file->basename
164 );
165 28 };
166 }
167 28 );
168 28 ▶ return undef unless @repo_path == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @repo_path == 1 4 24 28
169 24 $parsed->{remote_url} = $url;
170  
171 my ( $before, $after, $anchor )
172 24 ▶ = @{ $FORGE_URL_PARTS{ $url->host } || $DEFAULT_URL_PARTS };
Condition66%2/3
$FORGE_URL_PARTS{$url->host} || $DEFAULT_URL_PARTS
l!l&&r!l&&!r
8160
MC/DC0%0/2
$FORGE_URL_PARTS{$url->host} || $DEFAULT_URL_PARTS
$FORGE_URL_PARTS{$url->host} $DEFAULT_URL_PARTS
Truth table66%2/3
A || B
A= $FORGE_URL_PARTS{$url->host}B= $DEFAULT_URL_PARTS
ABresult
1X1
011
000
173  
174 24 my $clone = $url->clone;
175 24 my @parts = $clone->path_segments;
176 24 push( @parts, @$before, $branch, @$after, $repo_path[0] );
177 24 $clone->path( join '/', @parts );
178 24 ▶ if ( $parsed->{line_number} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"line_number"}) 6 18 24
179 6 $clone->fragment( $anchor . $parsed->{line_number} );
180 }
181  
182 24 $parsed->{remote_file_url} = $clone;
183 24 return $clone;
184 }
185  
186 86 ▶sub to_editor_args {
Subroutine to_editor_args: called CC=1 SCAR=0.0
Pod documented
187 86 return editor_args_from_parsed_text( parse_text(@_) );
188 }
189  
190 100 ▶sub editor_args_from_parsed_text {
Subroutine editor_args_from_parsed_text: called CC=16 SCAR=27.8
Pod documented
191 100 my $parsed = shift;
192 100 ▶ return unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 13 87 100
193  
194 87 ▶ die '$EDITOR has not been set' unless $ENV{EDITOR};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"EDITOR"} 1 86 87
195  
196 # Editors would otherwise parse a leading + or - as a command or option.
197 86 ( my $file_name = $parsed->{file_name} ) =~ s{\A(?=[-+])}{./};
198  
199 86 my @args;
200  
201 # kate --line 11 --column 2 filename
202 # idea.sh --line 11 --column 2 filename
203 86 ▶ if ( $ENV{EDITOR} eq 'kate'
Branch: 3 branches
BranchTrueFalseTotal
if ($ENV{'EDITOR'} eq 'kate' or $ENV{'EDITOR'} =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i) { } 25 61 86
elsif ($ENV{'EDITOR'} =~ /^cod(e|ium)/i) { } 7 54 61
elsif (exists $parsed->{'column_number'}) { } 2 52 54
Condition100%3/3
$ENV{"EDITOR"} eq "kate" or $ENV{"EDITOR"} =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i
l!l&&r!l&&!r
131261
MC/DC100%2/2
$ENV{"EDITOR"} eq "kate" or $ENV{"EDITOR"} =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i
$ENV{"EDITOR"} eq "kate" $ENV{"EDITOR"} =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i
Truth table100%3/3
A or B
A= $ENV{"EDITOR"} eq "kate"B= $ENV{"EDITOR"} =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i
ABresult
1X1
011
000
204 || $ENV{EDITOR}
205 =~ /^(idea|rubymine|pycharm|phpstorm|webstorm|goland|rider|clion|fleet|aqua|data(grip|spell)|appcode)/i
206 ) {
207 push @args, '--line', $parsed->{line_number}
208 25 ▶ if $parsed->{line_number};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->{"line_number"} 20 5 25
209  
210 push @args, '--column', $parsed->{column_number}
211 25 ▶ if $parsed->{column_number};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->{"column_number"} 5 20 25
212 }
213  
214 # code --goto filename:11:2
215 # codium --goto filename:11:2
216 elsif ( $ENV{EDITOR} =~ /^cod(e|ium)/i ) {
217 7 my $result = $file_name;
218 7 ▶ if ( $parsed->{line_number} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"line_number"}) 5 2 7
219 5 $result .= ":" . $parsed->{line_number};
220 5 ▶ if ( $parsed->{column_number} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"column_number"}) 2 3 5
221 2 $result .= ":" . $parsed->{column_number};
222 }
223 }
224 7 return ( '--goto', $result );
225 }
226  
227 # See https://vi.stackexchange.com/questions/18499/can-i-open-a-file-at-an-arbitrary-line-and-column-via-the-command-line
228 # nvim +'call cursor(11,2)' filename
229 # vi +'call cursor(11,2)' filename
230 # vim +'call cursor(11,2)' filename
231 elsif ( exists $parsed->{column_number} ) {
232 2 ▶ if ( $ENV{EDITOR} eq 'nvim'
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"EDITOR"} eq "nvim" or $ENV{"EDITOR"} eq "vi" or $ENV{"EDITOR"} eq "vim") 1 1 2
Condition50%3/6
$ENV{"EDITOR"} eq "nvim" or $ENV{"EDITOR"} eq "vi" or $ENV{"EDITOR"} eq "vim"
l!l&&r!l&&!r
011
$ENV{"EDITOR"} eq "nvim" or $ENV{"EDITOR"} eq "vi"
l!l&&r!l&&!r
002
MC/DC33%1/3
$ENV{"EDITOR"} eq "nvim" or $ENV{"EDITOR"} eq "vi" or $ENV{"EDITOR"} eq "vim"
$ENV{"EDITOR"} eq "nvim" $ENV{"EDITOR"} eq "vi" $ENV{"EDITOR"} eq "vim"
Truth table50%2/4
A or B or C
A= $ENV{"EDITOR"} eq "nvim"B= $ENV{"EDITOR"} eq "vi"C= $ENV{"EDITOR"} eq "vim"
ABCresult
1XX1
01X1
0011
0000
233 || $ENV{EDITOR} eq 'vi'
234 || $ENV{EDITOR} eq 'vim' ) {
235 @args = sprintf(
236 '+call cursor(%i,%i)',
237 $parsed->{line_number},
238 $parsed->{column_number},
239 1 );
240 }
241  
242 # nano +11,2 filename
243 2 ▶ if ( $ENV{EDITOR} eq 'nano' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"EDITOR"} eq "nano") 1 1 2
244 @args = sprintf(
245 '+%i,%i',
246 $parsed->{line_number},
247 $parsed->{column_number},
248 1 );
249 }
250 }
251  
252 else {
253 # emacs +11 filename
254 # nano +11 filename
255 # nvim +11 filename
256 # pico +11 filename
257 # vi +11 filename
258 # vim +11 filename
259 52 ▶ push @args, '+' . $parsed->{line_number} if $parsed->{line_number};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->{"line_number"} 35 17 52
260 }
261  
262 79 return ( @args, $file_name );
263 }
264  
265 133 ▶sub _maybe_extract_line_number {
Subroutine _maybe_extract_line_number: called CC=9 SCAR=22.0
266 133 my $text = shift; # scalar ref
267  
268 # Ansible: ": line 14, column 16,"
269 133 ▶ if ( $$text =~ s{ line (\d+).*, column (\d+).*}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/ line (\d+).*, column (\d+).*//) 4 129 133
270 4 return $1, $2;
271 }
272  
273 # Find a line number
274 # lib/Foo/Bar.pm line 222.
275  
276 129 ▶ if ( $$text =~ s{ line (\d+).*}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/ line (\d+).*//) 17 112 129
277 17 return $1;
278 }
279  
280 # mvn test output
281 # lib/Open/This.pm:[17,3]
282 112 ▶ if ( $$text =~ s{ (\w) : \[ (\d+) , (\d+) \] }{$1}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/ (\w) : \[ (\d+) , (\d+) \] /$1/x) 1 111 112
283 1 return $2, $3;
284 }
285  
286 # Bitbucket links: foo/bar.go#lines-100 or foo/bar.go#lines-100:110
287 # Must run before the git-grep rule, which would read ":110" as the line.
288 111 ▶ if ( $$text =~ s{(\w)#lines-(\d+)\b.*}{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/(\w)#lines-(\d+)\b.*/$1/) 4 107 111
289 4 return $2;
290 }
291  
292 # ripgrep (rg --vimgrep)
293 # ./lib/Open/This.pm:17:3
294 107 ▶ if ( $$text =~ s{(\w):(\d+):(\d+).*}{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/(\w):(\d+):(\d+).*/$1/) 11 96 107
295 11 return $2, $3;
296 }
297  
298 # git-grep (don't match on ::)
299 # lib/Open/This.pm:17
300 96 ▶ if ( $$text =~ s{(\w):(\d+)\b}{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/(\w):(\d+)\b/$1/) 13 83 96
301 13 return $2;
302 }
303  
304 # Github links: foo/bar.go#L100
305 # Github links: foo/bar.go#L100-L110
306 # In this case, discard everything after the matching line number as well.
307 83 ▶ if ( $$text =~ s{(\w)#L(\d+)\b.*}{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/(\w)#L(\d+)\b.*/$1/) 26 57 83
308 26 return $2;
309 }
310  
311 # git-grep contextual match
312 # lib/Open/This.pm-17-
313 57 ▶ if ( $$text =~ s{(\w)-(\d+)\-{0,1}\z}{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/(\w)-(\d+)\-{0,1}\z/$1/) 2 55 57
314 2 return $2;
315 }
316  
317 55 return undef;
318 }
319  
320 122 ▶sub _maybe_filter_text {
Subroutine _maybe_filter_text: called CC=3 SCAR=11.0
321 122 my $text = shift;
322  
323 # Ansible: The error appears to be in '/path/to/file':
324 122 ▶ if ( $$text =~ m{'(.*)'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /'(.*)'/) 4 118 122
325 4 my $maybe_file = $1;
326 4 ▶ $$text = $maybe_file if -e $maybe_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $maybe_file 4 0 4
327 }
328 }
329  
330 126 ▶sub _maybe_extract_subroutine_name {
Subroutine _maybe_extract_subroutine_name: called CC=2 SCAR=6.9
331 126 my $text = shift; # scalar ref
332  
333 126 ▶ if ( $$text =~ s{::(\w+)\(.*\)}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ s/::(\w+)\(.*\)//) 9 117 126
334 9 return $1;
335 }
336 117 return undef;
337 }
338  
339 55 ▶sub _maybe_extract_line_number_via_sub_name {
Subroutine _maybe_extract_line_number_via_sub_name: called CC=6 SCAR=18.0
340 55 my $file_name = shift;
341 55 my $sub_name = shift;
342  
343 55 ▶ if ( $file_name && $sub_name && open( my $fh, '<', $file_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name and $sub_name and open my $fh, "<", $file_name) 5 50 55
Condition83%5/6
$file_name and $sub_name and open my $fh, "<", $file_name
!ll&&!rl&&r
5005
$file_name and $sub_name
!ll&&!rl&&r
19315
MC/DC66%2/3
$file_name and $sub_name and open my $fh, "<", $file_name
$file_name $sub_name open my $fh, "<", $file_name
Truth table75%3/4
A and B and C
A= $file_nameB= $sub_nameC= open my $fh, "<", $file_name
ABCresult
0XX0
10X0
1100
1111
344 5 my $line_number = 1;
345 5 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 21 0 21
346 21 ▶ if ( $line =~ m{sub $sub_name\b} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /sub $sub_name\b/) 5 16 21
347 5 return $line_number;
348 }
349 16 ++$line_number;
350 }
351 }
352 }
353  
354 54 ▶sub _maybe_extract_file_from_url {
Subroutine _maybe_extract_file_from_url: called CC=18 SCAR=28.9
355 54 my $text = shift;
356  
357 54 require_module('URI');
358 54 my $uri = URI->new($$text);
359  
360 # path_segments decodes each segment, so "%20" becomes a space
361 54 my ( undef, @parts ) = $uri->path_segments;
362 54 ▶ my @p = map { $parts[$_] // q{} } 0 .. 3;
Condition100%2/2
$parts[$_] // ""
l!l
2079
MC/DC100%1/1
$parts[$_] // ""
$parts[$_]
Truth table100%2/2
A
A= $parts[$_]
Aresult
11
00
363  
364 # GitLab nests groups, so "-/blob" can follow any number of segments
365 my ($gitlab)
366 54 ▶ = grep { $parts[$_] eq '-' && ( $parts[ $_ + 1 ] // q{} ) eq 'blob' }
Condition100%5/5
$parts[$_] eq '-' && ($parts[$_ + 1] // '') eq 'blob'
!ll&&!rl&&r
36415
$parts[$_ + 1] // ""
l!l
51
MC/DC100%2/2
$parts[$_] eq '-' && ($parts[$_ + 1] // '') eq 'blob'
$parts[$_] eq '-' $parts[$_ + 1]
Truth table100%3/3
A && B
A= $parts[$_] eq '-'B= $parts[$_ + 1]
ABresult
0X0
100
111
367 0 .. $#parts;
368  
369 # Index of the first file segment, after owner/repo/<marker>/<ref>
370 54 my $start;
371 54 my $last = $#parts; # last index the file may start at
372 54 ▶ if ( defined $gitlab ) { $start = $gitlab + 3 }
Branch: 4 branches
BranchTrueFalseTotal
if (defined $gitlab) { } 5 49 54
elsif ($p[2] eq 'blob') { } 29 20 49
elsif ($p[2] eq 'tree') { } 4 16 20
elsif ($p[2] eq 'src') { } 13 3 16
373 29 elsif ( $p[2] eq 'blob' ) { $start = 4 } # GitHub
374 elsif ( $p[2] eq 'tree' ) {
375  
376 # SourceHut has tree/<ref>/item/<file>, so "item" ends the ref
377 4 my ($item) = grep { $parts[$_] eq 'item' } 4 .. $#parts;
378 4 ▶ $start = $last = $item + 1 if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 3 1 4
379 }
380 elsif ( $p[2] eq 'src' ) {
381  
382 # Codeberg has src/branch|tag|commit/<ref>, Bitbucket src/<ref>
383 13 ▶ $start = $p[3] =~ m{\A(?:branch|tag|commit)\z} ? 5 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$p[3] =~ /\A(?:branch|tag|commit)\z/ ? : 6 7 13
384 }
385  
386 # Forges never serve ".." or an encoded "/" or "\" (a Windows separator)
387 # in a path, so don't let a URL leave the repo; NUL can't name a file
388 54 ▶ return if !defined $start || grep { $_ eq '..' || m{[/\\\0]} } @parts;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $start or grep {$_ eq ".." or m[[/\\\0]];} @parts 8 46 54
Condition100%6/6
not defined $start or grep {$_ eq ".." or m[[/\\\0]];} @parts
l!l&&r!l&&!r
4446
$_ eq '..' || m[[/\\\0]]
l!l&&r!l&&!r
13358
MC/DC100%2/2
not defined $start or grep {$_ eq ".." or m[[/\\\0]];} @parts
not defined $start grep {$_ eq ".." or m[[/\\\0]];} @parts
MC/DC100%2/2
$_ eq '..' || m[[/\\\0]]
$_ eq '..' m[[/\\\0]]
Truth table100%3/3
not(A) or B
A= not defined $startB= grep {$_ eq ".." or m[[/\\\0]];} @parts
ABresult
1X1
011
000
Truth table100%3/3
A || B
A= $_ eq '..'B= m[[/\\\0]]
ABresult
1X1
011
000
389  
390 # A ref like "feature/x" spans segments, so try longer refs that git knows
391 46 for my $i ( $start .. $last ) {
392  
393 # Drop empty segments from "//" or a trailing slash; path() dies on them
394 56 my @file = grep { length } @parts[ $i .. $#parts ];
395 56 ▶ last unless @file;
Branch: 1 branch
BranchTrueFalseTotal
unless @file 0 56 56
396  
397 56 my $file = path(@file);
398 56 ▶ next unless $file->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->is_file 12 44 56
399 next
400 44 ▶ if $i > $start
Branch: 1 branch
BranchTrueFalseTotal
if $i > $start and not _is_git_ref(join("/", @parts[$start - 1 .. $i - 1])) 2 42 44
Condition100%3/3
$i > $start and not _is_git_ref(join("/", @parts[$start - 1 .. $i - 1]))
!ll&&!rl&&r
3572
MC/DC100%2/2
$i > $start and not _is_git_ref(join("/", @parts[$start - 1 .. $i - 1]))
$i > $start not _is_git_ref(join("/", @parts[$start - 1 .. $i - 1]))
Truth table100%3/3
A and not(B)
A= $i > $startB= not _is_git_ref(join("/", @parts[$start - 1 .. $i - 1]))
ABresult
0X0
100
111
401 && !_is_git_ref( join '/', @parts[ $start - 1 .. $i - 1 ] );
402  
403 42 return $file->stringify;
404 }
405 4 return;
406 }
407  
408 9 ▶sub _is_git_ref {
Subroutine _is_git_ref: called CC=3 SCAR=11.1
409 9 my $ref = shift;
410  
411 9 require_module('File::Spec');
412  
413 # show-ref matches refs/heads, refs/tags and refs/remotes/*; outside a
414 # repo git complains on stderr, so silence it
415 9 ▶ open my $stderr, '>&', \*STDERR or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $stderr, ">&", \*STDERR or return 0 9 0 9
416 9 my $found = 0;
417 9 ▶ if ( open STDERR, '>', File::Spec->devnull ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open STDERR, ">", "File::Spec"->devnull) 9 0 9
418 9 $found = system( 'git', 'show-ref', '--quiet', '--', $ref ) == 0;
419 }
420 9 open STDERR, '>&', $stderr;
421 9 return $found;
422 }
423  
424 18 ▶sub _maybe_find_local_file {
Subroutine _maybe_find_local_file: called CC=4 SCAR=13.9
425 18 my $text = shift;
426 18 my $possible_name = module_notional_filename($text);
427 my @dirs
428 = exists $ENV{OPEN_THIS_LIBS}
429 ? split m{,}, $ENV{OPEN_THIS_LIBS}
430 18 ▶ : ( 'lib', 't/lib' );
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'OPEN_THIS_LIBS'} ? : 2 16 18
431  
432 18 for my $dir (@dirs) {
433 26 my $path = path( $dir, $possible_name );
434 26 ▶ if ( $path->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->is_file) 14 12 26
435 14 return "$path";
436 }
437 }
438 4 return undef;
439 }
440  
441 79 ▶sub _maybe_git_diff_path {
Subroutine _maybe_git_diff_path: called CC=3 SCAR=11.0
442 79 my $file_name = shift;
443  
444 79 ▶ if ( $file_name =~ m|^[ab]/(.+)$| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name =~ m[^[ab]/(.+)$]) 3 76 79
445 3 ▶ if ( -e path($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e path($1)) 1 2 3
446 1 return $1;
447 }
448 }
449  
450 78 return undef;
451 }
452  
453 # search for binaries in path
454  
455 80 ▶sub _which {
Subroutine _which: called CC=5 SCAR=16.1
456 80 my $maybe_file = shift;
457 80 ▶ return unless $maybe_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $maybe_file 0 80 80
458  
459 80 require_module('File::Spec');
460  
461 # we only want binary names here -- no paths
462 80 my ( $volume, $dir, $file ) = File::Spec->splitpath($maybe_file);
463 80 ▶ return if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 59 21 80
464  
465 21 my @path = File::Spec->path;
466 21 for my $path (@path) {
467 178 my $abs = path( $path, $file );
468 178 ▶ return $abs->stringify if $abs->is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->is_file 2 176 178
469 }
470  
471 19 return;
472 }
473  
474 3 ▶sub _find_go_files {
Subroutine _find_go_files: called CC=8 SCAR=20.8
475 3 my $text = shift;
476 3 ▶ my $threshold_init = shift || 5000;
Condition100%2/2
shift() || 5000
l!l
12
MC/DC100%1/1
shift() || 5000
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
477  
478 3 my $file_name;
479 3 my $iter = path('.')->iterator( { recurse => 1 } );
480 3 my $threshold = $threshold_init;
481 3 ▶ while ( my $path = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = &$iter()) 282 2 284
482 282 ▶ next unless $path->is_file; # dirs will never match anything
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_file 89 193 282
483 193 ▶ ( $threshold, $file_name ) = ( 0, "$path" )
Branch: 1 branch
BranchTrueFalseTotal
if $path->basename eq $text 1 192 193
484 if $path->basename eq $text;
485 193 ▶ last if --$threshold == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$threshold == 0 1 192 193
486 }
487 3 ▶ if ( $threshold == 0 && !$file_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($threshold == 0 and not $file_name) 1 2 3
Condition66%2/3
$threshold == 0 and not $file_name
!ll&&!rl&&r
201
MC/DC50%1/2
$threshold == 0 and not $file_name
$threshold == 0 not $file_name
Truth table66%2/3
A and not(B)
A= $threshold == 0B= not $file_name
ABresult
0X0
100
111
488 1 warn "Only $threshold_init files searched recursively";
489 }
490 3 return $file_name;
491 }
492  
493 # ABSTRACT: Try to Do the Right Thing when opening files
494 33 1;
495  
496 __END__
↑ Summary script/ot »