| 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
| |||||||||||||||||||||||||||||
| 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'
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'
}
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | if (index($acl->{action}, $action) != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | unless ($acl->{allow}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ref =~ m[^refs/tags/]u and $git->get_config_boolean($CFG, "require-annotated-tags")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||