blib/lib/Git/Hooks/CheckReference.pm

filter: Statementstmt 86.2% 44 / 51 Branchbran 59.0% 13 / 22 Conditioncond 60.0% 3 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 77.5% 69 / 89 CC 15 SCAR 27.9
CC
15
Cov
77%
check_ref
22.5
BEGIN
1.0
BEGIN
1.0
CRAP 16.3
« blib/lib/Git/Hooks/CheckJira.pm ↑ Summary blib/lib/Git/Hooks/CheckCommit.pm »
1 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::CheckReference;
4 # ABSTRACT: Git::Hooks plugin for checking references
5 8 $Git::Hooks::CheckReference::VERSION = '4.0.0';
6 8 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use List::MoreUtils qw/any none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
13  
14 # Assign meaningful names to action codes.
15 8 my %ACTION = (
16 C => 'create',
17 R => 'rewrite',
18 U => 'update',
19 D => 'delete',
20 );
21  
22 8 ▶sub check_ref {
Subroutine check_ref: called CC=15 SCAR=31.1
Pod undocumented
23 8 my ($git, $ref) = @_;
24  
25 8 my $errors = 0;
26  
27 8 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
28  
29 # Grok which action we're doing on this ref
30 8 my $action;
31 8 ▶ if ($old_commit eq '0' x 40) {
Branch: 3 branches
BranchTrueFalseTotal
if ($old_commit eq '0000000000000000000000000000000000000000') { } 8 0 8
elsif ($new_commit eq '0000000000000000000000000000000000000000') { } 0 0 0
elsif (not $ref =~ m[^refs/heads/]u) { } 0 0 0
32 8 $action = 'C'; # create
33 } elsif ($new_commit eq '0' x 40) {
34 0 $action = 'D'; # delete
35 } elsif ($ref !~ m:^refs/heads/:) {
36 0 $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 0 ▶ $action = eval {
Condition0%0/2
eval { do { my $merge_base = $git->run('merge-base', $old_commit, $new_commit); $merge_base eq $old_commit ? 'U' : 'R' } } || 'R'
l!l
00
MC/DC0%0/1
eval { do { my $merge_base = $git->run('merge-base', $old_commit, $new_commit); $merge_base eq $old_commit ? 'U' : 'R' } } || 'R'
eval { do { my $merge_base = $git->run('merge-base', $old_commit, $new_commit); $merge_base eq $old_commit ? 'U' : 'R' } }
Truth table0%0/2
A
A= eval { do { my $merge_base = $git->run('merge-base', $old_commit, $new_commit); $merge_base eq $old_commit ? 'U' : 'R' } }
Aresult
11
00
40 0 my $merge_base = $git->run('merge-base' => $old_commit, $new_commit);
41 0 ▶ ($merge_base eq $old_commit) ? 'U' : 'R';
Branch: 1 branch
BranchTrueFalseTotal
$merge_base eq $old_commit ? : 0 0 0
42 } || 'R'; # Probably $old_commit and $new_commit do not have a common ancestor.
43 }
44  
45 8 my @acls = eval { $git->grok_acls($CFG, 'CRUD') };
46 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
47 0 $git->fault($@, {ref => $ref});
48 0 return 1;
49 }
50  
51 ACL:
52 8 foreach my $acl (@acls) {
53 5 ▶ next unless ref $acl->{spec} ? $ref =~ $acl->{spec} : $ref eq $acl->{spec};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $acl->{"spec"} ? $ref =~ /$acl->{"spec"}/u : $ref eq $acl->{"spec"} 1 4 5
ref $acl->{'spec'} ? : 4 1 5
54 4 ▶ if (index($acl->{action}, $action) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($acl->{"action"}, $action) != -1) 4 0 4
55 4 ▶ unless ($acl->{allow}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($acl->{"allow"}) 2 2 4
56 2 $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 2 ++$errors;
62 }
63 4 last ACL;
64 }
65 }
66  
67 8 ▶ if ($ref =~ m:^refs/tags/:
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ m[^refs/tags/]u and $git->get_config_boolean($CFG, "require-annotated-tags")) 2 6 8
Condition100%3/3
$ref =~ m[^refs/tags/]u and $git->get_config_boolean($CFG, "require-annotated-tags")
!ll&&!rl&&r
512
MC/DC100%2/2
$ref =~ m[^refs/tags/]u and $git->get_config_boolean($CFG, "require-annotated-tags")
$ref =~ m[^refs/tags/]u $git->get_config_boolean($CFG, "require-annotated-tags")
Truth table100%3/3
A and B
A= $ref =~ m[^refs/tags/]uB= $git->get_config_boolean($CFG, "require-annotated-tags")
ABresult
0X0
100
111
68 && $git->get_config_boolean($CFG => 'require-annotated-tags')) {
69 2 my $rev_type = $git->run('cat-file', '-t', $new_commit);
70 2 ▶ if ($rev_type ne 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev_type ne "tag") 1 1 2
71 1 $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 1 ++$errors;
77 }
78 }
79  
80 8 return $errors;
81 }
82  
83 # Install hooks
84 8 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref);
85  
86 8 1;
87  
88 __END__
« blib/lib/Git/Hooks/CheckJira.pm ↑ Summary blib/lib/Git/Hooks/CheckCommit.pm »