App/lcpan/Cmd/changes.pm

filter: Statementstmt 26.0% 19 / 73 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 21.0% 24 / 114 CC 17 SCAR 55.1
CC
17
Cov
18%
handle_cmd
306.0
BEGIN
1.0
BEGIN
1.0
CRAP 247.9
« App/lcpan/Cmd/related_mods.pm ↑ Summary App/lcpan/Cmd/mods_by_rdep_count.pm »
1 package App::lcpan::Cmd::changes;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require App::lcpan;
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2023-09-26'; # DATE
13 1 our $DIST = 'App-lcpan'; # DIST
14 1 our $VERSION = '1.074'; # VERSION
15  
16 1 our %SPEC;
17  
18 1 $SPEC{'handle_cmd'} = {
19 v => 1.1,
20 summary => 'Show Changes of distribution/module',
21 description => <<'_',
22  
23 This command will find a file named Changes/CHANGES/ChangeLog or other similar
24 name in the top-level directory inside the release tarballs and show it.
25  
26 _
27 args => {
28 %App::lcpan::common_args,
29 %App::lcpan::mod_or_dist_or_script_args,
30 #parse => {
31 # summary => 'Parse with CPAN::Changes',
32 # schema => 'bool',
33 #}.
34 },
35 examples => [
36 {
37 summary => 'Use module name',
38 argv => ['Data::CSel::Parser'],
39 test => 0,
40 'x.doc.show_result' => 0,
41 },
42 #{
43 # summary => 'Use dist name, parse',
44 # argv => ['--parse', 'App-PMUtils'],
45 # test => 0,
46 # 'x.doc.show_result' => 0,
47 #},
48 ],
49 };
50 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=17 SCAR=57.2
Pod documented
51 0 my %args = @_;
52  
53 0 my $state = App::lcpan::_init(\%args, 'ro');
54 0 my $dbh = $state->{dbh};
55  
56 0 my $mod_or_dist_or_script = $args{module_or_dist_or_script};
57 0 $mod_or_dist_or_script =~ s!/!::!g; # XXX this should be done by coercer
58  
59 0 my @join;
60 my @where;
61 0 my @bind;
62  
63 0 my @file_ids;
64 {
65 # search in module first
66 0 ▶ unless ($mod_or_dist_or_script =~ /-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod_or_dist_or_script =~ /-/) 0 0 0
67 0 my $sth = $dbh->prepare("SELECT file_id FROM module WHERE name=?");
68 0 $sth->execute($mod_or_dist_or_script);
69 0 ▶ while (my ($e) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($e) = $sth->fetchrow_array) 0 0 0
70 0 push @file_ids, $e;
71 }
72 }
73 # search in dist or script
74 0 ▶ unless ($mod_or_dist_or_script =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod_or_dist_or_script =~ /::/) 0 0 0
75 0 my $dist_found;
76 0 my $sth = $dbh->prepare("SELECT id FROM file WHERE dist_name=? ORDER BY dist_version_numified DESC LIMIT 1");
77 0 $sth->execute($mod_or_dist_or_script);
78 0 ▶ while (my ($e) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($e) = $sth->fetchrow_array) 0 0 0
79 0 $dist_found++;
80 0 push @file_ids, $e;
81 }
82  
83 0 ▶ unless ($dist_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist_found) 0 0 0
84 0 my $sth = $dbh->prepare("SELECT file_id FROM script WHERE name=?");
85 0 $sth->execute($mod_or_dist_or_script);
86 0 ▶ while (my ($e) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($e) = $sth->fetchrow_array) 0 0 0
87 0 push @file_ids, $e;
88 }
89 }
90 }
91  
92 0 ▶ return [404, "No such module/dist/script"] unless @file_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @file_ids 0 0 0
93 0 push @where, "file.id IN (".join(",", @file_ids).")";
94 }
95  
96 0 ▶ my $sql = "SELECT
Branch: 2 branches
BranchTrueFalseTotal
@join ? : 0 0 0
@where ? : 0 0 0
97 content.path content_path,
98 file.cpanid author,
99 file.name release
100 FROM content
101 LEFT JOIN file ON content.file_id=file.id
102 ".(@join ? join(" ", @join) : "")."
103 ".(@where ? " WHERE ".join(" AND ", @where) : "")."
104 ORDER BY content.path";
105 0 my $sth = $dbh->prepare($sql);
106 0 $sth->execute(@bind);
107  
108 0 my $first_row;
109 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
110 0 ▶ $first_row //= $row;
Condition0%0/3
$first_row //= $row
l!l&&r!l&&!r
000
MC/DC0%0/2
$first_row //= $row
$first_row $row
Truth table0%0/3
A //= B
A= $first_rowB= $row
ABresult
1X1
011
000
111 0 ▶ next unless $row->{content_path} =~ m!\A
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"content_path"} =~ m[\A (?:[^/]+/)? (changes|changelog) (?:\.(\w+))?\z]xi 0 0 0
112 (?:[^/]+/)?
113 (changes|changelog)
114 (?:\.(\w+))?\z!ix;
115 # XXX handle YAML file
116 my $path = App::lcpan::_fullpath(
117 0 $row->{release}, $state->{cpan}, $row->{author});
118  
119 # XXX needs to be refactored into common code (see also doc subcommand)
120 0 my $content;
121 0 ▶ if ($path =~ /\.zip$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\.zip$/i) { } 0 0 0
122 0 require Archive::Zip;
123 0 my $zip = Archive::Zip->new;
124 0 ▶ $zip->read($path) == Archive::Zip::AZ_OK()
Branch: 1 branch
BranchTrueFalseTotal
$zip->read($path) == Archive::Zip::AZ_OK() or return [500, "Can't read zip file '${path}'"] 0 0 0
125 or return [500, "Can't read zip file '$path'"];
126 0 $content = $zip->contents($row->{content_path});
127 } else {
128 0 require Archive::Tar;
129 0 my $tar;
130 0 eval {
131 0 $tar = Archive::Tar->new;
132 0 $content = $tar->read($path); # can still die untrapped when out of mem
133 };
134 0 ▶ return [500, "Can't read tar file '$path': $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
135 0 my ($obj) = $tar->get_files($row->{content_path});
136 0 $content = $obj->get_content;
137 }
138  
139 return [200, "OK", $content, {
140 'func.content_path' => $row->{content_path},
141 0 'cmdline.skip_format'=>1,
142 "cmdline.page_result"=>1,
143 }];
144 }
145  
146 0 ▶ if ($first_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_row) { } 0 0 0
147 0 return [404, "No Changes file found in $first_row->{release}"];
148 } else {
149 0 return [404, "No such module or dist"];
150 }
151 }
152  
153 1 1;
154 # ABSTRACT: Show Changes of distribution/module
155  
156 __END__
« App/lcpan/Cmd/related_mods.pm ↑ Summary App/lcpan/Cmd/mods_by_rdep_count.pm »