| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Alien::Build::Plugin::Fetch::Git; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 227357 | use strict; | 
|  | 3 |  |  |  |  | 13 |  | 
|  | 3 |  |  |  |  | 76 |  | 
| 4 | 3 |  |  | 3 |  | 16 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 54 |  | 
| 5 | 3 |  |  | 3 |  | 41 | use 5.008001; | 
|  | 3 |  |  |  |  | 9 |  | 
| 6 | 3 |  |  | 3 |  | 1047 | use Alien::Util qw( version_cmp ); | 
|  | 3 |  |  |  |  | 896 |  | 
|  | 3 |  |  |  |  | 144 |  | 
| 7 | 3 |  |  | 3 |  | 369 | use Alien::Build::Plugin; | 
|  | 3 |  |  |  |  | 9698 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 8 | 3 |  |  | 3 |  | 1541 | use URI; | 
|  | 3 |  |  |  |  | 11932 |  | 
|  | 3 |  |  |  |  | 79 |  | 
| 9 | 3 |  |  | 3 |  | 1103 | use URI::file; | 
|  | 3 |  |  |  |  | 13042 |  | 
|  | 3 |  |  |  |  | 92 |  | 
| 10 | 3 |  |  | 3 |  | 1056 | use URI::git; | 
|  | 3 |  |  |  |  | 9938 |  | 
|  | 3 |  |  |  |  | 101 |  | 
| 11 | 3 |  |  | 3 |  | 701 | use Path::Tiny qw( path ); | 
|  | 3 |  |  |  |  | 10172 |  | 
|  | 3 |  |  |  |  | 138 |  | 
| 12 | 3 |  |  | 3 |  | 644 | use File::Temp qw( tempdir ); | 
|  | 3 |  |  |  |  | 14238 |  | 
|  | 3 |  |  |  |  | 131 |  | 
| 13 | 3 |  |  | 3 |  | 1092 | use File::chdir; | 
|  | 3 |  |  |  |  | 7866 |  | 
|  | 3 |  |  |  |  | 298 |  | 
| 14 | 3 |  |  | 3 |  | 477 | use Capture::Tiny qw( capture_merged capture_stdout ); | 
|  | 3 |  |  |  |  | 3596 |  | 
|  | 3 |  |  |  |  | 2450 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # ABSTRACT: Alien::Build plugin to fetch from git | 
| 17 |  |  |  |  |  |  | our $VERSION = '0.09'; # VERSION | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub init | 
| 21 |  |  |  |  |  |  | { | 
| 22 | 1 |  |  | 1 | 1 | 37892 | my($self, $meta) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 1 |  |  |  |  | 5 | $meta->add_requires('share' => 'Alien::git' => 0); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | $meta->register_hook( | 
| 27 |  |  |  |  |  |  | fetch => sub { | 
| 28 | 2 |  |  | 2 |  | 63932 | my($build, $url) = @_; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 2 |  | 33 |  |  | 16 | $url ||= $build->meta_prop->{start_url}; | 
| 31 | 2 | 50 |  |  |  | 12 | die "no default URL provided!" unless defined $url; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 2 | 50 | 0 |  |  | 22 | if($url =~ /^[a-zA-Z0-9]+:/ && !( $url =~ /^[A-Z]:/i && $^O eq 'MSWin32' )) | 
|  |  |  | 33 |  |  |  |  | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 0 |  |  |  |  | 0 | $url = URI->new($url); | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | else | 
| 38 |  |  |  |  |  |  | { | 
| 39 | 2 |  |  |  |  | 47 | my $tmp = URI::file->new_abs("."); | 
| 40 | 2 | 50 |  |  |  | 18058 | if($^O eq 'MSWin32') | 
| 41 |  |  |  |  |  |  | { | 
| 42 | 0 |  |  |  |  | 0 | $tmp->host(''); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | else | 
| 45 |  |  |  |  |  |  | { | 
| 46 | 2 |  |  |  |  | 30 | $tmp->host('localhost'); | 
| 47 |  |  |  |  |  |  | } | 
| 48 | 2 | 100 |  |  |  | 149 | if($url =~ s/#(.*)$//) | 
| 49 |  |  |  |  |  |  | { | 
| 50 | 1 |  |  |  |  | 28 | $tmp->fragment($1); | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 2 |  |  |  |  | 78 | $tmp->path($url); | 
| 53 | 2 |  |  |  |  | 60 | $url = $tmp; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 2 |  |  |  |  | 67 | my $exe = Alien::git->exe; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 2 | 100 |  |  |  | 433 | if(defined $url->fragment) | 
| 59 |  |  |  |  |  |  | { | 
| 60 | 1 |  |  |  |  | 47 | local $CWD = tempdir( CLEANUP => 1 ); | 
| 61 | 1 |  |  |  |  | 679 | my($tag) = $url->fragment; | 
| 62 | 1 |  |  |  |  | 21 | $url->fragment(undef); | 
| 63 | 1 | 50 |  |  |  | 26 | if(can_branch_clone()) | 
| 64 |  |  |  |  |  |  | { | 
| 65 | 1 |  |  |  |  | 20 | $build->system('%{git}', 'clone', '--depth' => 1, '--branch', "$tag", "$url"); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | else | 
| 68 |  |  |  |  |  |  | { | 
| 69 | 0 |  |  |  |  | 0 | $build->system('%{git}', 'clone', "$url"); | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 1 | 50 |  |  |  | 70568 | die "command failed" if $?; | 
| 72 | 1 |  |  |  |  | 34 | my($dir) = path(".")->absolute->children; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 1 | 50 |  |  |  | 610 | if(can_branch_clone()) | 
| 75 |  |  |  |  |  |  | { | 
| 76 |  |  |  |  |  |  | # do nothing | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | else | 
| 79 |  |  |  |  |  |  | { | 
| 80 |  |  |  |  |  |  | # mildly prefer the -C version as it will handle spaces in $dir. | 
| 81 | 0 | 0 |  |  |  | 0 | if(can_minus_c()) | 
| 82 |  |  |  |  |  |  | { | 
| 83 | 0 |  |  |  |  | 0 | $build->system('%{git}', -C => "$dir", 'checkout', $tag); | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | else | 
| 86 |  |  |  |  |  |  | { | 
| 87 | 0 |  |  |  |  | 0 | $build->system("cd $dir ; %{git} checkout $tag"); | 
| 88 |  |  |  |  |  |  | } | 
| 89 | 0 | 0 |  |  |  | 0 | die "command failed" if $?; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | return { | 
| 92 | 1 |  |  |  |  | 22 | type     => 'file', | 
| 93 |  |  |  |  |  |  | filename => $dir->basename, | 
| 94 |  |  |  |  |  |  | path     => $dir->stringify, | 
| 95 |  |  |  |  |  |  | }; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | else | 
| 98 |  |  |  |  |  |  | { | 
| 99 | 1 |  |  |  |  | 26 | $build->log("fetching tags from $url"); | 
| 100 |  |  |  |  |  |  | my($output, $error) = capture_merged { | 
| 101 | 1 |  |  |  |  | 1252 | $build->system('%{git}', 'ls-remote', '--tags', "$url"); | 
| 102 | 1 |  |  |  |  | 14687 | $?; | 
| 103 | 1 |  |  |  |  | 263 | }; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 1 | 50 |  |  |  | 1820 | if($error) | 
| 106 |  |  |  |  |  |  | { | 
| 107 | 0 |  |  |  |  | 0 | print $output; | 
| 108 | 0 |  |  |  |  | 0 | die "command failed"; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | my @tags = sort | 
| 112 | 4 |  |  |  |  | 27 | grep { defined $_ } | 
| 113 | 1 | 100 |  |  |  | 56 | map { m{refs/tags/(.*)$} ? $1 : undef } | 
|  | 4 |  |  |  |  | 52 |  | 
| 114 |  |  |  |  |  |  | split /\n\r?/, $output; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | return { | 
| 117 |  |  |  |  |  |  | type => 'list', | 
| 118 |  |  |  |  |  |  | list => [ | 
| 119 |  |  |  |  |  |  | map { | 
| 120 | 1 |  |  |  |  | 20 | my $tag = $_; | 
|  | 3 |  |  |  |  | 14 |  | 
| 121 | 3 |  |  |  |  | 30 | my $url = $url->clone; | 
| 122 | 3 |  |  |  |  | 69 | $url->fragment($tag); | 
| 123 | 3 |  |  |  |  | 106 | my %h = ( | 
| 124 |  |  |  |  |  |  | filename => $tag, | 
| 125 |  |  |  |  |  |  | url      => "$url", | 
| 126 |  |  |  |  |  |  | ); | 
| 127 | 3 |  |  |  |  | 90 | \%h; | 
| 128 |  |  |  |  |  |  | } @tags | 
| 129 |  |  |  |  |  |  | ], | 
| 130 |  |  |  |  |  |  | }; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | }, | 
| 133 | 1 |  |  |  |  | 21 | ); | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | my $can_minus_c; | 
| 138 |  |  |  |  |  |  | sub can_minus_c | 
| 139 |  |  |  |  |  |  | { | 
| 140 | 0 | 0 |  | 0 | 0 | 0 | unless(defined $can_minus_c) | 
| 141 |  |  |  |  |  |  | { | 
| 142 | 0 |  |  |  |  | 0 | require Alien::git; | 
| 143 | 0 |  |  |  |  | 0 | my $tmp = path(tempdir( CLEANUP => 1)); | 
| 144 |  |  |  |  |  |  | my(undef, $ret) = capture_merged { | 
| 145 | 0 |  |  | 0 |  | 0 | system(Alien::git->exe, -C => $tmp, 'init'); | 
| 146 | 0 |  |  |  |  | 0 | $?; | 
| 147 | 0 |  |  |  |  | 0 | }; | 
| 148 | 0 |  | 0 |  |  | 0 | $can_minus_c = !! $? == 0 && -d $tmp->child('.git'); | 
| 149 | 0 |  |  |  |  | 0 | $tmp->remove_tree; | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 0 |  |  |  |  | 0 | $can_minus_c; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | my $can_branch_clone; | 
| 156 |  |  |  |  |  |  | sub can_branch_clone | 
| 157 |  |  |  |  |  |  | { | 
| 158 | 2 | 100 |  | 2 | 0 | 16 | unless(defined $can_branch_clone) | 
| 159 |  |  |  |  |  |  | { | 
| 160 | 1 |  |  |  |  | 11 | require Alien::git; | 
| 161 | 1 | 50 |  |  |  | 7 | if(version_cmp(Alien::git->version, "1.8.3.5") >= 0) | 
| 162 |  |  |  |  |  |  | { | 
| 163 | 1 |  |  |  |  | 77 | $can_branch_clone = 1; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  | else | 
| 166 |  |  |  |  |  |  | { | 
| 167 | 0 |  |  |  |  | 0 | $can_branch_clone = 0; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 2 |  |  |  |  | 25 | $can_branch_clone; | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | 1; | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | __END__ |