File: | blib/lib/Git/ReleaseRepo/Command/add.pm |
Coverage: | 75.0% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Git::ReleaseRepo::Command::add; | ||||||
2 | # ABSTRACT: Add a module to the next release | ||||||
3 | |||||||
4 | 2 2 2 | 1597 2 32 | use strict; | ||||
5 | 2 2 2 | 4 1 26 | use warnings; | ||||
6 | 2 2 2 | 4 1 5 | use Moose; | ||||
7 | 2 2 2 | 7109 1 7 | use Git::ReleaseRepo -command; | ||||
8 | 2 2 2 | 268 2 723 | use File::Spec::Functions qw( catdir ); | ||||
9 | |||||||
10 | with 'Git::ReleaseRepo::WithVersionPrefix'; | ||||||
11 | |||||||
12 | override usage_desc => sub { | ||||||
13 | my ( $self ) = @_; | ||||||
14 | return super() . " <module_name> [<module_url>]"; | ||||||
15 | }; | ||||||
16 | |||||||
17 | sub description { | ||||||
18 | 0 | 1 | 0 | return 'Add a module to the next release'; | |||
19 | } | ||||||
20 | |||||||
21 | around opt_spec => sub { | ||||||
22 | my ( $orig, $self ) = @_; | ||||||
23 | return ( | ||||||
24 | $self->$orig(), | ||||||
25 | [ 'bugfix' => 'Add to the latest release branch as a bug fix' ], | ||||||
26 | [ 'all|a' => "Add all out-of-date modules to the release" ], | ||||||
27 | ); | ||||||
28 | }; | ||||||
29 | |||||||
30 | sub validate_args { | ||||||
31 | 5 | 1 | 47 | my ( $self, $opt, $args ) = @_; | |||
32 | 5 | 12 | if ( $opt->all ) { | ||||
33 | 0 | 0 | if ( @$args ) { | ||||
34 | 0 | 0 | return $self->usage_error( "--all does not make sense with module names to add" ); | ||||
35 | } | ||||||
36 | } | ||||||
37 | else { | ||||||
38 | 5 | 22 | if ( scalar @$args < 1 ) { | ||||
39 | 0 | 0 | return $self->usage_error( "You must specify a submodule to add to the next release" ); | ||||
40 | } | ||||||
41 | 5 | 13 | if ( scalar @$args > 2 ) { | ||||
42 | 0 | 0 | return $self->usage_error( "Too many arguments" ); | ||||
43 | } | ||||||
44 | } | ||||||
45 | } | ||||||
46 | |||||||
47 | augment execute => sub { | ||||||
48 | my ( $self, $opt, $args ) = @_; | ||||||
49 | my $git = $self->git; | ||||||
50 | my $branch; | ||||||
51 | if ( $opt->bugfix ) { | ||||||
52 | $branch = $git->latest_release_branch; | ||||||
53 | die "Cannot add with --bugfix: No release branch found!\n" if !$branch; | ||||||
54 | } | ||||||
55 | else { | ||||||
56 | $branch = "master"; | ||||||
57 | } | ||||||
58 | if ( $git->has_branch( $branch ) ) { | ||||||
59 | $git->checkout( $branch ); | ||||||
60 | } | ||||||
61 | if ( $opt->all ) { | ||||||
62 | my @outdated = $git->outdated; | ||||||
63 | for my $outdated ( @outdated ) { | ||||||
64 | $self->update_submodule( $outdated, $branch ); | ||||||
65 | } | ||||||
66 | my $message = "Updating all outdated:\n" | ||||||
67 | . join "\n", map { sprintf "\t\%s", $_ } sort @outdated; | ||||||
68 | $git->run( commit => ( @outdated ), -m => $message ); | ||||||
69 | } | ||||||
70 | elsif ( @$args == 1 ) { | ||||||
71 | $self->update_submodule( @$args, $branch ); | ||||||
72 | $git->run( commit => ( @$args ), -m => "Updating $args->[0]" ); | ||||||
73 | } | ||||||
74 | elsif ( @$args == 2 ) { | ||||||
75 | $self->add_submodule( @$args ); | ||||||
76 | $git->run( commit => ( '.gitmodules', $args->[0] ), -m => "Adding $args->[0] to release" ); | ||||||
77 | } | ||||||
78 | $git->checkout; | ||||||
79 | }; | ||||||
80 | |||||||
81 | sub update_submodule { | ||||||
82 | 3 | 0 | 10 | my ( $self, $module, $branch ) = @_; | |||
83 | 3 | 9 | $branch ||= "master"; | ||||
84 | 3 | 20 | my $git = $self->git; | ||||
85 | 3 | 14 | if ( !$git->submodule->{ $module } ) { | ||||
86 | 0 | 0 | die "Cannot add $module: Submodule does not exist\n"; | ||||
87 | } | ||||||
88 | 3 | 23 | my $subgit = $git->submodule_git( $module ); | ||||
89 | 3 | 14 | my $cmd = $subgit->command( 'fetch' ); | ||||
90 | 3 | 13734 | $cmd->close; | ||||
91 | 3 | 41114 | $cmd = $subgit->command( checkout => 'origin/' . $branch ); | ||||
92 | 3 | 13706 | my @stdout = readline $cmd->stdout; | ||||
93 | 3 | 7709 | my @stderr = readline $cmd->stderr; | ||||
94 | 3 | 50 | $cmd->close; | ||||
95 | 3 | 162 | if ( $cmd->exit != 0 ) { | ||||
96 | 0 | 0 | die "Could not checkout 'origin/$branch': \nSTDERR: " . ( join "\n", @stderr ) | ||||
97 | . "\nSTDOUT: " . ( join "\n", @stdout ); | ||||||
98 | } | ||||||
99 | } | ||||||
100 | |||||||
101 | sub add_submodule { | ||||||
102 | 2 | 0 | 7 | my ( $self, $module, $repo ) = @_; | |||
103 | 2 | 11 | my $git = $self->git; | ||||
104 | 2 | 6 | $git->run( | ||||
105 | submodule => add => '--', $repo, $module, | ||||||
106 | ); | ||||||
107 | } | ||||||
108 | |||||||
109 | 2 2 2 | 5 1 7 | no Moose; | ||||
110 | __PACKAGE__->meta->make_immutable; | ||||||
111 | 1; |