| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | 'help' => sub { pod2usage(1) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | ) && @ARGV or pod2usage(2); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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
$ENV{"GIT_DIR"} // "."
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
Truth table50%1/2
A
A= $ENV{"GIT_DIR"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | if ($ARGV[0] =~ m,/, or $ARGV[0] eq 'cpanfile' or $ARGV[0] eq 'cpanfile.snapshot') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$ARGV[0] =~ m[/]u or $ARGV[0] eq "cpanfile" or $ARGV[0] eq "cpanfile.snapshot"
$ARGV[0] =~ m[/]u or $ARGV[0] eq "cpanfile"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # detect format | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($path =~ /cpanfile\.snapshot\z/ or $content =~ /^# carton snapshot/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$path =~ /cpanfile\.snapshot\z/u or $content =~ /^# carton snapshot/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $snap= CPAN::InGit->_parse_cpanfile_snapshot($content); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($cpan_snapshot) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@ARGV and $ARGV[0] =~ /^[><=0-9]/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $ver_spec= shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | ($ver_spec =~ /^(>|>=|=|<|<=|)([0-9]+(?:\.[0-9]+(?:_[0-9]+)*))\z/) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | or die "Unexpected version syntax '$ver_spec'"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $requirements ||= CPAN::Meta::Requirements->new; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$requirements ||= "CPAN::Meta::Requirements"->new
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
length $branch_name or length $cur_branch
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | unless length $branch_name || length $cur_branch; | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $branch_name //= $cur_branch; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$branch_name //= $cur_branch
MC/DC0%0/2
$branch_name //= $cur_branch
$branch_name
$cur_branch
Truth table66%2/3
A //= B
A= $branch_nameB= $cur_branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | my $atree= $cpan_repo->get_archive_tree($branch_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | unless ($atree) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $tree= $cpan_repo->lookup_tree($branch_name) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | if $cpan_snapshot; | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | %imported_dists= ( %imported_dists, %{ $atree->import_modules($requirements) } ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||