blib/lib/App/GitHooks/Hook/PrepareCommitMsg.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 41.6% 5 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 81.4% 57 / 70 CC 9 SCAR 19.9
CC
9
Cov
80%
run
12.9
BEGIN
1.0
BEGIN
1.0
CRAP 7.3
« blib/lib/App/GitHooks.pm ↑ Summary blib/lib/App/GitHooks/Test.pm »
1 package App::GitHooks::Hook::PrepareCommitMsg;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Inherit from the base Hook class.
7 2 ▶use base 'App::GitHooks::Hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # External dependencies.
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Internal dependencies.
15 2 ▶use App::GitHooks::CommitMessage;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES :HOOK_EXIT_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 =head1 NAME
20  
21 App::GitHooks::Hook::CommitMsg - Handler for commit-msg hook.
22  
23  
24 =head1 VERSION
25  
26 Version 1.7.3
27  
28 =cut
29  
30 2 our $VERSION = '1.7.3';
31  
32  
33 =head1 METHODS
34  
35 =head2 run()
36  
37 Run the hook handler and return an exit status to pass to git.
38  
39 my $exit_status = App::GitHooks::Hook::CommitMsg->run(
40 app => $app,
41 );
42  
43 Arguments:
44  
45 =over 4
46  
47 =item * app I<(mandatory)>
48  
49 An App::GitHooks object.
50  
51 =back
52  
53 =cut
54  
55 sub run
56 {
57 1 ▶ my ( $class, %args ) = @_;
Subroutine run: called CC=9 SCAR=25.6
Pod documented
58 1 my $app = delete( $args{'app'} );
59 1 ▶ croak 'Unknown argument(s): ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 1 1
60 if scalar( keys %args ) != 0;
61  
62 # Check parameters.
63 1 ▶ croak "The 'app' argument is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($app, "class", "App::GitHooks") 0 1 1
64 if !Data::Validate::Type::is_instance( $app, class => 'App::GitHooks' );
65  
66 # Retrieve the commit message.
67 1 my $command_line_arguments = $app->get_command_line_arguments();
68 1 my $commit_message_file = $command_line_arguments->[0];
69 1 ▶ my $commit_message = App::GitHooks::CommitMessage->new(
Condition50%1/2
Path::Tiny::path($commit_message_file)->slurp_utf8 // ""
l!l
10
MC/DC0%0/1
Path::Tiny::path($commit_message_file)->slurp_utf8 // ""
Path::Tiny::path($commit_message_file)->slurp_utf8
Truth table50%1/2
A
A= Path::Tiny::path($commit_message_file)->slurp_utf8
Aresult
11
00
70 message => Path::Tiny::path( $commit_message_file )->slurp_utf8() // '',
71 app => $app,
72 );
73  
74 # Find and run all the plugins that support the prepare-commit-msg hook.
75 1 my $tests_success = 1;
76 1 my $plugins = $app->get_hook_plugins( 'prepare-commit-msg' );
77 1 foreach my $plugin ( @$plugins )
78 {
79 1 my $check_result = $plugin->run_prepare_commit_msg(
80 app => $app,
81 commit_message => $commit_message,
82 );
83 1 ▶ $tests_success = 0
Branch: 1 branch
BranchTrueFalseTotal
if $check_result == $App::GitHooks::Hook::PrepareCommitMsg::PLUGIN_RETURN_FAILED 0 1 1
84 if $check_result == $PLUGIN_RETURN_FAILED;
85 }
86  
87 # If the commit message was modified above, we need to overwrite the file.
88 1 ▶ if ( $commit_message->has_changed() )
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_message->has_changed) 0 1 1
89 {
90 0 my $terminal = $app->get_terminal();
91 0 my $message = $commit_message->get_message();
92 0 my $file = Path::Tiny::path( $commit_message_file );
93 0 ▶ $terminal->is_utf8()
Branch: 1 branch
BranchTrueFalseTotal
$terminal->is_utf8 ? : 0 0 0
94 ? $file->spew_utf8( $message )
95 : $file->spew( $message );
96 }
97  
98 # .git/COMMIT-MSG-CHECKS is a file we use to track if the pre-commit hook has
99 # run, as opposed to being skipped with --no-verify. Since pre-commit can be
100 # skipped, but prepare-commit-msg cannot, plugins can use the presence of
101 # that file to determine if some optional processing should be performed in
102 # the prepare-commit-msg phase. For example, you may want to add a warning
103 # indicating that --no-verify was used. Note however that the githooks man
104 # page says "it should not be used as replacement for pre-commit hook".
105 #
106 # And since we're done with prepare-commit-msg checks now, we can safely
107 # remove the file.
108 1 unlink( '.git/COMMIT-MSG-CHECKS' );
109  
110 1 ▶ return $tests_success
Branch: 1 branch
BranchTrueFalseTotal
$tests_success ? : 1 0 1
111 ? $HOOK_EXIT_SUCCESS
112 : $HOOK_EXIT_FAILURE;
113 }
114  
115  
116 =head1 BUGS
117  
118 Please report any bugs or feature requests through the web interface at
119 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
120 I will be notified, and then you'll automatically be notified of progress on
121 your bug as I make changes.
122  
123  
124 =head1 SUPPORT
125  
126 You can find documentation for this module with the perldoc command.
127  
128 perldoc App::GitHooks::Hook::PrepareCommitMsg
129  
130  
131 You can also look for information at:
132  
133 =over
134  
135 =item * GitHub's request tracker
136  
137 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
138  
139 =item * AnnoCPAN: Annotated CPAN documentation
140  
141 L<http://annocpan.org/dist/app-githooks>
142  
143 =item * CPAN Ratings
144  
145 L<http://cpanratings.perl.org/d/app-githooks>
146  
147 =item * MetaCPAN
148  
149 L<https://metacpan.org/release/App-GitHooks>
150  
151 =back
152  
153  
154 =head1 AUTHOR
155  
156 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
157 C<< <aubertg at cpan.org> >>.
158  
159  
160 =head1 COPYRIGHT & LICENSE
161  
162 Copyright 2013-2015 Guillaume Aubert.
163  
164 This program is free software: you can redistribute it and/or modify it under
165 the terms of the GNU General Public License version 3 as published by the Free
166 Software Foundation.
167  
168 This program is distributed in the hope that it will be useful, but WITHOUT ANY
169 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
170 PARTICULAR PURPOSE. See the GNU General Public License for more details.
171  
172 You should have received a copy of the GNU General Public License along with
173 this program. If not, see http://www.gnu.org/licenses/
174  
175 =cut
176  
177 2 1;
« blib/lib/App/GitHooks.pm ↑ Summary blib/lib/App/GitHooks/Test.pm »