blib/lib/Git/Message.pm

filter: Statementstmt 84.8% 67 / 79 Branchbran 62.5% 20 / 32 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 9 / 9 totaltotal 81.8% 113 / 138 CC 21 SCAR 17.5
CC
21
Cov
79%
new
11.1
footer
6.0
title
4.5
CRAP 5.8
« blib/lib/Git/Hooks/CheckWhitespace.pm ↑ Summary blib/lib/Git/Hooks/PrepareLog.pm »
1 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Message;
4 # ABSTRACT: A Git commit message
5 49 $Git::Message::VERSION = '4.0.0';
6 49 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 49 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 49 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 42 ▶sub new {
Subroutine new: called CC=10 SCAR=24.1
Pod documented
11 42 my ($class, $msg) = @_;
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 42 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 42 ▶ my $title = (@blocks && ($blocks[0] =~ tr/\n/\n/) == 1)
Branch: 1 branch
BranchTrueFalseTotal
@blocks && $blocks[0] =~ tr/\n// == 1 ? : 29 13 42
Condition100%3/3
@blocks and $blocks[0] =~ tr/\n// == 1
!ll&&!rl&&r
11229
MC/DC100%2/2
@blocks and $blocks[0] =~ tr/\n// == 1
@blocks $blocks[0] =~ tr/\n// == 1
Truth table100%3/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 42 my %footer = ();
42  
43 42 ▶ if (my $footer = pop @blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $footer = pop @blocks) 20 22 42
44 20 my $key = '';
45 20 my $in_footer_comment = 0;
46 20 foreach (split /^/m, $footer) {
47 21 ▶ if ($in_footer_comment) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_footer_comment) { } 0 21 21
elsif (/^\[[\w-]+:/iu) { } 0 21 21
elsif (/^([\w-]+):\s*(.*)/iu) { } 3 18 21
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 3 $key = lc $1;
60 3 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 18 push @blocks, $footer;
65 # clean up %footer,
66 18 %footer = ();
67 # and break out of the loop.
68 18 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 42 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
@blocks ? : 19 23 42
79 title => $title,
80 body => @blocks ? join("\n\n", @blocks) : undef,
81 footer => \%footer,
82 } => $class;
83 }
84  
85 48 ▶sub title {
Subroutine title: called CC=3 SCAR=15.1
Pod documented
86 48 my ($self, $title) = @_;
87 48 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 0 48 48
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 48 return $self->{title};
93 }
94  
95 44 ▶sub body {
Subroutine body: called CC=3 SCAR=15.1
Pod documented
96 44 my ($self, $body) = @_;
97 44 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 44 44
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 44 return $self->{body};
103 }
104  
105 4 ▶sub footer {
Subroutine footer: called CC=6 SCAR=18.0
Pod documented
106 4 my ($self) = @_;
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 11 ▶sub get_footer_keys {
Subroutine get_footer_keys: called CC=1 SCAR=0.0
Pod documented
137 11 my ($self) = @_;
138 11 return keys $self->{footer}->%*;
139 }
140  
141 0 ▶sub delete_footer_key {
Subroutine delete_footer_key: not called CC=1 SCAR=6.9
Pod documented
142 0 my ($self, $key) = @_;
143 0 delete $self->{footer}{lc $key};
144 0 return;
145 }
146  
147 29 ▶sub get_footer_values {
Subroutine get_footer_values: called CC=2 SCAR=6.9
Pod documented
148 29 my ($self, $key) = @_;
149 29 ▶ if (my $values = $self->{footer}{lc $key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->{'footer'}{lc $key}) { } 1 28 29
150 1 return map {$_->[1]} grep {ref $_} @$values;
151 } else {
152 28 return ();
153 }
154 }
155  
156 2 ▶sub add_footer_values {
Subroutine add_footer_values: called CC=2 SCAR=7.0
Pod documented
157 2 my ($self, $key, @values) = @_;
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 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 4 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
172 4 my ($self) = @_;
173  
174 4 return join("\n", grep {defined} ($self->title, $self->body, $self->footer));
175 }
176  
177
178 49 1; # End of Git::Message
179  
180 __END__
« blib/lib/Git/Hooks/CheckWhitespace.pm ↑ Summary blib/lib/Git/Hooks/PrepareLog.pm »