blib/lib/Git/Hooks/CheckReference.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 87 CC 12 SCAR 46.5
CC
12
Cov
0%
check_ref
156.0
BEGIN
2.0
BEGIN
2.0
CRAP 104.7
1 use warnings;
2  
3 package Git::Hooks::CheckReference;
4 # ABSTRACT: Git::Hooks plugin for checking references
5 $Git::Hooks::CheckReference::VERSION = '3.5.0';
6 use v5.16.0;
7 use utf8;
8 use Log::Any '$log';
9 use Git::Hooks;
10 use List::MoreUtils qw/any none/;
11  
12 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
13  
14 # Assign meaningful names to action codes.
15 my %ACTION = (
16 C => 'create',
17 R => 'rewrite',
18 U => 'update',
19 D => 'delete',
20 );
21  
22 sub check_ref {
23 my ($git, $ref) = @_;
24  
25 my $errors = 0;
26  
27 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
28  
29 # Grok which action we're doing on this ref
30 my $action;
31 if ($old_commit eq '0' x 40) {
32 $action = 'C'; # create
33 } elsif ($new_commit eq '0' x 40) {
34 $action = 'D'; # delete
35 } elsif ($ref !~ m:^refs/heads/:) {
36 $action = 'R'; # rewrite a non-branch
37 } else {
38 # This is an U if "merge-base(old, new) == old". Otherwise it's an R.
39 $action = eval {
40 my $merge_base = $git->run('merge-base' => $old_commit, $new_commit);
41 ($merge_base eq $old_commit) ? 'U' : 'R';
42 } || 'R'; # Probably $old_commit and $new_commit do not have a common ancestor.
43 }
44  
45 my @acls = eval { $git->grok_acls($CFG, 'CRUD') };
46 if ($@) {
47 $git->fault($@, {ref => $ref});
48 return 1;
49 }
50  
51 ACL:
52 foreach my $acl (@acls) {
53 next unless ref $acl->{spec} ? $ref =~ $acl->{spec} : $ref eq $acl->{spec};
54 if (index($acl->{action}, $action) != -1) {
55 unless ($acl->{allow}) {
56 $git->fault(<<"EOS", {ref => $ref, option => 'acl'});
57 The reference name is not allowed due to the following acl:
58  
59 $acl->{acl}
60 EOS
61 ++$errors;
62 }
63 last ACL;
64 }
65 }
66  
67 if ($ref =~ m:^refs/tags/:
68 && $git->get_config_boolean($CFG => 'require-annotated-tags')) {
69 my $rev_type = $git->run('cat-file', '-t', $new_commit);
70 if ($rev_type ne 'tag') {
71 $git->fault(<<'EOS', {ref => $ref, option => 'require-annotated-tags'});
72 This is a lightweight tag.
73 The option in your configuration accepts only annotated tags.
74 Please, recreate your tag as an annotated tag (option -a).
75 EOS
76 ++$errors;
77 }
78 }
79  
80 return $errors;
81 }
82  
83 # Install hooks
84 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref);
85  
86 1;
87  
88 __END__