blib/lib/Git/Message.pm

filter: Statementstmt 70.2% 59 / 84 Branchbran 46.8% 15 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 10 / 13 Podpod 100% 9 / 9 totaltotal 65.7% 94 / 143 CC 21 SCAR 21.3
CC
21
Cov
63%
new
19.0
footer
6.0
get_footer_values
6.0
CRAP 8.4
« blib/lib/Git/Hooks/GerritChangeId.pm ↑ Summary blib/script/find-secret-leakage-in-git-diff.pl »
1 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Message;
4 # ABSTRACT: A Git commit message
5 2 $Git::Message::VERSION = '3.3.1';
6 2 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 2 ▶ my ($class, $msg) = @_;
Subroutine new: called CC=10 SCAR=29.4
Pod documented
12  
13 # We assume that $msg is the contents of a commit message file as
14 # returned by Git::Repository::Plugin::GitHooks::read_commit_msg_file,
15 # i.e., with whitespace cleaned up.
16  
17 # Our first mission is to split it up into blocks of consecutive
18 # non-blank lines separated by blank lines. The blocks all end in
19 # a newline.
20  
21 2 my @blocks = split /(?<=\n)\n+/, $msg;
22  
23 # The message blocks are aggregated in three components: title, body,
24 # and footer.
25  
26 # The title is the first block, but only if it has a single line.
27 # The footer is the last block, but only if it complies with a
28 # strict syntax, which we parse later. The body is comprised by
29 # the blocks in the middle, joined by blank lines. Note that all
30 # three components can be defined or not independently.
31  
32 2 ▶ my $title = (@blocks && ($blocks[0] =~ tr/\n/\n/) == 1)
Branch: 1 branch
BranchTrueFalseTotal
@blocks && $blocks[0] =~ tr/\n// == 1 ? : 2 0 2
Condition33%1/3
@blocks and $blocks[0] =~ tr/\n// == 1
!ll&&!rl&&r
002
MC/DC0%0/2
@blocks and $blocks[0] =~ tr/\n// == 1
@blocks $blocks[0] =~ tr/\n// == 1
Truth table33%1/3
A and B
A= @blocksB= $blocks[0] =~ tr/\n// == 1
ABresult
0X0
100
111
33 ? shift @blocks
34 : undef;
35  
36 # Our second mission is to parse the footer as a set of key:value
37 # specifications, in the same way that Gerrit's commit-msg hook
38 # does (http://goo.gl/tyjri). We parse the footer and populate a
39 # hash.
40  
41 2 my %footer = ();
42  
43 2 ▶ if (my $footer = pop @blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $footer = pop @blocks) 1 1 2
44 1 my $key = '';
45 1 my $in_footer_comment = 0;
46 1 foreach (split /^/m, $footer) {
47 2 ▶ if ($in_footer_comment) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_footer_comment) { } 0 2 2
elsif (/^\[[\w-]+:/iu) { } 0 2 2
elsif (/^([\w-]+):\s*(.*)/iu) { } 2 0 2
48 # A footer comment may span multiple lines and we
49 # simply keep appending them to what came previously.
50 0 $footer{$key}[-1] .= $_;
51 # A line ending in a ']' marks the end of the comment.
52 0 ▶ $in_footer_comment = 0 if /\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if /\]$/u 0 0 0
53 } elsif (/^\[[\w-]+:/i) {
54 # A line beginning with '[key:' starts a comment.
55 0 push @{$footer{$key}}, $_;
56 0 $in_footer_comment = 1;
57 } elsif (/^([\w-]+):\s*(.*)/i) {
58 # This is a key:value line
59 2 $key = lc $1;
60 2 push @{$footer{$key}}, [$1, $2];
61 } else {
62 # Oops. This is not a valid footer. So, let's push
63 # $footer back to @blocks,
64 0 push @blocks, $footer;
65 # clean up %footer,
66 0 %footer = ();
67 # and break out of the loop.
68 0 last;
69 }
70 }
71 # What should we do if $in_footer_comment is still true here?
72 # I think it's too drastic to consider the block a non-footer
73 # in this case. But I'm not sure about what to do with the
74 # unfinished comment we're reading. For now I'll leave it
75 # unfinished there.
76 }
77  
78 2 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
@blocks ? : 0 2 2
79 title => $title,
80 body => @blocks ? join("\n\n", @blocks) : undef,
81 footer => \%footer,
82 } => $class;
83 }
84  
85 sub title {
86 8 ▶ my ($self, $title) = @_;
Subroutine title: called CC=3 SCAR=15.1
Pod documented
87 8 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 0 8 8
88 0 ▶ $title =~ /^[^\n]+\n$/s
Branch: 1 branch
BranchTrueFalseTotal
$title =~ /^[^\n]+\n$/su or croak("A title must be a single line ending in a newline.\n") 0 0 0
89 or croak "A title must be a single line ending in a newline.\n";
90 0 $self->{title} = $title;
91 }
92 8 return $self->{title};
93 }
94  
95 sub body {
96 4 ▶ my ($self, $body) = @_;
Subroutine body: called CC=3 SCAR=15.1
Pod documented
97 4 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 4 4
98 0 ▶ $body =~ /\n$/s
Branch: 1 branch
BranchTrueFalseTotal
$body =~ /\n$/su or croak("A body must be end in a newline.\n") 0 0 0
99 or croak "A body must be end in a newline.\n";
100 0 $self->{body} = $body;
101 }
102 4 return $self->{body};
103 }
104  
105 sub footer {
106 4 ▶ my ($self) = @_;
Subroutine footer: called CC=6 SCAR=18.0
Pod documented
107  
108 # Reconstruct the footer. The keys are ordered lexicographically,
109 # except that the 'Signed-off-by' key must be the last one.
110  
111 4 my $footer = $self->{footer};
112 4 ▶ return unless %$footer;
Branch: 1 branch
BranchTrueFalseTotal
unless %$footer 1 3 4
113 3 my $foot = '';
114 3 my @keys;
115 3 ▶ if (my $signoff = delete $footer->{'signed-off-by'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $signoff = delete $footer->{'signed-off-by'}) { } 2 1 3
116 2 @keys = sort keys %$footer;
117 2 push @keys, 'signed-off-by';
118 2 $footer->{'signed-off-by'} = $signoff;
119 } else {
120 1 @keys = sort keys %$footer;
121 }
122 3 foreach my $key (@keys) {
123 4 foreach my $line (@{$footer->{$key}}) {
124 6 ▶ if (ref $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $line) { } 6 0 6
125 6 $foot .= join(': ', @$line);
126 } else {
127 0 $foot .= $line;
128 }
129 6 $foot .= "\n";
130 }
131 }
132  
133 3 return $foot;
134 }
135  
136 sub get_footer_keys {
137 0 ▶ my ($self) = @_;
Subroutine get_footer_keys: not called CC=1 SCAR=6.9
Pod documented
138 0 return keys %{$self->{footer}};
139 }
140  
141 sub delete_footer_key {
142 0 ▶ my ($self, $key) = @_;
Subroutine delete_footer_key: not called CC=1 SCAR=6.9
Pod documented
143 0 delete $self->{footer}{lc $key};
144 0 return;
145 }
146  
147 sub get_footer_values {
148 0 ▶ my ($self, $key) = @_;
Subroutine get_footer_values: not called CC=2 SCAR=17.9
Pod documented
149 0 ▶ if (my $values = $self->{footer}{lc $key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->{'footer'}{lc $key}) { } 0 0 0
150 0 return map {$_->[1]} grep {ref $_} @$values;
151 } else {
152 0 return ();
153 }
154 }
155  
156 sub add_footer_values {
157 2 ▶ my ($self, $key, @values) = @_;
Subroutine add_footer_values: called CC=2 SCAR=7.0
Pod documented
158 2 ▶ croak "Malformed footer key: '$key'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^[\w-]+$/iu 0 2 2
159 unless $key =~ /^[\w-]+$/i;
160  
161 ## no critic (BuiltinFunctions::ProhibitComplexMappings)
162 2 push @{$self->{footer}{lc $key}},
163 2 map { [$key => $_] }
164 2 map { s/\n+$//r } # strip trailing newlines to keep the footer structure
165 @values;
166 ## use critic
167  
168 2 return;
169 }
170  
171 sub as_string {
172 4 ▶ my ($self) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
173  
174 4 return join("\n", grep {defined} ($self->title, $self->body, $self->footer));
175 }
176  
177
178 2 1; # End of Git::Message
179  
180 __END__
« blib/lib/Git/Hooks/GerritChangeId.pm ↑ Summary blib/script/find-secret-leakage-in-git-diff.pl »