blib/lib/CPAN/InGit.pm

filter: Statementstmt 48.4% 92 / 190 Branchbran 31.0% 36 / 116 Conditioncond 40.0% 14 / 35 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 80.0% 20 / 25 Podpod 88.8% 8 / 9 totaltotal 43.8% 173 / 395 CC 78 SCAR 52.4
CC
78
Cov
42%
process_distfile
650.0
_parse_cpanfile_snapshot
182.0
lookup_tree
28.2
CRAP 189.3
↑ Summary blib/lib/CPAN/InGit/ArchiveTree.pm »
1 package CPAN::InGit;
2  
3 11 our $VERSION = '0.005'; # VERSION
4 # ABSTRACT: Manage custom CPAN trees to pin versions for your projects
5  
6 11 ▶use Git::Raw::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Archive::Tar::Constant; # for constants to be avilable at compile time
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use CPAN::InGit::MirrorTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use CPAN::Meta 2.150010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Fcntl qw( S_ISREG S_ISLNK S_IFMT S_ISDIR );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 15 ▶sub BUILDARGS($class, @list) {
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod undocumented
20 15 ▶ unshift @list, 'git_repo' if @list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 1 0 15 15
21 15 my $args= $class->next::method(@list);
22 15 ▶ $args->{git_repo}= delete $args->{repo} if defined $args->{repo};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"repo"} 9 6 15
23 15 $args;
24 }
25  
26 11 has git_repo => ( is => 'ro', required => 1, coerce => \&_open_repo );
27 11 has git_author_name => ( is => 'rw', default => 'CPAN::InGit' );
28 11 has git_author_email => ( is => 'rw', default => 'CPAN::InGit@localhost' );
29  
30 11 has workdir_branch_name => ( is => 'lazy' );
31 3 ▶sub _build_workdir_branch_name($self) {
Subroutine _build_workdir_branch_name: called CC=3 SCAR=13.1
32 3 ▶ return undef if $self->git_repo->is_bare || $self->git_repo->is_head_detached;
Branch: 1 branch
BranchTrueFalseTotal
if $self->git_repo->is_bare or $self->git_repo->is_head_detached 0 3 3
Condition33%1/3
$self->git_repo->is_bare or $self->git_repo->is_head_detached
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->git_repo->is_bare or $self->git_repo->is_head_detached
$self->git_repo->is_bare $self->git_repo->is_head_detached
Truth table33%1/3
A or B
A= $self->git_repo->is_bareB= $self->git_repo->is_head_detached
ABresult
1X1
011
000
33 3 return $self->git_repo->head->shorthand;
34 }
35  
36 11 has useragent => ( is => 'lazy' );
37 0 ▶sub _build_useragent($self) {
Subroutine _build_useragent: not called CC=1 SCAR=6.9
38 0 require Mojo::UserAgent;
39 0 return Mojo::UserAgent->new;
40 }
41  
42 15 ▶sub _open_repo($thing) {
Subroutine _open_repo: called CC=3 SCAR=15.4
43 15 ▶ return $thing if blessed($thing) && $thing->isa('Git::Raw::Repository');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($thing) and $thing->isa("Git::Raw::Repository") 15 0 15
Condition33%1/3
builtin::blessed($thing) and $thing->isa("Git::Raw::Repository")
!ll&&!rl&&r
0015
MC/DC0%0/2
builtin::blessed($thing) and $thing->isa("Git::Raw::Repository")
builtin::blessed($thing) $thing->isa("Git::Raw::Repository")
Truth table33%1/3
A and B
A= builtin::blessed($thing)B= $thing->isa("Git::Raw::Repository")
ABresult
0X0
100
111
44 0 return Git::Raw::Repository->open("$thing");
45 }
46  
47  
48 27 ▶sub get_archive_tree($self, $branch_or_tag_or_id) {
Subroutine get_archive_tree: called CC=12 SCAR=25.0
Pod documented
49 27 my ($tree, $origin)= $self->lookup_tree($branch_or_tag_or_id);
50 27 ▶ return undef unless $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree 2 25 27
51  
52 25 ▶ my $branch= $origin && ref($origin)->isa('Git::Raw::Branch')? $origin : undef;
Branch: 1 branch
BranchTrueFalseTotal
$origin && ref($origin)->isa('Git::Raw::Branch') ? : 22 3 25
Condition66%2/3
$origin and ref($origin)->isa("Git::Raw::Branch")
!ll&&!rl&&r
0322
MC/DC50%1/2
$origin and ref($origin)->isa("Git::Raw::Branch")
$origin ref($origin)->isa("Git::Raw::Branch")
Truth table66%2/3
A and B
A= $originB= ref($origin)->isa("Git::Raw::Branch")
ABresult
0X0
100
111
53  
54 # If HEAD requested or using the branch pointed to by HEAD, and if it has a work directory,
55 # then apply any changes to the workdir.
56 25 ▶ my $use_workdir= !$self->git_repo->is_bare && (($branch && $branch->is_head) || $branch_or_tag_or_id eq 'HEAD');
Condition100%9/9
!$self->git_repo->is_bare && ($branch && $branch->is_head || $branch_or_tag_or_id eq 'HEAD')
!ll&&!rl&&r
1933
$branch and $branch->is_head or $branch_or_tag_or_id eq "HEAD"
l!l&&r!l&&!r
213
$branch and $branch->is_head
!ll&&!rl&&r
132
MC/DC50%2/4
!$self->git_repo->is_bare && ($branch && $branch->is_head || $branch_or_tag_or_id eq 'HEAD')
!$self->git_repo->is_bare $branch $branch->is_head $branch_or_tag_or_id eq "HEAD"
Truth table66%4/6
not(A) && B and C or D
A= !$self->git_repo->is_bareB= $branchC= $branch->is_headD= $branch_or_tag_or_id eq "HEAD"
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
57  
58 # Does it look like an ArchiveTree?
59 25 my $config_blob;
60 25 ▶ if ($use_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_workdir) { } 3 22 25
61 3 my $ent= $self->git_repo->index->find('cpan_ingit.json');
62 3 ▶ return undef unless $ent;
Branch: 1 branch
BranchTrueFalseTotal
unless $ent 0 3 3
63 3 $config_blob= $ent->blob;
64 } else {
65 22 my $ent= $tree->entry_bypath('cpan_ingit.json');
66 22 ▶ return undef unless $ent;
Branch: 1 branch
BranchTrueFalseTotal
unless $ent 0 22 22
67 22 $config_blob= $ent->object;
68 }
69 25 my $cfg= JSON::PP->new->relaxed->decode($config_blob->content);
70 25 ▶ my $class= $cfg->{upstream_url}? 'CPAN::InGit::MirrorTree' : 'CPAN::InGit::ArchiveTree';
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'upstream_url'} ? : 0 25 25
71 25 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
$branch ? : 22 3 25
72 parent => $self,
73 tree => $tree,
74 name => $branch? $branch->shorthand : "$branch_or_tag_or_id",
75 use_workdir => $use_workdir,
76 (branch => $branch)x!!$branch,
77 );
78 }
79  
80  
81 15 ▶sub create_archive_tree($self, $name, %params) {
Subroutine create_archive_tree: called CC=4 SCAR=14.8
Pod documented
82 15 ▶ croak "Branch '$name' already exists"
Branch: 1 branch
BranchTrueFalseTotal
if "Git::Raw::Branch"->lookup($self->git_repo, $name, 1) 0 15 15
83 if Git::Raw::Branch->lookup($self->git_repo, $name, 1);
84 15 ▶ croak "Branch '$name' already exists upstream"
Branch: 1 branch
BranchTrueFalseTotal
if "Git::Raw::Branch"->lookup($self->git_repo, $name, 0) 0 15 15
85 if Git::Raw::Branch->lookup($self->git_repo, $name, 0);
86 15 my $t;
87 15 ▶ if ($params{upstream_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'upstream_url'}) { } 0 15 15
88 0 $t= CPAN::InGit::MirrorTree->new(%params, parent => $self);
89 0 $t->add_upstream_package_details;
90 } else {
91 15 $t= CPAN::InGit::ArchiveTree->new(%params, parent => $self);
92 15 $t->write_package_details;
93 }
94 # It won't exist until we create a commit and create a branch.
95 15 $t->write_config;
96 15 $t->commit("Called create_archive_tree", create_branch => $name);
97 15 return $t;
98 }
99  
100  
101 60 ▶sub lookup_tree($self, $branch_or_tag_or_id) {
Subroutine lookup_tree: called CC=14 SCAR=33.4
Pod documented
102 60 my ($tree, $origin);
103 60 ▶ defined $branch_or_tag_or_id or croak "missing argument";
Branch: 1 branch
BranchTrueFalseTotal
defined $branch_or_tag_or_id or croak("missing argument") 60 0 60
104 60 my $repo= $self->git_repo;
105 60 ▶ if (blessed($branch_or_tag_or_id) && (
Branch: 5 branches
BranchTrueFalseTotal
if (builtin::blessed($branch_or_tag_or_id) and $branch_or_tag_or_id->isa('Git::Raw::Branch') || $branch_or_tag_or_id->isa('Git::Raw::Tag')) { } 0 60 60
elsif ($branch_or_tag_or_id eq 'HEAD') { } 1 59 60
elsif ($origin = eval { do { 'Git::Raw::Branch'->lookup($repo, $branch_or_tag_or_id, 1) } }) { } 53 6 59
elsif ($origin = eval { do { 'Git::Raw::Tag'->lookup($repo, $branch_or_tag_or_id) } }) { } 0 6 6
elsif (my $obj = eval { do { $repo->lookup($branch_or_tag_or_id) } }) { } 4 2 6
Condition16%1/6
builtin::blessed($branch_or_tag_or_id) and $branch_or_tag_or_id->isa("Git::Raw::Branch") || $branch_or_tag_or_id->isa("Git::Raw::Tag")
!ll&&!rl&&r
6000
$branch_or_tag_or_id->isa("Git::Raw::Branch") or $branch_or_tag_or_id->isa("Git::Raw::Tag")
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::blessed($branch_or_tag_or_id) and $branch_or_tag_or_id->isa("Git::Raw::Branch") || $branch_or_tag_or_id->isa("Git::Raw::Tag")
builtin::blessed($branch_or_tag_or_id) $branch_or_tag_or_id->isa("Git::Raw::Branch") $branch_or_tag_or_id->isa("Git::Raw::Tag")
Truth table25%1/4
A and B or C
A= builtin::blessed($branch_or_tag_or_id)B= $branch_or_tag_or_id->isa("Git::Raw::Branch")C= $branch_or_tag_or_id->isa("Git::Raw::Tag")
ABCresult
0XX0
1000
11X1
1011
106 $branch_or_tag_or_id->isa('Git::Raw::Branch')
107 || $branch_or_tag_or_id->isa('Git::Raw::Tag')
108 )) {
109 0 $origin= $branch_or_tag_or_id;
110 0 $tree= $origin->peel('tree');
111 } elsif ($branch_or_tag_or_id eq 'HEAD') {
112 1 $tree= $repo->head->target->tree;
113 1 ▶ $origin= $repo->is_head_detached? undef
Branch: 1 branch
BranchTrueFalseTotal
$repo->is_head_detached ? : 0 1 1
114 : $repo->head;
115 59 } elsif ($origin= eval { Git::Raw::Branch->lookup($repo, $branch_or_tag_or_id, 1) }) {
116 53 $tree= $origin->peel('tree');
117 6 } elsif ($origin= eval { Git::Raw::Tag->lookup($repo, $branch_or_tag_or_id) }) {
118 0 $tree= $origin->peel('tree');
119 6 } elsif (my $obj= eval { $repo->lookup($branch_or_tag_or_id) }) {
120 4 ▶ if ($obj->isa('Git::Raw::Commit')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($obj->isa('Git::Raw::Commit')) { } 4 0 4
elsif ($obj->isa('Git::Raw::Tree')) { } 0 0 0
elsif ($obj->isa('Git::Raw::Tag')) { } 0 0 0
121 4 $origin= Git::Raw::Commit->lookup($repo, $obj->id);
122 4 $tree= $origin->tree;
123 } elsif ($obj->isa('Git::Raw::Tree')) {
124 0 $tree= Git::Raw::Tree->lookup($repo, $obj->id);
125 } elsif ($obj->isa('Git::Raw::Tag')) {
126 0 $origin= Git::Raw::Tag->lookup($repo, $obj->id);
127 0 $tree= $origin->target;
128 }
129 }
130 60 ▶ return wantarray? ($tree, $origin) : $tree;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 31 60
131 }
132  
133  
134 1 ▶sub add_git_tree_to_tar($self, $tar, $path, $tree) {
Subroutine add_git_tree_to_tar: called CC=4 SCAR=20.7
Pod documented
135 1 ▶ unless ($tree->can('entries')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->can("entries")) 0 1 1
136 0 my $id= $tree;
137 0 ▶ $tree = Git::Raw::Tree->lookup($self->git_repo, $id)
Branch: 1 branch
BranchTrueFalseTotal
$tree = "Git::Raw::Tree"->lookup($self->git_repo, $id) or die "Can't find TREE $id referenced by '${path}'" 0 0 0
138 or die "Can't find TREE $id referenced by '$path'";
139 }
140 $self->add_git_dirent_to_tar($tar, "$path/".$_->name, $_)
141 1 for $tree->entries;
142 }
143  
144 1 ▶sub add_git_dirent_to_tar($self, $tar, $path, $dirent) {
Subroutine add_git_dirent_to_tar: called CC=5 SCAR=20.9
Pod documented
145 1 ▶ if ($dirent->type == Git::Raw::Object::BLOB()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dirent->type == Git::Raw::Object::BLOB()) { } 1 0 1
elsif ($dirent->type == Git::Raw::Object::TREE()) { } 0 0 0
146 1 my $mode = $dirent->file_mode;
147 1 ▶ my $blob = Git::Raw::Blob->lookup($self->git_repo, $dirent->id)
Branch: 1 branch
BranchTrueFalseTotal
my $blob = "Git::Raw::Blob"->lookup($self->git_repo, $dirent->id) or die q[Can't find BLOB ] . $dirent->id . " referenced by '${path}'" 1 0 1
148 or die "Can't find BLOB ".$dirent->id." referenced by '$path'";
149 # Check if it's a symlink (mode 0120000 or 40960 decimal)
150 1 ▶ if (($mode & 0170000) == 0120000) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mode & 61440) == 40960) { } 0 1 1
151 # Symlink: content is the target path
152 0 $tar->add_data($path, $blob->content, {
153 mode => $mode,
154 type => Archive::Tar::Constant::SYMLINK,
155 linkname => $blob->content
156 });
157 }
158 else {
159 # Regular file
160 1 $tar->add_data($path, $blob->content, { mode => $mode });
161 }
162 }
163 elsif ($dirent->type == Git::Raw::Object::TREE()) {
164 0 $self->add_git_tree_to_tar($tar, $path, $dirent->id);
165 }
166 else {
167 0 warn "Omitting $path from TAR, not a BLOB or TREE";
168 }
169 }
170  
171  
172 30 ▶sub new_signature($self) {
Subroutine new_signature: called CC=1 SCAR=0.0
Pod documented
173 30 Git::Raw::Signature->now($self->git_author_name, $self->git_author_email);
174 }
175  
176  
177 0 ▶sub lookup_versions($self, $module_name) {
Subroutine lookup_versions: not called CC=2 SCAR=17.9
Pod documented
178 0 for my $up ($self->upstream_mirrors->@*) {
179 0 ...;
180 }
181 }
182  
183  
184 0 ▶sub process_distfile($self, %opts) {
Subroutine process_distfile: not called CC=25 SCAR=64.8
Pod documented
185 0 my ($tree, $file_path, $file_data, $extract)= @opts{'tree','file_path','file_data','extract'};
186 # Decompress tar in memory. The decompression gets complicated since it can be bz2 or gz
187 # so write to a temp file and then parse that with the auto-detection of "compress".
188 0 ▶ if ($file_path =~ /\.(tar\.gz|tar\.bz2|tgz)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ /\.(tar\.gz|tar\.bz2|tgz)\z/u) { } 0 0 0
189 0 my $path_without_extension= substr($file_path, 0, $-[0]);
190 0 my $tmp= File::Temp->new;
191 0 ▶ $tmp->print($$file_data) or die "write: $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->print($$file_data) or die "write: $!" 0 0 0
192 0 $tmp->flush;
193  
194 # Iterate across the files in the tar archive
195 0 my $tar= Archive::Tar->new("$tmp", 1);
196 0 my @files= $tar->get_files;
197 0 ▶ if (!@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 0 0
198 0 croak "Failed to extract any files from archive $file_path";
199 }
200 # Remove prefix directory if every file in archive starts with the same directory
201 0 (my $prefix= $files[0]->name) =~ s,/.*,,;
202 0 $prefix .= '/';
203 0 for (@files) {
204 0 ▶ if (substr($_->name,0,length $prefix) ne $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_->name, 0, length $prefix) ne $prefix) 0 0 0
205 0 $prefix= '';
206 0 last;
207 }
208 }
209 # Build by-name hash of files
210 0 my %files= map +( substr($_->name, length $prefix) => $_ ), @files;
211 0 my $meta;
212 # Look for a META.json
213 0 ▶ if (my $meta_json= $files{'META.json'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta_json = $files{"META.json"}) 0 0 0
214 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $cm = "CPAN::Meta"->load_json_string($meta_json->get_content); $meta = $cm->as_struct({"version", 2}) } } or warn "Failed to load $file_path/${prefix}META.json: $@" 0 0 0
215 0 my $cm= CPAN::Meta->load_json_string($meta_json->get_content);
216 0 $meta= $cm->as_struct({ version => 2 });
217 } or warn "Failed to load $file_path/${prefix}META.json: $@";
218 }
219 # else look for META.yml
220 0 ▶ if (!$meta && (my $meta_yml= $files{'META.yml'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta and my $meta_yml = $files{"META.yml"}) 0 0 0
Condition0%0/3
not $meta and my $meta_yml = $files{"META.yml"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $meta and my $meta_yml = $files{"META.yml"}
not $meta my $meta_yml = $files{"META.yml"}
Truth table0%0/3
not(A) and B
A= not $metaB= my $meta_yml = $files{"META.yml"}
ABresult
0X0
100
111
221 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $cm = "CPAN::Meta"->load_yaml_string($meta_yml->get_content); $meta = $cm->as_struct({"version", 2}) } } or warn "Failed to load $file_path/${prefix}META.yml: $@" 0 0 0
222 0 my $cm= CPAN::Meta->load_yaml_string($meta_yml->get_content);
223 0 $meta= $cm->as_struct({ version => 2 });
224 } or warn "Failed to load $file_path/${prefix}META.yml: $@";
225 }
226 # TODO: add some fall-back that guesses at prereqs.
227 0 ▶ $meta //= {};
Condition0%0/2
$meta //= {}
l!l
00
MC/DC0%0/1
$meta //= {}
$meta
Truth table0%0/2
A
A= $meta
Aresult
11
00
228 # If the meta didn't contain "provides", add that using Module::Metadata
229 0 ▶ if (!$meta->{provides}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"provides"}) 0 0 0
230 0 my $provides= $meta->{provides}= {};
231 0 ▶ for my $pm_fname (grep /\.pm\z/ && !m{^(t|xt|inc|script|bin)/}, keys %files) {
Condition0%0/3
/\.pm\z/u && !m[^(t|xt|inc|script|bin)/]u
!ll&&!rl&&r
000
MC/DC0%0/2
/\.pm\z/u && !m[^(t|xt|inc|script|bin)/]u
/\.pm\z/u !m[^(t|xt|inc|script|bin)/]u
Truth table0%0/3
A && not(B)
A= /\.pm\z/uB= !m[^(t|xt|inc|script|bin)/]u
ABresult
0X0
100
111
232 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { open my $pm_fh, "<", $files{$pm_fname}->get_content_by_ref or die; my $mm = "Module::Metadata"->new_from_handle($pm_fh, $pm_fname); foreach my $pkg (grep(($_ ne "main"), $mm->name, $mm->packages_inside)) { $provides->{$pkg}{"file"} = $pm_fname; $provides->{$pkg}{"version"} //= $mm->version($pkg); } ; 1 } } or warn "Failed to parse packages in $file_path/$prefix${pm_fname}: $@" 0 0 0
233 0 ▶ open my $pm_fh, '<', $files{$pm_fname}->get_content_by_ref or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $pm_fh, "<", $files{$pm_fname}->get_content_by_ref or die 0 0 0
234 0 my $mm= Module::Metadata->new_from_handle($pm_fh, $pm_fname);
235 0 for my $pkg (grep $_ ne 'main', $mm->name, $mm->packages_inside) {
236 0 $provides->{$pkg}{file}= $pm_fname;
237 0 ▶ $provides->{$pkg}{version} //= $mm->version($pkg);
Condition0%0/3
$provides->{$pkg}{"version"} //= $mm->version($pkg)
l!l&&r!l&&!r
000
MC/DC0%0/2
$provides->{$pkg}{"version"} //= $mm->version($pkg)
$provides->{$pkg}{"version"} $mm->version($pkg)
Truth table0%0/3
A //= B
A= $provides->{$pkg}{"version"}B= $mm->version($pkg)
ABresult
1X1
011
000
238 }
239 0 1;
240 } or warn "Failed to parse packages in $file_path/${prefix}$pm_fname: $@";
241 }
242 }
243 # If caller requests 'extract', add the tar's files and symlinks to the tree
244 0 ▶ if ($extract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extract) { } 0 0 0
245 0 for (keys %files) {
246 0 my $mode= $files{$_}->mode;
247 0 ▶ if (S_ISREG($mode)) {
Branch: 3 branches
BranchTrueFalseTotal
if (S_ISREG($mode)) { } 0 0 0
elsif (S_ISLNK($mode)) { } 0 0 0
elsif (not S_ISDIR($mode)) { } 0 0 0
248 # normalize to 644 or 755
249 0 ▶ $mode= S_IFMT($mode) | (($mode & 1)? 0755 : 0644);
Branch: 1 branch
BranchTrueFalseTotal
$mode & 1 ? : 0 0 0
250 0 $tree->set_path("$path_without_extension/$_", $files{$_}->get_content_by_ref, $mode);
251 } elsif (S_ISLNK($mode)) {
252 0 $tree->set_path("$path_without_extension/$_", \$files{$_}->linkname, S_IFMT($mode));
253 } elsif (!S_ISDIR($mode)) {
254 0 warn "Skipping tar entry for '$path_without_extension/$_' (mode=$mode)\n";
255 }
256 }
257 } else {
258 0 $tree->set_path($file_path, $file_data);
259 }
260 # Now serialzie the meta and write it to the tree alongside the TAR
261 0 my $json= CPAN::Meta->new($meta)->as_string({ version => 2 });
262 0 $tree->set_path($path_without_extension . '.json', \$json);
263 }
264 else {
265 0 warn "$file_path does not appear to be a TAR file. Skipping metadata processing.";
266 0 $tree->set_path($file_path, $file_data);
267 }
268 }
269  
270 #=method parse_cpanfile_snapshot
271 #
272 # $distribution_spec= $cpan_repo->parse_cpanfile_snapshot($file_contents);
273 #
274 #Given a scalar with the content of a cpanfile.snapshot from L<Carton>, this returns the data of
275 #that file as a hierarchial structure:
276 #
277 # {
278 # "Distribution-Name-1.002003" => {
279 # "pathname" => "A/AU/AUTHOR/Distribution-Name-1.002003.tar.gz",
280 # "provides" => {
281 # "Distribution::Name" => '1.002003',
282 # ...
283 # },
284 # "requirements" => {
285 # "Dependency" => "2.05",
286 # ...
287 # }
288 # }
289 # }
290 #
291 #=cut
292  
293 0 ▶sub _context {
Subroutine _context: not called CC=1 SCAR=6.9
294 0 my $context= substr($_, pos($_), $_[0]);
295 0 $context =~ s/\r/\\r/g;
296 0 $context =~ s/\n/\\n/g;
297 0 return $context;
298 }
299 0 ▶sub _parse_cpanfile_snapshot($self, $text) {
Subroutine _parse_cpanfile_snapshot: not called CC=13 SCAR=52.0
300 # TODO: use official module if available, else fall back to this:
301 0 my %distributions;
302 0 local $_= $text;
303 0 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { /^# carton snapshot format: version 1.0\r?\n/cgmu or die "Unsupported cpanfile.snapshot version\n"; /\GDISTRIBUTIONS\r?\n/cgu or die "expected DISTRIBUTIONS\n"; while (length $_ > pos $_) { my($dist_name) = /\G (\S+)\r?\n/cgu or die "expected dist name\n"; my $dist = ($distributions{$dist_name} = {}); while (/\G (\w[^\r\n:]*): *(.*?)\r?\n/cgu) { () = ($1, $2); if (length $val) { $dist->{$attr} = $val; } else { while (/\G (.*)\r?\n/cgu) { push @{$$dist{$attr};}, $1; } ; } ; die "Unexpected sub-element of $dist_name $attr\n" if /\G /cgu; } ; die "Unexpected sub-element of $dist_name\n" if /\G /cgu; foreach $_ ("provides", "requirements") { if (ref $dist->{$_} eq "ARRAY") { $dist->{$_} = {map((split(' ', $_, 0))[0, 1], @{$$dist{$_};})}; } ; } ; } ; 1 } }) 0 0 0
304 0 ▶ /^# carton snapshot format: version 1.0\r?\n/mgc
Branch: 1 branch
BranchTrueFalseTotal
/^# carton snapshot format: version 1.0\r?\n/cgmu or die "Unsupported cpanfile.snapshot version\n" 0 0 0
305 or die "Unsupported cpanfile.snapshot version\n";
306 0 ▶ /\GDISTRIBUTIONS\r?\n/gc
Branch: 1 branch
BranchTrueFalseTotal
/\GDISTRIBUTIONS\r?\n/cgu or die "expected DISTRIBUTIONS\n" 0 0 0
307 or die "expected DISTRIBUTIONS\n";
308 0 ▶ while (length > pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > pos $_) 0 0 0
309 0 ▶ my ($dist_name)= /\G (\S+)\r?\n/gc
Branch: 1 branch
BranchTrueFalseTotal
my($dist_name) = /\G (\S+)\r?\n/cgu or die "expected dist name\n" 0 0 0
310 or die "expected dist name\n";
311 0 my $dist= $distributions{$dist_name}= {};
312 0 ▶ while (/\G (\w[^\r\n:]*): *(.*?)\r?\n/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G (\w[^\r\n:]*): *(.*?)\r?\n/cgu) 0 0 0
313 0 my ($attr, $val)= ($1, $2);
314 0 ▶ if (length $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val) { } 0 0 0
315 0 $dist->{$attr}= $val;
316 } else {
317 0 ▶ while (/\G (.*)\r?\n/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G (.*)\r?\n/cgu) 0 0 0
318 0 push @{ $dist->{$attr} }, $1;
319 }
320 }
321 0 ▶ die "Unexpected sub-element of $dist_name $attr\n"
Branch: 1 branch
BranchTrueFalseTotal
if /\G /cgu 0 0 0
322 if /\G /gc;
323 }
324 0 ▶ die "Unexpected sub-element of $dist_name\n"
Branch: 1 branch
BranchTrueFalseTotal
if /\G /cgu 0 0 0
325 if /\G /gc;
326 # convert 'provides' and 'requirements' to hashrefs of versions
327 0 for (qw( provides requirements )) {
328 0 ▶ if (ref $dist->{$_} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dist->{$_} eq "ARRAY") 0 0 0
329 0 $dist->{$_}= { map +((split ' ')[0,1]), @{$dist->{$_}} };
330 }
331 }
332 }
333 0 1;
334 }) {
335 0 chomp $@;
336 0 my $context= _context(20);
337 0 croak "syntax error: $@, near \"$context\"";
338 }
339 0 \%distributions;
340 }
341  
342  
343 11 1;
344  
345 __END__
↑ Summary blib/lib/CPAN/InGit/ArchiveTree.pm »