Patch/Command/Import.pm

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