blib/lib/App/GitHooks/CommitMessage.pm

filter: Statementstmt 86.3% 57 / 66 Branchbran 72.7% 16 / 22 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 93.3% 14 / 15 Podpod 100% 10 / 10 totaltotal 76.6% 102 / 133 CC 17 SCAR 14.2
CC
17
Cov
77%
has_changed
7.8
get_lines
6.1
get_ticket_id
6.0
CRAP 4.1
« Build ↑ Summary blib/lib/App/GitHooks/Terminal.pm »
1 package App::GitHooks::CommitMessage;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # External dependencies.
7 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Internal dependencies.
10 14 ▶use App::GitHooks::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 =head1 NAME
15  
16 App::GitHooks::CommitMessage - A git commit message.
17  
18  
19 =head1 VERSION
20  
21 Version 1.7.3
22  
23 =cut
24  
25 14 our $VERSION = '1.7.3';
26  
27  
28 =head1 METHODS
29  
30 =head2 new()
31  
32 my $commit_message = App::GitHooks::CommitMessage->new(
33 app => $app,
34 message => $message,
35 );
36  
37 Arguments:
38  
39 =over 4
40  
41 =item * app I<(mandatory)>
42  
43 An C<App::GitHook> instance.
44  
45 =item * message I<(mandatory)>
46  
47 The commit message.
48  
49 =back
50  
51 =cut
52  
53 sub new
54 {
55 23 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
56 23 my $message = delete( $args{'message'} );
57 23 my $app = delete( $args{'app'} );
58  
59 # Check arguments.
60 23 ▶ croak 'A "message" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 1 22 23
61 if !defined( $message );
62 22 ▶ croak 'An "app" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($app, "class", "App::GitHooks") 1 21 22
63 if !Data::Validate::Type::is_instance( $app, class => 'App::GitHooks' );
64 21 ▶ croak 'Unknown arguments: ' . join( ', ', keys( %args ) )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 21 21
65 if scalar( keys %args ) != 0;
66  
67 # Create a new instance.
68 21 return bless(
69 {
70 message => $message,
71 original_message => $message,
72 app => $app,
73 },
74 $class,
75 );
76 }
77  
78  
79 =head2 update_message()
80  
81 Update the commit message.
82  
83 my $success = $commit_message->update_message( $new_message );
84  
85 =cut
86  
87 sub update_message
88 {
89 2 ▶ my ( $self, $message ) = @_;
Subroutine update_message: called CC=1 SCAR=0.0
Pod documented
90  
91 # Update the message.
92 2 $self->{'message'} = $message;
93  
94 # Remove various caches.
95 2 delete( $self->{'lines'} );
96 2 delete( $self->{'ticket_id'} );
97  
98 2 return 1;
99 }
100  
101  
102 =head2 get_lines()
103  
104 Return an arrayref of the lines that will be included in the commit message,
105 excluding blank lines and comments.
106  
107 my $lines = $commit_message->get_lines(
108 include_comments => $include_comments, # default: 0
109 include_blank_lines => $include_blank_lines, # default: 1
110 );
111  
112 =cut
113  
114 sub get_lines
115 {
116 14 ▶ my ( $self, %args ) = @_;
Subroutine get_lines: called CC=6 SCAR=18.1
Pod documented
117 14 ▶ my $include_comments = delete( $args{'include_comments'} ) // 0;
Condition50%1/2
delete $args{"include_comments"} // 0
l!l
140
MC/DC0%0/1
delete $args{"include_comments"} // 0
delete $args{"include_comments"}
Truth table50%1/2
A
A= delete $args{"include_comments"}
Aresult
11
00
118 14 ▶ my $include_blank_lines = delete( $args{'include_blank_lines'} ) // 1;
Condition50%1/2
delete $args{"include_blank_lines"} // 1
l!l
140
MC/DC0%0/1
delete $args{"include_blank_lines"} // 1
delete $args{"include_blank_lines"}
Truth table50%1/2
A
A= delete $args{"include_blank_lines"}
Aresult
11
00
119  
120 14 my $message = $self->get_message();
121  
122 14 ▶ my @lines = split( /\n/, $message // '' );
Condition50%1/2
$message // ""
l!l
140
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
123 14 ▶ @lines = grep { $_ !~ /^#/ } @lines
Branch: 1 branch
BranchTrueFalseTotal
unless $include_comments 12 2 14
124 if !$include_comments;
125 14 ▶ @lines = grep { $_ !~ /^\s*$/ } @lines
Branch: 1 branch
BranchTrueFalseTotal
unless $include_blank_lines 9 5 14
126 if !$include_blank_lines;
127  
128 14 return \@lines;
129 }
130  
131  
132 =head2 get_summary()
133  
134 Return the first line of the commit message.
135  
136 my $summary = $commit_message->get_summary();
137  
138 =cut
139  
140 sub get_summary
141 {
142 1 ▶ my ( $self ) = @_;
Subroutine get_summary: called CC=2 SCAR=7.1
Pod documented
143 1 my $lines = $self->get_lines(
144 include_comments => 0,
145 include_blank_lines => 1,
146 );
147  
148 1 ▶ return scalar( @$lines ) == 0
Branch: 1 branch
BranchTrueFalseTotal
scalar @$lines == 0 ? : 0 1 1
149 ? undef
150 : $lines->[0];
151 }
152  
153  
154 =head2 is_empty()
155  
156 Indicate whether a commit message is empty, after excluding comment lines and
157 blank lines.
158  
159 my $is_empty = $commit_message->is_empty();
160  
161 =cut
162  
163 sub is_empty
164 {
165 5 ▶ my ( $self ) = @_;
Subroutine is_empty: called CC=2 SCAR=6.9
Pod documented
166 5 my $lines = $self->get_lines(
167 include_comments => 0,
168 include_blank_lines => 0,
169 );
170  
171 5 ▶ return scalar( @$lines ) == 0
Branch: 1 branch
BranchTrueFalseTotal
scalar @$lines == 0 ? : 2 3 5
172 ? 1
173 : 0;
174 }
175  
176  
177 =head2 get_ticket_id()
178  
179 Extract and return a ticket ID from the commit message.
180  
181 my $ticket_id = $commit_message->get_ticket_id();
182  
183 =cut
184  
185 sub get_ticket_id
186 {
187 0 ▶ my ( $self ) = @_;
Subroutine get_ticket_id: not called CC=2 SCAR=17.9
Pod documented
188 0 my $message = $self->get_message();
189 0 my $app = $self->get_app();
190  
191 0 ▶ if ( !defined( $self->{'ticket_id'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ticket_id"}) 0 0 0
192 {
193 # Get regex to extract the ticket ID.
194 0 my $ticket_regex = App::GitHooks::Utils::get_ticket_id_from_commit_regex( $app );
195  
196 # Parse the first line of the commit message.
197 0 my $summary = $self->get_summary();
198 0 my ( $ticket_id ) = $summary =~ /$ticket_regex/i;
199 0 $self->{'ticket_id'} = $ticket_id;
200 }
201  
202 0 return $self->{'ticket_id'};
203 }
204  
205  
206 =head2 has_changed()
207  
208 Return whether the message has changed since the object was created.
209  
210 my $has_changed = $commit_message->has_changed();
211  
212 =cut
213  
214 sub has_changed
215 {
216 3 ▶ my ( $self ) = @_;
Subroutine has_changed: called CC=6 SCAR=20.5
Pod documented
217 3 my $message = $self->get_message();
218 3 my $original_message = $self->get_original_message();
219  
220 # If the defineness status doesn't match, it's definitely a change.
221 3 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $message xor defined $original_message 0 3 3
Condition25%1/4
defined $message xor defined $original_message
l&&rl&&!r!l&&r!l&&!r
3000
MC/DC0%0/2
defined $message xor defined $original_message
defined $message defined $original_message
Truth table25%1/4
A xor B
A= defined $messageB= defined $original_message
ABresult
110
101
011
000
222 if defined( $message ) xor defined( $original_message );
223  
224 # If both are undefined, there's no change.
225 3 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message or defined $original_message 0 3 3
Condition33%1/3
defined $message or defined $original_message
l!l&&r!l&&!r
300
MC/DC0%0/2
defined $message or defined $original_message
defined $message defined $original_message
Truth table33%1/3
A or B
A= defined $messageB= defined $original_message
ABresult
1X1
011
000
226 if !defined( $message ) && !defined( $original_message );
227  
228 # Both are defined, and we need to compare the messages.
229 3 ▶ return $message eq $original_message ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$message eq $original_message ? : 2 1 3
230 }
231  
232  
233 =head1 ACCESSORS
234  
235 =head2 get_original_message()
236  
237 Return the original message that was provided when the object was created.
238  
239 my $original_message = $commit_message->get_original_message();
240  
241 =cut
242  
243 sub get_original_message
244 {
245 4 ▶ my ( $self ) = @_;
Subroutine get_original_message: called CC=1 SCAR=0.0
Pod documented
246  
247 4 return $self->{'original_message'};
248 }
249  
250  
251 =head2 get_message()
252  
253 Return the commit message itself.
254  
255 my $message = $commit_message->get_message();
256  
257 =cut
258  
259 sub get_message
260 {
261 20 ▶ my ( $self ) = @_;
Subroutine get_message: called CC=1 SCAR=0.0
Pod documented
262  
263 20 return $self->{'message'};
264 }
265  
266  
267 =head2 get_app()
268  
269 Return the parent C<App::GitHooks> object.
270  
271 my $app = $commit_message->get_app();
272  
273 =cut
274  
275 sub get_app
276 {
277 1 ▶ my ( $self ) = @_;
Subroutine get_app: called CC=1 SCAR=0.0
Pod documented
278  
279 1 return $self->{'app'};
280 }
281  
282  
283 =head1 BUGS
284  
285 Please report any bugs or feature requests through the web interface at
286 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
287 I will be notified, and then you'll automatically be notified of progress on
288 your bug as I make changes.
289  
290  
291 =head1 SUPPORT
292  
293 You can find documentation for this module with the perldoc command.
294  
295 perldoc App::GitHooks::CommitMessage
296  
297  
298 You can also look for information at:
299  
300 =over
301  
302 =item * GitHub's request tracker
303  
304 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
305  
306 =item * AnnoCPAN: Annotated CPAN documentation
307  
308 L<http://annocpan.org/dist/app-githooks>
309  
310 =item * CPAN Ratings
311  
312 L<http://cpanratings.perl.org/d/app-githooks>
313  
314 =item * MetaCPAN
315  
316 L<https://metacpan.org/release/App-GitHooks>
317  
318 =back
319  
320  
321 =head1 AUTHOR
322  
323 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
324 C<< <aubertg at cpan.org> >>.
325  
326  
327 =head1 COPYRIGHT & LICENSE
328  
329 Copyright 2013-2015 Guillaume Aubert.
330  
331 This program is free software: you can redistribute it and/or modify it under
332 the terms of the GNU General Public License version 3 as published by the Free
333 Software Foundation.
334  
335 This program is distributed in the hope that it will be useful, but WITHOUT ANY
336 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
337 PARTICULAR PURPOSE. See the GNU General Public License for more details.
338  
339 You should have received a copy of the GNU General Public License along with
340 this program. If not, see http://www.gnu.org/licenses/
341  
342 =cut
343  
344 14 1;
« Build ↑ Summary blib/lib/App/GitHooks/Terminal.pm »