lib/Dist/Zilla/Plugin/Git/Checkout.pm

filter: Statementstmt 98.5% 133 / 135 Branchbran 90.0% 45 / 50 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 1 totaltotal 94.5% 209 / 221 CC 29 SCAR 17.9
CC
29
Cov
95%
_checkout
14.2
_process_options
5.0
_update_push_url
4.0
CRAP 6.0
↑ Summary
1 package Dist::Zilla::Plugin::Git::Checkout;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.004';
8  
9 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 with 'Dist::Zilla::Role::BeforeRelease';
12  
13 5 ▶use Git::Background 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Git::Version::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use MooseX::Types::Moose qw(Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 has branch => (
22 is => 'ro',
23 isa => Str,
24 );
25  
26 has dir => (
27 is => 'ro',
28 isa => Str,
29 lazy => 1,
30 3 ▶ default => sub { path( shift->repo )->basename('.git') },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 5 );
32  
33 5 has push_url => (
34 is => 'ro',
35 isa => Str,
36 );
37  
38 5 has repo => (
39 is => 'ro',
40 isa => Str,
41 required => 1,
42 );
43  
44 5 has revision => (
45 is => 'ro',
46 isa => Str,
47 );
48  
49 5 has tag => (
50 is => 'ro',
51 isa => Str,
52 );
53  
54 5 has _is_dirty => (
55 is => 'rw',
56 isa => Bool,
57 default => 0,
58 );
59  
60 5 my $BRANCH = 'branch';
61 5 my $REVISION = 'revision';
62 5 my $TAG = 'tag';
63  
64 4 ▶sub before_release {
Subroutine before_release: called CC=3 SCAR=11.3
Pod undocumented
65 4 my ($self) = @_;
66  
67 4 ▶ return if !$self->_is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_dirty 3 1 4
68  
69 1 ▶ return if $self->zilla->chrome->prompt_yn(
Branch: 1 branch
BranchTrueFalseTotal
if $self->zilla->chrome->prompt_yn('Workspace ' . $self->dir . ' is dirty and was not updated. Release anyway?', {"default", 0}) 0 1 1
70 'Workspace ' . $self->dir . ' is dirty and was not updated. Release anyway?',
71 { default => 0 },
72 );
73  
74 1 $self->log_fatal('Aborting release');
75  
76 # should never be reached
77 0 die 'Aborting release'; ## no critic (ErrorHandling::RequireCarping)
78 }
79  
80 42 ▶around plugin_from_config => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 42 my ( $orig, $plugin_class, $name, $payload, $section ) = @_;
82  
83 42 my $instance = $plugin_class->$orig( $name, $payload, $section );
84  
85 41 $instance->_run;
86  
87 25 return $instance;
88 5 };
89  
90 13 ▶sub _checkout {
Subroutine _checkout: called CC=14 SCAR=26.5
91 13 my ( $self, $dir, $commitish ) = @_;
92  
93 # BRANCH (default: master)
94 # - check if branch matches, otherwise abort
95 # - git pull
96 # TAG
97 # - git fetch --tags -f
98 # - git checkout TAG
99 # REV
100 # - git fetch --tags -f
101 # - git checkout REVISION
102  
103 13 ▶ $self->log_fatal("Directory $dir exists but is not a Git repository") if !$dir->child('.git')->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->child(".git")->is_dir 1 12 13
104  
105 12 my $git = Git::Background->new($dir);
106 12 my $repo = $self->repo;
107  
108 # check that the workspace is from the correct repository
109 12 my $origin_f = $git->run( 'config', 'remote.origin.url' )->await;
110 12 ▶ $self->log_fatal("Directory $dir is not a Git repository for $repo") if $origin_f->is_failed || ( $origin_f->stdout )[0] ne $repo;
Branch: 1 branch
BranchTrueFalseTotal
if $origin_f->is_failed or ($origin_f->stdout)[0] ne $repo 2 10 12
Condition100%3/3
$origin_f->is_failed or ($origin_f->stdout)[0] ne $repo
l!l&&r!l&&!r
1110
MC/DC100%2/2
$origin_f->is_failed or ($origin_f->stdout)[0] ne $repo
$origin_f->is_failed ($origin_f->stdout)[0] ne $repo
Truth table100%3/3
A or B
A= $origin_f->is_failedB= ($origin_f->stdout)[0] ne $repo
ABresult
1X1
011
000
111  
112 10 my $commitish_id = $commitish->{id};
113 10 ▶ if ( $self->_commitish_is_branch($commitish) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commitish_is_branch($commitish)) 9 1 10
114 9 ▶ if ( !defined $commitish_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $commitish_id) 7 2 9
115  
116 # no branch is specified, find the default branch ...
117 7 my $origin_head_f = $git->run(qw(symbolic-ref -q --short refs/remotes/origin/HEAD))->await;
118 7 ▶ if ( $origin_head_f->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin_head_f->is_done) 7 0 7
119 7 ($commitish_id) = $origin_head_f->stdout;
120 7 ▶ if ( defined $commitish_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commitish_id) 7 0 7
121 7 $commitish_id =~ s{ \A origin / }{}xsm;
122 }
123 }
124  
125 # ... or fall back to master
126 7 ▶ if ( !defined $commitish_id || $commitish_id eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $commitish_id or $commitish_id eq "") 0 7 7
Condition33%1/3
not defined $commitish_id or $commitish_id eq ""
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $commitish_id or $commitish_id eq ""
not defined $commitish_id $commitish_id eq ""
Truth table33%1/3
not(A) or B
A= not defined $commitish_idB= $commitish_id eq ""
ABresult
1X1
011
000
127 0 $commitish_id = 'master';
128 }
129 }
130  
131 # check if we are on correct branch
132 9 my $head_f = $git->run(qw(symbolic-ref -q --short HEAD))->await;
133 9 ▶ $self->log_fatal("Directory $dir is not on branch $commitish_id") if $head_f->is_failed || ( $head_f->stdout )[0] ne $commitish_id;
Branch: 1 branch
BranchTrueFalseTotal
if $head_f->is_failed or ($head_f->stdout)[0] ne $commitish_id 3 6 9
Condition100%3/3
$head_f->is_failed or ($head_f->stdout)[0] ne $commitish_id
l!l&&r!l&&!r
126
MC/DC100%2/2
$head_f->is_failed or ($head_f->stdout)[0] ne $commitish_id
$head_f->is_failed ($head_f->stdout)[0] ne $commitish_id
Truth table100%3/3
A or B
A= $head_f->is_failedB= ($head_f->stdout)[0] ne $commitish_id
ABresult
1X1
011
000
134 }
135  
136 # check if the workspace is dirty - skip updates for a dirty workspace
137 7 ▶ if ( $git->run( 'status', '--porcelain' )->stdout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($git->run("status", "--porcelain")->stdout) 2 5 7
138 2 $self->log( colored( "Git workspace $dir is dirty - skipping checkout", 'yellow' ) );
139 2 $self->_is_dirty(1);
140 2 return;
141 }
142  
143 # update the workspace
144 5 ▶ if ( $self->_commitish_is_branch($commitish) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commitish_is_branch($commitish)) { } 4 1 5
145 4 $self->log("Pulling $repo in $dir");
146 4 $git->run( 'pull', '--ff-only' )->get;
147 }
148 else {
149 1 $self->log("Fetching $repo in $dir");
150 1 $git->run( 'fetch', '--tags', '-f' )->get;
151  
152 1 $self->log("Checking out $commitish->{type} $commitish_id in $dir");
153 1 $git->run( 'checkout', $commitish_id )->get;
154 }
155  
156 5 return;
157 }
158  
159 18 ▶sub _clone {
Subroutine _clone: called CC=3 SCAR=11.0
160 18 my ( $self, $dir, $commitish ) = @_;
161  
162 # BRANCH (default: master)
163 # - clone --branch branch
164 # TAG
165 # - clone
166 # - checkout tag
167 # REV
168 # - clone
169 # - checkout revision
170  
171 18 my $repo = $self->repo;
172  
173 # clone the repository and checkout the correct commitish
174 18 ▶ if ( $self->_commitish_is_branch($commitish) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commitish_is_branch($commitish)) { } 15 3 18
175 15 ▶ if ( defined $commitish->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commitish->{'id'}) { } 2 13 15
176 2 $self->log("Cloning $repo into $dir (branch $commitish->{id})");
177 2 Git::Background->run( 'clone', '--branch', $commitish->{id}, $repo, $dir->stringify )->get;
178 }
179 else {
180 13 $self->log("Cloning $repo into $dir");
181 13 Git::Background->run( 'clone', $repo, $dir->stringify )->get;
182 }
183 }
184 else {
185 3 $self->log("Cloning $repo into $dir");
186 3 Git::Background->run( 'clone', $repo, $dir->stringify )->get;
187  
188 3 $self->log("Checking out $commitish->{type} $commitish->{id} in $dir");
189 3 Git::Background->run( 'checkout', $commitish->{id}, { dir => $dir } )->get;
190 }
191  
192 18 return;
193 }
194  
195 37 ▶sub _commitish_is_branch {
Subroutine _commitish_is_branch: called CC=2 SCAR=6.9
196 37 my ( $self, $commitish ) = @_;
197  
198 37 ▶ return 1 if $commitish->{type} eq $BRANCH;
Branch: 1 branch
BranchTrueFalseTotal
if $commitish->{"type"} eq $BRANCH 30 7 37
199 7 return;
200 }
201  
202 41 ▶sub _ensure_adequate_git {
Subroutine _ensure_adequate_git: called CC=3 SCAR=11.0
203 41 my ($self) = @_;
204  
205 # check that an adequate Git is available
206 41 my $git_version = Git::Background->version;
207 41 ▶ $self->log_fatal(q{No 'git' in PATH}) if !defined $git_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_version 1 40 41
208  
209 # Find which tag contains a commit: git tag --contains revision
210 #
211 # https://stackoverflow.com/a/6978402/8173111
212 # git status --porcelain, commit 6f15787, September 2009, git 1.7.0
213 # https://github.com/git/git/commit/6f15787181a163e158c6fee1d79085b97692ac2f
214 #
215 # git symbolic-ref --short, commit b8b5290, March 2012, git 1.7.10
216 # https://github.com/git/git/commit/b8b52907e386d064fb0303c08215d7b117d50ee9
217 40 ▶ $self->log_fatal(q{Your 'git' is too old. At least Git 1.7.10 is needed.}) if !Git::Version::Compare::ge_git( $git_version, '1.7.10' );
Branch: 1 branch
BranchTrueFalseTotal
unless Git::Version::Compare::ge_git($git_version, "1.7.10") 1 39 40
218  
219 39 return;
220 }
221  
222 39 ▶sub _process_options {
Subroutine _process_options: called CC=5 SCAR=16.1
223 39 my ($self) = @_;
224  
225 # verify plugin options
226 39 my $branch = $self->branch;
227 39 my $rev = $self->revision;
228 39 my $tag = $self->tag;
229  
230 39 my $num_options = grep { defined } ( $branch, $rev, $tag );
231 39 ▶ $self->log_fatal(q{Only one of branch, revision, or tag can be specified}) if $num_options > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_options > 1 4 35 39
232  
233 35 ▶ return { type => $BRANCH, id => $branch } if defined $branch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $branch 5 30 35
234 30 ▶ return { type => $REVISION, id => $rev } if defined $rev;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rev 1 29 30
235 29 ▶ return { type => $TAG, id => $tag } if defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 5 24 29
236 24 return { type => $BRANCH, id => undef };
237 }
238  
239 41 ▶sub _run {
Subroutine _run: called CC=2 SCAR=6.9
240 41 my ($self) = @_;
241  
242 # check an adequate git is installed
243 41 $self->_ensure_adequate_git;
244  
245 # verify the options given to this plugin
246 39 my $commitish = $self->_process_options;
247  
248 # clone or update the workspace
249 31 my $dir = path( $self->zilla->root )->child( path( $self->dir ) )->absolute;
250 31 ▶ if ( $dir->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->is_dir) { } 13 18 31
251 13 $self->_checkout( $dir, $commitish );
252 }
253 else {
254 18 $self->_clone( $dir, $commitish );
255 }
256  
257 # update the push URL
258 25 $self->_update_push_url($dir);
259  
260 25 return;
261 }
262  
263 25 ▶sub _update_push_url {
Subroutine _update_push_url: called CC=4 SCAR=13.9
264 25 my ( $self, $dir ) = @_;
265  
266 25 my $git = Git::Background->new($dir);
267  
268 # configure or remove the push url
269 25 ▶ if ( defined $self->push_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->push_url) { } 3 22 25
270 3 $git->run( 'remote', 'set-url', '--push', 'origin', $self->push_url )->get;
271 }
272 else {
273 22 my $push_url_f = $git->run( 'config', 'remote.origin.pushurl' )->await;
274 22 ▶ if ( $push_url_f->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($push_url_f->is_done) 1 21 22
275 1 my ($push_url) = $push_url_f->stdout;
276  
277 1 ▶ if ( defined $push_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $push_url) 1 0 1
278 1 $git->run( 'remote', 'set-url', '--delete', '--push', 'origin', $push_url )->get;
279 }
280 }
281 }
282  
283 25 return;
284 }
285  
286 5 __PACKAGE__->meta->make_immutable;
287  
288 5 1;
289  
290 __END__
↑ Summary