lib/Git/Repository/Plugin/GitHooks.pm

filter: Statementstmt 60.6% 386 / 637 Branchbran 44.8% 167 / 372 Conditioncond 33.7% 29 / 86 MC/DCmcdc 11.3% 5 / 44 Subroutinesub 76.1% 51 / 67 Podpod 100% 40 / 40 totaltotal 54.4% 678 / 1246 CC 234 SCAR 37.8
CC
234
Cov
53%
_gerrit_patchset_post_hook
210.0
_prepare_gerrit_args
156.0
_invoke_external_hook
153.9
CRAP 43.7
« blib/lib/Git/Hooks/CheckDiff.pm ↑ Summary blib/lib/Git/Hooks/CheckJira.pm »
1 271 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Repository::Plugin::GitHooks;
4 # ABSTRACT: A Git::Repository plugin with some goodies for hook developers
5 271 $Git::Repository::Plugin::GitHooks::VERSION = '4.0.0';
6 271 ▶use parent qw/Git::Repository::Plugin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 271 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 271 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 271 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 271 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 271 ▶use IO::Interactive 'is_interactive';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 271 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 273 ▶sub _keywords { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _keywords: called CC=1 SCAR=0.0
16  
17 return
18 273 qw/
19 prepare_hook load_plugins invoke_external_hooks
20  
21 post_hook post_hooks
22  
23 cache
24  
25 get_config get_config_boolean get_config_integer
26  
27 check_timeout
28  
29 fault get_faults fail_on_faults
30  
31 undef_commit empty_tree get_commit get_commits
32  
33 read_commit_msg_file write_commit_msg_file
34  
35 get_affected_refs get_affected_ref_range get_affected_ref_commits
36  
37 filter_name_status_in_index filter_name_status_in_range filter_name_status_in_commit
38  
39 filter_files_in_index filter_files_in_range filter_files_in_commit
40  
41 authenticated_user repository_name
42  
43 get_current_branch get_sha1 get_head_or_empty_tree
44  
45 blob file_size file_mode
46  
47 is_reference_enabled match_user im_admin grok_acls
48 /;
49 }
50  
51 # This package variable tells get_config which character encoding is used in
52 # the output of the git-config command. Usually none, and decoding isn't
53 # necessary. But sometimes it is...
54 271 our $CONFIG_ENCODING = undef;
55  
56 ##############
57 # The following routines prepare the arguments for some hooks to make
58 # it easier to deal with them later on.
59  
60 # Some hooks get information from STDIN as text lines with
61 # space-separated fields. This routine reads up all of STDIN and tucks
62 # that information in the Git::Repository object.
63  
64 47 ▶sub _push_input_data {
Subroutine _push_input_data: called CC=1 SCAR=0.0
65 47 my ($git, $data) = @_;
66 47 push $git->{_plugin_githooks}{input_data}->@*, $data;
67 47 return;
68 }
69  
70 47 ▶sub _get_input_data {
Subroutine _get_input_data: called CC=2 SCAR=7.2
71 47 my ($git) = @_;
72 47 ▶ return $git->{_plugin_githooks}{input_data} || [];
Condition50%1/2
$git->{'_plugin_githooks'}{'input_data'} || []
l!l
470
MC/DC0%0/1
$git->{'_plugin_githooks'}{'input_data'} || []
$git->{'_plugin_githooks'}{'input_data'}
Truth table50%1/2
A
A= $git->{'_plugin_githooks'}{'input_data'}
Aresult
11
00
73 }
74  
75 47 ▶sub _prepare_input_data {
Subroutine _prepare_input_data: called CC=2 SCAR=6.9
76 47 my ($git) = @_;
77 47 ▶ while (<STDIN>) { ## no critic (InputOutput::ProhibitExplicitStdin)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 47 47 94
78 47 chomp;
79 47 _push_input_data($git, [split]);
80 }
81 47 my $input_data = _get_input_data($git);
82 47 $log->info(_prepare_input_data => {input_data => $input_data});
83 47 return $input_data;
84 }
85  
86 # The pre-receive and post-receive hooks get the list of affected
87 # commits via STDIN. This routine gets them all and set all affected
88 # refs in the Git object.
89  
90 47 ▶sub _prepare_receive {
Subroutine _prepare_receive: called CC=2 SCAR=6.9
91 47 my ($git) = @_;
92 47 foreach (_prepare_input_data($git)->@*) {
93 47 my ($old_commit, $new_commit, $ref) = @$_;
94 47 _set_affected_ref($git, $ref, $old_commit, $new_commit);
95 }
96 47 return;
97 }
98  
99 # The update hook get three arguments telling which reference is being
100 # updated, from which commit, to which commit. Here we use these
101 # arguments to set the affected ref in the Git object.
102  
103 11 ▶sub _prepare_update {
Subroutine _prepare_update: called CC=1 SCAR=0.0
104 11 my ($git, $args) = @_;
105 11 _set_affected_ref($git, @$args);
106 11 $log->debug(_prepare_update => {affected_refs => _get_affected_refs_hash($git)});
107 11 return;
108 }
109  
110 # Gerrit hooks get a list of option/value pairs. Here we convert the list into a
111 # hash and change the original argument list into a single hash-ref. We also
112 # record information about the user performing the push. Based on:
113 # https://gerrit.googlesource.com/plugins/hooks/+/refs/heads/master/src/main/resources/Documentation/hooks.md
114  
115 0 ▶sub _prepare_gerrit_args {
Subroutine _prepare_gerrit_args: not called CC=12 SCAR=50.5
116 0 my ($git, $args) = @_;
117  
118 0 my %opt = @$args;
119  
120 # Each Gerrit hook receive the full name and email of the user
121 # performing the hooked operation via a specific option in the
122 # format "User Name (email@example.net)". Here we grok it.
123 my $user =
124 $opt{'--uploader'} ||
125 $opt{'--author'} ||
126 $opt{'--submitter'} ||
127 $opt{'--abandoner'} ||
128 $opt{'--restorer'} ||
129 0 ▶ $opt{'--reviewer'} ||
Condition0%0/17
$opt{'--uploader'} || $opt{'--author'} || $opt{'--submitter'} || $opt{'--abandoner'} || $opt{'--restorer'} || $opt{'--reviewer'} || undef
l!l
00
$opt{"--uploader"} or $opt{"--author"} or $opt{"--submitter"} or $opt{"--abandoner"} or $opt{"--restorer"} or $opt{"--reviewer"}
l!l&&r!l&&!r
000
$opt{"--uploader"} or $opt{"--author"} or $opt{"--submitter"} or $opt{"--abandoner"} or $opt{"--restorer"}
l!l&&r!l&&!r
000
$opt{"--uploader"} or $opt{"--author"} or $opt{"--submitter"} or $opt{"--abandoner"}
l!l&&r!l&&!r
000
$opt{"--uploader"} or $opt{"--author"} or $opt{"--submitter"}
l!l&&r!l&&!r
000
$opt{"--uploader"} or $opt{"--author"}
l!l&&r!l&&!r
000
MC/DC0%0/6
$opt{'--uploader'} || $opt{'--author'} || $opt{'--submitter'} || $opt{'--abandoner'} || $opt{'--restorer'} || $opt{'--reviewer'} || undef
$opt{"--uploader"} $opt{"--author"} $opt{"--submitter"} $opt{"--abandoner"} $opt{"--restorer"} $opt{"--reviewer"}
Truth table0%0/7
A or B or C or D or E or F
A= $opt{"--uploader"}B= $opt{"--author"}C= $opt{"--submitter"}D= $opt{"--abandoner"}E= $opt{"--restorer"}F= $opt{"--reviewer"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
130 undef;
131  
132 # Here we make the name and email available in two environment variables
133 # (GERRIT_USER_NAME and GERRIT_USER_EMAIL) so that
134 # Git::Repository::Plugin::GitHooks::authenticated_user can more easily
135 # grok the userid from them later.
136 0 ▶ if ($user && $user =~ /([^\(]+)\s+\(([^\)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and $user =~ /([^\(]+)\s+\(([^\)]+)\)/u) 0 0 0
Condition0%0/3
$user and $user =~ /([^\(]+)\s+\(([^\)]+)\)/u
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $user =~ /([^\(]+)\s+\(([^\)]+)\)/u
$user $user =~ /([^\(]+)\s+\(([^\)]+)\)/u
Truth table0%0/3
A and B
A= $userB= $user =~ /([^\(]+)\s+\(([^\)]+)\)/u
ABresult
0X0
100
111
137 0 $ENV{GERRIT_USER_NAME} = $1; ## no critic (Variables::RequireLocalizedPunctuationVars)
138 0 $ENV{GERRIT_USER_EMAIL} = $2; ## no critic (Variables::RequireLocalizedPunctuationVars)
139 }
140  
141 0 $log->debug(_prepare_gerrit_args => {opt => \%opt});
142  
143 # Now we create a Gerrit::REST object connected to the Gerrit
144 # server and tack it to the hook arguments so that Gerrit plugins
145 # can interact with it.
146  
147 # We 'require' the module instead of 'use' it because it's only
148 # used if one sets up Gerrit hooks, which may not be the most
149 # common usage of Git::Hooks.
150 0 ▶ eval {require Gerrit::REST}
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Gerrit::REST } } or croak(__PACKAGE__, ": Please, install the Gerrit::REST module to use Gerrit hooks.\n") 0 0 0
151 or croak __PACKAGE__, ": Please, install the Gerrit::REST module to use Gerrit hooks.\n";
152  
153 0 $opt{gerrit} = do {
154 0 my %info;
155 0 foreach my $arg (qw/url username password/) {
156 0 ▶ $info{$arg} = $git->get_config('githooks.gerrit' => $arg)
Branch: 1 branch
BranchTrueFalseTotal
$info{$arg} = $git->get_config("githooks.gerrit", $arg) or croak(__PACKAGE__, ": Missing githooks.gerrit.$arg configuration variable.\n") 0 0 0
157 or croak __PACKAGE__, ": Missing githooks.gerrit.$arg configuration variable.\n";
158 }
159  
160 0 Gerrit::REST->new(@info{qw/url username password/});
161 };
162  
163 0 @$args = (\%opt);
164  
165 0 $git->{_plugin_githooks}{gerrit_args} = \%opt;
166  
167 0 return;
168 }
169  
170 # The ref-update and the commit-received Gerrit hooks are invoked synchronously
171 # when a user pushes commits to a branch. So, they act much like Git's standard
172 # 'update' hook. This routine prepares the options as usual and sets the
173 # affected ref accordingly. The documented arguments for the hook are these:
174  
175 # ref-update --project <project name> --refname <refname> --uploader <uploader>
176 # --uploader-username <username> --oldrev <sha1> --newrev <sha1>
177  
178 # commit-received --project <project name> --refname <refname> --uploader
179 # <uploader> --uploader-username <username> --oldrev <sha1> --newrev <sha1>
180 # --cmdref <refname>
181  
182 0 ▶sub _prepare_gerrit_ref_update {
Subroutine _prepare_gerrit_ref_update: not called CC=2 SCAR=17.9
183 0 my ($git, $args) = @_;
184  
185 0 _prepare_gerrit_args($git, $args);
186  
187 # The --refname argument contains the branch short-name if it's in the
188 # refs/heads/ namespace. But we need to always use the branch long-name,
189 # so we change it here.
190 0 my $refname = $args->[0]{'--refname'};
191 0 ▶ $refname = "refs/heads/$refname"
Branch: 1 branch
BranchTrueFalseTotal
unless $refname =~ m[^refs/]u 0 0 0
192 unless $refname =~ m:^refs/:;
193  
194 0 _set_affected_ref($git, $refname, $args->[0]->%{qw/--oldrev --newrev/});
195 0 $log->debug(_prepare_gerrit_ref_update => {affected_refs => _get_affected_refs_hash($git)});
196 0 return;
197 }
198  
199 # The submit Gerrit hook is invoked synchronously when a user tries to submit a
200 # change. So, it acts much like Git's standard 'update' hook. This routine
201 # prepares the options as usual and sets the affected ref accordingly. The
202 # documented arguments for the hook are these:
203  
204 # submit --project <project name> --branch <branch> --submitter <submitter>
205 # --patchset <patchset id> --commit <sha1>
206  
207 0 ▶sub _prepare_gerrit_submit {
Subroutine _prepare_gerrit_submit: not called CC=2 SCAR=17.9
208 0 my ($git, $args) = @_;
209  
210 0 _prepare_gerrit_args($git, $args);
211  
212 # The --branch argument contains the branch short-name if it's in the
213 # refs/heads/ namespace. But we need to always use the branch long-name,
214 # so we change it here.
215 0 my $refname = $args->[0]{'--branch'};
216 0 ▶ $refname = "refs/heads/$refname"
Branch: 1 branch
BranchTrueFalseTotal
unless $refname =~ m[^refs/]u 0 0 0
217 unless $refname =~ m:^refs/:;
218  
219 0 my $parent = $git->get_sha1("$refname^");
220  
221 0 _set_affected_ref($git, $refname, $parent, $args->[0]{'--commit'});
222 0 $log->debug(_prepare_gerrit_submit => {affected_refs => _get_affected_refs_hash($git)});
223 0 return;
224 }
225  
226 # The following routine is the post_hook used by the Gerrit hooks
227 # patchset-created and draft-published. It basically casts a vote on the
228 # patchset based on the errors found during the hook processing.
229  
230 0 ▶sub _gerrit_patchset_post_hook {
Subroutine _gerrit_patchset_post_hook: not called CC=14 SCAR=53.5
231 0 my ($hook_name, $git, $args) = @_;
232  
233 0 for my $arg (qw/project branch change patchset/) {
234 0 ▶ exists $args->{"--$arg"}
Branch: 1 branch
BranchTrueFalseTotal
exists $$args{"--$arg"} or croak(__PACKAGE__, ": Missing --$arg argument to Gerrit's $hook_name hook.\n") 0 0 0
235 or croak __PACKAGE__, ": Missing --$arg argument to Gerrit's $hook_name hook.\n";
236 }
237  
238 # We have to use the most complete form of Gerrit change ids because
239 # it's the only unanbiguous one. Vide:
240 # https://gerrit.cpqd.com.br/Documentation/rest-api-changes.html#change-id.
241  
242 # Up to Gerrit 2.12 the argument --change passed the change's Change-Id
243 # code. So, we had to build the complete change id using the information
244 # passed on the arguments --project and --branch. From Gerrit 2.13 on
245 # the --change argument already contains the complete change id. So we
246 # have to figure out if we need to build it or not.
247  
248 # Also, for the old Gerrit we have to url-escape the change-id because
249 # the project name may contain slashes (and perhaps other reserved
250 # characters). This is possibly not a complete solution. Vide:
251 # http://mark.stosberg.com/blog/2010/12/percent-encoding-uris-in-perl.html.
252  
253 0 require URI::Escape;
254 my $id = $args->{'--change'} =~ /~/
255 ? $args->{'--change'}
256 0 ▶ : URI::Escape::uri_escape(join('~', $args->%{qw/--project --branch --change/}));
Branch: 1 branch
BranchTrueFalseTotal
$args->{'--change'} =~ /~/u ? : 0 0 0
257  
258 0 my $patchset = $args->{'--patchset'};
259  
260 # Grok all configuration options at once to make it easier to deal with them below.
261 0 my %cfg = (
262 'votes-to-approve' => $git->get_config('githooks.gerrit' => 'votes-to-approve'),
263 'votes-to-reject' => $git->get_config('githooks.gerrit' => 'votes-to-reject'),
264 'comment-ok' => $git->get_config('githooks.gerrit' => 'comment-ok'),
265 'auto-submit' => $git->get_config_boolean('githooks.gerrit' => 'auto-submit'),
266 );
267  
268 # https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#set-review
269 0 my %review_input = (tag => 'autogenerated:git-hooks');
270 0 my $auto_submit = 0;
271  
272 0 ▶ if (my $faults = $git->get_faults()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $faults = $git->get_faults) { } 0 0 0
273 0 ▶ $review_input{labels} = $cfg{'votes-to-reject'} || 'Code-Review-1';
Condition0%0/2
$cfg{'votes-to-reject'} || 'Code-Review-1'
l!l
00
MC/DC0%0/1
$cfg{'votes-to-reject'} || 'Code-Review-1'
$cfg{'votes-to-reject'}
Truth table0%0/2
A
A= $cfg{'votes-to-reject'}
Aresult
11
00
274  
275 # We have to truncate $faults down to a little less than 64kB because up
276 # to at least Gerrit 2.14.4 messages are saved in a MySQL column of type
277 # 'text', which has this limit.
278 0 ▶ if (length $faults > 65000) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $faults > 65000) 0 0 0
279 0 $faults = substr($faults, 0, 65000) . "...\n<truncated>\n";
280 }
281 0 $review_input{message} = $faults;
282 } else {
283 0 ▶ $review_input{labels} = $cfg{'votes-to-approve'} || 'Code-Review+1';
Condition0%0/2
$cfg{'votes-to-approve'} || 'Code-Review+1'
l!l
00
MC/DC0%0/1
$cfg{'votes-to-approve'} || 'Code-Review+1'
$cfg{'votes-to-approve'}
Truth table0%0/2
A
A= $cfg{'votes-to-approve'}
Aresult
11
00
284 $review_input{message} = "[Git::Hooks] $cfg{'comment-ok'}"
285 0 ▶ if $cfg{'comment-ok'};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg{"comment-ok"} 0 0 0
286 0 ▶ $auto_submit = 1 if $cfg{'auto-submit'};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg{"auto-submit"} 0 0 0
287 }
288  
289 # Convert, e.g., 'LabelA-1,LabelB+2' into { LabelA => '-1', LabelB => '+2' }
290 0 $review_input{labels} = { map {/^([-\w]+)([-+]\d+)$/i} split(',', $review_input{labels}) };
291  
292 0 ▶ if (my $notify = $git->get_config('githooks.gerrit' => 'notify')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $notify = $git->get_config("githooks.gerrit", "notify")) 0 0 0
293 0 $review_input{notify} = $notify;
294 }
295  
296 0 $log->debug(_gerrit_patchset_post_hook => {
297 review_input => \%review_input,
298 auto_submit => $auto_submit,
299 });
300  
301 # Cast review
302 0 ▶ eval { $args->{gerrit}->POST("/changes/$id/revisions/$patchset/review", \%review_input) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $args->{"gerrit"}->POST("/changes/$id/revisions/$patchset/review", \%review_input) } } or croak('Git::Repository::Plugin::GitHooks' . ": error in Gerrit::REST::POST(/changes/$id/revisions/$patchset/review): $@\n") 0 0 0
303 or croak __PACKAGE__ . ": error in Gerrit::REST::POST(/changes/$id/revisions/$patchset/review): $@\n";
304  
305 # Auto submit if requested and passed verification
306 0 ▶ if ($auto_submit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_submit) 0 0 0
307 0 ▶ eval { $args->{gerrit}->POST("/changes/$id/submit", {wait_for_merge => 'true'}) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $args->{"gerrit"}->POST("/changes/$id/submit", {"wait_for_merge", "true"}) } } or croak('Git::Repository::Plugin::GitHooks' . ": I couldn't submit the change. Perhaps you have to rebase it manually to resolve a conflict. Please go to its web page to check it out. The error message follows: $@\n") 0 0 0
308 or croak __PACKAGE__ . ": I couldn't submit the change. Perhaps you have to rebase it manually to resolve a conflict. Please go to its web page to check it out. The error message follows: $@\n";
309 }
310  
311 0 return;
312 }
313  
314 # Gerrit's patchset-created hook is invoked when a commit is pushed to a
315 # refs/for/* branch for revision. It's invoked asynchronously, i.e., it
316 # can't stop the push to happen. Instead, if it detects any problem, we must
317 # reject the commit via Gerrit's own revision process. So, we prepare a post
318 # hook action in which we see if there were errors that should be signaled
319 # via a code review action. Note, however, that draft changes can only be
320 # accessed by their respective owners and usually can't be voted on by the
321 # hook. So, draft changes aren't voted on and we exit the hook prematurely.
322 # The arguments for the hook are these:
323  
324 # patchset-created --change <change id> --is-draft <boolean> \
325 # --kind <change kind> --change-url <change url> \
326 # --change-owner <change owner> --project <project name> \
327 # --branch <branch> --topic <topic> --uploader <uploader>
328 # --commit <sha1> --patchset <patchset id>
329  
330 # Gerrit's draft-published hook is invoked when a draft change is
331 # published. In this state they're are visible by the hook and can be voted
332 # on. The arguments for the hook are these:
333  
334 # draft-published --change <change id> --change-url <change url> \
335 # --change-owner <change owner> --project <project name> \
336 # --branch <branch> --topic <topic> --uploader <uploader> \
337 # --commit <sha1> --patchset <patchset id>
338  
339 0 ▶sub _prepare_gerrit_patchset {
Subroutine _prepare_gerrit_patchset: not called CC=3 SCAR=24.8
340 0 my ($git, $args) = @_;
341  
342 0 _prepare_gerrit_args($git, $args);
343  
344 0 ▶ exit(0) if exists $args->[0]{'--is-draft'} and $args->[0]{'--is-draft'} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->[0]{"--is-draft"} and $args->[0]{"--is-draft"} eq "true" 0 0 0
Condition0%0/3
exists $args->[0]{"--is-draft"} and $args->[0]{"--is-draft"} eq "true"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args->[0]{"--is-draft"} and $args->[0]{"--is-draft"} eq "true"
exists $args->[0]{"--is-draft"} $args->[0]{"--is-draft"} eq "true"
Truth table0%0/3
A and B
A= exists $args->[0]{"--is-draft"}B= $args->[0]{"--is-draft"} eq "true"
ABresult
0X0
100
111
345  
346 0 $git->post_hook(\&_gerrit_patchset_post_hook);
347  
348 0 return;
349 }
350  
351 # The %prepare_hook hash maps hook names to the routine that must be
352 # invoked in order to "prepare" their arguments.
353  
354 271 my %prepare_hook = (
355 'update' => \&_prepare_update,
356 'pre-push' => \&_prepare_input_data,
357 'post-rewrite' => \&_prepare_input_data,
358 'pre-receive' => \&_prepare_receive,
359 'post-receive' => \&_prepare_receive,
360 'ref-update' => \&_prepare_gerrit_ref_update,
361 'commit-received' => \&_prepare_gerrit_ref_update,
362 'submit' => \&_prepare_gerrit_submit,
363 'patchset-created' => \&_prepare_gerrit_patchset,
364 'draft-published' => \&_prepare_gerrit_patchset,
365 );
366  
367 256 ▶sub prepare_hook {
Subroutine prepare_hook: called CC=2 SCAR=6.9
Pod documented
368 256 my ($git, $hook_name, $args) = @_;
369  
370 256 $git->{_plugin_githooks}{arguments} = $args;
371 256 my $basename = path($hook_name)->basename;
372 256 $git->{_plugin_githooks}{hookname} = $basename;
373  
374 # Some hooks need some argument munging before we invoke them
375 256 ▶ if (my $prepare = $prepare_hook{$basename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prepare = $prepare_hook{$basename}) 58 198 256
376 58 $prepare->($git, $args);
377 }
378  
379 256 return $basename;
380 }
381  
382 256 ▶sub load_plugins {
Subroutine load_plugins: called CC=13 SCAR=30.0
Pod documented
383 256 my ($git) = @_;
384  
385 256 my %plugins;
386  
387 256 foreach my $plugin (map {split} $git->get_config(githooks => 'plugin')) {
388 226 my ($negation, $prefix, $basename) = ($plugin =~ /^(\!?)((?:.+::)?)(.+)/);
389  
390 226 ▶ if (exists $ENV{$basename} && ! $ENV{$basename}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{$basename} and not $ENV{$basename}) { } 0 226 226
elsif ($negation) { } 0 226 226
Condition33%1/3
exists $ENV{$basename} and not $ENV{$basename}
!ll&&!rl&&r
22600
MC/DC0%0/2
exists $ENV{$basename} and not $ENV{$basename}
exists $ENV{$basename} not $ENV{$basename}
Truth table33%1/3
A and not(B)
A= exists $ENV{$basename}B= not $ENV{$basename}
ABresult
0X0
100
111
391 0 delete @plugins{$basename, "$prefix$basename"};
392 } elsif ($negation) {
393 0 delete $plugins{"$prefix$basename"};
394 } else {
395 226 $plugins{"$prefix$basename"} = [$prefix, $basename];
396 }
397 }
398  
399 256 ▶ return unless %plugins; # no one configured
Branch: 1 branch
BranchTrueFalseTotal
unless %plugins 30 226 256
400  
401 # Define the list of directories where we'll look for the hook
402 # plugins. First the local directory 'githooks' under the
403 # repository path, then the optional list of directories
404 # specified by the githooks.plugins config option, and,
405 # finally, the Git::Hooks standard hooks directory.
406 452 my @plugin_dirs = grep {-d} (
407 'githooks',
408 $git->get_config(githooks => 'plugins'),
409 226 path($INC{'Git/Hooks.pm'})->parent->child('Hooks'),
410 );
411  
412 226 $log->debug(load_plugins => {plugins => \%plugins, plugin_dirs => \@plugin_dirs});
413  
414 # Load remaining enabled plugins
415 226 ▶ while (my ($key, $plugin) = each %plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %plugins) 226 226 452
416 226 my ($prefix, $basename) = @$plugin;
417 226 my $exit = do {
418 226 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) { } 0 226 226
419 # It must be a module name
420 ## no critic (ProhibitStringyEval, RequireCheckingReturnValueOfEval)
421 0 eval "require $prefix$basename";
422 ## use critic
423 } else {
424 # Otherwise, it's a basename we must look for in @plugin_dirs
425 226 ▶ $basename .= '.pm' unless $basename =~ /\.p[lm]$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $basename =~ /\.p[lm]$/iu 226 0 226
426 226 my @scripts = grep {!-d} map {path($_)->child($basename)} @plugin_dirs;
427 226 ▶ $basename = shift @scripts
Branch: 1 branch
BranchTrueFalseTotal
$basename = shift @scripts or croak(__PACKAGE__, ": can't find enabled hook $basename.\n") 226 0 226
428 or croak __PACKAGE__, ": can't find enabled hook $basename.\n";
429 226 do $basename;
430 }
431 };
432 226 ▶ unless ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exit) 0 226 226
433 0 ▶ croak __PACKAGE__, ": couldn't parse $basename: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
434 0 ▶ croak __PACKAGE__, ": couldn't do $basename: $!\n" unless defined $exit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exit 0 0 0
435 0 croak __PACKAGE__, ": couldn't run $basename\n";
436 }
437 }
438  
439 226 return;
440 }
441  
442 2 ▶sub _invoke_external_hook { ## no critic (ProhibitExcessComplexity)
Subroutine _invoke_external_hook: called CC=24 SCAR=50.4
443 2 my ($git, $file, $hook, @args) = @_;
444  
445 2 my $prefix = '[' . __PACKAGE__ . '(' . path($file)->basename . ')]';
446  
447 2 my $tempfile = Path::Tiny->tempfile(UNLINK => 1);
448  
449 ## no critic (RequireBriefOpen, RequireCarping)
450 2 ▶ open(my $oldout, '>&', \*STDOUT) or croak "Can't dup STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $oldout, ">&", \*STDOUT or croak("Can't dup STDOUT: $!") 2 0 2
451 2 ▶ open(STDOUT , '>' , $tempfile) or croak "Can't redirect STDOUT to \$tempfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $tempfile or croak("Can't redirect STDOUT to \$tempfile: $!") 2 0 2
452 2 ▶ open(my $olderr, '>&', \*STDERR) or croak "Can't dup STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $olderr, ">&", \*STDERR or croak("Can't dup STDERR: $!") 2 0 2
453 2 ▶ open(STDERR , '>&', \*STDOUT) or croak "Can't dup STDOUT for STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*STDOUT or croak("Can't dup STDOUT for STDERR: $!") 2 0 2
454 ## use critic
455  
456 2 ▶ if ($hook =~ /^(?:pre-receive|post-receive|pre-push|post-rewrite)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook =~ /^(?:pre-receive|post-receive|pre-push|post-rewrite)$/u) { } 0 2 2
457  
458 # These hooks receive information via STDIN that we read once
459 # before invoking any hook. Now, we must regenerate the same
460 # information and output it to the external hooks we invoke.
461  
462 0 my $pid = open my $pipe, '|-'; ## no critic (InputOutput::RequireBriefOpen)
463  
464 0 ▶ if (! defined $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif ($pid) { } 0 0 0
465 0 $git->fault("I can't fork: $!", {prefix => $prefix});
466 } elsif ($pid) {
467 # parent
468 0 $pipe->print(join("\n", map {join(' ', @$_)} _get_input_data($git)->@*) . "\n");
469 0 my $exit = $pipe->close;
470  
471 ## no critic (RequireBriefOpen, RequireCarping)
472 0 ▶ open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or croak("Can't dup \$oldout: $!") 0 0 0
473 0 ▶ open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or croak("Can't dup \$olderr: $!") 0 0 0
474 ## use critic
475  
476 0 my $output = $tempfile->slurp;
477 0 ▶ if ($exit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit) { } 0 0 0
elsif ($!) { } 0 0 0
478 0 ▶ say STDERR $output if length $output;
Branch: 1 branch
BranchTrueFalseTotal
if length $output 0 0 0
479 0 return 1;
480 } elsif ($!) {
481 0 $git->fault("Error closing pipe to external hook: $!", {
482 prefix => $prefix,
483 details => $output,
484 });
485 } else {
486 0 $git->fault("External hook exited with code $?", {
487 prefix => $prefix,
488 details => $output,
489 });
490 }
491 } else {
492 # child
493 0 { exec {$file} ($hook, @args) }
494  
495 ## no critic (RequireBriefOpen, RequireCarping)
496 0 ▶ open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or croak("Can't dup \$oldout: $!") 0 0 0
497 0 ▶ open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or croak("Can't dup \$olderr: $!") 0 0 0
498 ## use critic
499  
500 0 croak "$prefix: can't exec: $!\n";
501 }
502  
503 } else {
504  
505 2 ▶ if (@args && ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0]) 0 2 2
Condition33%1/3
@args and ref $args[0]
!ll&&!rl&&r
200
MC/DC0%0/2
@args and ref $args[0]
@args ref $args[0]
Truth table33%1/3
A and B
A= @argsB= ref $args[0]
ABresult
0X0
100
111
506 # This is a Gerrit hook and we need to expand its arguments
507 0 @args = $args[0]->%*;
508 }
509  
510 2 my $exit = system {$file} ($hook, @args);
511  
512 ## no critic (RequireBriefOpen, RequireCarping)
513 2 ▶ open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or croak("Can't dup \$oldout: $!") 2 0 2
514 2 ▶ open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or croak("Can't dup \$olderr: $!") 2 0 2
515 ## use critic
516  
517 2 my $output = $tempfile->slurp;
518  
519 2 ▶ if ($exit == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 0) { } 1 1 2
520 1 ▶ say STDERR $output if length $output;
Branch: 1 branch
BranchTrueFalseTotal
if length $output 0 1 1
521 1 return 1;
522 } else {
523 1 my $message = do {
524 1 ▶ if ($exit == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit == -1) { } 0 1 1
elsif ($exit & 127) { } 0 1 1
525 0 "failed to execute external hook: $!";
526 } elsif ($exit & 127) {
527 0 ▶ sprintf("external hook died with signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$exit & 128 ? : 0 0 0
528 ($exit & 127), ($exit & 128) ? 'with' : 'without');
529 } else {
530 1 sprintf("'$file' exited abnormally with value %d", $exit >> 8);
531 }
532 };
533 1 $git->fault($message, {prefix => $prefix, details => $output});
534 }
535 }
536  
537 1 return 0;
538 }
539  
540 256 ▶sub invoke_external_hooks {
Subroutine invoke_external_hooks: called CC=9 SCAR=23.1
Pod documented
541 256 my ($git, @args) = @_;
542  
543 256 ▶ return if $^O eq 'MSWin32' || ! $git->get_config_boolean(githooks => 'externals');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" or not $git->get_config_boolean("githooks", "externals") 1 255 256
Condition66%2/3
$^O eq "MSWin32" or not $git->get_config_boolean("githooks", "externals")
l!l&&r!l&&!r
01255
MC/DC50%1/2
$^O eq "MSWin32" or not $git->get_config_boolean("githooks", "externals")
$^O eq "MSWin32" not $git->get_config_boolean("githooks", "externals")
Truth table66%2/3
A or not(B)
A= $^O eq "MSWin32"B= not $git->get_config_boolean("githooks", "externals")
ABresult
1X1
011
000
544  
545 255 my $hookname = $git->{_plugin_githooks}{hookname};
546  
547 255 foreach my $dir (
548 255 grep {-e}
549 255 map {path($_)->child($hookname)}
550 ($git->get_config(githooks => 'hooks'), path($git->git_dir())->child('hooks.d'))
551 ) {
552 2 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or $git->fault("I cannot opendir '${dir}'", {"details", $!}) and next 2 0 2
Condition20%1/5
opendir(my $dh, $dir) || $git->fault("I cannot opendir '${dir}'", {'details', $!}) && (next)
l!l&&r!l&&!r
200
$git->fault("I cannot opendir '${dir}'", {"details", $!}) and next
!ll
00
MC/DC0%0/2
opendir(my $dh, $dir) || $git->fault("I cannot opendir '${dir}'", {'details', $!}) && (next)
opendir(my $dh, $dir) $git->fault("I cannot opendir '${dir}'", {"details", $!})
Truth table33%1/3
A || B
A= opendir(my $dh, $dir)B= $git->fault("I cannot opendir '${dir}'", {"details", $!})
ABresult
1X1
011
000
553 or $git->fault("I cannot opendir '$dir'", {details => $!})
554 and next;
555 2 ▶ foreach my $file (grep {!-d && -x} map {path($dir)->child($_)} readdir $dh) {
Condition66%2/3
!(-d $_) && -x $_
!ll&&!rl&&r
402
MC/DC50%1/2
!(-d $_) && -x $_
!(-d $_) -x $_
Truth table66%2/3
not(A) && B
A= !(-d $_)B= -x $_
ABresult
0X0
100
111
556 2 ▶ _invoke_external_hook($git, $file, $hookname, @args)
Branch: 1 branch
BranchTrueFalseTotal
_invoke_external_hook($git, $file, $hookname, @args) or $git->fault(": error in external hook '${file}'") 1 1 2
557 or $git->fault(": error in external hook '$file'");
558 }
559 } continue {
560 2 $git->check_timeout();
561 }
562  
563 255 return;
564 }
565  
566 ##############
567 # The following routines are invoked after all hooks have been
568 # processed. Some hooks may need to take a global action depending on
569 # the overall result of all hooks.
570  
571 0 ▶sub post_hook {
Subroutine post_hook: not called CC=1 SCAR=6.9
Pod documented
572 0 my ($git, $sub) = @_;
573 0 push $git->{_plugin_githooks}{post_hooks}->@*, $sub;
574 0 return;
575 }
576  
577 256 ▶sub post_hooks {
Subroutine post_hooks: called CC=2 SCAR=7.6
Pod documented
578 256 my ($git) = @_;
579 256 ▶ if ($git->{_plugin_githooks}{post_hooks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($git->{'_plugin_githooks'}{'post_hooks'}) { } 0 256 256
580 return $git->{_plugin_githooks}{post_hooks}->@*
581 0 } else {
582 256 return;
583 }
584 }
585  
586 943 ▶sub cache {
Subroutine cache: called CC=2 SCAR=6.9
Pod documented
587 943 my ($git, $section) = @_;
588  
589 943 ▶ unless (exists $git->{_plugin_githooks}{cache}{$section}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $git->{"_plugin_githooks"}{"cache"}{$section}) 617 326 943
590 617 $git->{_plugin_githooks}{cache}{$section} = {};
591 }
592  
593 943 return $git->{_plugin_githooks}{cache}{$section};
594 }
595  
596 4276 ▶sub get_config {
Subroutine get_config: called CC=18 SCAR=31.0
Pod documented
597 4276 my ($git, $section, $var) = @_;
598  
599 4276 ▶ unless (exists $git->{_plugin_githooks}{config}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $git->{"_plugin_githooks"}{"config"}) 259 4017 4276
600 259 my %config;
601  
602 259 my $config = do {
603 259 local $/ = "\c@";
604 259 $git->run(qw/config --null --list/);
605 };
606  
607 259 ▶ if (defined $CONFIG_ENCODING) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Git::Repository::Plugin::GitHooks::CONFIG_ENCODING) 0 259 259
608 0 require Encode;
609 0 $config = Encode::decode($CONFIG_ENCODING, $config);
610 }
611  
612 259 ▶ if (defined $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 259 0 259
613 # The --null option to git-config makes it output a null character
614 # after each option/value. The option and value are separated by a
615 # newline, unless there is no value, in which case, there is no
616 # newline.
617 259 ▶ while ($config =~ /([^\cJ]+)(\cJ[^\c@]*|)\c@/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config =~ /([^\cJ]+)(\cJ[^\c\@]*|)\c@/gsu) 2523 259 2782
618 2523 my ($option, $value) = ($1, $2);
619 2523 ▶ if ($option =~ /(.+)\.(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /(.+)\.(.+)/u) { } 2523 0 2523
620 2523 my ($osection, $okey) = (lc $1, lc $2);
621 2523 ▶ if ($value =~ s/^\cJ//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ s/^\cJ//u) { } 2523 0 2523
622 ## no critic (ProhibitDeepNests)
623 2523 ▶ if ($value eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq 'undef') { } 0 2523 2523
624 # The 'undef' string is a special mark telling us to
625 # disregard every previous value already set for
626 # this variable.
627 0 delete $config{$osection}{$okey};
628 } else {
629 2523 push $config{$osection}{$okey}->@*, $value;
630 }
631 } else {
632 # An option without a value is considered a boolean
633 # true. We mark it explicitly so instead of leaving it
634 # undefined because Perl would consider it false.
635 0 push $config{$osection}{$okey}->@*, 'true';
636 }
637 } else {
638 0 croak __PACKAGE__, ": Cannot grok config variable name '$option'.\n";
639 }
640 }
641 }
642  
643 # Set default values for undefined ones.
644 259 ▶ $config{githooks}{externals} //= ['true'];
Condition100%2/2
$config{"githooks"}{"externals"} //= ["true"]
l!l
1258
MC/DC100%1/1
$config{"githooks"}{"externals"} //= ["true"]
$config{"githooks"}{"externals"}
Truth table100%2/2
A
A= $config{"githooks"}{"externals"}
Aresult
11
00
645 259 ▶ $config{githooks}{gerrit}{enabled} //= ['true'];
Condition50%1/2
$config{"githooks"}{"gerrit"}{"enabled"} //= ["true"]
l!l
0259
MC/DC0%0/1
$config{"githooks"}{"gerrit"}{"enabled"} //= ["true"]
$config{"githooks"}{"gerrit"}{"enabled"}
Truth table50%1/2
A
A= $config{"githooks"}{"gerrit"}{"enabled"}
Aresult
11
00
646 259 ▶ $config{githooks}{'abort-commit'} //= ['true'];
Condition50%1/2
$config{"githooks"}{"abort-commit"} //= ["true"]
l!l
0259
MC/DC0%0/1
$config{"githooks"}{"abort-commit"} //= ["true"]
$config{"githooks"}{"abort-commit"}
Truth table50%1/2
A
A= $config{"githooks"}{"abort-commit"}
Aresult
11
00
647  
648 259 $git->{_plugin_githooks}{config} = \%config;
649 }
650  
651 4276 my $config = $git->{_plugin_githooks}{config};
652  
653 4276 ▶ $section = lc $section if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 4120 156 4276
654 4276 ▶ $var = lc $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 4120 156 4276
655  
656 4276 ▶ if (! defined $section) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $section) { } 156 4120 4276
elsif (not defined $var) { } 0 4120 4120
elsif (exists $config->{$section}{$var}) { } 1268 2852 4120
657 156 return $config;
658 } elsif (! defined $var) {
659 0 ▶ $config->{$section} = {} unless exists $config->{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{$section} 0 0 0
660 0 return $config->{$section};
661 } elsif (exists $config->{$section}{$var}) {
662 1268 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 360 908 1268
663 $log->trace(get_config => {
664 wantarray => 1,
665 section => $section,
666 var => $var,
667 360 result => $config->{$section}{$var},
668 });
669 360 return $config->{$section}{$var}->@*;
670 } else {
671 $log->trace(get_config => {
672 wantarray => 0,
673 section => $section,
674 var => $var,
675 908 result => $config->{$section}{$var}[-1],
676 });
677 908 return $config->{$section}{$var}[-1];
678 }
679 } else {
680 2852 $log->trace(get_config => {
681 wantarray => wantarray,
682 section => $section,
683 var => $var,
684 result => [],
685 });
686 2852 return;
687 }
688 }
689  
690 724 ▶sub get_config_boolean {
Subroutine get_config_boolean: called CC=5 SCAR=16.8
Pod documented
691 724 my ($git, $section, $var) = @_;
692  
693 724 my $bool = $git->get_config($section, $var);
694  
695 724 ▶ if (! defined $bool) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $bool) { } 182 542 724
elsif (ref $bool) { } 0 542 542
elsif ($bool =~ /^(?:yes|on|true|1)$/iu) { } 538 4 542
elsif ($bool =~ /^(?:no|off|false|0|)$/iu) { } 4 0 4
696 182 return;
697 } elsif (ref $bool) {
698 0 croak __PACKAGE__, ": get_bool_config method requires two arguments\n";
699 } elsif ($bool =~ /^(?:yes|on|true|1)$/i) {
700 538 return 1;
701 } elsif ($bool =~ /^(?:no|off|false|0|)$/i) {
702 4 return 0;
703 } else {
704 0 croak __PACKAGE__, ": get_config_boolean($section, $var) not a valid boolean: '$bool'\n";
705 }
706 }
707  
708 472 ▶sub get_config_integer {
Subroutine get_config_integer: called CC=9 SCAR=31.1
Pod documented
709 472 my ($git, $section, $var) = @_;
710  
711 472 my $int = $git->get_config($section, $var);
712  
713 472 ▶ if (! defined $int) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $int) { } 350 122 472
elsif (ref $int) { } 0 122 122
elsif ($int =~ /^([+-]?)([0-9]+)([kmg]?)$/iu) { } 122 0 122
714 350 return;
715 } elsif (ref $int) {
716 0 croak __PACKAGE__, ": get_config_integer() requires two arguments\n";
717 } elsif ($int =~ /^([+-]?)([0-9]+)([kmg]?)$/i) {
718 122 my ($signal, $num, $unit) = ($1, $2, lc $3);
719 122 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 0 122 122
720 0 ▶ if ($unit eq 'k') {
Branch: 3 branches
BranchTrueFalseTotal
if ($unit eq 'k') { } 0 0 0
elsif ($unit eq 'm') { } 0 0 0
elsif ($unit eq 'g') { } 0 0 0
721 0 $num *= 1024;
722 } elsif ($unit eq 'm') {
723 0 $num *= 1024*1024;
724 } elsif ($unit eq 'g') {
725 0 $num *= 1024*1024*1024;
726 }
727 }
728 122 ▶ if ($signal eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal eq "-") 0 122 122
729 0 $num *= -1;
730 }
731 122 return $num;
732 } else {
733 0 croak __PACKAGE__, ": get_config_integer($section, $var) not a valid integer: '$int'\n";
734 }
735 }
736  
737 183 ▶sub _githooks_colors {
Subroutine _githooks_colors: called CC=4 SCAR=15.1
738 183 my ($git) = @_;
739  
740 183 my $cache = $git->cache('colors');
741  
742 183 ▶ unless (exists $cache->{reset}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{"reset"}) 83 100 183
743 # Check if we want to colorize the output, and if so, return a hash
744 # containing the default colors. Otherwise, return a hash containing no
745 # color codes at all.
746  
747 # NOTE: We have to pass the TERM environment variable explicitly because
748 # Git::Repository's constructor deletes it by default. (Se discussion in
749 # https://rt.cpan.org/Ticket/Display.html?id=124711.)
750  
751 83 ▶ my $stdout_is_tty = is_interactive() ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
is_interactive() ? : 0 83 83
752 my $githooks_color = $git->run(qw/config --get-colorbool githooks.color/, $stdout_is_tty,
753 83 {env => {TERM => $ENV{TERM}}});
754 83 ▶ if ($githooks_color eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($githooks_color eq 'true') { } 0 83 83
755 0 $cache->{header} = $git->run(qw/config --get-color githooks.color.header/, 'green');
756 0 $cache->{footer} = $git->run(qw/config --get-color githooks.color.footer/, 'green');
757 0 $cache->{context} = $git->run(qw/config --get-color githooks.color.context/, 'red bold');
758 0 $cache->{message} = $git->run(qw/config --get-color githooks.color.message/, 'yellow');
759 0 $cache->{details} = '';
760 0 $cache->{reset} = $git->run(qw/config --get-color/, '', 'reset');
761 } else {
762 83 $cache->{header} = '';
763 83 $cache->{footer} = '';
764 83 $cache->{context} = '';
765 83 $cache->{message} = '';
766 83 $cache->{details} = '';
767 83 $cache->{reset} = '';
768 }
769 }
770  
771 183 return $cache;
772 }
773  
774 257 ▶sub check_timeout {
Subroutine check_timeout: called CC=3 SCAR=16.8
Pod documented
775 257 my ($git) = @_;
776  
777 257 my $cache = $git->cache('timeout');
778  
779 257 $cache->{timeout} = $git->get_config_integer(githooks => 'timeout');
780  
781 257 ▶ return unless $cache->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless $cache->{"timeout"} 257 0 257
782  
783 0 $cache->{start_time} = time;
784  
785 0 my $now = time;
786  
787 0 ▶ if (($now - $cache->{start_time}) >= $cache->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $cache->{"start_time"} >= $cache->{"timeout"}) 0 0 0
788 0 $git->fault("Hook timeout");
789 0 $git->fail_on_faults();
790 }
791  
792 0 return;
793 }
794  
795 101 ▶sub fault {
Subroutine fault: called CC=10 SCAR=23.0
Pod documented
796 101 my ($git, $message, $info) = @_;
797 101 ▶ $info //= {};
Condition100%2/2
$info //= {}
l!l
965
MC/DC100%1/1
$info //= {}
$info
Truth table100%2/2
A
A= $info
Aresult
11
00
798  
799 101 my $colors = _githooks_colors($git);
800  
801 101 my $msg;
802  
803 {
804 101 ▶ my $prefix = $info->{prefix} || caller;
Condition66%2/3
$info->{'prefix'} || caller
l!l&&r!l&&!r
6950
MC/DC0%0/2
$info->{'prefix'} || caller
$info->{'prefix'} caller
Truth table66%2/3
A || B
A= $info->{'prefix'}B= caller
ABresult
1X1
011
000
805 101 my @context;
806 101 ▶ if (my $commit = $info->{commit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $commit = $info->{"commit"}) 45 56 101
807 45 ▶ $commit = $commit->commit
Branch: 1 branch
BranchTrueFalseTotal
if ref $commit 42 3 45
808 if ref $commit; # It's a Git::Repository::Log object
809 45 ▶ $commit = $git->run('rev-parse', '--short', $commit)
Branch: 1 branch
BranchTrueFalseTotal
if $commit =~ /^[0-9a-f]{40}$/u 10 35 45
810 if $commit =~ /^[0-9a-f]{40}$/; # It can be '<new>' or ':0' sometimes
811 45 push @context, "commit $commit";
812 }
813 101 ▶ if (my $ref = $info->{ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $info->{"ref"}) 29 72 101
814 29 push @context, "on ref $ref";
815 }
816 101 ▶ if (my $option = $info->{option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $info->{"option"}) 68 33 101
817 68 push @context, "violates option '$option'";
818 }
819 101 $msg = "$colors->{context}\[$prefix";
820 101 ▶ $msg .= ': ' . join(' ', @context) if @context;
Branch: 1 branch
BranchTrueFalseTotal
if @context 88 13 101
821 101 $msg .= "]$colors->{reset}\n";
822 }
823  
824 101 chomp $message; # strip trailing newlines
825 101 $msg .= "\n$colors->{message}$message$colors->{reset}\n";
826  
827 101 ▶ if (my $details = $info->{details}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $details = $info->{"details"}) 13 88 101
828 13 $details =~ s/\n*$//s; # strip trailing newlines
829 13 $details =~ s/^/ /gm; # prefix each line with two spaces
830 13 $msg .= "\n$colors->{details}$details$colors->{reset}\n\n";
831 }
832  
833 101 push $git->{_plugin_githooks}{faults}->@*, $msg;
834  
835 # Return true to allow for the idiom: <expression> or $git->fault(...) and <next|last|return>;
836 101 return 1;
837 }
838  
839 256 ▶sub get_faults {
Subroutine get_faults: called CC=10 SCAR=27.1
Pod documented
840 256 my ($git) = @_;
841  
842 256 ▶ return unless exists $git->{_plugin_githooks}{faults};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $git->{"_plugin_githooks"}{"faults"} 174 82 256
843  
844 82 my $colors = _githooks_colors($git);
845  
846 82 my $faults = '';
847  
848 82 ▶ if (my $header = $git->get_config(githooks => 'error-header')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $header = $git->get_config("githooks", "error-header")) 2 80 82
849 2 $faults .= $colors->{header} . qx{$header} . "$colors->{reset}\n"; ## no critic (ProhibitBacktickOperators)
850 }
851  
852 82 $faults .= join("\n\n", $git->{_plugin_githooks}{faults}->@*);
853  
854 82 ▶ if ($git->{_plugin_githooks}{hookname} =~ /^commit-msg|pre-commit$/
Branch: 1 branch
BranchTrueFalseTotal
if ($git->{"_plugin_githooks"}{"hookname"} =~ /^commit-msg|pre-commit$/u and not $git->get_config_boolean("githooks", "abort-commit")) 0 82 82
Condition66%2/3
$git->{"_plugin_githooks"}{"hookname"} =~ /^commit-msg|pre-commit$/u and not $git->get_config_boolean("githooks", "abort-commit")
!ll&&!rl&&r
19630
MC/DC0%0/2
$git->{"_plugin_githooks"}{"hookname"} =~ /^commit-msg|pre-commit$/u and not $git->get_config_boolean("githooks", "abort-commit")
$git->{"_plugin_githooks"}{"hookname"} =~ /^commit-msg|pre-commit$/u not $git->get_config_boolean("githooks", "abort-commit")
Truth table66%2/3
A and not(B)
A= $git->{"_plugin_githooks"}{"hookname"} =~ /^commit-msg|pre-commit$/uB= not $git->get_config_boolean("githooks", "abort-commit")
ABresult
0X0
100
111
855 && ! $git->get_config_boolean(githooks => 'abort-commit')) {
856 0 $faults .= <<'EOS';
857  
858 ATTENTION: To fix the problems in this commit, please consider amending it:
859  
860 git commit --amend
861 EOS
862 }
863  
864 82 ▶ if (my $footer = $git->get_config(githooks => 'error-footer')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $footer = $git->get_config("githooks", "error-footer")) 1 81 82
865 1 $faults .= "\n$colors->{footer}" . qx{$footer} . "$colors->{reset}\n"; ## no critic (ProhibitBacktickOperators)
866 }
867  
868 82 ▶ if (my $prefix = $git->get_config(githooks => 'error-prefix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefix = $git->get_config("githooks", "error-prefix")) 0 82 82
869 0 $faults =~ s/^/$prefix/gm;
870 }
871  
872 82 ▶ if (my $limit = $git->get_config_integer(githooks => 'error-length-limit')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $git->get_config_integer("githooks", "error-length-limit")) 0 82 82
873 0 ▶ if ($limit > 0 && $limit < length($faults)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > 0 and $limit < length $faults) 0 0 0
Condition0%0/3
$limit > 0 and $limit < length $faults
!ll&&!rl&&r
000
MC/DC0%0/2
$limit > 0 and $limit < length $faults
$limit > 0 $limit < length $faults
Truth table0%0/3
A and B
A= $limit > 0B= $limit < length $faults
ABresult
0X0
100
111
874 0 my $mark = "\n\n[MESSAGE TRUNCATED at githooks.error-length-limit]\n";
875 0 substr($faults, $limit - length($mark) - 1, length($faults), $mark);
876 }
877 }
878  
879 82 return $faults;
880 }
881  
882 256 ▶sub fail_on_faults {
Subroutine fail_on_faults: called CC=4 SCAR=14.5
Pod documented
883 256 my ($git, $warn_only) = @_;
884  
885 256 ▶ if (my $faults = $git->get_faults()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $faults = $git->get_faults) 82 174 256
886 82 $log->debug(Environment => {ENV => \%ENV});
887 82 ▶ $faults .= "\n" unless $faults =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $faults =~ /\n$/u 0 82 82
888 82 ▶ if ($warn_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn_only) { } 0 82 82
889 0 $log->warning(Warning => {faults => $faults});
890 0 warn $faults; ## no critic (RequireCarping)
891 } else {
892 82 $log->error(Error => {faults => $faults});
893 82 die $faults; ## no critic (RequireCarping)
894 }
895 }
896  
897 174 return;
898 }
899  
900 70 ▶sub undef_commit {
Subroutine undef_commit: called CC=1 SCAR=0.0
Pod documented
901 70 return '0000000000000000000000000000000000000000';
902 }
903  
904 6 ▶sub empty_tree {
Subroutine empty_tree: called CC=1 SCAR=0.0
Pod documented
905 6 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
906 }
907  
908 11 ▶sub get_commit {
Subroutine get_commit: called CC=3 SCAR=11.4
Pod documented
909 11 my ($git, $commit) = @_;
910  
911 11 my $cache = $git->cache('commits');
912  
913 # $commit may be a symbolic reference, but we only want to cache commits
914 # by their SHA1 ids, since the symbolic references may change.
915 11 ▶ unless ($commit =~ /^[0-9A-F]{40}$/ && exists $cache->{$commit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($commit =~ /^[0-9A-F]{40}$/u and exists $cache->{$commit}) 11 0 11
Condition33%1/3
$commit =~ /^[0-9A-F]{40}$/u and exists $cache->{$commit}
!ll&&!rl&&r
1100
MC/DC0%0/2
$commit =~ /^[0-9A-F]{40}$/u and exists $cache->{$commit}
$commit =~ /^[0-9A-F]{40}$/u exists $cache->{$commit}
Truth table33%1/3
A and B
A= $commit =~ /^[0-9A-F]{40}$/uB= exists $cache->{$commit}
ABresult
0X0
100
111
916 11 my @commits = $git->log('-1', $commit);
917 11 $commit = $commits[0]->{commit};
918 11 $cache->{$commit} = $commits[0];
919 }
920  
921 11 return $cache->{$commit};
922 }
923  
924 41 ▶sub get_commits {
Subroutine get_commits: called CC=10 SCAR=23.1
Pod documented
925 41 my ($git, $old_commit, $new_commit, $options, $paths) = @_;
926  
927 41 my $cache = $git->cache('ranges');
928  
929 41 ▶ my $range = join(
Branch: 2 branches
BranchTrueFalseTotal
defined $options ? : 21 20 41
defined $paths ? : 10 31 41
930 ':',
931 $old_commit,
932 $new_commit,
933 defined $options ? join('', @$options) : '',
934 defined $paths ? join('', @$paths) : '',
935 );
936  
937 41 ▶ unless (exists $cache->{$range}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{$range}) 41 0 41
938 # We're interested in all commits reachable from $new_commit but
939 # neither reachable from $old_commit nor from any other existing
940 # reference.
941  
942 # We're going to use the "git rev-list" command for that. As you can
943 # read on its documentation, the syntax to specify this set of
944 # commits is this:
945 # "--not --branches --tags --not $new_commit ^$old_commit".
946  
947 # However, there are some special cases...
948  
949 # When an old branch is deleted $new_commit is null (i.e.,
950 # '0'x40). In this case previous commits are being forgotten and the
951 # hooks usually don't need to check them. So, in this situation we
952 # simply return an empty list of commits.
953  
954 41 ▶ return if $new_commit eq $git->undef_commit;
Branch: 1 branch
BranchTrueFalseTotal
if $new_commit eq $git->undef_commit 2 39 41
955  
956 # The @excludes list will contain the arguments to git-log necessary to
957 # exclude from $new_commit history all commits already reachable by any
958 # other reference.
959 39 my @excludes;
960  
961 39 ▶ if ($git->{_plugin_githooks}{hookname} !~ /^post-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $git->{'_plugin_githooks'}{'hookname'} =~ /^post-/u) { } 29 10 39
962 # In pre-* hooks (e.g., pre-receive, update) we can use the '--not
963 # --branches --tags' arguments.
964 29 @excludes = qw/--not --branches --tags --not/;
965 } else {
966 # When we're called in a post-receive or post-update hook, the
967 # pushed references already point to $new_commit. So, in these cases
968 # the "--not --branches --tags" options would exclude from the
969 # results all commits reachable from $new_commit, which is exactly
970 # what we don't want... In order to avoid that we can't use these
971 # options directly with git-log. Instead, we use the git-rev-parse
972 # command to get a list of all commits directly reachable by
973 # existing references. Then we'll see if we have to remove any
974 # commit from that list.
975  
976 10 @excludes = $git->run(qw/rev-parse --not --branches --tags/);
977  
978 # But we can't simply remove $new_commit from @excludes because it
979 # can be reachable by other references. This can happen, for
980 # instance, when one creates a new branch and pushes it before
981 # making any commits to it or when one pushes a branch after a
982 # fast-forward merge. So, we only remove it if it's reachable by a
983 # single reference, which must be the reference being pushed.
984  
985 # The --points-at option was implemented in this version of Git
986 10 my @new_commit_refs = $git->run(
987 qw/for-each-ref --format %(refname) --count 2 --points-at/, $new_commit,
988 );
989 10 ▶ if (@new_commit_refs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_commit_refs == 1) 10 0 10
990 10 @excludes = grep {$_ ne "^$new_commit"} @excludes;
991 }
992  
993 # And we have to make sure $old_commit is on the list, as --not
994 # --branches --tags wouldn't bring it when we're being called in a
995 # post-receive or post-update hook.
996  
997 10 ▶ push @excludes, "^$old_commit" unless $old_commit eq $git->undef_commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_commit eq $git->undef_commit 10 0 10
998 }
999  
1000 39 my @arguments;
1001  
1002 39 ▶ push @arguments, @$options if defined $options;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options 21 18 39
1003 39 push @arguments, @excludes, $new_commit;
1004 39 ▶ push @arguments, '--', @$paths if defined $paths;
Branch: 1 branch
BranchTrueFalseTotal
if defined $paths 10 29 39
1005  
1006 39 $cache->{$range} = [$git->log(@arguments)];
1007 }
1008  
1009 39 return $cache->{$range}->@*;
1010 }
1011  
1012 93 ▶sub read_commit_msg_file {
Subroutine read_commit_msg_file: called CC=3 SCAR=11.0
Pod documented
1013 93 my ($git, $msgfile) = @_;
1014  
1015 93 ▶ my $encoding = $git->get_config(i18n => 'commitEncoding') || 'utf-8';
Condition50%1/2
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
l!l
093
MC/DC0%0/1
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
$git->get_config('i18n', 'commitEncoding')
Truth table50%1/2
A
A= $git->get_config('i18n', 'commitEncoding')
Aresult
11
00
1016  
1017 93 my $msg = path($msgfile)->slurp({binmode => ":encoding($encoding)"});
1018  
1019 # Truncate the message just before the diff, if any.
1020 93 $msg =~ s:\ndiff --git .*::s;
1021  
1022 # The comments in the following lines were taken from the "git
1023 # help stripspace" documentation to guide the
1024 # implementation. Previously we invoked the "git stripspace -s"
1025 # external command via Git::command_bidi_pipe to do the cleaning
1026 # but it seems that it doesn't work on FreeBSD. So, we reimplement
1027 # its functionality here.
1028  
1029 93 for ($msg) {
1030 # Skip and remove all lines starting with comment character
1031 # (default #).
1032 93 s/^#.*//gm;
1033  
1034 # remove trailing whitespace from all lines
1035 93 s/[ \t\f]+$//gm;
1036  
1037 # collapse multiple consecutive empty lines into one empty line
1038 93 s/\n{3,}/\n\n/gs;
1039  
1040 # remove empty lines from the beginning and end of the input
1041 # add a missing \n to the last line if necessary.
1042 93 s/^\n+//s;
1043 93 s/\n*$/\n/s;
1044  
1045 # In the case where the input consists entirely of whitespace
1046 # characters, no output will be produced.
1047 93 s/^\s+$//s;
1048 }
1049  
1050 93 return $msg;
1051 }
1052  
1053 2 ▶sub write_commit_msg_file {
Subroutine write_commit_msg_file: called CC=2 SCAR=7.0
Pod documented
1054 2 my ($git, $msgfile, @msg) = @_;
1055  
1056 2 ▶ my $encoding = $git->get_config(i18n => 'commitEncoding') || 'utf-8';
Condition50%1/2
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
l!l
02
MC/DC0%0/1
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
$git->get_config('i18n', 'commitEncoding')
Truth table50%1/2
A
A= $git->get_config('i18n', 'commitEncoding')
Aresult
11
00
1057  
1058 2 path($msgfile)->spew({binmode => ":encoding($encoding)"}, @msg);
1059  
1060 2 return;
1061 }
1062  
1063 # Internal funtion to set the affected references in an update or
1064 # pre-receive hook.
1065  
1066 58 ▶sub _set_affected_ref {
Subroutine _set_affected_ref: called CC=1 SCAR=0.0
1067 58 my ($git, $ref, $old_commit, $new_commit) = @_;
1068 58 $git->{_plugin_githooks}{affected_refs}{$ref}{range} = [$old_commit, $new_commit];
1069 58 return;
1070 }
1071  
1072 # internal method
1073 127 ▶sub _get_affected_refs_hash {
Subroutine _get_affected_refs_hash: called CC=2 SCAR=7.2
1074 127 my ($git) = @_;
1075  
1076 127 ▶ return $git->{_plugin_githooks}{affected_refs} || {};
Condition50%1/2
$git->{'_plugin_githooks'}{'affected_refs'} || {}
l!l
1270
MC/DC0%0/1
$git->{'_plugin_githooks'}{'affected_refs'} || {}
$git->{'_plugin_githooks'}{'affected_refs'}
Truth table50%1/2
A
A= $git->{'_plugin_githooks'}{'affected_refs'}
Aresult
11
00
1077 }
1078  
1079 58 ▶sub get_affected_refs {
Subroutine get_affected_refs: called CC=1 SCAR=0.0
Pod documented
1080 58 my ($git) = @_;
1081  
1082 58 return keys _get_affected_refs_hash($git)->%*;
1083 }
1084  
1085 58 ▶sub get_affected_ref_range {
Subroutine get_affected_ref_range: called CC=2 SCAR=7.0
Pod documented
1086 58 my ($git, $ref) = @_;
1087  
1088 58 my $affected = _get_affected_refs_hash($git);
1089  
1090 exists $affected->{$ref}{range}
1091 58 ▶ or croak __PACKAGE__, ": get_affected_ref_range($ref): no such affected ref\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $affected->{$ref}{"range"} or croak(__PACKAGE__, ": get_affected_ref_range($ref): no such affected ref\n") 58 0 58
1092  
1093 58 return $affected->{$ref}{range}->@*;
1094 }
1095  
1096 20 ▶sub get_affected_ref_commits {
Subroutine get_affected_ref_commits: called CC=1 SCAR=0.0
Pod documented
1097 20 my ($git, $ref, $options, $paths) = @_;
1098  
1099 20 return $git->get_commits($git->get_affected_ref_range($ref), $options, $paths);
1100 }
1101  
1102 0 ▶sub filter_name_status_in_index {
Subroutine filter_name_status_in_index: not called CC=2 SCAR=17.9
Pod documented
1103 0 my ($git, $filter) = @_;
1104  
1105 0 my %actions;
1106  
1107 0 my $output = $git->run(
1108 qw/diff-index --name-status --ignore-submodules --no-commit-id --cached -r -z/,
1109 "--diff-filter=$filter",
1110 $git->get_head_or_empty_tree(),
1111 );
1112  
1113 0 my @output = split /\0/, $output;
1114 0 ▶ while (@output >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@output >= 2) 0 0 0
1115 0 my ($action, $file) = splice @output, 0, 2;
1116 0 $actions{$file} = $action;
1117 }
1118  
1119 0 return \%actions;
1120 }
1121  
1122 0 ▶sub filter_name_status_in_range {
Subroutine filter_name_status_in_range: not called CC=6 SCAR=37.4
Pod documented
1123 0 my ($git, $filter, $from, $to, $options, $paths) = @_;
1124  
1125 # If $to is the undefined commit this means that a branch or tag is being
1126 # removed. In this situation we return the empty list, bacause no file
1127 # has been affected.
1128 0 ▶ return {} if $to eq $git->undef_commit;
Branch: 1 branch
BranchTrueFalseTotal
if $to eq $git->undef_commit 0 0 0
1129  
1130 0 ▶ if ($from eq $git->undef_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq $git->undef_commit) 0 0 0
1131 # If $from is the undefined commit we get the list of commits
1132 # reachable from $to and not reachable from $from and all other
1133 # references. This list is in chronological order. We want to grok
1134 # the files changed from the list's first commit's PARENT commit to
1135 # the list's last commit.
1136  
1137 0 ▶ if (my @commits = $git->get_commits($from, $to, $options, $paths)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@commits) = $git->get_commits($from, $to, $options, $paths)) { } 0 0 0
1138 0 ▶ if (my @parents = $commits[0]->parent()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parents) = $commits[0]->parent) { } 0 0 0
1139 0 $from = $parents[0];
1140 } else {
1141 # If the list's first commit has no parent (i.e., it's a root
1142 # commit) then we return the empty hash because git-diff-tree
1143 # cannot compare the undefined commit with a commit.
1144 0 return {};
1145 }
1146 } else {
1147 # If @commits is empty we return an empty hash because no new commit
1148 # was pushed.
1149 0 return {};
1150 }
1151 }
1152  
1153 0 my %actions;
1154  
1155 0 my $output = $git->run(
1156 qw/diff-tree --name-status --ignore-submodules --no-commit-id -r -z/,
1157 "--diff-filter=$filter",
1158 $from, $to, '--',
1159 );
1160  
1161 0 my @output = split /\0/, $output;
1162 0 ▶ while (@output >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@output >= 2) 0 0 0
1163 0 my ($action, $file) = splice @output, 0, 2;
1164 0 $actions{$file} = $action;
1165 }
1166  
1167 0 return \%actions;
1168 }
1169  
1170 0 ▶sub filter_name_status_in_commit {
Subroutine filter_name_status_in_commit: not called CC=10 SCAR=47.0
Pod documented
1171 0 my ($git, $filter, $commit) = @_;
1172  
1173 0 my $output = $git->run(
1174 qw/diff-tree --name-status --ignore-submodules -m -r -z/,
1175 "--diff-filter=$filter",
1176 $commit,
1177 );
1178  
1179 0 my @output = split /\0/, $output;
1180  
1181 # @output is a sequence of commits, actions, and files, with the following
1182 # general pattern: { COMMIT { ACTION FILE }* }+,
1183  
1184 # COMMIT is the parent commit of $commit. There can be more than one if
1185 # $commit is a merge commit.
1186  
1187 # Below we parse the sequence, tucking all the information in %actions.
1188  
1189 0 my %actions;
1190  
1191 my $sha1;
1192 0 my $action;
1193 0 my $parents = 0;
1194 0 my $expect = 'sha1';
1195  
1196 # PARSE @output
1197 0 ▶ while (@output) {
Branch: 1 branch
BranchTrueFalseTotal
if (@output) 0 0 0
1198 0 ▶ if ($expect eq 'sha1') {
Branch: 3 branches
BranchTrueFalseTotal
if ($expect eq 'sha1') { } 0 0 0
elsif ($expect eq 'sha1 or action') { } 0 0 0
elsif ($expect eq 'file') { } 0 0 0
1199 0 ▶ if ($output[0] =~ /^[0-9a-f]{40}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output[0] =~ /^[0-9a-f]{40}$/u) { } 0 0 0
1200 0 $sha1 = shift @output;
1201 0 ++$parents;
1202 0 $expect = 'sha1 or action';
1203 } else {
1204 0 croak;
1205 }
1206 } elsif ($expect eq 'sha1 or action') {
1207 0 ▶ if ($output[0] =~ /^[0-9a-f]{40}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output[0] =~ /^[0-9a-f]{40}$/u) { } 0 0 0
elsif ($output[0] =~ /^[A-Z]$/u) { } 0 0 0
1208 0 $sha1 = shift @output;
1209 0 ++$parents;
1210 } elsif ($output[0] =~ /^[A-Z]$/) {
1211 0 $action = shift @output;
1212 0 $expect = 'file';
1213 } else {
1214 0 croak;
1215 }
1216 } elsif ($expect eq 'file') {
1217 0 $actions{shift @output}{$sha1} = $action;
1218 0 $expect = 'sha1 or action';
1219 } else {
1220 0 croak;
1221 }
1222 }
1223  
1224 # %actions is a multi-level hash: $actions{$file}{$sha1} = $action. Next
1225 # we remove the $commit level, joining all $actions together under $file.
1226  
1227 0 foreach my $file (keys %actions) {
1228 0 ▶ if (keys($actions{$file}->%*) == $parents) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$actions{$file}} == $parents) { } 0 0 0
1229 # For merge commits we're interested only in files that were
1230 # affected in all parent commits. For files affected in all parents
1231 # we join their actions together. Non-merge commits ($parents == 1)
1232 # reduce to the general case of merge commits.
1233 0 $actions{$file} = join('', values $actions{$file}->%*);
1234 } else {
1235 # Files not affected in all parents we don't care about.
1236 0 delete $actions{$file};
1237 }
1238 }
1239  
1240 0 return \%actions;
1241 }
1242  
1243 0 ▶sub filter_files_in_index {
Subroutine filter_files_in_index: not called CC=1 SCAR=6.9
Pod documented
1244 0 my ($git, $filter) = @_;
1245 0 my @files = sort keys $git->filter_name_status_in_index($filter)->%*;
1246 0 return @files;
1247 }
1248  
1249 0 ▶sub filter_files_in_range {
Subroutine filter_files_in_range: not called CC=1 SCAR=6.9
Pod documented
1250 0 my ($git, @args) = @_;
1251 0 my @files = sort keys $git->filter_name_status_in_range(@args)->%*;
1252 0 return @files;
1253 }
1254  
1255 0 ▶sub filter_files_in_commit {
Subroutine filter_files_in_commit: not called CC=1 SCAR=6.9
Pod documented
1256 0 my ($git, $commit) = @_;
1257 0 my @files = sort keys $git->filter_name_status_in_commit($commit)->%*;
1258 0 return @files;
1259 }
1260  
1261 279 ▶sub authenticated_user {
Subroutine authenticated_user: called CC=10 SCAR=27.6
Pod documented
1262 279 my ($git) = @_;
1263  
1264 279 ▶ unless (exists $git->{_plugin_githooks}{authenticated_user}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $git->{"_plugin_githooks"}{"authenticated_user"}) 257 22 279
1265 257 ▶ if (my $userenv = $git->get_config(githooks => 'userenv')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $userenv = $git->get_config('githooks', 'userenv')) { } 6 251 257
1266 6 ▶ if ($userenv =~ /^eval:(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($userenv =~ /^eval:(.*)/u) { } 0 6 6
elsif (exists $ENV{$userenv}) { } 6 0 6
1267 0 $git->{_plugin_githooks}{authenticated_user} = eval $1; ## no critic (BuiltinFunctions::ProhibitStringyEval)
1268 0 ▶ croak __PACKAGE__, ": error evaluating userenv value ($userenv): $@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1269 if $@;
1270 } elsif (exists $ENV{$userenv}) {
1271 6 $git->{_plugin_githooks}{authenticated_user} = $ENV{$userenv};
1272 } else {
1273 0 croak __PACKAGE__, ": option userenv environment variable ($userenv) is not defined.\n";
1274 }
1275 } else {
1276 251 ▶ $git->{_plugin_githooks}{authenticated_user} = $ENV{GERRIT_USER_EMAIL} || $ENV{BB_USER_NAME} || $ENV{GL_USERNAME} || $ENV{USER} || undef;
Condition54%6/11
$ENV{'GERRIT_USER_EMAIL'} || $ENV{'BB_USER_NAME'} || $ENV{'GL_USERNAME'} || $ENV{'USER'} || undef
l!l
39212
$ENV{"GERRIT_USER_EMAIL"} or $ENV{"BB_USER_NAME"} or $ENV{"GL_USERNAME"} or $ENV{"USER"}
l!l&&r!l&&!r
039212
$ENV{"GERRIT_USER_EMAIL"} or $ENV{"BB_USER_NAME"} or $ENV{"GL_USERNAME"}
l!l&&r!l&&!r
00251
$ENV{"GERRIT_USER_EMAIL"} or $ENV{"BB_USER_NAME"}
l!l&&r!l&&!r
00251
MC/DC25%1/4
$ENV{'GERRIT_USER_EMAIL'} || $ENV{'BB_USER_NAME'} || $ENV{'GL_USERNAME'} || $ENV{'USER'} || undef
$ENV{"GERRIT_USER_EMAIL"} $ENV{"BB_USER_NAME"} $ENV{"GL_USERNAME"} $ENV{"USER"}
Truth table40%2/5
A or B or C or D
A= $ENV{"GERRIT_USER_EMAIL"}B= $ENV{"BB_USER_NAME"}C= $ENV{"GL_USERNAME"}D= $ENV{"USER"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1277 }
1278 }
1279  
1280 279 return $git->{_plugin_githooks}{authenticated_user};
1281 }
1282  
1283 9 ▶sub repository_name {
Subroutine repository_name: called CC=6 SCAR=21.4
Pod documented
1284 9 my ($git) = @_;
1285  
1286 9 ▶ unless (exists $git->{_plugin_githooks}{repository_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $git->{"_plugin_githooks"}{"repository_name"}) 9 0 9
1287 9 ▶ if (my $gerrit_args = $git->{_plugin_githooks}{gerrit_args}) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $gerrit_args = $git->{'_plugin_githooks'}{'gerrit_args'}) { } 0 9 9
elsif (exists $ENV{'BB_REPO_SLUG'}) { } 0 9 9
elsif (exists $ENV{'GL_PROJECT_PATH'}) { } 0 9 9
1288 # Gerrit
1289 0 $git->{_plugin_githooks}{repository_name} = $gerrit_args->{'--project'};
1290 } elsif (exists $ENV{BB_REPO_SLUG}) {
1291 # Bitbucket Server environment variables available for hooks:
1292 # https://developer.atlassian.com/server/bitbucket/how-tos/write-hook-scripts/
1293 0 $git->{_plugin_githooks}{repository_name} = "$ENV{BB_PROJECT_KEY}/$ENV{BB_REPO_SLUG}";
1294 } elsif (exists $ENV{GL_PROJECT_PATH}) {
1295 # GitLab environment variables available for hooks:
1296 # https://docs.gitlab.com/ee/administration/server_hooks.html
1297 0 $git->{_plugin_githooks}{repository_name} = "$ENV{GL_PROJECT_PATH}";
1298 } else {
1299 # As a last resort, return GIT_DIR's basename
1300 9 my $gitdir = path($git->git_dir());
1301 9 my $basename = $gitdir->basename;
1302 9 ▶ if ($basename eq '.git') {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename eq ".git") 0 9 9
1303 0 $basename = $gitdir->parent->basename;
1304 }
1305 9 $git->{_plugin_githooks}{repository_name} = $basename;
1306 }
1307 }
1308  
1309 9 return $git->{_plugin_githooks}{repository_name};
1310 }
1311  
1312 134 ▶sub get_current_branch {
Subroutine get_current_branch: called CC=2 SCAR=7.1
Pod documented
1313 134 my ($git) = @_;
1314 134 my $branch = $git->run({fatal => [-129, -128], quiet => 1}, qw/symbolic-ref HEAD/);
1315  
1316 # Return undef if we're in detached head state
1317 134 ▶ return $? == 0 ? $branch : undef;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 134 0 134
1318 }
1319  
1320 0 ▶sub get_sha1 {
Subroutine get_sha1: not called CC=1 SCAR=6.9
Pod documented
1321 0 my ($git, $rev) = @_;
1322  
1323 0 return $git->run(qw/rev-parse --verify/, $rev);
1324 }
1325  
1326 41 ▶sub get_head_or_empty_tree {
Subroutine get_head_or_empty_tree: called CC=2 SCAR=6.9
Pod documented
1327 41 my ($git) = @_;
1328  
1329 41 my $head = $git->run({fatal => [-129, -128], quiet => 1}, qw/rev-parse --verify HEAD/);
1330  
1331 # Return the empty tree object if in the initial commit
1332 41 ▶ return $? == 0 ? $head : $git->empty_tree;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 39 2 41
1333 }
1334  
1335 8 ▶sub blob {
Subroutine blob: called CC=9 SCAR=22.5
Pod documented
1336 8 my ($git, $rev, $file, @args) = @_;
1337  
1338 8 my $cache = $git->cache('blob');
1339  
1340 8 my $blob = "$rev:$file";
1341  
1342 8 ▶ unless (exists $cache->{$blob}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{$blob}) 8 0 8
1343 8 ▶ $cache->{tmpdir} //= Path::Tiny->tempdir(@args);
Condition33%1/3
$cache->{"tmpdir"} //= "Path::Tiny"->tempdir(@args)
l!l&&r!l&&!r
080
MC/DC0%0/2
$cache->{"tmpdir"} //= "Path::Tiny"->tempdir(@args)
$cache->{"tmpdir"} "Path::Tiny"->tempdir(@args)
Truth table33%1/3
A //= B
A= $cache->{"tmpdir"}B= "Path::Tiny"->tempdir(@args)
ABresult
1X1
011
000
1344  
1345 8 my $path = path($file);
1346  
1347 # Calculate temporary file path
1348 8 my $revdir = $rev =~ s/^://r; # remove ':' from ':0' because Windows don't like ':' in filenames
1349 8 my $filepath = $cache->{tmpdir}->child($revdir, $path);
1350  
1351 # Create directory path for the temporary file.
1352 8 $filepath->parent->mkpath;
1353  
1354 # Create temporary file and copy contents to it
1355 8 ▶ open my $tmp, '>:', $filepath ## no critic (RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp, ">:", $filepath or croak("Internal error: can't create file '${filepath}': $!") 8 0 8
1356 or croak "Internal error: can't create file '$filepath': $!";
1357  
1358 8 my $cmd = $git->command(qw/cat-file blob/, $blob);
1359 8 my $stdout = $cmd->stdout;
1360 8 my $read;
1361 8 ▶ while ($read = sysread $stdout, my $buffer, 64 * 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read = sysread($stdout, my $buffer, 65536)) 8 8 16
1362 8 my $length = length $buffer;
1363 8 my $offset = 0;
1364 8 ▶ while ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 8 8 16
1365 8 my $written = syswrite $tmp, $buffer, $length, $offset;
1366 8 ▶ defined $written
Branch: 1 branch
BranchTrueFalseTotal
defined $written or croak("Internal error: can't write to '${filepath}': $!") 8 0 8
1367 or croak "Internal error: can't write to '$filepath': $!";
1368 8 $length -= $written;
1369 8 $offset += $written;
1370 }
1371 }
1372 8 ▶ defined $read
Branch: 1 branch
BranchTrueFalseTotal
defined $read or croak("Internal error: can't read from git cat-file pipe: $!") 8 0 8
1373 or croak "Internal error: can't read from git cat-file pipe: $!";
1374 8 $cmd->close;
1375  
1376 8 $tmp->close;
1377  
1378 8 ▶ if (my $exit = $cmd->exit) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exit = $cmd->exit) 0 8 8
1379 0 croak "Command 'git cat-file blob $blob' exited with code $exit\n";
1380 }
1381  
1382 8 $cache->{$blob} = $filepath;
1383 }
1384  
1385 8 return $cache->{$blob}->stringify;
1386 }
1387  
1388 8 ▶sub file_size {
Subroutine file_size: called CC=1 SCAR=0.0
Pod documented
1389 8 my ($git, $rev, $file) = @_;
1390  
1391 8 return $git->run(qw/cat-file -s/, "$rev:$file");
1392 }
1393  
1394 4 ▶sub file_mode {
Subroutine file_mode: called CC=6 SCAR=27.4
Pod documented
1395 4 my ($git, $rev, $file) = @_;
1396  
1397 4 ▶ if ($rev eq ':0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev eq ':0') { } 4 0 4
1398 4 my @diff_index = $git->run(qw/diff-index --cached --raw --no-color HEAD/, $file);
1399  
1400 4 ▶ if (@diff_index == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diff_index == 1) { } 4 0 4
1401 4 ▶ if (my ($src_mode, $dst_mode, $rest) = $diff_index[0] =~ /^:(\d+) (\d+) (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $diff_index[0] =~ /^:(\d+) (\d+) (.*)/u) { } 4 0 4
1402 4 return oct $dst_mode;
1403 } else {
1404 0 croak "Internal error: cannot parse output of git-diff-idex:\n\n $diff_index[0]";
1405 }
1406 } else {
1407 0 croak "Internal error: git-diff-index should return a single line";
1408 }
1409 } else {
1410 0 my @ls_tree = $git->run('ls-tree', "$rev:", $file );
1411  
1412 0 ▶ if (@ls_tree == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ls_tree == 1) { } 0 0 0
1413 0 ▶ if (my ($mode, $type, $object, $filename) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $ls_tree[0] =~ /^(\d+) ([a-z]+) ([a-z0-9]{40})\t(.+)/u) { } 0 0 0
1414 $ls_tree[0] =~ /^(\d+) ([a-z]+) ([a-z0-9]{40})\t(.+)/) {
1415 0 return oct $mode;
1416 } else {
1417 0 croak "Internal error: cannot parse output of git-ls-tree:\n\n $ls_tree[0]";
1418 }
1419 } else {
1420 0 croak "Internal error: $rev:$file should be a blob";
1421 }
1422 }
1423  
1424 0 croak "Can't happen!";
1425 }
1426  
1427 184 ▶sub is_reference_enabled {
Subroutine is_reference_enabled: called CC=3 SCAR=11.1
Pod documented
1428 184 my ($git, $reference) = @_;
1429  
1430 184 ▶ return 1 unless defined $reference;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reference 0 184 184
1431  
1432 184 my $cache = $git->cache('is_reference_enabled');
1433  
1434 184 ▶ unless (exists $cache->{$reference}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{$reference}) 184 0 184
1435 184 ▶ my $check_reference = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
1436 184 foreach ($git->get_config(githooks => 'ref')) {
1437 29 ▶ if (/^\^/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\^/u) { } 1 28 29
1438 1 ▶ return 1 if $reference =~ qr/$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $reference =~ qr/$_/u 1 0 1
1439 } else {
1440 28 ▶ return 1 if $reference eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $reference eq $_ 28 0 28
1441 }
1442 }
1443  
1444 155 foreach ($git->get_config(githooks => 'noref')) {
1445 7 ▶ if (/^\^/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\^/u) { } 1 6 7
1446 1 ▶ return 0 if $reference =~ qr/$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $reference =~ qr/$_/u 1 0 1
1447 } else {
1448 6 ▶ return 0 if $reference eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $reference eq $_ 5 1 6
1449 }
1450 }
1451  
1452 149 return 1;
1453 184 };
1454  
1455 184 $cache->{$reference} = $check_reference->();
1456 }
1457  
1458 184 return $cache->{$reference};
1459 }
1460  
1461 0 ▶sub _grok_groups_spec {
Subroutine _grok_groups_spec: not called CC=8 SCAR=42.8
1462 0 my ($groups, $specs, $source) = @_;
1463 0 foreach (@$specs) {
1464 0 s/\#.*//; # strip comments
1465 0 ▶ next unless /\S/; # skip blank lines
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/u 0 0 0
1466 0 ▶ /^\s*([\w-]+)\s*=\s*(.+?)\s*$/
Branch: 1 branch
BranchTrueFalseTotal
/^\s*([\w-]+)\s*=\s*(.+?)\s*$/u or croak(__PACKAGE__, ": invalid line in '${source}': $_\n") 0 0 0
1467 or croak __PACKAGE__, ": invalid line in '$source': $_\n";
1468 0 my ($groupname, $members) = ($1, $2);
1469 0 ▶ exists $groups->{"\@$groupname"}
Branch: 1 branch
BranchTrueFalseTotal
exists $$groups{"\@$groupname"} and croak(__PACKAGE__, ": redefinition of group ($groupname) in '${source}': $_\n") 0 0 0
1470 and croak __PACKAGE__, ": redefinition of group ($groupname) in '$source': $_\n";
1471 0 foreach my $member (split ' ', $members) {
1472 0 ▶ if ($member =~ /^\@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member =~ /^\@/u) { } 0 0 0
1473 # group member
1474 0 ▶ $groups->{"\@$groupname"}{$member} = $groups->{$member}
Branch: 1 branch
BranchTrueFalseTotal
$$groups{"\@$groupname"}{$member} = $groups->{$member} or croak(__PACKAGE__, ": unknown group ($member) cited in '${source}': $_\n") 0 0 0
1475 or croak __PACKAGE__, ": unknown group ($member) cited in '$source': $_\n";
1476 } else {
1477 # user member
1478 0 $groups->{"\@$groupname"}{$member} = undef;
1479 }
1480 }
1481 }
1482 0 return;
1483 }
1484  
1485 0 ▶sub _grok_groups {
Subroutine _grok_groups: not called CC=6 SCAR=37.4
1486 0 my ($git) = @_;
1487  
1488 0 my $cache = $git->cache('githooks');
1489  
1490 0 ▶ unless (exists $cache->{groups}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{"groups"}) 0 0 0
1491 0 ▶ my @groups = $git->get_config(githooks => 'groups')
Branch: 1 branch
BranchTrueFalseTotal
my(@groups) = $git->get_config("githooks", "groups") or croak(__PACKAGE__, ": you have to define the githooks.groups option to use groups.\n") 0 0 0
1492 or croak __PACKAGE__, ": you have to define the githooks.groups option to use groups.\n";
1493  
1494 0 my $groups = {};
1495 0 foreach my $spec (@groups) {
1496 0 ▶ if (my ($groupfile) = ($spec =~ /^file:(.*)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($groupfile) = $spec =~ /^file:(.*)/u) { } 0 0 0
1497 0 my @groupspecs = path($groupfile)->lines;
1498 0 ▶ defined $groupspecs[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $groupspecs[0] or croak(__PACKAGE__, ": can't open groups file ($groupfile): $!\n") 0 0 0
1499 or croak __PACKAGE__, ": can't open groups file ($groupfile): $!\n";
1500 0 _grok_groups_spec($groups, \@groupspecs, $groupfile);
1501 } else {
1502 0 my @groupspecs = split /\n/, $spec;
1503 0 _grok_groups_spec($groups, \@groupspecs, "githooks.groups");
1504 }
1505 }
1506 0 $cache->{groups} = $groups;
1507 }
1508  
1509 0 return $cache->{groups};
1510 }
1511  
1512 0 ▶sub _im_memberof {
Subroutine _im_memberof: not called CC=6 SCAR=37.4
1513 0 my ($git, $myself, $groupname) = @_;
1514  
1515 0 my $groups = _grok_groups($git);
1516  
1517 0 ▶ exists $groups->{$groupname}
Branch: 1 branch
BranchTrueFalseTotal
exists $groups->{$groupname} or croak(__PACKAGE__, ": group $groupname is not defined.\n") 0 0 0
1518 or croak __PACKAGE__, ": group $groupname is not defined.\n";
1519  
1520 0 my $group = $groups->{$groupname};
1521 0 ▶ return 1 if exists $group->{$myself};
Branch: 1 branch
BranchTrueFalseTotal
if exists $group->{$myself} 0 0 0
1522 0 ▶ while (my ($member, $subgroup) = each %$group) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$group) 0 0 0
1523 0 ▶ next unless defined $subgroup;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subgroup 0 0 0
1524 0 ▶ return 1 if _im_memberof($git, $myself, $member);
Branch: 1 branch
BranchTrueFalseTotal
if _im_memberof($git, $myself, $member) 0 0 0
1525 }
1526 0 return 0;
1527 }
1528  
1529 6 ▶sub match_user {
Subroutine match_user: called CC=7 SCAR=25.0
Pod documented
1530 6 my ($git, $spec) = @_;
1531  
1532 6 ▶ if (my $myself = $git->authenticated_user()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $myself = $git->authenticated_user) 6 0 6
1533 6 ▶ if ($spec =~ /^\^/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /^\^/u) { } 0 6 6
elsif ($spec =~ /^@/u) { } 0 6 6
1534 0 ▶ return 1 if $myself =~ $spec;
Branch: 1 branch
BranchTrueFalseTotal
if $myself =~ /$spec/u 0 0 0
1535 } elsif ($spec =~ /^@/) {
1536 0 ▶ return 1 if _im_memberof($git, $myself, $spec);
Branch: 1 branch
BranchTrueFalseTotal
if _im_memberof($git, $myself, $spec) 0 0 0
1537 } else {
1538 6 ▶ return 1 if $myself eq $spec;
Branch: 1 branch
BranchTrueFalseTotal
if $myself eq $spec 3 3 6
1539 }
1540 }
1541  
1542 3 return 0;
1543 }
1544  
1545 173 ▶sub im_admin {
Subroutine im_admin: called CC=3 SCAR=14.2
Pod documented
1546 173 my ($git) = @_;
1547 173 foreach my $spec ($git->get_config(githooks => 'admin')) {
1548 0 ▶ return 1 if match_user($git, $spec);
Branch: 1 branch
BranchTrueFalseTotal
if match_user($git, $spec) 0 0 0
1549 }
1550 173 return 0;
1551 }
1552  
1553 54 ▶sub grok_acls {
Subroutine grok_acls: called CC=7 SCAR=19.5
Pod documented
1554 54 my ($git, $cfg, $actions) = @_;
1555  
1556 54 my @acls;
1557  
1558 ACL:
1559 54 foreach ($git->get_config($cfg => 'acl')) {
1560 28 my %acl;
1561 28 ▶ if (/^\s*(allow|deny)\s+([$actions]+)\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(allow|deny)\s+([$actions]+)\s+(\S+)/u) { } 27 1 28
1562 27 $acl{acl} = $_;
1563 27 $acl{allow} = $1 eq 'allow';
1564 27 $acl{action} = $2;
1565 27 my $spec = $3;
1566  
1567 # Interpolate environment variables embedded as "{VAR}".
1568 27 $spec =~ s/{(\w+)}/$ENV{$1}/ige;
1569 # Pre-compile regex
1570 27 ▶ $acl{spec} = substr($spec, 0, 1) eq '^' ? qr/$spec/ : $spec;
Branch: 1 branch
BranchTrueFalseTotal
substr($spec, 0, 1) eq '^' ? : 11 16 27
1571 } else {
1572 1 croak "invalid acl syntax for actions '$actions': $_\n";
1573 }
1574  
1575 27 ▶ if (substr($_, $+[0]) =~ /^\s*by\s+(\S+)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($_, $+[0]) =~ /^\s*by\s+(\S+)\s*$/u) { } 3 24 27
elsif (not substr($_, $+[0]) =~ /^\s*$/u) { } 0 24 24
1576 3 $acl{who} = $1;
1577 # Discard this ACL if it doesn't match the user
1578 3 ▶ next ACL unless $git->match_user($acl{who});
Branch: 1 branch
BranchTrueFalseTotal
unless $git->match_user($acl{"who"}) 2 1 3
1579 } elsif (substr($_, $+[0]) !~ /^\s*$/) {
1580 0 croak "invalid acl syntax for actions '$actions: $_\n";
1581 }
1582  
1583 25 unshift @acls, \%acl;
1584 }
1585  
1586 53 return @acls;
1587 }
1588  
1589
1590 271 1; # End of Git::Repository::Plugin::GitHooks
1591  
1592 __END__
« blib/lib/Git/Hooks/CheckDiff.pm ↑ Summary blib/lib/Git/Hooks/CheckJira.pm »