bin/cpangit-add

filter: Statementstmt 64.9% 50 / 77 Branchbran 32.5% 13 / 40 Conditioncond 34.4% 10 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 46.8% 82 / 175 CC 3 SCAR 4.1
CC
3
Cov
50%
BEGIN
2.1
__ANON__
2.1
__ANON__
2.0
CRAP 1.5
« blib/lib/CPAN/InGit/Server.pm ↑ Summary bin/cpangit-server »
1 #! /usr/bin/env perl
2 # PODNAME: cpangit-add
3 3 our $VERSION = '0.006'; # VERSION
4 # ABSTRACT: import CPAN packages to local CPAN trees
5  
6 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶BEGIN { push @INC, "$FindBin::RealBin/../lib" if -f "$FindBin::RealBin/../dist.ini" }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if -f "$FindBin::RealBin/../dist.ini" 3 0 3
9 3 ▶use CPAN::InGit;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Log::Any::Adapter 'Stdout', log_level => 'info';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use CPAN::Meta::Requirements;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 3 my $log_level= Log::Any::Adapter::Util::numeric_level('info');
17 GetOptions(
18 'git-dir=s' => \(my $git_dir= $ENV{GIT_DIR} // '.'),
19 'branch=s' => \my $branch_name,
20 0 ▶ 'verbose|v' => sub { ++$log_level },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 9 ▶ 'quiet|q' => sub { --$log_level if $log_level },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $log_level 9 0 9
22 0 ▶ 'help' => sub { pod2usage(1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 3 ▶) && @ARGV or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("git-dir=s", \(my $git_dir = $ENV{"GIT_DIR"} // "."), "branch=s", \my $branch_name, "verbose|v", sub { ++$log_level; } , "quiet|q", sub { --$log_level if $log_level; } , "help", sub { pod2usage(1); } ) and @ARGV or pod2usage(2) 3 0 3
Condition40%2/5
&GetOptions("git-dir=s", \(my $git_dir = $ENV{"GIT_DIR"} // "."), "branch=s", \my $branch_name, "verbose|v", sub { ++$log_level; } , "quiet|q", sub { --$log_level if $log_level; } , "help", sub { pod2usage(1); } ) and @ARGV
!ll&&!rl&&r
003
$ENV{"GIT_DIR"} // "."
l!l
03
MC/DC0%0/2
&GetOptions("git-dir=s", \(my $git_dir = $ENV{"GIT_DIR"} // "."), "branch=s", \my $branch_name, "verbose|v", sub { ++$log_level; } , "quiet|q", sub { --$log_level if $log_level; } , "help", sub { pod2usage(1); } ) and @ARGV
&GetOptions("git-dir=s", \(my $git_dir = $ENV{"GIT_DIR"} // "."), "branch=s", \my $branch_name, "verbose|v", sub { ++$log_level; } , "quiet|q", sub { --$log_level if $log_level; } , "help", sub { pod2usage(1); } ) @ARGV
MC/DC0%0/1
$ENV{"GIT_DIR"} // "."
$ENV{"GIT_DIR"}
Truth table33%1/3
A and B
A= &GetOptions("git-dir=s", \(my $git_dir = $ENV{"GIT_DIR"} // "."), "branch=s", \my $branch_name, "verbose|v", sub { ++$log_level; } , "quiet|q", sub { --$log_level if $log_level; } , "help", sub { pod2usage(1); } )B= @ARGV
ABresult
0X0
100
111
Truth table50%1/2
A
A= $ENV{"GIT_DIR"}
Aresult
11
00
24  
25 3 Log::Any::Adapter->set('Stdout', log_level => $log_level);
26  
27 3 my $prereq_phases= [qw( configure build runtime test )];
28 3 my $prereq_types= [qw( requires )];
29 3 my $requirements;
30 my $cpan_snapshot;
31  
32 3 ▶while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 3 3 6
33 3 ▶ if ($ARGV[0] =~ m,/, or $ARGV[0] eq 'cpanfile' or $ARGV[0] eq 'cpanfile.snapshot') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ARGV[0] =~ m[/]u or $ARGV[0] eq 'cpanfile' or $ARGV[0] eq 'cpanfile.snapshot') { } 0 3 3
elsif ($ARGV[0] =~ /^\w+(::\w+)*\z/u) { } 3 0 3
Condition33%2/6
$ARGV[0] =~ m[/]u or $ARGV[0] eq "cpanfile" or $ARGV[0] eq "cpanfile.snapshot"
l!l&&r!l&&!r
003
$ARGV[0] =~ m[/]u or $ARGV[0] eq "cpanfile"
l!l&&r!l&&!r
003
MC/DC0%0/3
$ARGV[0] =~ m[/]u or $ARGV[0] eq "cpanfile" or $ARGV[0] eq "cpanfile.snapshot"
$ARGV[0] =~ m[/]u $ARGV[0] eq "cpanfile" $ARGV[0] eq "cpanfile.snapshot"
Truth table25%1/4
A or B or C
A= $ARGV[0] =~ m[/]uB= $ARGV[0] eq "cpanfile"C= $ARGV[0] eq "cpanfile.snapshot"
ABCresult
1XX1
01X1
0011
0000
34 0 my $path= shift;
35 # load content
36 0 ▶ my $content= do { open my $fh, '<', $path or die "open($path): $!"; local $/; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "open($path): $!" 0 0 0
37 # detect format
38 0 ▶ if ($path =~ /cpanfile\.snapshot\z/ or $content =~ /^# carton snapshot/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /cpanfile\.snapshot\z/u or $content =~ /^# carton snapshot/u) { } 0 0 0
elsif ($path =~ /cpanfile\z/u or $content =~ /^ *requires +\S/mu) { } 0 0 0
Condition0%0/3
$path =~ /cpanfile\.snapshot\z/u or $content =~ /^# carton snapshot/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$path =~ /cpanfile\.snapshot\z/u or $content =~ /^# carton snapshot/u
$path =~ /cpanfile\.snapshot\z/u $content =~ /^# carton snapshot/u
Truth table0%0/3
A or B
A= $path =~ /cpanfile\.snapshot\z/uB= $content =~ /^# carton snapshot/u
ABresult
1X1
011
000
39 0 my $snap= CPAN::InGit->_parse_cpanfile_snapshot($content);
40 0 ▶ if ($cpan_snapshot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpan_snapshot) { } 0 0 0
41 0 %$cpan_snapshot= ( %$cpan_snapshot, %$snap );
42 } else {
43 0 $cpan_snapshot= $snap;
44 }
45 ▶ } elsif ($path =~ /cpanfile\z/ or $content =~ /^ *requires +\S/m) {
Condition0%0/3
$path =~ /cpanfile\z/u or $content =~ /^ *requires +\S/mu
l!l&&r!l&&!r
000
MC/DC0%0/2
$path =~ /cpanfile\z/u or $content =~ /^ *requires +\S/mu
$path =~ /cpanfile\z/u $content =~ /^ *requires +\S/mu
Truth table0%0/3
A or B
A= $path =~ /cpanfile\z/uB= $content =~ /^ *requires +\S/mu
ABresult
1X1
011
000
46 0 require Module::CPANfile;
47 0 my $pre= Module::CPANfile->load(\$content)->prereqs;
48 0 my $req= $pre->merged_requirements($prereq_phases, $prereq_types);
49 0 ▶ if ($requirements) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requirements) { } 0 0 0
50 0 $requirements->add_requirements($req);
51 } else {
52 0 $requirements= $req
53 }
54 } else {
55 0 die "Unrecognized format in file '$path'";
56 }
57 }
58 elsif ($ARGV[0] =~ /^\w+(::\w+)*\z/) {
59 3 my $mod_name= shift;
60 3 my $ver_spec= 0;
61 3 ▶ if (@ARGV && $ARGV[0] =~ /^[><=0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^[><=0-9]/u) 0 3 3
Condition33%1/3
@ARGV and $ARGV[0] =~ /^[><=0-9]/u
!ll&&!rl&&r
300
MC/DC0%0/2
@ARGV and $ARGV[0] =~ /^[><=0-9]/u
@ARGV $ARGV[0] =~ /^[><=0-9]/u
Truth table33%1/3
A and B
A= @ARGVB= $ARGV[0] =~ /^[><=0-9]/u
ABresult
0X0
100
111
62 0 $ver_spec= shift;
63 0 ▶ ($ver_spec =~ /^(>|>=|=|<|<=|)([0-9]+(?:\.[0-9]+(?:_[0-9]+)*))\z/)
Branch: 1 branch
BranchTrueFalseTotal
$ver_spec =~ /^(>|>=|=|<|<=|)([0-9]+(?:\.[0-9]+(?:_[0-9]+)*))\z/u or die "Unexpected version syntax '${ver_spec}'" 0 0 0
64 or die "Unexpected version syntax '$ver_spec'";
65 }
66 3 ▶ $requirements ||= CPAN::Meta::Requirements->new;
Condition33%1/3
$requirements ||= "CPAN::Meta::Requirements"->new
l!l&&r!l&&!r
030
MC/DC0%0/2
$requirements ||= "CPAN::Meta::Requirements"->new
$requirements "CPAN::Meta::Requirements"->new
Truth table33%1/3
A ||= B
A= $requirementsB= "CPAN::Meta::Requirements"->new
ABresult
1X1
011
000
67 3 $requirements->add_string_requirement($mod_name, $ver_spec);
68 }
69 }
70  
71 { # scope to help run destructors in the right order
72 3 my $git_repo= Git::Raw::Repository->discover($git_dir);
73 3 my $cpan_repo= CPAN::InGit->new(git_repo => $git_repo);
74 # If branch name is not provided, use HEAD. But there must be a working copy.
75 # If the branch name is the same as the one checked out, we need to modify the working
76 # copy instead of the branch itself, but the ArchiveTree object handles that.
77 3 my $cur_branch= $cpan_repo->workdir_branch_name;
78 3 ▶ die "Must specify --branch=X for bare git repo"
Branch: 1 branch
BranchTrueFalseTotal
unless length $branch_name or length $cur_branch 0 3 3
Condition66%2/3
length $branch_name or length $cur_branch
l!l&&r!l&&!r
210
MC/DC0%0/2
length $branch_name or length $cur_branch
length $branch_name length $cur_branch
Truth table66%2/3
A or B
A= length $branch_nameB= length $cur_branch
ABresult
1X1
011
000
79 unless length $branch_name || length $cur_branch;
80 3 ▶ $branch_name //= $cur_branch;
Condition66%2/3
$branch_name //= $cur_branch
l!l&&r!l&&!r
210
MC/DC0%0/2
$branch_name //= $cur_branch
$branch_name $cur_branch
Truth table66%2/3
A //= B
A= $branch_nameB= $cur_branch
ABresult
1X1
011
000
81  
82 3 my $atree= $cpan_repo->get_archive_tree($branch_name);
83 3 ▶ unless ($atree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atree) 0 3 3
84 0 ▶ my $tree= $cpan_repo->lookup_tree($branch_name)
Branch: 1 branch
BranchTrueFalseTotal
my $tree = $cpan_repo->lookup_tree($branch_name) or die "No tree named '${branch_name}'" 0 0 0
85 or die "No tree named '$branch_name'";
86 0 my $ent= $tree->entry_bypath('cpan_ingit.json');
87 0 ▶ die "Tree '$branch_name' does not appear to be a 'PAN"
Branch: 1 branch
BranchTrueFalseTotal
$ent ? : 0 0 0
88 .($ent? '' : ' (no /cpan_ingit.json file)');
89 }
90  
91 3 my %imported_dists;
92 0 ▶ %imported_dists= %{ $atree->import_cpanfile_snapshot($cpan_snapshot) }
Branch: 1 branch
BranchTrueFalseTotal
if $cpan_snapshot 0 3 3
93 if $cpan_snapshot;
94 3 ▶ %imported_dists= ( %imported_dists, %{ $atree->import_modules($requirements) } )
Branch: 1 branch
BranchTrueFalseTotal
if $requirements 3 0 3
95 if $requirements;
96 # If this is not the working branch, also need to commit it
97 3 ▶ unless ($atree->use_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atree->use_workdir) 0 3 3
98 # TODO: smarter choice of commit title
99 0 my $title= "Imported modules";
100 0 $atree->commit("$title\n\nImported modules:\n".join('', map " - $_\n", sort keys %imported_dists))
101 }
102 }
103 3 exit 0;
104  
105 __END__
« blib/lib/CPAN/InGit/Server.pm ↑ Summary bin/cpangit-server »