Patch/Import.pm

filter: Statementstmt 24.5% 75 / 306 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 62.1% 23 / 37 Podpod 0.0% 0 / 12 totaltotal 19.2% 98 / 508 CC 64 SCAR 54.0
CC
64
Cov
17%
main
600.0
import_one_backpan_release
342.0
import_from_backpan
90.0
CRAP 221.3
↑ Summary Patch/Command/Sources.pm »
1 package Git::CPAN::Patch::Import;
2 6 our $AUTHORITY = 'cpan:YANICK';
3 6 $Git::CPAN::Patch::Import::VERSION = '2.3.2';
4 6 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 {
10 6 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use File::chmod (); # must be before 'autodie' to hush the warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 $Archive::Extract::PREFER_BIN = 1;
19  
20 6 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Path::Class qw/ file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Cwd qw/ getcwd /;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use CLASS;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 6 ▶use CPANPLUS;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use BackPAN::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 }
37  
38 6 our $BackPAN_URL = "http://backpan.perl.org/";
39 6 our $PERL_GIT_URL = 'git://perl5.git.perl.org/perl.git';
40  
41 sub backpan_index {
42 0 ▶ state $backpan = do {
Subroutine backpan_index: not called CC=1 SCAR=6.9
Pod undocumented
43 0 say "Loading BackPAN index (this may take a while)";
44 0 BackPAN::Index->new;
45 };
46 0 return $backpan;
47 }
48  
49 sub cpanplus {
50 0 ▶ state $cpanplus = CPANPLUS::Backend->new;
Subroutine cpanplus: not called CC=1 SCAR=6.9
Pod undocumented
51 0 return $cpanplus;
52 }
53  
54 # Make sure we can read tarballs and change directories
55 sub _fix_permissions {
56 0 ▶ my $dir = shift;
Subroutine _fix_permissions: not called CC=1 SCAR=6.9
57  
58 0 File::chmod::chmod "u+rx", $dir;
59 find(sub {
60 0 ▶ -d $_ ? File::chmod::chmod "u+rx", $_ : File::chmod::chmod "u+r", $_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
61 0 }, $dir);
62 }
63  
64 sub init_repo {
65 0 ▶ my $module = shift;
Subroutine init_repo: not called CC=8 SCAR=42.8
Pod undocumented
66 0 my $opts = shift;
67  
68 0 my $dirname = ".";
69 0 ▶ if ( defined $opts->{mkdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"mkdir"}) 0 0 0
70 0 ▶ ( $dirname = $opts->{mkdir} || $module ) =~ s/::/-/g;
Condition0%0/3
$opts->{'mkdir'} || $module
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'mkdir'} || $module
$opts->{'mkdir'} $module
Truth table0%0/3
A || B
A= $opts->{'mkdir'}B= $module
ABresult
1X1
011
000
71  
72 0 ▶ if( -d $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dirname) { } 0 0 0
73 0 ▶ die "$dirname already exists\n" unless $opts->{update};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"update"} 0 0 0
74 }
75 else {
76 0 say "creating directory $dirname";
77  
78 # mkpath() does not play nice with overloaded objects
79 0 mkpath "$dirname";
80 }
81 }
82  
83 {
84 0 local $CWD = $dirname;
85  
86 0 ▶ if ( -d '.git' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d '.git') { } 0 0 0
87 0 ▶ if ( !$opts->{force} and !$opts->{update} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"force"} or $opts->{"update"}) 0 0 0
Condition0%0/3
$opts->{"force"} or $opts->{"update"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"force"} or $opts->{"update"}
$opts->{"force"} $opts->{"update"}
Truth table0%0/3
A or B
A= $opts->{"force"}B= $opts->{"update"}
ABresult
1X1
011
000
88 0 die "Aborting: git repository already present.\n",
89 "use '--force' if it's really what you want to do\n";
90 }
91 }
92 else {
93 0 Git::Repository->run('init');
94 }
95 }
96  
97 0 return File::Spec->rel2abs($dirname);
98 }
99  
100  
101 sub releases_in_git {
102 0 ▶ my $repo = Git::Repository->new;
Subroutine releases_in_git: not called CC=2 SCAR=17.9
Pod undocumented
103 0 ▶ return unless contains_git_revisions();
Branch: 1 branch
BranchTrueFalseTotal
unless contains_git_revisions() 0 0 0
104 0 my @releases = map { m{\bgit-cpan-version:\s*(\S+)}x; $1 }
105 grep /^\s*git-cpan-version:/,
106 $repo->run(log => '--pretty=format:%b');
107 0 return @releases;
108 }
109  
110  
111 sub rev_exists {
112 0 ▶ my $rev = shift;
Subroutine rev_exists: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $repo = Git::Repository->new;
114  
115 0 return eval { $repo->run( 'rev-parse', $rev ); };
116 }
117  
118  
119 sub contains_git_revisions {
120 0 ▶ return unless -d ".git";
Subroutine contains_git_revisions: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless -d ".git" 0 0 0
121 0 return rev_exists("HEAD");
122 }
123  
124  
125 sub import_one_backpan_release {
126 0 ▶ my $release = shift;
Subroutine import_one_backpan_release: not called CC=18 SCAR=58.3
Pod undocumented
127 0 my $opts = shift;
128 0 ▶ my $backpan_urls = $opts->{backpan} || $BackPAN_URL;
Condition0%0/3
$opts->{'backpan'} || $Git::CPAN::Patch::Import::BackPAN_URL
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'backpan'} || $Git::CPAN::Patch::Import::BackPAN_URL
$opts->{'backpan'} $Git::CPAN::Patch::Import::BackPAN_URL
Truth table0%0/3
A || B
A= $opts->{'backpan'}B= $Git::CPAN::Patch::Import::BackPAN_URL
ABresult
1X1
011
000
129  
130 # allow multiple backpan URLs to be supplied
131 0 ▶ $backpan_urls = [ $backpan_urls ] unless (ref($backpan_urls) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $backpan_urls eq "ARRAY" 0 0 0
132  
133 0 my $repo = Git::Repository->new;
134  
135 0 my( $last_commit, $last_version );
136  
137 # figure out if there is already an imported module
138 0 ▶ if ( $last_commit = eval { $repo->run("rev-parse", "-q", "--verify", "cpan/master") } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_commit = eval { do { $repo->run("rev-parse", "-q", "--verify", "cpan/master") } }) 0 0 0
139 0 $last_version = $repo->run("cpan-last-version");
140 }
141  
142 my $tmp_dir = File::Temp->newdir(
143 0 ▶ $opts->{tempdir} ? (DIR => $opts->{tempdir}) : ()
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'tempdir'} ? : 0 0 0
144 );
145  
146 0 my $archive_file = catfile($tmp_dir, $release->filename);
147 0 mkpath dirname $archive_file;
148  
149 0 my $response;
150 0 for my $backpan_url (@$backpan_urls) {
151 0 my $release_url = $backpan_url . "/" . $release->prefix;
152  
153 0 say "Downloading $release_url";
154 0 $response = get_from_url($release_url, $archive_file);
155 0 ▶ last if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 0 0 0
156  
157 0 say " failed @{[ $response->status_line ]}";
158 }
159  
160 0 ▶ if( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
161 0 say "Fetch failed. Skipping.";
162 0 return;
163 }
164  
165 0 ▶ if( !-e $archive_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $archive_file) 0 0 0
166 0 say "$archive_file is missing. Skipping.";
167 0 return;
168 }
169  
170 0 say "extracting distribution";
171 0 my $ae = Archive::Extract->new( archive => $archive_file );
172 0 ▶ unless( $ae->extract( to => $tmp_dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ae->extract("to", $tmp_dir)) 0 0 0
173 0 say "Couldn't extract $archive_file to $tmp_dir because ".$ae->error;
174 0 say "Skipping";
175 0 return;
176 }
177  
178 0 my $dir = $ae->extract_path;
179 0 ▶ if( !$dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 0 0
180 0 say "The archive is empty, skipping";
181 0 return;
182 }
183 0 _fix_permissions($dir);
184  
185 0 my $tree = do {
186 # don't overwrite the user's index
187 0 local $ENV{GIT_INDEX_FILE} = catfile($tmp_dir, "temp_git_index");
188 0 local $ENV{GIT_DIR} = catfile( getcwd(), '.git' );
189 0 local $ENV{GIT_WORK_TREE} = $dir;
190  
191 0 local $CWD = $dir;
192  
193 0 my $write_tree_repo = Git::Repository->new( work_tree => $dir ) ;
194  
195 0 $write_tree_repo->run( qw(add -v --force .) );
196 0 $write_tree_repo->run( "write-tree" );
197 };
198  
199 # Create a commit for the imported tree object and write it into
200 # refs/remotes/cpan/master
201 0 local %ENV = %ENV;
202 0 ▶ $ENV{GIT_AUTHOR_DATE} ||= $release->date;
Condition0%0/3
$ENV{"GIT_AUTHOR_DATE"} ||= $release->date
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"GIT_AUTHOR_DATE"} ||= $release->date
$ENV{"GIT_AUTHOR_DATE"} $release->date
Truth table0%0/3
A ||= B
A= $ENV{"GIT_AUTHOR_DATE"}B= $release->date
ABresult
1X1
011
000
203  
204 0 my $author = $CLASS->cpanplus->author_tree($release->cpanid);
205 0 ▶ $ENV{GIT_AUTHOR_NAME} ||= $author->author;
Condition0%0/3
$ENV{"GIT_AUTHOR_NAME"} ||= $author->author
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"GIT_AUTHOR_NAME"} ||= $author->author
$ENV{"GIT_AUTHOR_NAME"} $author->author
Truth table0%0/3
A ||= B
A= $ENV{"GIT_AUTHOR_NAME"}B= $author->author
ABresult
1X1
011
000
206 0 ▶ $ENV{GIT_AUTHOR_EMAIL} ||= $author->email;
Condition0%0/3
$ENV{"GIT_AUTHOR_EMAIL"} ||= $author->email
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"GIT_AUTHOR_EMAIL"} ||= $author->email
$ENV{"GIT_AUTHOR_EMAIL"} $author->email
Truth table0%0/3
A ||= B
A= $ENV{"GIT_AUTHOR_EMAIL"}B= $author->email
ABresult
1X1
011
000
207  
208 0 my @parents = grep { $_ } $last_commit;
209  
210  
211 # commit message
212 0 my $name = $release->dist;
213 0 ▶ my $version = $release->version || '';
Condition0%0/2
$release->version || ''
l!l
00
MC/DC0%0/1
$release->version || ''
$release->version
Truth table0%0/2
A
A= $release->version
Aresult
11
00
214 0 ▶ my $message = join ' ', ( $last_version ? "import" : "initial import of"), "$name $version from CPAN\n";
Branch: 1 branch
BranchTrueFalseTotal
$last_version ? : 0 0 0
215 0 $message .= <<"END";
216  
217 git-cpan-module: $name
218 git-cpan-version: $version
219 0 git-cpan-authorid: @{[ $author->cpanid ]}
220 0 git-cpan-file: @{[ $release->prefix ]}
221  
222 END
223  
224 my $commit = $repo->run( { input => $message }, 'commit-tree', $tree,
225 0 map { ( -p => $_ ) } @parents );
226  
227 # finally, update the fake branch and create a tag for convenience
228 0 my $dist = $release->dist;
229 0 print $repo->run('update-ref', '-m' => "import $dist", 'refs/heads/cpan/master', $commit );
230  
231 0 ▶ if( $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 0 0 0
232 0 my $tag = $version;
233 0 $tag =~ s{^\.}{0.}; # git does not like a leading . as a tag name
234 0 $tag =~ s{\.$}{}; # nor a trailing one
235 0 ▶ if( $repo->run( "tag", "-l" => $tag ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repo->run("tag", "-l", $tag)) 0 0 0
236 0 say "Tag $tag already exists, overwriting";
237 }
238 0 print $repo->run( "tag", "-f" => $tag, $commit );
239 0 say "created tag '$tag' ($commit)";
240 }
241 }
242  
243  
244 sub get_from_url {
245 0 ▶ my($url, $file) = @_;
Subroutine get_from_url: not called CC=1 SCAR=6.9
Pod undocumented
246  
247 0 require LWP::UserAgent;
248 0 my $ua = LWP::UserAgent->new;
249  
250 0 my $req = HTTP::Request->new( GET => $url );
251 0 my $res = $ua->request($req, $file);
252  
253 0 return $res;
254 }
255  
256  
257 sub import_from_backpan {
258 0 ▶ my ( $distname, $opts ) = @_;
Subroutine import_from_backpan: not called CC=9 SCAR=45.0
Pod undocumented
259  
260 0 $distname =~ s/::/-/g;
261  
262 # handle --mkdir and raise an error if the target directory has already been git-initialized
263 0 my $repo_dir = init_repo($distname, $opts);
264  
265 0 local $CWD = $repo_dir;
266  
267 0 my $backpan = $CLASS->backpan_index;
268 0 ▶ my $dist = $backpan->dist($distname)
Branch: 1 branch
BranchTrueFalseTotal
my $dist = $backpan->dist($distname) or die "Error: no distributions found. ", "Are you sure you spelled the module name correctly?\n" 0 0 0
269 or die "Error: no distributions found. ",
270 "Are you sure you spelled the module name correctly?\n";
271  
272 0 fixup_repository();
273  
274 0 my %existing_releases;
275 0 ▶ %existing_releases = map { $_ => 1 } releases_in_git() if $opts->{update};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"update"} 0 0 0
276 0 my $release_added = 0;
277 0 for my $release ($dist->releases->search( undef, { order_by => "date" } )) {
278 0 ▶ next if $existing_releases{$release->version};
Branch: 1 branch
BranchTrueFalseTotal
if $existing_releases{$release->version} 0 0 0
279  
280 # skip .ppm files
281 0 ▶ next if $release->filename =~ m{\.ppm\b};
Branch: 1 branch
BranchTrueFalseTotal
if $release->filename =~ /\.ppm\b/ 0 0 0
282  
283 0 say "importing $release";
284 0 import_one_backpan_release(
285 $release,
286 $opts,
287 );
288 0 $release_added++;
289 }
290  
291 0 ▶ if( !$release_added ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($release_added) 0 0 0
292 0 ▶ if( !keys %existing_releases ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %existing_releases) { } 0 0 0
293 0 say "Empty repository for $dist. Deleting.";
294  
295 # We can't delete it if we're inside it.
296 0 $CWD = "..";
297 0 rmtree $repo_dir;
298  
299 0 return;
300 }
301 else {
302 0 say "No updates for $dist.";
303 0 return;
304 }
305 }
306  
307 0 my $repo = Git::Repository->new;
308 0 ▶ if( !rev_exists("master") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not rev_exists('master')) { } 0 0 0
309 0 print $repo->run('checkout', '-t', '-b', 'master', 'cpan/master');
310 }
311 else {
312 0 print $repo->run('checkout', 'master', '.'),
313 $repo->run('merge', 'cpan/master');
314 }
315  
316 0 return $repo_dir;
317 }
318  
319  
320 sub fixup_repository {
321 0 ▶ my $repo = Git::Repository->new;
Subroutine fixup_repository: not called CC=4 SCAR=30.0
Pod undocumented
322  
323 0 ▶ return unless -d ".git";
Branch: 1 branch
BranchTrueFalseTotal
unless -d ".git" 0 0 0
324  
325 # We do our work in cpan/master, it might not exist if this
326 # repo was cloned from gitpan.
327 0 ▶ if( !rev_exists("cpan/master") and rev_exists("master") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not rev_exists("cpan/master") and rev_exists("master")) 0 0 0
Condition0%0/3
not rev_exists("cpan/master") and rev_exists("master")
!ll&&!rl&&r
000
MC/DC0%0/2
not rev_exists("cpan/master") and rev_exists("master")
not rev_exists("cpan/master") rev_exists("master")
Truth table0%0/3
not(A) and B
A= not rev_exists("cpan/master")B= rev_exists("master")
ABresult
0X0
100
111
328 0 print $repo->run('branch', '-t', 'cpan/master', 'master');
329 }
330 }
331  
332 6 ▶use MetaCPAN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
333 6 my $mcpan = MetaCPAN::API->new;
334  
335 sub find_release {
336 0 ▶ my $input = shift;
Subroutine find_release: not called CC=3 SCAR=24.8
Pod undocumented
337  
338 return eval { $mcpan->release(
339 distribution => $mcpan->module($input)->{distribution}
340 0 ) }
341 0 ▶ || eval { $mcpan->release( distribution => $input ) }
Condition0%0/5
eval { || die("could not find release for '${input}' on metacpan\n")
l!l
00
eval { or eval {
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $mcpan->release('distribution', $mcpan->module($input)->{'distribution'}) } } || eval { do { $mcpan->release('distribution', $input) } } || die("could not find release for '${input}' on metacpan\n")
eval { do { $mcpan->release("distribution", $mcpan->module($input)->{"distribution"}) } } eval { do { $mcpan->release("distribution", $input) } }
Truth table0%0/3
A or B
A= eval { do { $mcpan->release("distribution", $mcpan->module($input)->{"distribution"}) } }B= eval { do { $mcpan->release("distribution", $input) } }
ABresult
1X1
011
000
342 || die "could not find release for '$input' on metacpan\n";
343  
344 }
345  
346 sub main {
347 0 ▶ my $module = shift;
Subroutine main: not called CC=24 SCAR=64.0
Pod undocumented
348 0 my $opts = shift;
349  
350 0 ▶ if ( delete $opts->{backpan} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $opts->{"backpan"}) 0 0 0
351 0 return import_from_backpan( $module, $opts );
352 }
353  
354 0 my $repo = Git::Repository->new;
355  
356 0 my ( $last_commit, $last_version );
357  
358 # figure out if there is already an imported module
359 0 ▶ if ( $last_commit = eval { $repo->run("rev-parse", "-q", "--verify", "cpan/master") } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_commit = eval { do { $repo->run("rev-parse", "-q", "--verify", "cpan/master") } }) 0 0 0
360 0 ▶ $module ||= $repo->run("cpan-which");
Condition0%0/3
$module ||= $repo->run("cpan-which")
l!l&&r!l&&!r
000
MC/DC0%0/2
$module ||= $repo->run("cpan-which")
$module $repo->run("cpan-which")
Truth table0%0/3
A ||= B
A= $moduleB= $repo->run("cpan-which")
ABresult
1X1
011
000
361 0 $last_version = $repo->run("cpan-last-version");
362 }
363  
364 0 ▶ die("Usage: git-cpan import Foo::Bar\n") unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
365  
366 # first we figure out a module object from the module argument
367  
368 0 my $release = find_release($module);
369  
370 # based on the version number it figured out for us we decide whether or not to
371 # actually import.
372  
373 0 my $name = $release->{name};
374 0 my $version = $release->{version};
375 0 my $dist = $release->{distribution};
376  
377 0 ▶ if ( $dist eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist eq "perl") 0 0 0
378 0 say "$name is a core modules, ",
379 "clone perl from $PERL_GIT_URL instead.";
380 0 exit;
381 }
382  
383 0 my $prettyname = $dist . ( " ($module)" x ( $dist ne $module ) );
384  
385 0 ▶ if ( $last_version and $opts->{checkversion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_version and $opts->{'checkversion'}) { } 0 0 0
Condition0%0/3
$last_version and $opts->{"checkversion"}
!ll&&!rl&&r
000
MC/DC0%0/2
$last_version and $opts->{"checkversion"}
$last_version $opts->{"checkversion"}
Truth table0%0/3
A and B
A= $last_versionB= $opts->{"checkversion"}
ABresult
0X0
100
111
386 # if last_version is defined this is an update
387 0 my $imported = version->new($last_version);
388 0 my $will_import = version->new($release->{version});
389  
390 0 ▶ die "$name has already been imported\n" if $imported == $will_import;
Branch: 1 branch
BranchTrueFalseTotal
if $imported == $will_import 0 0 0
391  
392 0 ▶ die "imported version $imported is more recent than $will_import, can't import\n"
Branch: 1 branch
BranchTrueFalseTotal
if $imported > $will_import 0 0 0
393 if $imported > $will_import;
394  
395 0 say "updating $prettyname from $imported to $will_import";
396  
397 } else {
398 0 say "importing $prettyname";
399 }
400  
401 0 require LWP::UserAgent;
402  
403 0 my $ua = LWP::UserAgent->new;
404  
405 # download the dist and extract into a temporary directory
406 0 my $tmp_dir = tempdir( CLEANUP => 0 );
407  
408 0 say "downloading $dist";
409  
410 0 my $tarball = file( $tmp_dir, $release->{archive} );
411  
412 $ua->mirror(
413 0 ▶ $release->{download_url} => $tarball
Branch: 1 branch
BranchTrueFalseTotal
$ua->mirror($release->{"download_url"}, $tarball) or die "couldn't fetch tarball\n" 0 0 0
414 ) or die "couldn't fetch tarball\n";
415  
416 0 say "extracting distribution";
417  
418 0 my $archive = Archive::Extract->new( archive => $tarball );
419 0 $archive->extract( to => $tmp_dir );
420  
421 0 ▶ my $dist_dir = $archive->extract_path
Branch: 1 branch
BranchTrueFalseTotal
my $dist_dir = $archive->extract_path or die "extraction failed\n" 0 0 0
422 or die "extraction failed\n";
423  
424 # create a tree object for the CPAN module
425 # this imports the source code without touching the user's working directory or
426 # index
427  
428 0 my $tree = do {
429 # don't overwrite the user's index
430 0 local $ENV{GIT_INDEX_FILE} = catfile($tmp_dir, "temp_git_index");
431 0 local $ENV{GIT_DIR} = catfile( getcwd(), '.git' );
432 0 local $ENV{GIT_WORK_TREE} = $dist_dir;
433  
434 0 local $CWD = $dist_dir;
435  
436 0 my $write_tree_repo = Git::Repository->new( work_tree => $dist_dir );
437  
438 0 $write_tree_repo->run( qw(add -v --force .) );
439 0 $write_tree_repo->run( "write-tree" );
440 };
441  
442 # create a commit for the imported tree object and write it into
443 # refs/heads/cpan/master
444  
445 {
446 0 local %ENV = %ENV;
447  
448 0 my $author_obj = $mcpan->author($release->{author});
449  
450 # try to find a date for the version using the backpan index
451 # secondly, if the CPANPLUS author object is a fake one (e.g. when importing a
452 # URI), get the user object by using the ID from the backpan index
453 0 ▶ unless ( $ENV{GIT_AUTHOR_DATE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"GIT_AUTHOR_DATE"}) 0 0 0
454 0 my $mtime = eval {
455 0 DateTime->from_epoch( epoch => $release->{stat}{mtime})->ymd;
456 };
457  
458 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
459  
460 # CPAN::Checksums makes YYYY-MM-DD dates, but GIT_AUTHOR_DATE
461 # doesn't support that.
462 0 ▶ $mtime .= 'T00:00::00'
Branch: 1 branch
BranchTrueFalseTotal
if $mtime =~ /\A (\d\d\d\d) - (\d\d?) - (\d\d?) \z/x 0 0 0
463 if $mtime =~ m/\A (\d\d\d\d) - (\d\d?) - (\d\d?) \z/x;
464  
465 0 ▶ if ( $mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime) { } 0 0 0
466 0 $ENV{GIT_AUTHOR_DATE} = $mtime;
467 } else {
468 0 my %dists;
469  
470 0 ▶ if ( $opts->{backpan} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"backpan"}) 0 0 0
471 # we need the backpan index for dates
472 0 my $backpan = $CLASS->backpan_index;
473  
474 0 %dists = map { $_->filename => $_ }
475 0 $backpan->releases($release->{name});
476 }
477  
478 0 ▶ if ( my $bp_dist = $dists{$dist} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bp_dist = $dists{$dist}) { } 0 0 0
479  
480 0 $ENV{GIT_AUTHOR_DATE} = $bp_dist->date;
481  
482 0 ▶ if ( $author_obj->isa("CPANPLUS::Module::Author::Fake") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author_obj->isa("CPANPLUS::Module::Author::Fake")) 0 0 0
483 0 $author_obj = $mcpan->author_tree($bp_dist->cpanid);
484 }
485 } else {
486 0 say "Couldn't find upload date for $dist";
487  
488 0 ▶ if ( $author_obj->isa("CPANPLUS::Module::Author::Fake") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author_obj->isa("CPANPLUS::Module::Author::Fake")) 0 0 0
489 0 say "Couldn't find author for $dist";
490 }
491 }
492 }
493 }
494  
495 # create the commit object
496 0 ▶ $ENV{GIT_AUTHOR_NAME} = $author_obj->{name} unless $ENV{GIT_AUTHOR_NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"GIT_AUTHOR_NAME"} 0 0 0
497 0 ▶ $ENV{GIT_AUTHOR_EMAIL} = $author_obj->{email}[0] unless $ENV{GIT_AUTHOR_EMAIL};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"GIT_AUTHOR_EMAIL"} 0 0 0
498  
499 0 ▶ my @parents = ( grep { $_ } $last_commit, @{ $opts->{parent} || [] } );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{"parent"} or [] 0 0 0
500  
501 0 ▶ my $message = join ' ',
Branch: 1 branch
BranchTrueFalseTotal
$last_version ? : 0 0 0
502 ( $last_version ? "import" : "initial import of" ),
503 "$name $version from CPAN\n";
504 0 $message .= <<"END";
505  
506 git-cpan-module: $name
507 git-cpan-version: $version
508 0 git-cpan-authorid: @{[ $author_obj->{pauseid} ]}
509  
510 END
511  
512 my $commit = $repo->run(
513 { input => $message },
514 0 'commit-tree', $tree, map { ( -p => $_ ) } @parents );
515  
516 # finally, update the fake remote branch and create a tag for convenience
517  
518 0 print $repo->run('update-ref', '-m' => "import $dist", 'refs/remotes/cpan/master', $commit );
519  
520 0 print $repo->run( tag => $version, $commit );
521  
522 0 say "created tag '$version' ($commit)";
523 }
524  
525 }
526  
527 6 1;
528  
529 =pod
530  
531 =encoding UTF-8
532  
533 =head1 NAME
534  
535 Git::CPAN::Patch::Import
536  
537 =head1 VERSION
538  
539 version 2.3.2
540  
541 =head1 DESCRIPTION
542  
543 This is the guts of Git::CPAN::Patch::Import moved here to make it callable
544 as a function so git-backpan-init goes faster.
545  
546 =head1 NAME
547  
548 Git::CPAN::Patch::Import - The meat of git-cpan-import
549  
550 =head1 AUTHOR
551  
552 Yanick Champoux <yanick@cpan.org>
553  
554 =head1 COPYRIGHT AND LICENSE
555  
556 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009 by Yanick Champoux.
557  
558 This is free software; you can redistribute it and/or modify it under
559 the same terms as the Perl 5 programming language system itself.
560  
561 =cut
562  
563 __END__
564  
565  
566 1;
↑ Summary Patch/Command/Sources.pm »