File Coverage

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


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Git::Checkout;
2              
3 2     2   3177571 use 5.006;
  2         8  
4 2     2   11 use strict;
  2         4  
  2         61  
5 2     2   11 use warnings;
  2         4  
  2         95  
6              
7             our $VERSION = '0.003';
8              
9 2     2   1875 use Moose;
  2         496958  
  2         12  
10              
11             with 'Dist::Zilla::Role::BeforeRelease';
12              
13 2     2   16436 use Git::Wrapper;
  2         34321  
  2         122  
14 2     2   486 use MooseX::Types::Moose qw(Bool Str);
  2         71308  
  2         26  
15 2     2   12695 use Path::Tiny;
  2         12335  
  2         157  
16 2     2   1989 use Term::ANSIColor qw(colored);
  2         19231  
  2         1782  
17              
18 2     2   19 use namespace::autoclean;
  2         5  
  2         21  
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 297504 my ($self) = @_;
53              
54 4 100       274 return if !$self->_is_dirty;
55              
56 1 50       69 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         1806 $self->log_fatal('Aborting release');
62              
63 0         0 return;
64             }
65              
66             sub _checkout {
67 18     18   63 my ($self) = @_;
68              
69 18         835 my $dir = path( $self->zilla->root )->child( path( $self->dir ) )->absolute;
70 18         4256 my $repo = $self->repo;
71 18         701 my $checkout = $self->checkout;
72              
73 18         222 my $git = Git::Wrapper->new( $dir->stringify );
74              
75 18 100       1392 if ( -d $dir ) {
76 7 100       354 $self->log_fatal("Directory $dir exists but is not a Git repository") if !-d $dir->child('.git');
77              
78 6         696 my ($origin) = $git->config('remote.origin.url');
79 6 100       57184 $self->log_fatal("Directory $dir is not a Git repository for $repo") if $origin ne $repo;
80              
81 5 100       153 if ( $git->status->is_dirty ) {
82 2         37931 $self->log( colored( "Git workspace $dir is dirty - skipping checkout", 'yellow' ) );
83 2         3274 $self->_is_dirty(1);
84 2         55 return;
85             }
86              
87 3         58361 $self->log("Fetching $repo in $dir");
88 3         4420 $git->fetch;
89             }
90             else {
91 11         572 $self->log("Cloning $repo into $dir");
92 11         9997 $git->clone( $repo, $dir->stringify );
93             }
94              
95             # Configure or remove the push url
96 14 100       391936 if ( defined $self->push_url ) {
97 3         169 $git->remote( 'set-url', '--push', 'origin', $self->push_url );
98             }
99             else {
100 11         69 my ($push_url) = eval { $git->config('remote.origin.pushurl'); };
  11         289  
101 11 100       105284 if ( defined $push_url ) {
102 1         52 $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         40351 $self->log("Checking out $checkout in $dir");
108 14         14523 $git->checkout($checkout);
109              
110 14         170948 $git->pull('--ff-only');
111              
112 14         493502 return;
113             }
114              
115             around plugin_from_config => sub {
116             my ( $orig, $plugin_class, $name, $payload, $section ) = @_;
117              
118             my $instance = $plugin_class->$orig( $name, $payload, $section );
119              
120             $instance->_checkout;
121              
122             return $instance;
123             };
124              
125             __PACKAGE__->meta->make_immutable;
126              
127             1;
128              
129             __END__
130              
131             =pod
132              
133             =encoding UTF-8
134              
135             =head1 NAME
136              
137             Dist::Zilla::Plugin::Git::Checkout - clone and checkout a Git repository
138              
139             =head1 VERSION
140              
141             Version 0.003
142              
143             =head1 SYNOPSIS
144              
145             # in dist.ini:
146             [Git::Checkout]
147             repo = https://github.com/skirmess/dzil-inc.git
148              
149             =head1 DESCRIPTION
150              
151             This plugin clones, or if it is already cloned, fetches and updates a Git
152             repository.
153              
154             The plugin runs during the initialization phase, which is the same for
155             bundles and plugins. You can check out a Git repository and load bundles
156             or plugins from this repository.
157              
158             # in dist.ini
159             [Git::Checkout]
160             repo = https://github.com/skirmess/dzil-inc.git
161              
162             ; add the lib directory inside the checked out Git repository to @INC
163             [lib]
164             lib = dzil-inc/lib
165              
166             ; this bundle is run from inside the checked out Git repositories lib
167             ; directory
168             [@BundleFromRepository]
169              
170             =head1 USAGE
171              
172             =head2 checkout
173              
174             Specifies what to check out. This can be a branch, a tag or a revision.
175             Defaults to C<master>.
176              
177             =head2 dir
178              
179             The repositories workspace is checked out into this directory. This defaults
180             to the basename of the repo without the C<.git> suffix.
181              
182             =head2 push_url
183              
184             Allows you to specify a different push url for the repositories origin. One
185             possible scenario would be if you would like to clone via http but push via
186             ssh. This is optional.
187              
188             =head2 repo
189              
190             Specifies the address of the repository to clone. This is required.
191              
192             =head1 SUPPORT
193              
194             =head2 Bugs / Feature Requests
195              
196             Please report any bugs or feature requests through the issue tracker
197             at L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout/issues>.
198             You will be notified automatically of any progress on your issue.
199              
200             =head2 Source Code
201              
202             This is open source software. The code repository is available for
203             public review and contribution under the terms of the license.
204              
205             L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout>
206              
207             git clone https://github.com/skirmess/Dist-Zilla-Plugin-Git-Checkout.git
208              
209             =head1 AUTHOR
210              
211             Sven Kirmess <sven.kirmess@kzone.ch>
212              
213             =head1 COPYRIGHT AND LICENSE
214              
215             This software is Copyright (c) 2020-2021 by Sven Kirmess.
216              
217             This is free software, licensed under:
218              
219             The (two-clause) FreeBSD License
220              
221             =head1 SEE ALSO
222              
223             L<Dist::Zilla>, L<lib>
224              
225             =cut
226              
227             # vim: ts=4 sts=4 sw=4 et: syntax=perl