Patch/Import.pm

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