| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Alien::Build::Plugin::Fetch::Git; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 258803 | use strict; | 
|  | 3 |  |  |  |  | 15 |  | 
|  | 3 |  |  |  |  | 91 |  | 
| 4 | 3 |  |  | 3 |  | 14 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 67 |  | 
| 5 | 3 |  |  | 3 |  | 53 | use 5.008001; | 
|  | 3 |  |  |  |  | 18 |  | 
| 6 | 3 |  |  | 3 |  | 476 | use Alien::Build::Plugin; | 
|  | 3 |  |  |  |  | 2498 |  | 
|  | 3 |  |  |  |  | 40 |  | 
| 7 | 3 |  |  | 3 |  | 1836 | use URI; | 
|  | 3 |  |  |  |  | 13567 |  | 
|  | 3 |  |  |  |  | 100 |  | 
| 8 | 3 |  |  | 3 |  | 1353 | use URI::file; | 
|  | 3 |  |  |  |  | 15116 |  | 
|  | 3 |  |  |  |  | 102 |  | 
| 9 | 3 |  |  | 3 |  | 1195 | use URI::git; | 
|  | 3 |  |  |  |  | 11231 |  | 
|  | 3 |  |  |  |  | 111 |  | 
| 10 | 3 |  |  | 3 |  | 768 | use Path::Tiny qw( path ); | 
|  | 3 |  |  |  |  | 10999 |  | 
|  | 3 |  |  |  |  | 157 |  | 
| 11 | 3 |  |  | 3 |  | 717 | use File::Temp qw( tempdir ); | 
|  | 3 |  |  |  |  | 16184 |  | 
|  | 3 |  |  |  |  | 137 |  | 
| 12 | 3 |  |  | 3 |  | 1218 | use File::chdir; | 
|  | 3 |  |  |  |  | 9197 |  | 
|  | 3 |  |  |  |  | 312 |  | 
| 13 | 3 |  |  | 3 |  | 517 | use Capture::Tiny qw( capture_merged ); | 
|  | 3 |  |  |  |  | 6335 |  | 
|  | 3 |  |  |  |  | 2375 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # ABSTRACT: Alien::Build plugin to fetch from git | 
| 16 |  |  |  |  |  |  | our $VERSION = '0.08'; # VERSION | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub init | 
| 20 |  |  |  |  |  |  | { | 
| 21 | 1 |  |  | 1 | 1 | 40042 | my($self, $meta) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 1 |  |  |  |  | 15 | $meta->add_requires('share' => 'Alien::git' => 0); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | $meta->register_hook( | 
| 26 |  |  |  |  |  |  | fetch => sub { | 
| 27 | 2 |  |  | 2 |  | 69094 | my($build, $url) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 2 |  | 33 |  |  | 12 | $url ||= $build->meta_prop->{start_url}; | 
| 30 | 2 | 50 |  |  |  | 15 | die "no default URL provided!" unless defined $url; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 2 | 50 | 0 |  |  | 26 | if($url =~ /^[a-zA-Z0-9]+:/ && !( $url =~ /^[A-Z]:/i && $^O eq 'MSWin32' )) | 
|  |  |  | 33 |  |  |  |  | 
| 33 |  |  |  |  |  |  | { | 
| 34 | 0 |  |  |  |  | 0 | $url = URI->new($url); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | else | 
| 37 |  |  |  |  |  |  | { | 
| 38 | 2 |  |  |  |  | 60 | my $tmp = URI::file->new_abs("."); | 
| 39 | 2 | 50 |  |  |  | 11620 | if($^O eq 'MSWin32') | 
| 40 |  |  |  |  |  |  | { | 
| 41 | 0 |  |  |  |  | 0 | $tmp->host(''); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | else | 
| 44 |  |  |  |  |  |  | { | 
| 45 | 2 |  |  |  |  | 32 | $tmp->host('localhost'); | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 2 | 100 |  |  |  | 152 | if($url =~ s/#(.*)$//) | 
| 48 |  |  |  |  |  |  | { | 
| 49 | 1 |  |  |  |  | 25 | $tmp->fragment($1); | 
| 50 |  |  |  |  |  |  | } | 
| 51 | 2 |  |  |  |  | 63 | $tmp->path($url); | 
| 52 | 2 |  |  |  |  | 70 | $url = $tmp; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 2 |  |  |  |  | 62 | my $exe = Alien::git->exe; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 2 | 100 |  |  |  | 495 | if(defined $url->fragment) | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 1 |  |  |  |  | 37 | local $CWD = tempdir( CLEANUP => 1 ); | 
| 60 | 1 |  |  |  |  | 714 | my($tag) = $url->fragment; | 
| 61 | 1 |  |  |  |  | 29 | $url->fragment(undef); | 
| 62 | 1 |  |  |  |  | 21 | $build->system('%{git}', 'clone', "$url"); | 
| 63 | 1 | 50 |  |  |  | 36561 | die "command failed" if $?; | 
| 64 | 1 |  |  |  |  | 35 | my($dir) = path(".")->absolute->children; | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | # mildly prefer the -C version as it will handle spaces in $dir. | 
| 67 | 1 | 50 |  |  |  | 705 | if(can_minus_c()) | 
| 68 |  |  |  |  |  |  | { | 
| 69 | 1 |  |  |  |  | 12 | $build->system('%{git}', -C => "$dir", 'checkout', $tag); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | else | 
| 72 |  |  |  |  |  |  | { | 
| 73 | 0 |  |  |  |  | 0 | $build->system("cd $dir ; %{git} checkout $tag"); | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 1 | 50 |  |  |  | 6538 | die "command failed" if $?; | 
| 76 |  |  |  |  |  |  | return { | 
| 77 | 1 |  |  |  |  | 36 | type     => 'file', | 
| 78 |  |  |  |  |  |  | filename => $dir->basename, | 
| 79 |  |  |  |  |  |  | path     => $dir->stringify, | 
| 80 |  |  |  |  |  |  | }; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | else | 
| 83 |  |  |  |  |  |  | { | 
| 84 | 1 |  |  |  |  | 26 | $build->log("fetching tags from $url"); | 
| 85 |  |  |  |  |  |  | my($output, $error) = capture_merged { | 
| 86 | 1 |  |  |  |  | 1462 | $build->system('%{git}', 'ls-remote', '--tags', "$url"); | 
| 87 | 1 |  |  |  |  | 8780 | $?; | 
| 88 | 1 |  |  |  |  | 175 | }; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 1 | 50 |  |  |  | 1349 | if($error) | 
| 91 |  |  |  |  |  |  | { | 
| 92 | 0 |  |  |  |  | 0 | print $output; | 
| 93 | 0 |  |  |  |  | 0 | die "command failed"; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my @tags = sort | 
| 97 | 4 |  |  |  |  | 24 | grep { defined $_ } | 
| 98 | 1 | 100 |  |  |  | 32 | map { m{refs/tags/(.*)$} ? $1 : undef } | 
|  | 4 |  |  |  |  | 28 |  | 
| 99 |  |  |  |  |  |  | split /\n\r?/, $output; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | return { | 
| 102 |  |  |  |  |  |  | type => 'list', | 
| 103 |  |  |  |  |  |  | list => [ | 
| 104 |  |  |  |  |  |  | map { | 
| 105 | 1 |  |  |  |  | 14 | my $tag = $_; | 
|  | 3 |  |  |  |  | 10 |  | 
| 106 | 3 |  |  |  |  | 39 | my $url = $url->clone; | 
| 107 | 3 |  |  |  |  | 58 | $url->fragment($tag); | 
| 108 | 3 |  |  |  |  | 73 | my %h = ( | 
| 109 |  |  |  |  |  |  | filename => $tag, | 
| 110 |  |  |  |  |  |  | url      => "$url", | 
| 111 |  |  |  |  |  |  | ); | 
| 112 | 3 |  |  |  |  | 58 | \%h; | 
| 113 |  |  |  |  |  |  | } @tags | 
| 114 |  |  |  |  |  |  | ], | 
| 115 |  |  |  |  |  |  | }; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | }, | 
| 118 | 1 |  |  |  |  | 22 | ); | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | my $can_minus_c; | 
| 123 |  |  |  |  |  |  | sub can_minus_c | 
| 124 |  |  |  |  |  |  | { | 
| 125 | 1 | 50 |  | 1 | 0 | 25 | unless(defined $can_minus_c) | 
| 126 |  |  |  |  |  |  | { | 
| 127 | 1 |  |  |  |  | 23 | require Alien::git; | 
| 128 | 1 |  |  |  |  | 27 | my $tmp = path(tempdir( CLEANUP => 1)); | 
| 129 |  |  |  |  |  |  | my(undef, $ret) = capture_merged { | 
| 130 | 1 |  |  | 1 |  | 1471 | system(Alien::git->exe, -C => $tmp, 'init'); | 
| 131 | 1 |  |  |  |  | 8730 | $?; | 
| 132 | 1 |  |  |  |  | 853 | }; | 
| 133 | 1 |  | 33 |  |  | 1289 | $can_minus_c = !! $? == 0 && -d $tmp->child('.git'); | 
| 134 | 1 |  |  |  |  | 166 | $tmp->remove_tree; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 1 |  |  |  |  | 2880 | $can_minus_c; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | 1; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | __END__ |