blib/lib/Git/Hooks/GerritChangeId.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 71.4% 10 / 14 Conditioncond 35.0% 7 / 20 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 73.6% 84 / 114 CC 15 SCAR 18.3
CC
15
Cov
79%
insert_change_id
12.3
gen_change_id
5.7
rewrite_message
3.1
CRAP 6.3
« blib/lib/Git/Hooks/Notify.pm ↑ Summary blib/lib/Git/Hooks/CheckWhitespace.pm »
1 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::GerritChangeId;
4 # ABSTRACT: Git::Hooks plugin to insert a Change-Id in a commit message
5 17 $Git::Hooks::GerritChangeId::VERSION = '4.0.0';
6 17 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Git::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
15  
16 ##########
17  
18 2 ▶sub gen_change_id {
Subroutine gen_change_id: called CC=5 SCAR=17.4
Pod undocumented
19 2 my ($git, $msg) = @_;
20  
21 2 my $filename = Path::Tiny->tempfile(UNLINK => 1);
22 2 ▶ open my $fh, '>', $filename ## no critic (RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or $git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!}) and croak() 2 0 2
Condition20%1/5
open(my $fh, '>', $filename) || $git->fault("Internal error: can't open '${filename}' for writing:", {'details', $!}) && croak()
l!l&&r!l&&!r
200
$git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!}) and croak()
!ll
00
MC/DC0%0/2
open(my $fh, '>', $filename) || $git->fault("Internal error: can't open '${filename}' for writing:", {'details', $!}) && croak()
open(my $fh, '>', $filename) $git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!})
Truth table33%1/3
A || B
A= open(my $fh, '>', $filename)B= $git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!})
ABresult
1X1
011
000
23 or $git->fault("Internal error: can't open '$filename' for writing:", {details => $!})
24 and croak;
25  
26 2 foreach my $info (
27 [ tree => [qw/write-tree/] ],
28 [ parent => [qw/rev-parse HEAD^0/] ],
29 [ author => [qw/var GIT_AUTHOR_IDENT/] ],
30 [ committer => [qw/var GIT_COMMITTER_IDENT/] ],
31 ) {
32 8 my $value = eval { $git->run($info->[1]->@*) };
33 8 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 8 0 8
34 # It's OK if we can't find value.
35 8 $fh->print("$info->[0] $value");
36 }
37 }
38  
39 2 $fh->print("\n", $msg);
40 2 $fh->close();
41  
42 2 return 'I' . $git->run(qw/hash-object -t blob/, $filename);
43 }
44  
45 33 ▶sub insert_change_id {
Subroutine insert_change_id: called CC=9 SCAR=25.1
Pod undocumented
46 33 my ($git, $msg) = @_;
47  
48 # Does Change-Id: already exist? if so, exit (no change).
49 33 ▶ return if $msg =~ /^Change-Id:/im;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^Change-Id:/imu 20 13 33
50  
51 13 my $cmsg = Git::Message->new($msg);
52  
53 # Don't mess with the message if it's empty.
54 13 ▶ if ( (! defined $cmsg->title || $cmsg->title !~ /\S/)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u) 11 2 13
Condition55%5/9
defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u
l!l&&r!l&&!r
2011
defined $cmsg->title and $cmsg->title =~ /\S/u
!ll&&!rl&&r
1102
defined $cmsg->body and $cmsg->body =~ /\S/u
!ll&&!rl&&r
1100
MC/DC25%1/4
defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u
defined $cmsg->title $cmsg->title =~ /\S/u defined $cmsg->body $cmsg->body =~ /\S/u
Truth table28%2/7
A and B or C and D
A= defined $cmsg->titleB= $cmsg->title =~ /\S/uC= defined $cmsg->bodyD= $cmsg->body =~ /\S/u
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
55 && (! defined $cmsg->body || $cmsg->body !~ /\S/)
56 ) {
57 # (Signed-off-by footers don't count.)
58 11 my @footer = $cmsg->get_footer_keys;
59 11 ▶ return if @footer == 0 || @footer == 1 && $footer[0] eq 'signed-off-by';
Branch: 1 branch
BranchTrueFalseTotal
if @footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by" 11 0 11
Condition16%1/6
@footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by"
l!l&&r!l&&!r
1100
@footer == 1 and $footer[0] eq "signed-off-by"
!ll&&!rl&&r
000
MC/DC0%0/3
@footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by"
@footer == 0 @footer == 1 $footer[0] eq "signed-off-by"
Truth table25%1/4
A or B and C
A= @footer == 0B= @footer == 1C= $footer[0] eq "signed-off-by"
ABCresult
1XX1
0111
00X0
0100
60 }
61  
62 # Insert the Change-Id footer
63 2 $cmsg->add_footer_values('Change-Id' => gen_change_id($git, $cmsg->as_string));
64  
65 2 return $cmsg->as_string;
66 }
67  
68 33 ▶sub rewrite_message {
Subroutine rewrite_message: called CC=3 SCAR=11.3
Pod undocumented
69 33 my ($git, $commit_msg_file) = @_;
70  
71 33 $log->debug(__PACKAGE__ . "::rewrite_message($commit_msg_file)");
72  
73 33 my $msg = eval { $git->read_commit_msg_file($commit_msg_file) };
74 33 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 33 33
75 0 $git->fault("Internal error: cannot read commit message file '$commit_msg_file'",
76 {details => $@});
77 0 return 0;
78 }
79  
80 # Rewrite the message file
81 33 ▶ if (my $new_msg = insert_change_id($git, $msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_msg = insert_change_id($git, $msg)) 2 31 33
82 2 $git->write_commit_msg_file($commit_msg_file, $new_msg);
83 }
84  
85 33 return 1;
86 }
87  
88 # Install hooks
89 17 APPLYPATCH_MSG(\&rewrite_message);
90 17 COMMIT_MSG(\&rewrite_message);
91  
92 17 1;
93  
94 __END__
« blib/lib/Git/Hooks/Notify.pm ↑ Summary blib/lib/Git/Hooks/CheckWhitespace.pm »