blib/lib/Git/Repository/Plugin/GitHooks.pm untested

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