blib/lib/App/GitHooks/Utils.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 75.0% 12 / 16 Conditioncond 62.5% 10 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 83.9% 94 / 112 CC 15 SCAR 15.0
CC
15
Cov
88%
__ANON__
8.9
get_ticket_id_from_branch_name
4.2
get_project_prefix_regex
3.0
CRAP 4.5
« /home/cpancover/.cpan/build/App-GitHooks-v1.7.3-0/bin/githooks ↑ Summary Build »
1 package App::GitHooks::Utils;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # External dependencies.
7 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # No internal dependencies - keep this as a leaf module in the graph so that we
12 # can include it everywhere.
13  
14  
15 =head1 NAME
16  
17 App::GitHooks::Utils - Support functions for App::GitHooks and its plugins.
18  
19  
20 =head1 VERSION
21  
22 Version 1.7.3
23  
24 =cut
25  
26 20 our $VERSION = '1.7.3';
27  
28  
29 =head2 get_project_prefixes()
30  
31 Get an arrayref of valid project prefixes.
32  
33 my $project_prefixes = App::GitHooks::Utils::get_project_prefixes( $app );
34  
35 Arguments:
36  
37 =over 4
38  
39 =item * $app
40  
41 An C<App::GitHooks> instance.
42  
43 =back
44  
45 =cut
46  
47 sub get_project_prefixes
48 {
49 13 ▶ my ( $app ) = @_;
Subroutine get_project_prefixes: called CC=2 SCAR=6.9
Pod documented
50 13 ▶ my $config_line = $app->get_config()->{'_'}->{'project_prefixes'} // '';
Condition100%2/2
$app->get_config->{"_"}{"project_prefixes"} // ""
l!l
103
MC/DC100%1/1
$app->get_config->{"_"}{"project_prefixes"} // ""
$app->get_config->{"_"}{"project_prefixes"}
Truth table100%2/2
A
A= $app->get_config->{"_"}{"project_prefixes"}
Aresult
11
00
51  
52 # Strip leading/trailing whitespace.
53 13 $config_line =~ s/(?:^\s+|\s+$)//g;
54  
55 13 return [ split( /\s*[, ]\s*/, $config_line ) ];
56 }
57  
58  
59 =head2 get_project_prefix_regex()
60  
61 Return a non-capturing regex that will match all the valid project prefixes.
62  
63 my $project_prefix_regex = App::GitHooks::Utils::get_project_prefix_regex( $app );
64  
65 Arguments:
66  
67 =over 4
68  
69 =item * $app
70  
71 An C<App::GitHooks> instance.
72  
73 =back
74  
75 =cut
76  
77 sub get_project_prefix_regex
78 {
79 9 ▶ my ( $app ) = @_;
Subroutine get_project_prefix_regex: called CC=3 SCAR=11.0
Pod documented
80  
81 9 my $prefixes = get_project_prefixes( $app );
82  
83 9 ▶ if ( scalar( @$prefixes ) == 0 )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$prefixes == 0) { } 3 6 9
elsif (scalar @$prefixes == 1) { } 1 5 6
84 {
85 3 return '';
86 }
87 elsif ( scalar( @$prefixes ) == 1 )
88 {
89 1 return $prefixes->[0];
90 }
91 else
92 {
93 5 return '(?:' . join( '|', @$prefixes ) . ')';
94 }
95 }
96  
97  
98 =head2 get_ticket_id_from_commit_regex()
99  
100 Return a regex that will extract a ticket ID from a commit message, if it
101 exists.
102  
103 my $ticket_id_regex = App::GitHooks::Utils::get_ticket_id_from_commit_regex( $app );
104  
105 Arguments:
106  
107 =over 4
108  
109 =item * $app
110  
111 An C<App::GitHooks> instance.
112  
113 =back
114  
115 =cut
116  
117 sub get_ticket_id_from_commit_regex
118 {
119 2 ▶ my ( $app ) = @_;
Subroutine get_ticket_id_from_commit_regex: called CC=2 SCAR=6.9
Pod documented
120 2 my $config = $app->get_config();
121  
122 # Retrieve the regular expression from the config or use a default.
123 2 ▶ my $ticket_regex = $app->get_config()->get_regex( '_', 'extract_ticket_id_from_commit' )
Condition100%2/2
$app->get_config->get_regex("_", "extract_ticket_id_from_commit") // "^(\$project_prefixes-\\d+|--)\\: ?"
l!l
11
MC/DC100%1/1
$app->get_config->get_regex("_", "extract_ticket_id_from_commit") // "^(\$project_prefixes-\\d+|--)\\: ?"
$app->get_config->get_regex("_", "extract_ticket_id_from_commit")
Truth table100%2/2
A
A= $app->get_config->get_regex("_", "extract_ticket_id_from_commit")
Aresult
11
00
124 // '^($project_prefixes-\d+|--)\: ?';
125  
126 # Replace the list of project prefixes if it is mentioned in the regex.
127 2 my $project_prefix_regex = get_project_prefix_regex( $app );
128 2 $ticket_regex =~ s/\$project_prefixes/$project_prefix_regex/g;
129  
130 2 return $ticket_regex;
131 }
132  
133  
134 =head2 get_ticket_id_from_branch_name()
135  
136 Return the ticket ID derived from the name of the current branch for this
137 repository.
138  
139 my $ticket_id = App::GitHooks::Utils::get_ticket_id_from_branch_name( $app );
140  
141 Arguments:
142  
143 =over 4
144  
145 =item * $app
146  
147 An C<App::GitHooks> instance.
148  
149 =back
150  
151 =cut
152  
153 sub get_ticket_id_from_branch_name
154 {
155 4 ▶ my ( $app ) = @_;
Subroutine get_ticket_id_from_branch_name: called CC=4 SCAR=14.4
Pod documented
156 4 my $repository = $app->get_repository();
157 4 my $config = $app->get_config();
158  
159 # If the config doesn't specify a way to extract the ticket ID from the
160 # branch, there's nothing we can do here.
161 4 my $ticket_regex = $config->get_regex( '_', 'extract_ticket_id_from_branch' );
162 return undef
163 4 ▶ if !defined( $ticket_regex );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ticket_regex 1 3 4
164  
165 # Check if we're in a rebase. During a rebase (regardless of whether it's
166 # interractive or not), the HEAD goes in a detached state, and we won't be
167 # able to call symbolic-ref on it to get a branch name.
168 3 my $git_directory = $repository->git_dir();
169 return undef
170 3 ▶ if ( -d File::Spec->catfile( $git_directory, 'rebase-merge' ) ) # detect rebase -i
Branch: 1 branch
BranchTrueFalseTotal
if -d "File::Spec"->catfile($git_directory, "rebase-merge") or -d "File::Spec"->catfile($git_directory, "rebase-apply") 0 3 3
Condition33%1/3
-d "File::Spec"->catfile($git_directory, "rebase-merge") or -d "File::Spec"->catfile($git_directory, "rebase-apply")
l!l&&r!l&&!r
003
MC/DC0%0/2
-d "File::Spec"->catfile($git_directory, "rebase-merge") or -d "File::Spec"->catfile($git_directory, "rebase-apply")
-d "File::Spec"->catfile($git_directory, "rebase-merge") -d "File::Spec"->catfile($git_directory, "rebase-apply")
Truth table33%1/3
A or B
A= -d "File::Spec"->catfile($git_directory, "rebase-merge")B= -d "File::Spec"->catfile($git_directory, "rebase-apply")
ABresult
1X1
011
000
171 || ( -d File::Spec->catfile( $git_directory, 'rebase-apply' ) ); # detect rebase
172  
173 3 my $ticket_id;
174 try
175 {
176  
177 # Retrieve the branch name.
178 3 ▶ my $branch_name = $repository->run('symbolic-ref', 'HEAD');
Subroutine __ANON__: called CC=8 SCAR=21.9
179 3 my ( $branch_name_without_prefixes ) = $branch_name =~ /([^\/]+)$/;
180  
181 # Extract the ticket ID from the branch name.
182 3 my $project_prefix_regex = get_project_prefix_regex( $app );
183 3 $ticket_regex =~ s/\$project_prefixes/$project_prefix_regex/g;
184 3 ( $ticket_id ) = $branch_name_without_prefixes =~ /$ticket_regex/i;
185  
186 3 my $normalize = $config->get( '_', 'normalize_branch_ticket_id' );
187 3 ▶ if ( defined( $ticket_id ) && defined( $normalize ) && ( $normalize =~ /\S/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ticket_id and defined $normalize and $normalize =~ /\S/) 1 2 3
Condition66%4/6
defined $ticket_id and defined $normalize and $normalize =~ /\S/
!ll&&!rl&&r
201
defined $ticket_id and defined $normalize
!ll&&!rl&&r
021
MC/DC33%1/3
defined $ticket_id and defined $normalize and $normalize =~ /\S/
defined $ticket_id defined $normalize $normalize =~ /\S/
Truth table50%2/4
A and B and C
A= defined $ticket_idB= defined $normalizeC= $normalize =~ /\S/
ABCresult
0XX0
10X0
1100
1111
188 {
189 1 my ( $match, $replacement ) = $normalize =~ m|^\s*s/(.*?)(?<!\\)/(.*)/\s*|x;
190 1 ▶ croak "Invalid format for 'normalize_branch_ticket_id' in configuration file."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match and defined $replacement 0 1 1
Condition33%1/3
defined $match and defined $replacement
!ll&&!rl&&r
001
MC/DC0%0/2
defined $match and defined $replacement
defined $match defined $replacement
Truth table33%1/3
A and B
A= defined $matchB= defined $replacement
ABresult
0X0
100
111
191 if !defined( $match ) || !defined( $replacement );
192 1 ▶ croak "Unsafe matching pattern in 'normalize_branch_ticket_id', escape your slashes"
Branch: 1 branch
BranchTrueFalseTotal
if $match =~ m[(?<!\\)/] 0 1 1
193 if $match =~ /(?<!\\)\//;
194 1 ▶ croak "Unsafe replacement pattern in 'normalize_branch_ticket_id', escape your slashes"
Branch: 1 branch
BranchTrueFalseTotal
if $replacement =~ m[(?<!\\)/] 0 1 1
195 if $replacement =~ /(?<!\\)\//;
196 1 eval( "\$ticket_id =~ s/$match/$replacement/i" ); ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
197 }
198 }
199 catch
200 {
201 0 ▶ carp "ERROR: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 3 };
203  
204 3 return $ticket_id;
205 }
206  
207  
208 =head1 BUGS
209  
210 Please report any bugs or feature requests through the web interface at
211 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
212 I will be notified, and then you'll automatically be notified of progress on
213 your bug as I make changes.
214  
215  
216 =head1 SUPPORT
217  
218 You can find documentation for this module with the perldoc command.
219  
220 perldoc App::GitHooks::Utils
221  
222  
223 You can also look for information at:
224  
225 =over
226  
227 =item * GitHub's request tracker
228  
229 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
230  
231 =item * AnnoCPAN: Annotated CPAN documentation
232  
233 L<http://annocpan.org/dist/app-githooks>
234  
235 =item * CPAN Ratings
236  
237 L<http://cpanratings.perl.org/d/app-githooks>
238  
239 =item * MetaCPAN
240  
241 L<https://metacpan.org/release/App-GitHooks>
242  
243 =back
244  
245  
246 =head1 AUTHOR
247  
248 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
249 C<< <aubertg at cpan.org> >>.
250  
251  
252 =head1 COPYRIGHT & LICENSE
253  
254 Copyright 2013-2015 Guillaume Aubert.
255  
256 This program is free software: you can redistribute it and/or modify it under
257 the terms of the GNU General Public License version 3 as published by the Free
258 Software Foundation.
259  
260 This program is distributed in the hope that it will be useful, but WITHOUT ANY
261 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
262 PARTICULAR PURPOSE. See the GNU General Public License for more details.
263  
264 You should have received a copy of the GNU General Public License along with
265 this program. If not, see http://www.gnu.org/licenses/
266  
267 =cut
268  
269 20 1;
« /home/cpancover/.cpan/build/App-GitHooks-v1.7.3-0/bin/githooks ↑ Summary Build »