File Coverage

blib/lib/Dist/Zilla/Plugin/Git/Push.pm
Criterion Covered Total %
statement 45 48 93.7
branch 9 12 75.0
condition n/a
subroutine 11 11 100.0
pod 0 3 0.0
total 65 74 87.8


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dist-Zilla-Plugin-Git
3             #
4             # This software is copyright (c) 2009 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 3     3   8982 use 5.008;
  3         13  
10 3     3   17 use strict;
  3         8  
  3         110  
11 3     3   20 use warnings;
  3         7  
  3         274  
12              
13             package Dist::Zilla::Plugin::Git::Push;
14             # ABSTRACT: Push current branch
15              
16             our $VERSION = '2.048';
17              
18 3     3   23 use Moose;
  3         8  
  3         30  
19 3     3   22199 use MooseX::Has::Sugar;
  3         7  
  3         43  
20 3     3   397 use Types::Standard qw{ ArrayRef Str Bool };
  3         9  
  3         38  
21              
22 3     3   2491 use namespace::autoclean;
  3         8  
  3         34  
23              
24             with 'Dist::Zilla::Role::BeforeRelease',
25             'Dist::Zilla::Role::AfterRelease';
26             with 'Dist::Zilla::Role::Git::Repo',
27             'Dist::Zilla::Role::GitConfig';
28              
29 7     7 0 3573 sub mvp_multivalue_args { qw(push_to) }
30              
31             sub _git_config_mapping { +{
32 2     2   285 push_to => '%{remote}s %{local_branch}s:%{remote_branch}s',
33             } }
34              
35             # -- attributes
36              
37             has remotes_must_exist => ( ro, isa=>Bool, default=>1 );
38              
39             has push_to => (
40             is => 'ro',
41             isa => ArrayRef[Str],
42             lazy => 1,
43             default => sub { [ qw(origin) ] },
44             );
45              
46             around dump_config => sub
47             {
48             my $orig = shift;
49             my $self = shift;
50              
51             my $config = $self->$orig;
52              
53             $config->{+__PACKAGE__} = {
54             push_to => $self->push_to,
55             remotes_must_exist => $self->remotes_must_exist ? 1 : 0,
56             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
57             };
58              
59             return $config;
60             };
61              
62             sub before_release {
63 5     5 0 60244 my $self = shift;
64              
65 5 50       808 return unless $self->remotes_must_exist;
66              
67 5         198 my %valid_remote = map { $_ => 1 } $self->git->remote;
  6         55076  
68 5         77 my @bad_remotes;
69              
70             # Make sure the remotes we'll be pushing to exist
71 5         45 for my $remote_spec ( @{ $self->push_to } ) {
  5         778  
72 7         167 (my $remote = $remote_spec) =~ s/\s.*//s; # Discard branch (if specified)
73 7 50       110 if ($remote =~ m![:/]!) {
74             # Appears to be a URL or path, don't check it
75 0         0 $self->log("Will push to $remote (not checked)");
76             } else {
77             # Named remotes must exist
78 7 100       110 push @bad_remotes, $remote unless $valid_remote{$remote};
79             }
80             }
81              
82 5 100       185 $self->log_fatal("These remotes do not exist: @bad_remotes")
83             if @bad_remotes;
84             }
85              
86              
87             sub after_release {
88 3     3 0 3669 my $self = shift;
89 3         105 my $git = $self->git;
90              
91             # push everything on remote branch
92 3         43 for my $remote ( @{ $self->push_to } ) {
  3         187  
93 4         30900 $self->log("pushing to $remote");
94 4         2367 my @remote = split(/\s+/,$remote);
95 4 100       76 if (@remote == 1) {
96             # Newer versions of Git may not push the current branch automatically.
97             # Append the current branch since the remote didn't specify a branch.
98 3         235 my $branch = $self->current_git_branch;
99 3 50       30 unless (defined $branch) {
100 0         0 $self->log("skipped push to @remote (can't determine branch to push)");
101 0         0 next;
102             }
103 3         41 push @remote, $branch;
104             }
105 4         248 $self->log_debug($_) for $git->push( @remote );
106 4         145552 $self->log_debug($_) for $git->push( { tags=>1 }, $remote[0] );
107             }
108             }
109              
110             __PACKAGE__->meta->make_immutable;
111             1;
112              
113             __END__
114              
115             =pod
116              
117             =encoding UTF-8
118              
119             =head1 NAME
120              
121             Dist::Zilla::Plugin::Git::Push - Push current branch
122              
123             =head1 VERSION
124              
125             version 2.048
126              
127             =head1 SYNOPSIS
128              
129             In your F<dist.ini>:
130              
131             [Git::Push]
132             push_to = origin ; this is the default
133             push_to = origin HEAD:refs/heads/released ; also push to released branch
134             remotes_must_exist = 1 ; this is the default
135              
136             =head1 DESCRIPTION
137              
138             Once the release is done, this plugin will push the current git branch to
139             the remote, with the associated tags.
140              
141             The plugin accepts the following options:
142              
143             =over 4
144              
145             =item *
146              
147             push_to - the name of the a remote to push to. The default is F<origin>.
148             This may be specified multiple times to push to multiple repositories.
149              
150             =item *
151              
152             remotes_must_exist - if true, then Git::Push checks before a release
153             to ensure that all named remotes specified in C<push_to> are
154             configured in your repo. The default is true. Remotes specified as a
155             URL or path are not checked, but will produce a
156             C<Will push to %s (not checked)> message.
157              
158             =back
159              
160             =for Pod::Coverage after_release
161             before_release
162             mvp_multivalue_args
163              
164             =head1 SUPPORT
165              
166             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Git>
167             (or L<bug-Dist-Zilla-Plugin-Git@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Git@rt.cpan.org>).
168              
169             There is also a mailing list available for users of this distribution, at
170             L<http://dzil.org/#mailing-list>.
171              
172             There is also an irc channel available for users of this distribution, at
173             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
174              
175             =head1 AUTHOR
176              
177             Jerome Quelin
178              
179             =head1 COPYRIGHT AND LICENCE
180              
181             This software is copyright (c) 2009 by Jerome Quelin.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut