File Coverage

blib/lib/App/GitHooks/Hook.pm
Criterion Covered Total %
statement 15 31 48.3
branch 0 4 0.0
condition n/a
subroutine 5 8 62.5
pod 1 1 100.0
total 21 44 47.7


line stmt bran cond sub pod time code
1             package App::GitHooks::Hook;
2              
3 5     5   17517 use strict;
  5         9  
  5         179  
4 5     5   22 use warnings;
  5         6  
  5         113  
5              
6             # External dependencies.
7 5     5   16 use Carp;
  5         5  
  5         215  
8 5     5   2470 use Try::Tiny;
  5         5683  
  5         264  
9              
10             # Internal dependencies.
11 5     5   1447 use App::GitHooks::Constants qw( :HOOK_EXIT_CODES :PLUGIN_RETURN_CODES );
  5         7  
  5         1770  
12              
13              
14             =head1 NAME
15              
16             App::GitHooks::Hook - Base class for all git hook handlers.
17              
18              
19             =head1 VERSION
20              
21             Version 1.7.3
22              
23             =cut
24              
25             our $VERSION = '1.7.3';
26              
27              
28             =head1 METHODS
29              
30             =head2 run()
31              
32             Run the hook handler and return an exit status to pass to git.
33              
34             my $exit_status = App::GitHooks::Hook->run(
35             app => $app,
36             );
37              
38             Arguments:
39              
40             =over 4
41              
42             =item * app I<(mandatory)>
43              
44             An L object.
45              
46             =item * stdin I<(optional)>
47              
48             An arrayref of lines retrieved from SDTIN.
49              
50             See for example the C hook for uses of this argument.
51              
52             =back
53              
54             =cut
55              
56             sub run
57             {
58 0     0 1   my ( $class, %args ) = @_;
59 0           my $app = $args{'app'};
60 0           my $stdin = $args{'stdin'};
61              
62             # Find all the plugins that are applicable for this hook.
63 0           my $plugins = $app->get_hook_plugins( $app->get_hook_name() );
64              
65             # Run all the plugins.
66 0           my $has_errors = 0;
67 0           foreach my $plugin ( @$plugins )
68             {
69             # Since Perl doesn't allow dashes in method names but git hook names have
70             # dashes, we need to make sure we convert dashes to underscores when
71             # generating the method name to run.
72 0           my $method = 'run_' . $app->get_hook_name();
73 0           $method =~ s/-/_/g;
74              
75             # Run the plugin method corresponding to this hook.
76             # If the plugin throws an exception, print the error message and consider
77             # the return code to be a failure.
78             my $return_code = try
79             {
80 0     0     return $plugin->$method(
81             app => $app,
82             stdin => $stdin,
83             );
84             }
85             catch
86             {
87 0     0     chomp( $_ );
88 0           my $failure_character = $app->get_failure_character();
89 0           print $app->color( 'red', "$failure_character $_\n" );
90 0           return $PLUGIN_RETURN_FAILED;
91 0           };
92              
93 0 0         $has_errors = 1
94             if $return_code == $PLUGIN_RETURN_FAILED;
95             }
96              
97             # Return an exit code for Git.
98 0 0         return $has_errors
99             ? $HOOK_EXIT_FAILURE
100             : $HOOK_EXIT_SUCCESS;
101             }
102              
103              
104             =head1 BUGS
105              
106             Please report any bugs or feature requests through the web interface at
107             L.
108             I will be notified, and then you'll automatically be notified of progress on
109             your bug as I make changes.
110              
111              
112             =head1 SUPPORT
113              
114             You can find documentation for this module with the perldoc command.
115              
116             perldoc App::GitHooks::Hook
117              
118              
119             You can also look for information at:
120              
121             =over
122              
123             =item * GitHub's request tracker
124              
125             L
126              
127             =item * AnnoCPAN: Annotated CPAN documentation
128              
129             L
130              
131             =item * CPAN Ratings
132              
133             L
134              
135             =item * MetaCPAN
136              
137             L
138              
139             =back
140              
141              
142             =head1 AUTHOR
143              
144             L,
145             C<< >>.
146              
147              
148             =head1 COPYRIGHT & LICENSE
149              
150             Copyright 2013-2015 Guillaume Aubert.
151              
152             This program is free software: you can redistribute it and/or modify it under
153             the terms of the GNU General Public License version 3 as published by the Free
154             Software Foundation.
155              
156             This program is distributed in the hope that it will be useful, but WITHOUT ANY
157             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
158             PARTICULAR PURPOSE. See the GNU General Public License for more details.
159              
160             You should have received a copy of the GNU General Public License along with
161             this program. If not, see http://www.gnu.org/licenses/
162              
163             =cut
164              
165             1;