blib/lib/Git/Hooks.pm

filter: Statementstmt 87.5% 120 / 137 Branchbran 58.7% 27 / 46 Conditioncond 82.3% 14 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 95.0% 19 / 20 Podpod 100% 5 / 5 totaltotal 80.7% 189 / 234 CC 39 SCAR 22.3
CC
39
Cov
80%
__ANON__
42.0
run_hook
11.5
__ANON__
6.3
CRAP 9.3
« blib/lib/Git/Hooks/CheckFile.pm ↑ Summary blib/lib/Git/Hooks/Notify.pm »
1 258 ▶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 258 $Git::Hooks::VERSION = '4.0.0';
6 258 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 258 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 258 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 258 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 258 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 258 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 258 ▶use Git::Repository qw/GitHooks Log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 258 our @EXPORT; ## no critic (Modules::ProhibitAutomaticExportation)
15  
16 258 my %Hooks;
17  
18 258 ▶BEGIN { ## no critic (RequireArgUnpacking)
Subroutine BEGIN: called CC=2 SCAR=6.9
19 258 my @directives =
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 258 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 258 for my $directive (@directives) {
38 5934 my $hook = $directive;
39 5934 $hook =~ tr/A-Z_/a-z-/;
40 258 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5934 ▶ *{"Git::Hooks::$directive"} = sub (&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 223 push $Hooks{$hook}->@*, {
43 package => scalar(caller),
44 sub => shift(@_),
45 };
46 }
47 5934 }
48  
49 258 @EXPORT = (@directives, @drivers, 'run_hook');
50 }
51  
52 174 ▶sub GITHOOKS_CHECK_AFFECTED_REFS (&;$) {
Subroutine GITHOOKS_CHECK_AFFECTED_REFS: called CC=3 SCAR=11.0
Pod documented
53 174 my ($check_ref, $options) = @_;
54 174 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
15123
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
55 174 my $caller = caller;
56  
57 my $hook = {
58 package => $caller,
59 47 ▶ sub => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
60 47 my ($git) = @_;
61  
62 47 $log->debug("$caller(GITHOOKS_CHECK_AFFECTED_REFS)");
63  
64 47 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 31 16 47
65  
66 47 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 47 47
67  
68 47 my $errors = 0;
69  
70 47 foreach my $ref ($git->get_affected_refs()) {
71 47 ▶ next unless $git->is_reference_enabled($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($ref) 0 47 47
72 47 $errors += $check_ref->($git, $ref);
73 }
74  
75 47 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 10 37 47
76  
77 47 return $errors == 0;
78 },
79 174 };
80  
81 174 foreach my $name (qw/commit-received pre-receive ref-update submit update/) {
82 870 push $Hooks{$name}->@*, $hook;
83 }
84  
85 174 return;
86 }
87  
88 91 ▶sub GITHOOKS_CHECK_PRE_COMMIT (&;$) {
Subroutine GITHOOKS_CHECK_PRE_COMMIT: called CC=3 SCAR=11.0
Pod documented
89 91 my ($check_commit, $options) = @_;
90 91 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
7615
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
91 91 my $caller = caller;
92  
93 my $hook = {
94 package => $caller,
95 62 ▶ sub => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
96 62 my ($git) = @_;
97  
98 62 $log->debug("$caller(GITHOOKS_CHECK_COMMIT)");
99  
100 62 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 62 62
101  
102 62 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 55 7 62
103  
104 62 my $current_branch = $git->get_current_branch();
105  
106 62 ▶ return 1 unless $git->is_reference_enabled($current_branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($current_branch) 2 60 62
107  
108 60 my $errors = $check_commit->($git, $current_branch);
109  
110 60 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 0 60 60
111  
112 60 return $errors == 0;
113 },
114 91 };
115  
116 91 foreach my $name (qw/pre-applypatch pre-commit/) {
117 182 push $Hooks{$name}->@*, $hook;
118 }
119  
120 91 return;
121 }
122  
123 166 ▶sub GITHOOKS_CHECK_PATCHSET (&;$) {
Subroutine GITHOOKS_CHECK_PATCHSET: called CC=3 SCAR=18.3
Pod documented
124 166 my ($check_patchset, $options) = @_;
125 166 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
15115
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
126 166 my $caller = caller;
127  
128 my $hook = {
129 package => $caller,
130 0 ▶ sub => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
131 0 my ($git, $opts) = @_;
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 166 };
158  
159 166 foreach my $name (qw/draft-published patchset-created/) {
160 332 push $Hooks{$name}->@*, $hook;
161 }
162  
163 166 return;
164 }
165  
166 75 ▶sub GITHOOKS_CHECK_MESSAGE_FILE (&;$) {
Subroutine GITHOOKS_CHECK_MESSAGE_FILE: called CC=3 SCAR=11.1
Pod documented
167 75 my ($check_message_file, $options) = @_;
168 75 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
750
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
169 75 my $caller = caller;
170 75 (my $cfg = $caller) =~ s/.*::/githooks./;
171  
172 my $hook = {
173 package => $caller,
174 64 ▶ sub => sub {
Subroutine __ANON__: called CC=6 SCAR=18.4
175 64 my ($git, $commit_msg_file) = @_;
176  
177 64 $log->debug("$caller(GITHOOKS_CHECK_MESSAGE_FILE)");
178  
179 64 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 64 64
180  
181 64 ▶ $options->{config}($git) if exists $options->{config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"config"} 64 0 64
182  
183 64 my $current_branch = $git->get_current_branch();
184  
185 64 ▶ return 1 unless $git->is_reference_enabled($current_branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($current_branch) 4 60 64
186  
187 60 my $msg = eval {$git->read_commit_msg_file($commit_msg_file)};
188  
189 60 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 60 60
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 60 my $errors = $check_message_file->($git, $msg, $current_branch);
197  
198 58 ▶ $options->{destroy}($git) if exists $options->{destroy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"destroy"} 29 29 58
199  
200 58 return $errors == 0;
201 },
202 75 };
203  
204 75 foreach my $name (qw/applypatch-msg commit-msg/) {
205 150 push $Hooks{$name}->@*, $hook;
206 }
207  
208 75 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 256 ▶sub run_hook {
Subroutine run_hook: called CC=11 SCAR=24.4
Pod documented
216 256 my ($hook_name, @args) = @_;
217  
218 256 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 256 ▶ $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") 58 198 256
Condition66%2/3
$hook_basename =~ /^(?:(pre|post)?-receive|(post-)?update|push-to-checkout)$/u and $Git::Hooks::log->can("context")
!ll&&!rl&&r
198058
MC/DC50%1/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 table66%2/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 256 $log->info("run_hook($hook_basename)", {args => \@args});
227  
228 256 my $git = Git::Repository->new();
229  
230 256 local $ENV{GITHOOKS_AUTHENTICATED_USER} = $git->authenticated_user();
231  
232 256 $git->prepare_hook($hook_name, \@args);
233  
234 256 $git->load_plugins();
235  
236 # Call every hook function installed by the hook scripts before.
237 256 for my $hook ($Hooks{$hook_basename}->@*) {
238 229 my $ok = eval { $hook->{sub}->($git, @args) };
239 229 ▶ if (defined $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ok) { } 224 5 229
elsif (length $@) { } 5 0 5
240 # Modern hooks return a boolean value indicating their success.
241 # If they fail they invoke
242 # Git::Repository::Plugin::GitHooks::fault.
243 224 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 92 132 224
244 # Let's see if there is a help-on-error message configured
245 # specifically for this plugin.
246 92 my $CFG = $hook->{package} =~ s/.*::/githooks./r;
247 92 ▶ 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 92 92
248 0 $git->fault($help, {prefix => $hook->{package}});
249 }
250 }
251 } elsif (length $@) {
252 # Old hooks die when they fail...
253 5 $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 229 $git->check_timeout();
262 }
263  
264 # Invoke enabled external hooks. This doesn't work in Windows yet.
265 256 $git->invoke_external_hooks(@args);
266  
267 # Some hooks want to do some post-processing
268 256 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 256 ▶ $git->fail_on_faults(
Condition83%5/6
($hook_basename eq 'commit-msg' || $hook_basename eq 'pre-commit') && !$git->get_config_boolean('githooks', 'abort-commit')
!ll&&!rl&&r
791770
$hook_basename eq "commit-msg" or $hook_basename eq "pre-commit"
l!l&&r!l&&!r
898879
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 table60%3/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 174 return;
280 }
281  
282
283 258 1; # End of Git::Hooks
284  
285 __END__
« blib/lib/Git/Hooks/CheckFile.pm ↑ Summary blib/lib/Git/Hooks/Notify.pm »