File Coverage

blib/lib/App/GitHooks/Hook.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package App::GitHooks::Hook;
2              
3 23     23   29532 use strict;
  23         36  
  23         654  
4 23     23   93 use warnings;
  23         31  
  23         591  
5              
6             # External dependencies.
7 23     23   128 use Carp;
  23         32  
  23         1669  
8 23     23   3277 use Try::Tiny;
  23         12942  
  23         1364  
9              
10             # Internal dependencies.
11 23     23   2144 use App::GitHooks::Constants qw( :HOOK_EXIT_CODES :PLUGIN_RETURN_CODES );
  23         41  
  23         10892  
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.9.0
22              
23             =cut
24              
25             our $VERSION = '1.9.0';
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<App::GitHooks> object.
45              
46             =item * stdin I<(optional)>
47              
48             An arrayref of lines retrieved from SDTIN.
49              
50             See for example the C<pre-push> hook for uses of this argument.
51              
52             =back
53              
54             =cut
55              
56             sub run
57             {
58 3     3 1 49 my ( $class, %args ) = @_;
59 3         5 my $app = $args{'app'};
60 3         6 my $stdin = $args{'stdin'};
61              
62             # Find all the plugins that are applicable for this hook.
63 3         14 my $plugins = $app->get_hook_plugins( $app->get_hook_name() );
64              
65             # Run all the plugins.
66 3         5 my $has_errors = 0;
67 3         7 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 2         9 my $method = 'run_' . $app->get_hook_name();
73 2         6 $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 2     2   76 return $plugin->$method(
81             app => $app,
82             stdin => $stdin,
83             );
84             }
85             catch
86             {
87 1     1   17 chomp( $_ );
88 1         14 my $failure_character = $app->get_failure_character();
89 1         6 print $app->color( 'red', "$failure_character $_\n" );
90 1         5 return $PLUGIN_RETURN_FAILED;
91 2         16 };
92              
93 2 100       22 $has_errors = 1
94             if $return_code == $PLUGIN_RETURN_FAILED;
95             }
96              
97             # Return an exit code for Git.
98 3 100       22 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<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
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<https://github.com/guillaumeaubert/App-GitHooks/issues>
126              
127             =item * AnnoCPAN: Annotated CPAN documentation
128              
129             L<http://annocpan.org/dist/app-githooks>
130              
131             =item * CPAN Ratings
132              
133             L<http://cpanratings.perl.org/d/app-githooks>
134              
135             =item * MetaCPAN
136              
137             L<https://metacpan.org/release/App-GitHooks>
138              
139             =back
140              
141              
142             =head1 AUTHOR
143              
144             L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
145             C<< <aubertg at cpan.org> >>.
146              
147              
148             =head1 COPYRIGHT & LICENSE
149              
150             Copyright 2013-2017 Guillaume Aubert.
151              
152             This code is free software; you can redistribute it and/or modify it under the
153             same terms as Perl 5 itself.
154              
155             This program is distributed in the hope that it will be useful, but WITHOUT ANY
156             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
157             PARTICULAR PURPOSE. See the LICENSE file for more details.
158              
159             =cut
160              
161             1;