blib/lib/Git/Hooks.pm

filter: Statementstmt 45.4% 65 / 143 Branchbran 0.0% 0 / 46 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 70.0% 14 / 20 Podpod 100% 5 / 5 totaltotal 36.2% 87 / 240 CC 39 SCAR 37.5
CC
39
Cov
33%
run_hook
132.0
__ANON__
42.0
__ANON__
42.0
CRAP 42.5
« blib/lib/Git/Hooks/Notify.pm ↑ Summary blib/lib/Git/Hooks/CheckCommit.pm »
1 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks;
4 # ABSTRACT: Framework for implementing Git (and Gerrit) hooks
5 2 $Git::Hooks::VERSION = '3.5.0';
6 2 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Git::Repository qw/GitHooks Log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our @EXPORT; ## no critic (Modules::ProhibitAutomaticExportation)
15  
16 2 my %Hooks;
17  
18 BEGIN { ## no critic (RequireArgUnpacking)
19 2 ▶ my @directives =
Subroutine BEGIN: called CC=2 SCAR=6.9
20 qw/ APPLYPATCH_MSG PRE_APPLYPATCH POST_APPLYPATCH
21 PRE_COMMIT PREPARE_COMMIT_MSG COMMIT_MSG
22 POST_COMMIT PRE_REBASE POST_CHECKOUT POST_MERGE
23 PRE_PUSH PRE_RECEIVE UPDATE POST_RECEIVE POST_UPDATE
24 PUSH_TO_CHECKOUT PRE_AUTO_GC POST_REWRITE
25  
26 REF_UPDATE PATCHSET_CREATED DRAFT_PUBLISHED
27 COMMIT_RECEIVED SUBMIT
28 /;
29  
30 2 my @drivers =
31 qw/ GITHOOKS_CHECK_AFFECTED_REFS
32 GITHOOKS_CHECK_PRE_COMMIT
33 GITHOOKS_CHECK_PATCHSET
34 GITHOOKS_CHECK_MESSAGE_FILE
35 /;
36  
37 2 for my $directive (@directives) {
38 46 my $hook = $directive;
39 46 $hook =~ tr/A-Z_/a-z-/;
40 2 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
41 46 *{"Git::Hooks::$directive"} = sub (&) {
42 2 ▶ push @{$Hooks{$hook}}, {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 package => scalar(caller),
44 sub => shift(@_),
45 };
46 }
47 46 }
48  
49 2 @EXPORT = (@directives, @drivers, 'run_hook');
50 }
51  
52 sub GITHOOKS_CHECK_AFFECTED_REFS (&;$) {
53 1 ▶ my ($check_ref, $options) = @_;
Subroutine GITHOOKS_CHECK_AFFECTED_REFS: called CC=3 SCAR=17.6
Pod documented
54 1 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
10
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
55 1 my $caller = caller;
56  
57 my $hook = {
58 package => $caller,
59 sub => sub {
60 0 ▶ my ($git) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
61  
62 0 $log->debug("$caller(GITHOOKS_CHECK_AFFECTED_REFS)");
63  
64 0 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 0 0 0
65  
66 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
67  
68 0 my $errors = 0;
69  
70 0 foreach my $ref ($git->get_affected_refs()) {
71 0 ▶ next unless $git->is_reference_enabled($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($ref) 0 0 0
72 0 $errors += $check_ref->($git, $ref);
73 }
74  
75 0 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 0 0 0
76  
77 0 return $errors == 0;
78 },
79 1 };
80  
81 1 foreach my $name (qw/commit-received pre-receive ref-update submit update/) {
82 5 push @{$Hooks{$name}}, $hook;
83 }
84  
85 1 return;
86 }
87  
88 sub GITHOOKS_CHECK_PRE_COMMIT (&;$) {
89 0 ▶ my ($check_commit, $options) = @_;
Subroutine GITHOOKS_CHECK_PRE_COMMIT: not called CC=3 SCAR=24.8
Pod documented
90 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
91 0 my $caller = caller;
92  
93 my $hook = {
94 package => $caller,
95 sub => sub {
96 0 ▶ my ($git) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
97  
98 0 $log->debug("$caller(GITHOOKS_CHECK_COMMIT)");
99  
100 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
101  
102 0 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 0 0 0
103  
104 0 my $current_branch = $git->get_current_branch();
105  
106 0 ▶ return 1 unless $git->is_reference_enabled($current_branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($current_branch) 0 0 0
107  
108 0 my $errors = $check_commit->($git, $current_branch);
109  
110 0 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 0 0 0
111  
112 0 return $errors == 0;
113 },
114 0 };
115  
116 0 foreach my $name (qw/pre-applypatch pre-commit/) {
117 0 push @{$Hooks{$name}}, $hook;
118 }
119  
120 0 return;
121 }
122  
123 sub GITHOOKS_CHECK_PATCHSET (&;$) {
124 1 ▶ my ($check_patchset, $options) = @_;
Subroutine GITHOOKS_CHECK_PATCHSET: called CC=3 SCAR=18.5
Pod documented
125 1 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
10
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
126 1 my $caller = caller;
127  
128 my $hook = {
129 package => $caller,
130 sub => sub {
131 0 ▶ my ($git, $opts) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
132  
133 0 $log->debug("$caller(GITHOOKS_CHECK_PATCHSET)");
134  
135 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
136  
137 0 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 0 0 0
138  
139 0 my $sha1 = $opts->{'--commit'};
140 0 my $commit = $git->get_commit($sha1);
141  
142 # The --branch argument contains the branch short-name if it's in the
143 # refs/heads/ namespace. But we need to always use the branch long-name,
144 # so we change it here.
145 0 my $branch = $opts->{'--branch'};
146 0 ▶ $branch = "refs/heads/$branch"
Branch: 1 branch
BranchTrueFalseTotal
unless $branch =~ m[^refs/]u 0 0 0
147 unless $branch =~ m:^refs/:;
148  
149 0 ▶ return 1 unless $git->is_reference_enabled($branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($branch) 0 0 0
150  
151 0 my $errors = $check_patchset->($git, $branch, $commit);
152  
153 0 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 0 0 0
154  
155 0 return $errors == 0;
156 },
157 1 };
158  
159 1 foreach my $name (qw/draft-published patchset-created/) {
160 2 push @{$Hooks{$name}}, $hook;
161 }
162  
163 1 return;
164 }
165  
166 sub GITHOOKS_CHECK_MESSAGE_FILE (&;$) {
167 1 ▶ my ($check_message_file, $options) = @_;
Subroutine GITHOOKS_CHECK_MESSAGE_FILE: called CC=3 SCAR=18.4
Pod documented
168 1 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
10
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
169 1 my $caller = caller;
170 1 (my $cfg = $caller) =~ s/.*::/githooks./;
171  
172 my $hook = {
173 package => $caller,
174 sub => sub {
175 0 ▶ my ($git, $commit_msg_file) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
176  
177 0 $log->debug("$caller(GITHOOKS_CHECK_MESSAGE_FILE)");
178  
179 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
180  
181 0 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 0 0 0
182  
183 0 my $current_branch = $git->get_current_branch();
184  
185 0 ▶ return 1 unless $git->is_reference_enabled($current_branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($current_branch) 0 0 0
186  
187 0 my $msg = eval {$git->read_commit_msg_file($commit_msg_file)};
188  
189 0 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
190 0 $git->fault(<<"EOS", {details => $@});
191 I cannot read the commit message file '$commit_msg_file'.
192 EOS
193 0 return 0;
194 }
195  
196 0 my $errors = $check_message_file->($git, $msg, $current_branch);
197  
198 0 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 0 0 0
199  
200 0 return $errors == 0;
201 },
202 1 };
203  
204 1 foreach my $name (qw/applypatch-msg commit-msg/) {
205 2 push @{$Hooks{$name}}, $hook;
206 }
207  
208 1 return;
209 }
210  
211 # This is the main routine of Git::Hooks. It gets the original hook
212 # name and arguments, sets up the environment, loads plugins and
213 # invokes the appropriate hook functions.
214  
215 sub run_hook {
216 0 ▶ my ($hook_name, @args) = @_;
Subroutine run_hook: not called CC=11 SCAR=48.8
Pod documented
217  
218 0 my $hook_basename = path($hook_name)->basename;
219  
220 # Contextualize the logs with the PID on server hooks. However, note that
221 # the Log::Any::context method was implemented on Log::Any version 1.050.
222 0 ▶ $log->context->{pid} = $$
Branch: 1 branch
BranchTrueFalseTotal
if $hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/u and $Git::Hooks::log->can("context") 0 0 0
Condition0%0/3
$hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/u and $Git::Hooks::log->can("context")
!ll&&!rl&&r
000
MC/DC0%0/2
$hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/u and $Git::Hooks::log->can("context")
$hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/u $Git::Hooks::log->can("context")
Truth table0%0/3
A and B
A= $hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/uB= $Git::Hooks::log->can("context")
ABresult
0X0
100
111
223 if $hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/
224 && $log->can('context');
225  
226 0 $log->info("run_hook($hook_basename)", {args => \@args});
227  
228 0 my $git = Git::Repository->new();
229  
230 0 local $ENV{GITHOOKS_AUTHENTICATED_USER} = $git->authenticated_user();
231  
232 0 $git->prepare_hook($hook_name, \@args);
233  
234 0 $git->load_plugins();
235  
236 # Call every hook function installed by the hook scripts before.
237 0 for my $hook (@{$Hooks{$hook_basename}}) {
238 0 my $ok = eval { $hook->{sub}->($git, @args) };
239 0 ▶ if (defined $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ok) { } 0 0 0
elsif (length $@) { } 0 0 0
240 # Modern hooks return a boolean value indicating their success.
241 # If they fail they invoke
242 # Git::Repository::Plugin::GitHooks::fault.
243 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
244 # Let's see if there is a help-on-error message configured
245 # specifically for this plugin.
246 0 my $CFG = $hook->{package} =~ s/.*::/githooks./r;
247 0 ▶ if (my $help = $git->get_config(lc $CFG => 'help-on-error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $help = $git->get_config(lc $CFG, "help-on-error")) 0 0 0
248 0 $git->fault($help, {prefix => $hook->{package}});
249 }
250 }
251 } elsif (length $@) {
252 # Old hooks die when they fail...
253 0 $git->fault("Hook failed", {
254 prefix => __PACKAGE__ . "($hook_basename)",
255 details => $@,
256 });
257 } else {
258 # ...and return undef when they succeed.
259 }
260 } continue {
261 0 $git->check_timeout();
262 }
263  
264 # Invoke enabled external hooks. This doesn't work in Windows yet.
265 0 $git->invoke_external_hooks(@args);
266  
267 # Some hooks want to do some post-processing
268 0 foreach my $post_hook ($git->post_hooks) {
269 0 $post_hook->($hook_basename, $git, @args);
270 } continue {
271 0 $git->check_timeout();
272 }
273  
274 0 ▶ $git->fail_on_faults(
Condition0%0/6
($hook_basename eq 'commit-msg' || $hook_basename eq 'pre-commit') && !$git->get_config_boolean('githooks', 'abort-commit')
!ll&&!rl&&r
000
$hook_basename eq "commit-msg" or $hook_basename eq "pre-commit"
l!l&&r!l&&!r
000
MC/DC0%0/3
($hook_basename eq 'commit-msg' || $hook_basename eq 'pre-commit') && !$git->get_config_boolean('githooks', 'abort-commit')
$hook_basename eq "commit-msg" $hook_basename eq "pre-commit" !$git->get_config_boolean('githooks', 'abort-commit')
Truth table0%0/5
A or B && not(C)
A= $hook_basename eq "commit-msg"B= $hook_basename eq "pre-commit"C= !$git->get_config_boolean('githooks', 'abort-commit')
ABCresult
00X0
1X00
0100
1X11
0111
275 ($hook_basename eq 'commit-msg' or $hook_basename eq 'pre-commit')
276 and not $git->get_config_boolean(githooks => 'abort-commit')
277 );
278  
279 0 return;
280 }
281  
282
283 2 1; # End of Git::Hooks
284  
285 __END__
« blib/lib/Git/Hooks/Notify.pm ↑ Summary blib/lib/Git/Hooks/CheckCommit.pm »