File Coverage

blib/lib/Dist/Zilla/Plugin/Git/Checkout.pm
Criterion Covered Total %
statement 56 57 98.2
branch 15 16 93.7
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 82 85 96.4


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Git::Checkout;
2              
3 2     2   2661487 use 5.006;
  2         7  
4 2     2   11 use strict;
  2         3  
  2         41  
5 2     2   7 use warnings;
  2         4  
  2         97  
6              
7             our $VERSION = '0.002';
8              
9 2     2   788 use Moose;
  2         402924  
  2         13  
10              
11             with 'Dist::Zilla::Role::BeforeRelease';
12              
13 2     2   13796 use Git::Wrapper;
  2         30165  
  2         75  
14 2     2   432 use MooseX::Types::Moose qw(Bool Str);
  2         55098  
  2         29  
15 2     2   10037 use Path::Tiny;
  2         9505  
  2         125  
16 2     2   1431 use Term::ANSIColor qw(colored);
  2         14955  
  2         1603  
17              
18 2     2   20 use namespace::autoclean;
  2         4  
  2         22  
19              
20             has checkout => (
21             is => 'ro',
22             isa => Str,
23             lazy => 1,
24             default => 'master',
25             );
26              
27             has dir => (
28             is => 'ro',
29             isa => Str,
30             lazy => 1,
31             default => sub { path( shift->repo )->basename('.git') },
32             );
33              
34             has push_url => (
35             is => 'ro',
36             isa => Str,
37             );
38              
39             has repo => (
40             is => 'ro',
41             isa => Str,
42             required => 1,
43             );
44              
45             has _is_dirty => (
46             is => 'rw',
47             isa => Bool,
48             default => 0,
49             );
50              
51             sub before_release {
52 4     4 0 274728 my ($self) = @_;
53              
54 4 100       175 return if !$self->_is_dirty;
55              
56 1 50       59 return if $self->zilla->chrome->prompt_yn(
57             'Workspace ' . $self->dir . ' is dirty and was not updated. Release anyway?',
58             { default => 0 },
59             );
60              
61 1         1482 $self->log_fatal('Aborting release');
62              
63 0         0 return;
64             }
65              
66             sub _checkout {
67 18     18   99 my ($self) = @_;
68              
69 18         736 my $dir = path( $self->zilla->root )->child( path( $self->dir ) )->absolute;
70 18         3933 my $repo = $self->repo;
71 18         572 my $checkout = $self->checkout;
72              
73 18         187 my $git = Git::Wrapper->new( $dir->stringify );
74              
75 18 100       979 if ( -d $dir ) {
76 7 100       411 $self->log_fatal("Directory $dir exists but is not a Git repository") if !-d $dir->child('.git');
77              
78 6         495 my ($origin) = $git->config('remote.origin.url');
79 6 100       54692 $self->log_fatal("Directory $dir is not a Git repository for $repo") if $origin ne $repo;
80              
81 5 100       106 if ( $git->status->is_dirty ) {
82 2         34159 $self->log( colored( "Git workspace $dir is dirty - skipping checkout", 'yellow' ) );
83 2         3040 $self->_is_dirty(1);
84 2         154 return;
85             }
86              
87 3         54391 $self->log("Fetching $repo in $dir");
88 3         3927 $git->fetch;
89             }
90             else {
91 11         625 $self->log("Cloning $repo into $dir");
92 11         9169 $git->clone( $repo, $dir->stringify );
93             }
94              
95             # Configure or remove the push url
96 14 100       499358 if ( defined $self->push_url ) {
97 3         169 $git->remote( 'set-url', '--push', 'origin', $self->push_url );
98             }
99             else {
100 11         73 my ($push_url) = eval { $git->config('remote.origin.pushurl'); };
  11         285  
101 11 100       115450 if ( defined $push_url ) {
102 1         38 $git->remote( 'set-url', '--delete', '--push', 'origin', $push_url );
103             }
104             }
105              
106             # We don't know what the default branch is. It's easier to just check it out again.
107 14         42500 $self->log("Checking out $checkout in $dir");
108 14         14021 $git->checkout($checkout);
109              
110 14         169530 return;
111             }
112              
113             around plugin_from_config => sub {
114             my ( $orig, $plugin_class, $name, $payload, $section ) = @_;
115              
116             my $instance = $plugin_class->$orig( $name, $payload, $section );
117              
118             $instance->_checkout;
119              
120             return $instance;
121             };
122              
123             __PACKAGE__->meta->make_immutable;
124              
125             1;
126              
127             __END__
128              
129             =pod
130              
131             =encoding UTF-8
132              
133             =head1 NAME
134              
135             Dist::Zilla::Plugin::Git::Checkout - clone and checkout a Git repository
136              
137             =head1 VERSION
138              
139             Version 0.002
140              
141             =head1 SYNOPSIS
142              
143             # in dist.ini:
144             [Git::Checkout]
145             repo = https://github.com/skirmess/dzil-inc.git
146              
147             =head1 DESCRIPTION
148              
149             This plugin clones, or if it is already cloned, fetches and updates a Git
150             repository.
151              
152             The plugin runs during the initialization phase, which is the same for
153             bundles and plugins. You can check out a Git repository and load bundles
154             or plugins from this repository.
155              
156             # in dist.ini
157             [Git::Checkout]
158             repo = https://github.com/skirmess/dzil-inc.git
159              
160             ; add the lib directory inside the checked out Git repository to @INC
161             [lib]
162             lib = dzil-inc/lib
163              
164             ; this bundle is run from inside the checked out Git repositories lib
165             ; directory
166             [@BundleFromRepository]
167              
168             =head1 USAGE
169              
170             =head2 checkout
171              
172             Specifies what to check out. This can be a branch, a tag or a revision.
173             Defaults to C<master>.
174              
175             =head2 dir
176              
177             The repositories workspace is checked out into this directory. This defaults
178             to the basename of the repo without the C<.git> suffix.
179              
180             =head2 push_url
181              
182             Allows you to specify a different push url for the repositories origin. One
183             possible scenario would be if you would like to clone via http but push via
184             ssh. This is optional.
185              
186             =head2 repo
187              
188             Specifies the address of the repository to clone. This is required.
189              
190             =head1 SUPPORT
191              
192             =head2 Bugs / Feature Requests
193              
194             Please report any bugs or feature requests through the issue tracker
195             at L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout/issues>.
196             You will be notified automatically of any progress on your issue.
197              
198             =head2 Source Code
199              
200             This is open source software. The code repository is available for
201             public review and contribution under the terms of the license.
202              
203             L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout>
204              
205             git clone https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout.git
206              
207             =head1 AUTHOR
208              
209             Sven Kirmess <sven.kirmess@kzone.ch>
210              
211             =head1 COPYRIGHT AND LICENSE
212              
213             This software is Copyright (c) 2020 by Sven Kirmess.
214              
215             This is free software, licensed under:
216              
217             The (two-clause) FreeBSD License
218              
219             =head1 SEE ALSO
220              
221             L<Dist::Zilla>, L<lib>
222              
223             =cut
224              
225             # vim: ts=4 sts=4 sw=4 et: syntax=perl