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

filter: Statementstmt 88.1% 52 / 59 Branchbran 44.4% 8 / 18 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 73.7% 73 / 99 CC 14 SCAR 28.1
CC
14
Cov
74%
run
26.5
BEGIN
1.0
BEGIN
1.0
CRAP 16.5
« blib/lib/App/GitHooks/StagedChanges.pm ↑ Summary blib/lib/App/GitHooks.pm »
1 package App::GitHooks::Hook::CommitMsg;
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( :HOOK_EXIT_CODES :PLUGIN_RETURN_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use App::GitHooks::StagedChanges;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 =head1 NAME
21  
22 App::GitHooks::Hook::CommitMsg - Handle the commit-msg hook.
23  
24  
25 =head1 VERSION
26  
27 Version 1.7.3
28  
29 =cut
30  
31 2 our $VERSION = '1.7.3';
32  
33  
34 =head1 METHODS
35  
36 =head2 run()
37  
38 Run the hook handler and return an exit status to pass to git.
39  
40 my $exit_status = App::GitHooks::Hook::CommitMsg->run(
41 app => $app,
42 );
43  
44 Arguments:
45  
46 =over 4
47  
48 =item * app I<(mandatory)>
49  
50 An App::GitHooks object.
51  
52 =back
53  
54 =cut
55  
56 sub run
57 {
58 1 ▶ my ( $class, %args ) = @_;
Subroutine run: called CC=14 SCAR=32.8
Pod documented
59 1 my $app = delete( $args{'app'} );
60 1 ▶ croak 'Unknown argument(s): ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 1 1
61 if scalar( keys %args ) != 0;
62  
63 # Check parameters.
64 1 ▶ croak "The 'app' argument is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($app, "class", "App::GitHooks") 0 1 1
65 if !Data::Validate::Type::is_instance( $app, class => 'App::GitHooks' );
66  
67 # Reassigns standard input back to the keyboard.
68 # Note: this will silently fail in some non-interactive shells where /dev/tty
69 # can't be referenced, which is fine since there will be no user typing anyway.
70 1 ▶ my $console = $^O eq 'MSWin32'
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
71 ? 'CON:'
72 : '/dev/tty';
73 1 open( STDIN, '<', $console ); ## no critic (InputOutput::RequireCheckedOpen)
74  
75 # If the terminal isn't interactive, we won't have a human available to fix the
76 # problems so we just let the commit go as is.
77 1 my $config = $app->get_config();
78 1 my $force_interactive = $config->get( 'testing', 'force_interactive' );
79 1 ▶ return $HOOK_EXIT_SUCCESS
Branch: 1 branch
BranchTrueFalseTotal
unless $app->get_terminal->is_interactive or $force_interactive 0 1 1
Condition33%1/3
$app->get_terminal->is_interactive or $force_interactive
l!l&&r!l&&!r
010
MC/DC0%0/2
$app->get_terminal->is_interactive or $force_interactive
$app->get_terminal->is_interactive $force_interactive
Truth table33%1/3
A or B
A= $app->get_terminal->is_interactiveB= $force_interactive
ABresult
1X1
011
000
80 if !$app->get_terminal()->is_interactive() && !$force_interactive;
81  
82 # Analyze the commit message and prompt the user to fix it if needed until it
83 # passes the checks.
84 1 my $has_errors = 0;
85 1 while ( 1 )
86 {
87 # Retrieve the commit message.
88 1 my $command_line_arguments = $app->get_command_line_arguments();
89 1 my $commit_message_file = $command_line_arguments->[0];
90 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
91 message => Path::Tiny::path( $commit_message_file )->slurp_utf8() // '',
92 app => $app,
93 );
94  
95 # If the commit message is empty, don't bother running any checks - git will
96 # abort the commit.
97 last
98 1 ▶ if $commit_message->is_empty();
Branch: 1 branch
BranchTrueFalseTotal
if $commit_message->is_empty 0 1 1
99  
100 # Find all the tests we will need to run.
101 1 my $plugins = $app->get_hook_plugins( $app->get_hook_name() );
102 1 $has_errors = 0;
103 1 foreach my $plugin ( @$plugins )
104 {
105 1 my $return_code = $plugin->run_commit_msg(
106 app => $app,
107 commit_message => $commit_message,
108 );
109 1 ▶ $has_errors = 1
Branch: 1 branch
BranchTrueFalseTotal
if $return_code == $App::GitHooks::Hook::CommitMsg::PLUGIN_RETURN_FAILED 0 1 1
110 if $return_code == $PLUGIN_RETURN_FAILED;
111 }
112  
113 # If errors were found, let the user try to fix the commit message,
114 # otherwise finish and let git complete.
115 1 ▶ if ( $has_errors )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_errors) { } 0 1 1
116 {
117 0 print "Press <Enter> to edit again the commit message or Ctrl-C to abort the commit.\n";
118 0 ▶ if ( $app->get_terminal()->is_interactive() )
Branch: 1 branch
BranchTrueFalseTotal
if ($app->get_terminal->is_interactive) { } 0 0 0
119 {
120 0 my $input = <STDIN>; ## no critic (InputOutput::ProhibitExplicitStdin)
121  
122 0 ▶ my $editor = $ENV{'EDITOR'} // 'vim';
Condition0%0/2
$ENV{"EDITOR"} // "vim"
l!l
00
MC/DC0%0/1
$ENV{"EDITOR"} // "vim"
$ENV{"EDITOR"}
Truth table0%0/2
A
A= $ENV{"EDITOR"}
Aresult
11
00
123 0 system("$editor $commit_message_file");
124 0 print "\n";
125 }
126 else
127 {
128 # $has_errors is set to 1, but we're not in interactive mode so we
129 # can't wait for STDIN.
130 0 last;
131 }
132 }
133 else
134 {
135 # No errors, $has_errors is set to 0, finish.
136 1 last;
137 }
138 }
139  
140 # Success.
141 1 ▶ return $has_errors
Branch: 1 branch
BranchTrueFalseTotal
$has_errors ? : 0 1 1
142 ? $HOOK_EXIT_FAILURE
143 : $HOOK_EXIT_SUCCESS;
144 }
145  
146  
147 =head1 BUGS
148  
149 Please report any bugs or feature requests through the web interface at
150 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
151 I will be notified, and then you'll automatically be notified of progress on
152 your bug as I make changes.
153  
154  
155 =head1 SUPPORT
156  
157 You can find documentation for this module with the perldoc command.
158  
159 perldoc App::GitHooks::Hook::CommitMsg
160  
161  
162 You can also look for information at:
163  
164 =over
165  
166 =item * GitHub's request tracker
167  
168 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
169  
170 =item * AnnoCPAN: Annotated CPAN documentation
171  
172 L<http://annocpan.org/dist/app-githooks>
173  
174 =item * CPAN Ratings
175  
176 L<http://cpanratings.perl.org/d/app-githooks>
177  
178 =item * MetaCPAN
179  
180 L<https://metacpan.org/release/App-GitHooks>
181  
182 =back
183  
184  
185 =head1 AUTHOR
186  
187 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
188 C<< <aubertg at cpan.org> >>.
189  
190  
191 =head1 COPYRIGHT & LICENSE
192  
193 Copyright 2013-2015 Guillaume Aubert.
194  
195 This program is free software: you can redistribute it and/or modify it under
196 the terms of the GNU General Public License version 3 as published by the Free
197 Software Foundation.
198  
199 This program is distributed in the hope that it will be useful, but WITHOUT ANY
200 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
201 PARTICULAR PURPOSE. See the GNU General Public License for more details.
202  
203 You should have received a copy of the GNU General Public License along with
204 this program. If not, see http://www.gnu.org/licenses/
205  
206 =cut
207  
208 2 1;
« blib/lib/App/GitHooks/StagedChanges.pm ↑ Summary blib/lib/App/GitHooks.pm »