blib/lib/Pinto/Role/Committable.pm

filter: Statementstmt 85.7% 84 / 98 Branchbran 59.0% 13 / 22 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 5 totaltotal 77.1% 118 / 153 CC 14 SCAR 16.7
CC
14
Cov
79%
compose_message
14.4
after_execute
5.2
generate_message_title
2.0
CRAP 5.3
« blib/lib/App/Pinto/Command/log.pm ↑ Summary blib/lib/App/Pinto/Command/stacks.pm »
1 # ABSTRACT: Role for actions that commit changes to the repository
2  
3 package Pinto::Role::Committable;
4  
5 48 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use MooseX::Types::Moose qw(Bool Str ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use MooseX::MarkAsMethods ( autoclean => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 48 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 48 ▶use Pinto::Constants qw(:lock);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use Pinto::Types qw(StackName StackDefault StackObject DiffStyle);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 48 ▶use Pinto::Util qw(is_interactive throw is_blank is_not_blank);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #------------------------------------------------------------------------------
17  
18 48 our $VERSION = '0.14'; # VERSION
19  
20 #------------------------------------------------------------------------------
21  
22 48 with qw(Pinto::Role::Plated);
23  
24 #------------------------------------------------------------------------------
25  
26 48 has stack => (
27 is => 'ro',
28 isa => StackName | StackDefault | StackObject,
29 writer => '_set_stack',
30 default => undef,
31 );
32  
33 48 has dry_run => (
34 is => 'ro',
35 isa => Bool,
36 default => 0,
37 );
38  
39 48 has message => (
40 is => 'ro',
41 isa => Str,
42 predicate => 'has_message',
43 );
44  
45 48 has use_default_message => (
46 is => 'ro',
47 isa => Bool,
48 default => 0,
49 );
50  
51 48 has diff_style => (
52 is => 'ro',
53 isa => DiffStyle,
54 predicate => 'has_diff_style',
55 );
56  
57 48 has lock_type => (
58 is => 'ro',
59 isa => Str,
60 default => $PINTO_LOCK_TYPE_EXCLUSIVE,
61 init_arg => undef,
62 );
63  
64 has affected => (
65 is => 'ro',
66 isa => ArrayRef,
67 70 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 48 init_arg => undef,
69 );
70  
71 #------------------------------------------------------------------------------
72  
73 48 requires qw( execute repo );
74  
75 #------------------------------------------------------------------------------
76  
77 70 ▶around BUILD => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 70 my ( $orig, $self ) = @_;
79  
80 # Inflate the stack into a real object. As a side
81 # effect, this also verifies that the stack exists.
82  
83 70 my $stack = $self->repo->get_stack( $self->stack );
84 70 $self->_set_stack($stack);
85  
86 # Make sure we aren't locked if we intend to commit
87 70 ▶ $self->stack->assert_not_locked unless $self->dry_run;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dry_run 69 1 70
88  
89 69 return $self->$orig;
90 48 };
91  
92 #------------------------------------------------------------------------------
93  
94 66 ▶around execute => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 66 my ( $orig, $self, @args ) = @_;
96  
97 66 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 66 $self->repo->txn_begin;
99 66 $self->before_execute;
100 66 $self->$orig(@args);
101 59 $self->after_execute;
102 }
103 63 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 63 $self->repo->txn_rollback;
105 63 $self->repo->clean_files;
106 63 throw $_;
107 66 };
108  
109 3 return $self->result;
110 48 };
111  
112 #------------------------------------------------------------------------------
113  
114 66 ▶sub before_execute {
Subroutine before_execute: called CC=1 SCAR=0.0
Pod undocumented
115 66 my ($self) = @_;
116  
117 66 $self->stack->start_revision;
118  
119 66 return $self;
120 }
121  
122 #------------------------------------------------------------------------------
123  
124 59 ▶sub after_execute {
Subroutine after_execute: called CC=5 SCAR=16.5
Pod undocumented
125 59 my ($self, @dists) = @_;
126  
127 59 ▶ local $ENV{PINTO_DIFF_STYLE} = $self->diff_style
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_diff_style 0 59 59
128 if $self->has_diff_style;
129  
130 59 my $stack = $self->stack;
131 59 ▶ if ( $self->dry_run ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->dry_run) { } 1 58 59
elsif ($stack->refresh->has_not_changed) { } 0 58 58
132  
133 1 ▶ $stack->refresh->has_changed
Branch: 1 branch
BranchTrueFalseTotal
$stack->refresh->has_changed ? : 1 0 1
134 ? $self->show($stack->diff, {no_newline => 1})
135 : $self->notice('No changes were made');
136  
137 1 $self->repo->txn_rollback;
138 1 $self->repo->clean_files;
139 }
140 elsif ( $stack->refresh->has_not_changed ) {
141  
142 0 $self->diag('No changes were made');
143 0 $self->repo->txn_rollback;
144 }
145 else {
146  
147 58 my $msg = $self->compose_message;
148 58 $stack->commit_revision( message => $msg );
149  
150 2 $self->result->changed;
151 2 $self->repo->txn_commit;
152 }
153  
154 # Release the exclusive lock and just use a shared lock, since
155 # we won't be writing to the repository at this point.
156 3 $self->repo->unlock; $self->repo->lock($PINTO_LOCK_TYPE_SHARED);
157  
158 3 return $self;
159 }
160  
161 #------------------------------------------------------------------------------
162  
163 58 ▶sub compose_message {
Subroutine compose_message: called CC=8 SCAR=26.7
Pod undocumented
164 58 my ($self) = @_;
165  
166 58 my $stack = $self->stack;
167 58 my $title = $self->generate_message_title;
168  
169 58 ▶ return $self->message
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_message and is_not_blank($self->message) 24 34 58
Condition66%2/3
$self->has_message and is_not_blank($self->message)
!ll&&!rl&&r
34024
MC/DC50%1/2
$self->has_message and is_not_blank($self->message)
$self->has_message is_not_blank($self->message)
Truth table66%2/3
A and B
A= $self->has_messageB= is_not_blank($self->message)
ABresult
0X0
100
111
170 if $self->has_message and is_not_blank( $self->message );
171  
172 34 ▶ return $title
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_message and is_blank($self->message) 0 34 34
Condition33%1/3
$self->has_message and is_blank($self->message)
!ll&&!rl&&r
3400
MC/DC0%0/2
$self->has_message and is_blank($self->message)
$self->has_message is_blank($self->message)
Truth table33%1/3
A and B
A= $self->has_messageB= is_blank($self->message)
ABresult
0X0
100
111
173 if $self->has_message and is_blank( $self->message );
174  
175 34 ▶ return $title
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_default_message 0 34 34
176 if $self->use_default_message;
177  
178 34 ▶ return $title
Branch: 1 branch
BranchTrueFalseTotal
unless is_interactive() 34 0 34
179 if not is_interactive;
180  
181 0 my $template = $self->generate_message_template($title);
182 0 my $message = $self->chrome->edit( $template );
183 0 $message =~ s/^ [#] .* $//gmsx; # Strip comments
184  
185 0 ▶ throw 'Aborting due to empty commit message' if is_blank($message);
Branch: 1 branch
BranchTrueFalseTotal
if is_blank($message) 0 0 0
186  
187 0 return $message;
188 }
189  
190 #------------------------------------------------------------------------------
191  
192 58 ▶sub generate_message_title {
Subroutine generate_message_title: called CC=2 SCAR=7.0
Pod undocumented
193 58 my ( $self, $extra ) = @_;
194  
195 58 my $class = ref $self;
196 58 my ($action) = $class =~ m/ ( [^:]* ) $/x;
197 58 my @dists = uniq( sort @{$self->affected} );
198 58 ▶ my $title = "$action " . join( ', ', @dists ) . ( $extra ? " $extra" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$extra ? : 0 58 58
199  
200 58 return $title;
201 }
202  
203 #------------------------------------------------------------------------------
204  
205 0 ▶sub generate_message_template {
Subroutine generate_message_template: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my ( $self, $title ) = @_;
207  
208 0 my $stack = $self->stack;
209 0 my $diff = $stack->diff;
210  
211 # Prepend "#" to each line of the diff,
212 # so they are treated as comments.
213 0 $diff =~ s/^/# /gm;
214  
215 0 my $msg = <<"END_MESSAGE";
216 $title
217  
218  
219 #-------------------------------------------------------------------------------
220 # Please edit or amend the message above as you see fit. The first line of the
221 # message will be used as the title. Any line that starts with a "#" will be
222 # ignored. To abort the commit, delete the entire message above, save the file,
223 # and close the editor.
224 #
225 # Changes to be committed to stack $stack:
226 #
227 $diff
228 END_MESSAGE
229  
230 0 chomp $msg;
231 0 return $msg;
232 }
233  
234 #------------------------------------------------------------------------------
235 48 1;
236  
237 __END__
« blib/lib/App/Pinto/Command/log.pm ↑ Summary blib/lib/App/Pinto/Command/stacks.pm »