Patch/Command/Import.pm

filter: Statementstmt 65.6% 130 / 198 Branchbran 28.5% 12 / 42 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 82.3% 28 / 34 Podpod n/a totaltotal 55.8% 177 / 317 CC 45 SCAR 26.6
CC
45
Cov
56%
__ANON__
36.6
get_releases_from_cpan
36.6
__ANON__
19.1
CRAP 14.3
« 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.3.2';
5 4 ▶use 5.10.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 File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Method::Signatures::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Git::CPAN::Patch::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Git::CPAN::Patch::Release;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Path::Class qw/ dir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # TODO Path::Class => Path::Tiny
19  
20 4 ▶use MooseX::App::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 extends 'Git::CPAN::Patch';
23 4 with 'Git::CPAN::Patch::Role::Git';
24  
25 has tmpdir => (
26 is => 'ro',
27 isa => 'Path::Tiny',
28 lazy => 1,
29 default => sub {
30 1 ▶ return Path::Tiny->tempdir();
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32 4 );
33  
34 4 ▶use experimental qw(smartmatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 our $PERL_GIT_URL = 'git://perl5.git.perl.org/perl.git';
37  
38 4 option 'norepository' => (
39 is => 'ro',
40 isa => 'Bool',
41 default => 0,
42 documentation => "don't clone git repository",
43 );
44  
45 4 option 'latest' => (
46 is => 'ro',
47 isa => 'Bool',
48 default => 0,
49 documentation => 'only pick latest release, if clone from CPAN',
50 );
51  
52 4 option check => (
53 is => 'ro',
54 isa => 'Bool',
55 default => 1,
56 documentation => q{Verifies that the imported version is greater than what is already imported},
57 );
58  
59 option parent => (
60 is => 'ro',
61 isa => 'ArrayRef',
62 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 4 documentation => q{Parent of the imported release (can have more than one)},
64 );
65  
66 4 parameter thing_to_import => (
67 is => 'rw',
68 isa => 'Str',
69 required => 0,
70 );
71  
72 has metacpan => (
73 is => 'ro',
74 default => sub {
75 0 ▶ MetaCPAN::Client->new;
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 },
77 4 );
78  
79 4 option author_name => (
80 is => 'ro',
81 documentation => "explicitly set the author's name",
82 );
83  
84 4 option author_email => (
85 is => 'ro',
86 documentation => "explicitly set the author's email",
87 );
88  
89 4 ▶method get_releases_from_url($url) {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=2 SCAR=16.3
90 0 require LWP::Simple;
91  
92 0 ( my $name = $url ) =~ s#^.*/##;
93 0 my $destination = $self->tmpdir . '/'.$name;
94  
95 0 say "copying '$url' to '$destination'";
96  
97 0 ▶ LWP::Simple::mirror( $url => $destination )
Branch: 1 branch
BranchTrueFalseTotal
LWP::Simple::mirror($url, $destination) or die "Failed to mirror $url\n" 0 0 0
98 or die "Failed to mirror $url\n";
99  
100 0 return Git::CPAN::Patch::Release->new(
101 metacpan => $self->metacpan,
102 tarball => $destination
103 );
104 4 }
105  
106 4 ▶method get_releases_from_local_file($path) {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=1 SCAR=4.1
107 0 return Git::CPAN::Patch::Release->new( metacpan => $self->metacpan, tarball => $path );
108 4 }
109  
110 4 ▶method clone_git_repo($release,$url) {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=1 SCAR=5.6
111 0 $self->git_run( 'remote', 'add', 'cpan', $url );
112 {
113 # git will output the tags on STDERR
114 0 local *STDERR;
115 0 open STDERR, '>', \my $err;
116 0 $self->git_run( 'fetch', 'cpan' );
117 0 say $err;
118 }
119 0 $self->git_run( config => 'cpan.module-name', $release->dist_name );
120 4 }
121  
122 sub looks_like_git {
123 1 ▶ my $repo = shift or return;
Subroutine looks_like_git: called CC=3 SCAR=18.4
Branch: 1 branch
BranchTrueFalseTotal
my $repo = shift() or return 1 0 1
124  
125 0 ▶ return 1 if $repo->{type} eq 'git';
Branch: 1 branch
BranchTrueFalseTotal
if $repo->{"type"} eq "git" 0 0 0
126  
127 0 return $repo->{url} =~ /github\.com|\.git$/;
128 }
129  
130 4 ▶method get_releases_from_cpan($dist_or_module) {
Subroutine BEGIN: called CC=1 SCAR=0.8
Subroutine get_releases_from_cpan: not called CC=9 SCAR=36.0
Subroutine __ANON__: called CC=9 SCAR=36.0
131  
132 # is it a module belonging to a distribution?
133 0 ▶ my $dist = eval{ $self->metacpan->module($dist_or_module)->data->{distribution}
Condition16%1/6
eval { || $dist_or_module
l!l&&r!l&&!r
000
eval { || $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
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 table0%0/3
A || B
A= eval { do { $self->metacpan->module($dist_or_module)->data->{'distribution'} } }B= $dist_or_module
ABresult
1X1
011
000
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
134 0 } || $dist_or_module;
135  
136 0 ▶ if ( $dist eq 'perl' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dist eq "perl") 0 0 0
if ($dist eq "perl") 0 1 1
137 0 die "$dist_or_module is a core modules, ",
138 "clone perl from $PERL_GIT_URL instead.\n";
139 }
140  
141 0 ▶ if( my $latest_release = !$self->norepository && $self->metacpan->release($dist)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $latest_release = !$self->norepository && $self->metacpan->release($dist)) 0 0 0
if (my $latest_release = !$self->norepository && $self->metacpan->release($dist)) 1 0 1
Condition16%1/6
!$self->norepository && $self->metacpan->release($dist)
!ll&&!rl&&r
000
!$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)
MC/DC0%0/2
!$self->norepository && $self->metacpan->release($dist)
!$self->norepository $self->metacpan->release($dist)
Truth table0%0/3
not(A) && B
A= !$self->norepositoryB= $self->metacpan->release($dist)
ABresult
0X0
100
111
Truth table33%1/3
not(A) && B
A= !$self->norepositoryB= $self->metacpan->release($dist)
ABresult
0X0
100
111
142 0 my $repo = $latest_release->data->{metadata}{resources}{repository};
143 0 ▶ if ( looks_like_git($repo) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (looks_like_git($repo)) 0 0 0
if (looks_like_git($repo)) 0 1 1
144 0 say "Git repository found: ", $repo->{url};
145 $self->clone_git_repo(Git::CPAN::Patch::Release->new(
146 metacpan => $self->metacpan,
147 dist_name => $dist,
148 meta_info => $latest_release,
149 0 ),$repo->{url});
150 0 return;
151 }
152 }
153  
154 0 ▶ if ( $self->latest ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->latest) 0 0 0
if ($self->latest) 0 1 1
155 0 my $rel = $self->metacpan->release($dist);
156 return Git::CPAN::Patch::Release->new(
157 metacpan => $self->metacpan,
158 meta_info => $rel->data,
159 0 map { $_ => $rel->data->{$_} } qw/ name author date download_url version /
160 );
161 }
162  
163 0 ▶ my $releases = $self->metacpan->release( {
Branch: 2 branches
BranchTrueFalseTotal
my $releases = $self->metacpan->release({"distribution", $dist}) or die "could not find release for '${dist_or_module}' on metacpan\n" 0 0 0
my $releases = $self->metacpan->release({"distribution", $dist}) or die "could not find release for '${dist_or_module}' on metacpan\n" 0 1 1
164 distribution => $dist
165 }) or die "could not find release for '$dist_or_module' on metacpan\n";
166  
167 0 my @releases;
168  
169 0 ▶ while( my $r = $releases->next ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $releases->next) 0 0 0
if (my $r = $releases->next) 1 1 2
170 0 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 4 }
178  
179 4 ▶method releases_to_import {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.1
180 1 given ( $self->thing_to_import ) {
181 1 when ( qr/^(?:https?|file|ftp)::/ ) {
182 0 return $self->get_releases_from_url( $_ );
183 }
184 1 when ( -f $_ ) {
185 0 return $self->get_releases_from_local_file( $_ );
186 }
187 1 default {
188 1 return $self->get_releases_from_cpan($_);
189 }
190 }
191 4 }
192  
193 4 ▶method import_release($release) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine import_release: called CC=11 SCAR=29.5
Subroutine __ANON__: not called CC=11 SCAR=29.5
194 1 my $import_version = $release->dist_version;
195  
196 1 ▶ if ( $self->check and $self->last_imported_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check and $self->last_imported_version) 0 1 1
Condition33%1/3
$self->check and $self->last_imported_version
!ll&&!rl&&r
010
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
197 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
198 if $import_version == $self->last_imported_version;
199  
200 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
201 . ", can't import",
202 $self->last_imported_version, $import_version
203 if $import_version <= $self->last_imported_version;
204 }
205  
206 # create a tree object for the CPAN module
207 # this imports the source code without touching the user's working directory or
208 # index
209  
210 1 my $tree = do {
211 # don't overwrite the user's index
212 1 local $ENV{GIT_INDEX_FILE} = $self->tmpdir . "/temp_git_index";
213 1 local $ENV{GIT_DIR} = dir($self->root . '/.git')->absolute->stringify;
214 1 local $ENV{GIT_WORK_TREE} = $release->extracted_dir;
215  
216 1 local $CWD = $release->extracted_dir;
217  
218 1 my $write_tree_repo = Git::Repository->new( work_tree => $CWD );
219  
220 1 $write_tree_repo->run( qw(add -v --all --force .) );
221 1 $write_tree_repo->run( "write-tree" );
222 };
223  
224 # create a commit for the imported tree object and write it into
225 # refs/heads/cpan/master
226 {
227 1 local %ENV = %ENV;
228  
229 # TODO authors and author_date
230  
231 # create the commit object
232 1 ▶ $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
100
$self->author_name or $release->author_name
l!l&&r!l&&!r
010
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
233 1 ▶ $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
100
$self->author_email or $release->author_email
l!l&&r!l&&!r
010
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
234 1 ▶ $ENV{GIT_AUTHOR_DATE} = $release->date if $release->date;
Branch: 1 branch
BranchTrueFalseTotal
if $release->date 1 0 1
235  
236 1 my @parents = grep { $_ } $self->last_commit, @{ $self->parent };
237  
238 1 ▶ my $message = sprintf "%s %s %s\n",
Branch: 1 branch
BranchTrueFalseTotal
$self->first_import ? : 1 0 1
239 ( $self->first_import ? 'initial import of' : 'import' ),
240 $release->dist_name, $release->dist_version;
241  
242 4 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 1 $message .= <<"END";
244  
245 1 git-cpan-module: @{[ $release->dist_name ]}
246 1 git-cpan-version: @{[ $release->dist_version ]}
247 1 git-cpan-authorid: @{[ $release->author_cpan ]}
248  
249 END
250  
251 my $commit = $self->git_run(
252 { input => $message },
253 1 'commit-tree', $tree, map { ( -p => $_ ) } @parents );
254  
255 # finally, update the fake remote branch and create a tag for convenience
256  
257 0 print $self->git_run('update-ref', '-m' => "import " . $release->dist_name, 'refs/remotes/cpan/master', $commit );
258  
259 0 print $self->git_run( tag => 'v'.$release->dist_version, $commit );
260  
261 0 say "created tag '@{[ 'v'.$release->dist_version ]}' ($commit)";
262 }
263  
264 0 $self->git_run( config => 'cpan.module-name', $release->dist_name );
265 4 }
266  
267 4 ▶method run {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.0
268 1 my @releases = $self->releases_to_import;
269  
270 1 for my $r ( @releases ) {
271 1 eval { $self->import_release($r) };
272 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
273 1 warn "failed to import release, skipping...\n$@\n";
274 }
275 }
276 4 }
277  
278 4 __PACKAGE__->meta->make_immutable;
279  
280 4 1;
281  
282 __END__
283  
284 =pod
285  
286 =encoding UTF-8
287  
288 =head1 NAME
289  
290 Git::CPAN::Patch::Command::Import - Import a module into a git repository
291  
292 =head1 VERSION
293  
294 version 2.3.2
295  
296 =head1 SYNOPSIS
297  
298 # takes any string CPANPLUS handles:
299  
300 % git-cpan import Foo::Bar
301 % git-cpan import A/AU/AUTHORID/Foo-Bar-0.03.tar.gz
302 % git-cpan import http://backpan.cpan.org/authors/id/A/AU/AUTHORID/Foo-Bar-0.03.tar.gz
303  
304 # If the repository is already initialized, can be run with no arguments to
305 # import the latest version
306 git-cpan import
307  
308 =head1 DESCRIPTION
309  
310 This command is used internally by C<git-cpan-init>, C<git-cpan-update> and
311 C<git-backpan-init>.
312  
313 This command takes a tarball, extracts it, and imports it into the repository.
314  
315 It is only possible to update to a newer version of a module.
316  
317 The module history is tracked in C<refs/remotes/cpan/master>.
318  
319 Tags are created for each version of the module.
320  
321 This command does not touch the working directory, and is safe to run even if
322 you have pending work.
323  
324 =head1 OPTIONS
325  
326 =over
327  
328 =item --check, --nocheck
329  
330 Explicitly enables/disables version checking. If version checking is
331 enabled, which is the default, git-cpan-import will refuse to import a
332 version of the package
333 that has a smaller version number than the HEAD of the branch I<cpan/master>.
334  
335 =item --parent
336  
337 Allows adding extra parents when
338 importing, so that when a patch has been incorporated into an upstream
339 version the generated commit is like a merge commit, incorporating both
340 the CPAN history and the user's local history.
341  
342 For example, this will set the current HEAD of the master branch as a parent of
343 the imported CPAN package:
344  
345 $ git checkout master
346 $ git-cpan import --parent HEAD My-Module
347  
348 More than one '--parent' can be specified.
349  
350 =item --author_name
351  
352 Forces the author name to the given value, instead of trying to resolve it from
353 the release metadata.
354  
355 =item --author_email
356  
357 Forces the author email to the given value, instead of trying to resolve it from
358 the release metadata.
359  
360 =back
361  
362 =head1 AUTHORS
363  
364 Yuval Kogman C<< <nothingmuch@woobling.org> >>
365  
366 Yanick Champoux C<< <yanick@cpan.org> >>
367  
368 =head1 SEE ALSO
369  
370 L<Git::CPAN::Patch>
371  
372 =head1 AUTHOR
373  
374 Yanick Champoux <yanick@cpan.org>
375  
376 =head1 COPYRIGHT AND LICENSE
377  
378 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009 by Yanick Champoux.
379  
380 This is free software; you can redistribute it and/or modify it under
381 the same terms as the Perl 5 programming language system itself.
382  
383 =cut
« Patch/Command/Sources.pm ↑ Summary Patch/Command/SendPatch.pm »