blib/lib/Git/Hooks/GerritChangeId.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 57.1% 8 / 14 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 65.5% 76 / 116 CC 15 SCAR 24.9
CC
15
Cov
71%
insert_change_id
28.4
gen_change_id
5.6
rewrite_message
3.1
CRAP 12.0
« lib/Git/Hooks/Test.pm ↑ Summary blib/lib/Git/Message.pm »
1 1 ▶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 1 $Git::Hooks::GerritChangeId::VERSION = '3.5.0';
6 1 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Git::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
15  
16 ##########
17  
18 sub gen_change_id {
19 2 ▶ my ($git, $msg) = @_;
Subroutine gen_change_id: called CC=5 SCAR=17.2
Pod undocumented
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() 0 2 2
Condition20%1/5
open(my $fh, '>', $filename) || $git->fault("Internal error: can't open '${filename}' for writing:", {'details', $!}) && croak()
l!l
20
$git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!}) and croak()
!ll&&!rl&&r
000
MC/DC0%0/3
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", $!}) croak()
Truth table0%0/4
A || B and C
A= open(my $fh, '>', $filename)B= $git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!})C= croak()
ABCresult
1XX1
0111
00X0
0100
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 sub insert_change_id {
46 17 ▶ my ($git, $msg) = @_;
Subroutine insert_change_id: called CC=9 SCAR=33.5
Pod undocumented
47  
48 # Does Change-Id: already exist? if so, exit (no change).
49 17 ▶ return if $msg =~ /^Change-Id:/im;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^Change-Id:/imu 15 2 17
50  
51 2 my $cmsg = Git::Message->new($msg);
52  
53 # Don't mess with the message if it's empty.
54 2 ▶ 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) 0 2 2
Condition22%2/9
defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u
l!l&&r!l&&!r
200
defined $cmsg->title and $cmsg->title =~ /\S/u
!ll&&!rl&&r
002
defined $cmsg->body and $cmsg->body =~ /\S/u
!ll&&!rl&&r
000
MC/DC0%0/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 table14%1/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 0 my @footer = $cmsg->get_footer_keys;
59 0 ▶ 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" 0 0 0
Condition0%0/6
@footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by"
l!l&&r!l&&!r
000
@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 table0%0/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 sub rewrite_message {
69 17 ▶ my ($git, $commit_msg_file) = @_;
Subroutine rewrite_message: called CC=3 SCAR=11.3
Pod undocumented
70  
71 17 $log->debug(__PACKAGE__ . "::rewrite_message($commit_msg_file)");
72  
73 17 my $msg = eval { $git->read_commit_msg_file($commit_msg_file) };
74 17 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 17 17
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 17 ▶ if (my $new_msg = insert_change_id($git, $msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_msg = insert_change_id($git, $msg)) 2 15 17
82 2 $git->write_commit_msg_file($commit_msg_file, $new_msg);
83 }
84  
85 17 return 1;
86 }
87  
88 # Install hooks
89 1 APPLYPATCH_MSG(\&rewrite_message);
90 1 COMMIT_MSG(\&rewrite_message);
91  
92 1 1;
93  
94 __END__
« lib/Git/Hooks/Test.pm ↑ Summary blib/lib/Git/Message.pm »