Patch/Command/Import.pm

filter: Statementstmt 74.0% 100 / 135 Branchbran 41.6% 15 / 36 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 86.9% 20 / 23 Podpod n/a totaltotal 62.5% 142 / 227 CC 27 SCAR 23.7
CC
27
Cov
64%
import_release
26.0
get_releases_from_cpan
16.3
looks_like_git
6.3
CRAP 10.8
« Patch/Command/Sources.pm ↑ Summary Patch/Command/SendPatch.pm »
1 package Git::CPAN::Patch::Command::Import;
2 4 our $AUTHORITY = 'cpan:YANICK';
3 #ABSTRACT: Import a module into a git repository
4 4 $Git::CPAN::Patch::Command::Import::VERSION = '2.5.2';
5 4 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Git::CPAN::Patch::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Git::CPAN::Patch::Release;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Path::Tiny qw/ path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use MooseX::App::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 extends 'Git::CPAN::Patch';
19 4 with 'Git::CPAN::Patch::Role::Git';
20  
21 has tmpdir => (
22 is => 'ro',
23 isa => 'Path::Tiny',
24 lazy => 1,
25 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 return Path::Tiny->tempdir();
27 }
28 4 );
29  
30 4 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our $PERL_GIT_URL = 'https://github.com/Perl/perl5.git';
33  
34 4 option 'norepository' => (
35 is => 'ro',
36 isa => 'Bool',
37 default => 0,
38 documentation => "don't clone git repository",
39 );
40  
41 4 option 'latest' => (
42 is => 'ro',
43 isa => 'Bool',
44 default => 0,
45 documentation => 'only pick latest release, if clone from CPAN',
46 );
47  
48 4 option check => (
49 is => 'ro',
50 isa => 'Bool',
51 default => 1,
52 cmd_negate => 'nocheck',
53 documentation => q{Verifies that the imported version is greater than what is already imported},
54 );
55  
56 option parent => (
57 is => 'ro',
58 isa => 'ArrayRef',
59 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 4 documentation => q{Parent of the imported release (can have more than one)},
61 );
62  
63 4 parameter thing_to_import => (
64 is => 'rw',
65 isa => 'Str',
66 required => 0,
67 );
68  
69 has metacpan => (
70 is => 'ro',
71 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 MetaCPAN::Client->new;
73 },
74 4 );
75  
76 4 option author_name => (
77 is => 'ro',
78 documentation => "explicitly set the author's name",
79 );
80  
81 4 option author_email => (
82 is => 'ro',
83 documentation => "explicitly set the author's email",
84 );
85  
86 0 ▶sub get_releases_from_url($self,$url) {
Subroutine get_releases_from_url: not called CC=2 SCAR=17.9
87 0 require HTTP::Tiny;
88  
89 0 ( my $name = $url ) =~ s#^.*/##;
90 0 my $destination = $self->tmpdir . '/'.$name;
91  
92 0 say "copying '$url' to '$destination'";
93  
94 0 my $response = HTTP::Tiny->new->mirror($url => $destination);
95 die "Failed to mirror $url\n"
96 0 ▶ if !$response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
97  
98 0 return Git::CPAN::Patch::Release->new(
99 metacpan => $self->metacpan,
100 tarball => $destination
101 );
102 }
103  
104 1 ▶sub get_releases_from_local_file($self,$path) {
Subroutine get_releases_from_local_file: called CC=1 SCAR=0.0
105 1 return Git::CPAN::Patch::Release->new( metacpan => $self->metacpan, tarball => $path );
106 }
107  
108 0 ▶sub clone_git_repo($self,$release,$url) {
Subroutine clone_git_repo: not called CC=1 SCAR=6.9
109 0 $self->git_run( 'remote', 'add', 'cpan', $url );
110 {
111 # git will output the tags on STDERR
112 0 local *STDERR;
113 0 open STDERR, '>', \my $err;
114 0 $self->git_run( 'fetch', 'cpan' );
115 0 say $err;
116 }
117 0 $self->git_run( config => 'cpan.module-name', $release->dist_name );
118 }
119  
120 1 ▶sub looks_like_git {
Subroutine looks_like_git: called CC=3 SCAR=18.4
121 1 ▶ my $repo = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $repo = shift() or return 0 1 1
122  
123 0 ▶ return 1 if $repo->{type} eq 'git';
Branch: 1 branch
BranchTrueFalseTotal
if $repo->{"type"} eq "git" 0 0 0
124  
125 0 return $repo->{url} =~ /github\.com|\.git$/;
126 }
127  
128 1 ▶sub get_releases_from_cpan($self,$dist_or_module) {
Subroutine get_releases_from_cpan: called CC=9 SCAR=27.9
129  
130 # is it a module belonging to a distribution?
131 1 ▶ my $dist = eval{ $self->metacpan->module($dist_or_module)->data->{distribution}
Condition33%1/3
eval { do { $self->metacpan->module($dist_or_module)->data->{'distribution'} } } || $dist_or_module
l!l&&r!l&&!r
010
MC/DC0%0/2
eval { do { $self->metacpan->module($dist_or_module)->data->{'distribution'} } } || $dist_or_module
eval { do { $self->metacpan->module($dist_or_module)->data->{'distribution'} } } $dist_or_module
Truth table33%1/3
A || B
A= eval { do { $self->metacpan->module($dist_or_module)->data->{'distribution'} } }B= $dist_or_module
ABresult
1X1
011
000
132 1 } || $dist_or_module;
133  
134 1 ▶ if ( $dist eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist eq "perl") 0 1 1
135 0 die "$dist_or_module is a core modules, ",
136 "clone perl from $PERL_GIT_URL instead.\n";
137 }
138  
139 1 ▶ if( my $latest_release = !$self->norepository && $self->metacpan->release($dist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $latest_release = !$self->norepository && $self->metacpan->release($dist)) 1 0 1
Condition33%1/3
!$self->norepository && $self->metacpan->release($dist)
!ll&&!rl&&r
001
MC/DC0%0/2
!$self->norepository && $self->metacpan->release($dist)
!$self->norepository $self->metacpan->release($dist)
Truth table33%1/3
not(A) && B
A= !$self->norepositoryB= $self->metacpan->release($dist)
ABresult
0X0
100
111
140 1 my $repo = $latest_release->data->{metadata}{resources}{repository};
141 1 ▶ if ( looks_like_git($repo) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_git($repo)) 0 1 1
142 0 say "Git repository found: ", $repo->{url};
143 $self->clone_git_repo(Git::CPAN::Patch::Release->new(
144 metacpan => $self->metacpan,
145 dist_name => $dist,
146 meta_info => $latest_release,
147 0 ),$repo->{url});
148 0 return;
149 }
150 }
151  
152 1 ▶ if ( $self->latest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->latest) 0 1 1
153 0 my $rel = $self->metacpan->release($dist);
154 return Git::CPAN::Patch::Release->new(
155 metacpan => $self->metacpan,
156 meta_info => $rel->data,
157 0 map { $_ => $rel->data->{$_} } qw/ name author date download_url version /
158 );
159 }
160  
161 1 ▶ my $releases = $self->metacpan->release( {
Branch: 1 branch
BranchTrueFalseTotal
my $releases = $self->metacpan->release({"distribution", $dist}, {"sort", [{"date", {"order", "asc"}}]}) or die "could not find release for '${dist_or_module}' on metacpan\n" 1 0 1
162 distribution => $dist
163 }, {
164 sort => [{ date => { order => 'asc' } }],
165 }) or die "could not find release for '$dist_or_module' on metacpan\n";
166  
167 1 my @releases;
168  
169 1 ▶ while( my $r = $releases->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $releases->next) 1 1 2
170 1 push @releases, Git::CPAN::Patch::Release->new(
171 metacpan => $self->metacpan,
172 meta_info => $r->data
173 );
174 }
175  
176 0 return sort { $a->date cmp $b->date } @releases;
177 }
178  
179 2 ▶sub releases_to_import ($self) {
Subroutine releases_to_import: called CC=3 SCAR=11.0
180 2 my $thing = $self->thing_to_import;
181  
182 2 ▶ return $self->get_releases_from_url( $thing )
Branch: 1 branch
BranchTrueFalseTotal
if $thing =~ /^(?:https?|file|ftp)::/u 0 2 2
183 if $thing =~ /^(?:https?|file|ftp)::/;
184  
185 2 ▶ return $self->get_releases_from_local_file( $thing )
Branch: 1 branch
BranchTrueFalseTotal
if -f $thing 1 1 2
186 if -f $thing;
187  
188 1 return $self->get_releases_from_cpan( $thing );
189 }
190  
191 2 ▶sub import_release($self,$release) {
Subroutine import_release: called CC=12 SCAR=32.6
192 2 my $import_version = $release->dist_version;
193  
194 2 ▶ if ( $self->check and $self->last_imported_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check and $self->last_imported_version) 0 2 2
Condition33%1/3
$self->check and $self->last_imported_version
!ll&&!rl&&r
020
MC/DC0%0/2
$self->check and $self->last_imported_version
$self->check $self->last_imported_version
Truth table33%1/3
A and B
A= $self->checkB= $self->last_imported_version
ABresult
0X0
100
111
195 0 ▶ return say $release->dist_name . " $import_version has already been imported\n"
Branch: 1 branch
BranchTrueFalseTotal
if $import_version == $self->last_imported_version 0 0 0
196 if $import_version == $self->last_imported_version;
197  
198 0 ▶ return say sprintf "last imported version %s is more recent than %s"
Branch: 1 branch
BranchTrueFalseTotal
if $import_version <= $self->last_imported_version 0 0 0
199 . ", can't import",
200 $self->last_imported_version, $import_version
201 if $import_version <= $self->last_imported_version;
202 }
203  
204 # create a tree object for the CPAN module
205 # this imports the source code without touching the user's working directory or
206 # index
207  
208 2 my $tree = do {
209 # don't overwrite the user's index
210 2 local $ENV{GIT_INDEX_FILE} = $self->tmpdir . "/temp_git_index";
211 2 local $ENV{GIT_DIR} = path($self->root, '.git')->absolute->stringify;
212 2 local $ENV{GIT_WORK_TREE} = $release->extracted_dir;
213  
214 2 local $CWD = $release->extracted_dir;
215  
216 2 my $write_tree_repo = Git::Repository->new( work_tree => $CWD );
217  
218 2 $write_tree_repo->run( qw(add -v --all --force .) );
219 2 $write_tree_repo->run( "write-tree" );
220 };
221  
222 # create a commit for the imported tree object and write it into
223 # refs/heads/cpan/master
224 {
225 2 local %ENV = %ENV;
226  
227 # TODO authors and author_date
228  
229 # create the commit object
230 2 ▶ $ENV{GIT_AUTHOR_NAME} = $self->author_name || $release->author_name || $ENV{GIT_AUTHOR_NAME};
Condition33%2/6
$self->author_name || $release->author_name || $ENV{'GIT_AUTHOR_NAME'}
l!l&&r!l&&!r
200
$self->author_name or $release->author_name
l!l&&r!l&&!r
020
MC/DC0%0/3
$self->author_name || $release->author_name || $ENV{'GIT_AUTHOR_NAME'}
$self->author_name $release->author_name $ENV{'GIT_AUTHOR_NAME'}
Truth table25%1/4
A or B || C
A= $self->author_nameB= $release->author_nameC= $ENV{'GIT_AUTHOR_NAME'}
ABCresult
1XX1
01X1
0011
0000
231 2 ▶ $ENV{GIT_AUTHOR_EMAIL} = $self->author_email || $release->author_email || $ENV{GIT_AUTHOR_EMAIL};
Condition33%2/6
$self->author_email || $release->author_email || $ENV{'GIT_AUTHOR_EMAIL'}
l!l&&r!l&&!r
200
$self->author_email or $release->author_email
l!l&&r!l&&!r
020
MC/DC0%0/3
$self->author_email || $release->author_email || $ENV{'GIT_AUTHOR_EMAIL'}
$self->author_email $release->author_email $ENV{'GIT_AUTHOR_EMAIL'}
Truth table25%1/4
A or B || C
A= $self->author_emailB= $release->author_emailC= $ENV{'GIT_AUTHOR_EMAIL'}
ABCresult
1XX1
01X1
0011
0000
232 2 ▶ $ENV{GIT_AUTHOR_DATE} = $release->date if $release->date;
Branch: 1 branch
BranchTrueFalseTotal
if $release->date 2 0 2
233  
234 0 my @parents = grep { $_ } $self->last_commit, @{ $self->parent };
235  
236 2 ▶ my $message = sprintf "%s %s %s\n",
Branch: 1 branch
BranchTrueFalseTotal
$self->first_import ? : 2 0 2
237 ( $self->first_import ? 'initial import of' : 'import' ),
238 $release->dist_name, $release->dist_version;
239  
240 4 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 2 $message .= <<"END";
242  
243 2 git-cpan-module: @{[ $release->dist_name ]}
244 2 git-cpan-version: @{[ $release->dist_version ]}
245 2 git-cpan-authorid: @{[ $release->author_cpan ]}
246  
247 END
248  
249 my $commit = $self->git_run(
250 { input => $message },
251 0 'commit-tree', $tree, map { ( -p => $_ ) } @parents );
252  
253 # finally, update the fake remote branch and create a tag for convenience
254  
255 0 print $self->git_run('update-ref', '-m' => "import " . $release->dist_name, 'refs/remotes/cpan/master', $commit );
256  
257 0 ▶ my $tag_name = $release->dist_version =~ /^v/ ? $release->dist_version : 'v'.$release->dist_version;
Branch: 1 branch
BranchTrueFalseTotal
$release->dist_version =~ /^v/u ? : 0 0 0
258 0 print $self->git_run( tag => $tag_name, '--no-sign', $commit );
259  
260 0 say "created tag '@{[ $tag_name ]}' ($commit)";
261 }
262  
263 0 $self->git_run( config => 'cpan.module-name', $release->dist_name );
264 }
265  
266 2 ▶sub run ($self) {
Subroutine run: called CC=3 SCAR=11.0
267 2 my @releases = $self->releases_to_import;
268  
269 2 for my $r ( @releases ) {
270 2 eval { $self->import_release($r) };
271 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 0 2
272 2 warn "failed to import release, skipping...\n$@\n";
273 }
274 }
275 }
276  
277 4 __PACKAGE__->meta->make_immutable;
278  
279 4 1;
280  
281 __END__
« Patch/Command/Sources.pm ↑ Summary Patch/Command/SendPatch.pm »