Flux/Utils.pm

filter: Statementstmt 7.5% 13 / 172 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 12.1% 4 / 33 Podpod 100% 28 / 28 totaltotal 14.3% 45 / 314 CC 32 SCAR 24.5
CC
32
Cov
5%
git_compare_branches
30.0
gitflux_resolve_nameprefix
30.0
gitflux_is_initialized
20.0
CRAP 11.6
« Flux/Command/feature.pm ↑ Summary Flux.pm »
1 package Git::Flux::Utils;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use mixin::with 'Git::Flux';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use List::MoreUtils 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub git_local_branches {
Subroutine git_local_branches: not called CC=1 SCAR=6.9
Pod documented
9 0 my $self = shift;
10 0 my $repo = $self->{'repo'};
11 0 my @branches = map { $_ =~ s/^\*?\s+//; $_; }
12 $repo->run( branch => '--no-color' );
13  
14 0 return @branches;
15 }
16  
17 0 ▶sub git_remote_branches {
Subroutine git_remote_branches: not called CC=1 SCAR=6.9
Pod documented
18 0 my $self = shift;
19 0 my $repo = $self->{'repo'};
20 0 my @branches = map { $_ =~ s/^\*?\s+//; $_; }
21 $repo->run( branch => '-r', '--no-color' );
22  
23 0 return @branches;
24 }
25  
26 0 ▶sub git_all_branches {
Subroutine git_all_branches: not called CC=1 SCAR=6.9
Pod documented
27 0 my $self = shift;
28 0 my $repo = $self->{'repo'};
29 0 my @branches = ( $self->git_local_branches, $self->git_remote_branches );
30  
31 0 return @branches;
32 }
33  
34 0 ▶sub git_all_tags {
Subroutine git_all_tags: not called CC=1 SCAR=6.9
Pod documented
35 0 my $self = shift;
36 0 my $repo = $self->{'repo'};
37 0 my @tags = $repo->run('tag');
38  
39 0 return @tags;
40 }
41  
42 0 ▶sub git_current_branch {
Subroutine git_current_branch: not called CC=1 SCAR=6.9
Pod documented
43 0 my $self = shift;
44 0 my $repo = $self->{'repo'};
45  
46 0 my ($branch) = map { $_ =~ s/^\*\s+//g; $_; }
47 0 grep { $_ !~ /no branch/ }
48 0 grep { $_ =~ /^\*\s/ }
49 $repo->run( branch => '--no-color' );
50  
51 0 return $branch;
52 }
53  
54 0 ▶sub git_is_clean_working_tree {
Subroutine git_is_clean_working_tree: not called CC=3 SCAR=24.8
Pod documented
55 0 my $self = shift;
56 0 my $repo = $self->{'repo'};
57  
58 0 my $cmd = $repo->command(
59 diff => qw/ --no-ext-diff --ignore-submodules --quiet --exit-code /
60 );
61 0 $cmd->close;
62 0 my $diff = $cmd->exit;
63  
64 0 $cmd = $repo->command(
65 'diff-index' => qw/ --cached --quiet --ignore-submodules HEAD -- /
66 );
67 0 $cmd->close;
68 0 my $diff_index = $cmd->exit;
69  
70 0 ▶ $diff and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$diff and return 1 0 0 0
71 0 ▶ $diff_index and return 2;
Branch: 1 branch
BranchTrueFalseTotal
$diff_index and return 2 0 0 0
72  
73 0 return 0;
74 }
75  
76 0 ▶sub git_repo_is_headless {
Subroutine git_repo_is_headless: not called CC=1 SCAR=6.9
Pod documented
77 0 my $self = shift;
78 0 my $repo = $self->{'repo'};
79 0 my $result = $repo->run( 'rev-parse' => qw/ --quiet --verify HEAD / );
80  
81 0 return not $result;
82 }
83  
84 0 ▶sub git_local_branch_exists {
Subroutine git_local_branch_exists: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 0 my $branch = shift;
87 0 my @branches = $self->git_local_branches;
88  
89 0 grep { $_ eq $branch } @branches;
90 }
91  
92 0 ▶sub git_branch_exists {
Subroutine git_branch_exists: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
94 0 my $branch = shift;
95 0 my @branches = $self->git_all_branches;
96  
97 0 grep { $_ eq $branch } @branches;
98 }
99  
100 0 ▶sub git_tag_exists {
Subroutine git_tag_exists: not called CC=1 SCAR=6.9
Pod documented
101 0 my $self = shift;
102 0 my $tag = shift;
103 0 my @tags = $self->git_all_tags;
104  
105 0 grep { $_ eq $tag } @tags;
106 }
107  
108 # Tests whether branches and their "origin" counterparts have diverged and need
109 # merging first. It returns error codes to provide more detail, like so:
110 #
111 # 0 Branch heads point to the same commit
112 # 1 First given branch needs fast-forwarding
113 # 2 Second given branch needs fast-forwarding
114 # 3 Branch needs a real merge
115 # 4 There is no merge base, i.e. the branches have no common ancestors
116 0 ▶sub git_compare_branches {
Subroutine git_compare_branches: not called CC=5 SCAR=34.0
Pod documented
117 0 my $self = shift;
118 0 my ( $c1, $c2 ) = @_;
119 0 my $repo = $self->{'repo'};
120  
121 0 my $commit1 = $repo->run( 'rev-parse' => $c1 );
122 0 my $commit2 = $repo->run( 'rev-parse' => $c2 );
123  
124 0 ▶ if ( $commit1 != $commit2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit1 != $commit2) { } 0 0 0
125 0 my $cmd = $repo->command( 'merge-base' => $c1, $c2 );
126 0 $cmd->close;
127  
128 0 ▶ $cmd->exit == 0 and return 4;
Branch: 1 branch
BranchTrueFalseTotal
$cmd->exit == 0 and return 4 0 0 0
129 0 ▶ $commit1 eq $cmd->stdout and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$commit1 eq $cmd->stdout and return 1 0 0 0
130 0 ▶ $commit2 eq $cmd->stdout and return 2;
Branch: 1 branch
BranchTrueFalseTotal
$commit2 eq $cmd->stdout and return 2 0 0 0
131  
132 0 return 3;
133 } else {
134 0 return 0;
135 }
136 }
137  
138 # Checks whether branch $1 is succesfully merged into $2
139 0 ▶sub git_is_branch_merged_into {
Subroutine git_is_branch_merged_into: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 my ( $subject, $base ) = @_;
142 0 my $repo = $self->{'repo'};
143  
144 0 my @all_merges = map { $_ =~ s/^\*?\s+//; $_; }
145 $repo->run(
146 branch => qw/ --no-color --contains /, $subject
147 );
148  
149 0 return grep { $_ eq $base } @all_merges;
150 }
151  
152 0 ▶sub gitflux_has_master_configured {
Subroutine gitflux_has_master_configured: not called CC=3 SCAR=24.8
Pod documented
153 0 my $self = shift;
154 0 my $repo = $self->{'repo'};
155 0 my $master = $repo->run( config => qw/ --get gitflux.branch.master / );
156  
157 0 ▶ return ( defined $master and $master ne '' ) &&
Condition0%0/6
defined $master && $master ne '' && $self->git_local_branch_exists($master)
!ll&&!rl&&r
000
defined $master and $master ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $master && $master ne '' && $self->git_local_branch_exists($master)
defined $master $master ne "" $self->git_local_branch_exists($master)
Truth table0%0/4
A and B && C
A= defined $masterB= $master ne ""C= $self->git_local_branch_exists($master)
ABCresult
0XX0
10X0
1100
1111
158 $self->git_local_branch_exists($master);
159 }
160  
161 0 ▶sub gitflux_has_devel_configured {
Subroutine gitflux_has_devel_configured: not called CC=3 SCAR=24.8
Pod documented
162 0 my $self = shift;
163 0 my $repo = $self->{'repo'};
164 0 my $devel = $repo->run( config => qw/ --get gitflux.branch.devel / );
165  
166 0 ▶ return ( defined $devel and $devel ne '' ) &&
Condition0%0/6
defined $devel && $devel ne '' && $self->git_local_branch_exists($devel)
!ll&&!rl&&r
000
defined $devel and $devel ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $devel && $devel ne '' && $self->git_local_branch_exists($devel)
defined $devel $devel ne "" $self->git_local_branch_exists($devel)
Truth table0%0/4
A and B && C
A= defined $develB= $devel ne ""C= $self->git_local_branch_exists($devel)
ABCresult
0XX0
10X0
1100
1111
167 $self->git_local_branch_exists($devel);
168 }
169  
170 0 ▶sub gitflux_has_prefixes_configured {
Subroutine gitflux_has_prefixes_configured: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172 0 my $repo = $self->{'repo'};
173 my @results = map {
174 0 $repo->run( config => '--get', "gitflux.prefix.$_" );
175 } qw/ feature release hotfix support versiontag /;
176  
177 0 ▶ return all { defined $_ and $_ } @results;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ 0 0 0
178 }
179  
180 0 ▶sub gitflux_is_initialized {
Subroutine gitflux_is_initialized: not called CC=4 SCAR=30.0
Pod documented
181 0 my $self = shift;
182 0 my $repo = $self->{'repo'};
183  
184 0 ▶ return $self->gitflux_has_master_configured &&
Condition0%0/9
$self->gitflux_has_master_configured && $self->gitflux_has_devel_configured && $repo->run('config', ('--get', 'gitflux.branch.master')) ne $repo->run('config', ('--get', 'gitflux.branch.devel')) && $self->gitflux_has_prefixes_configured
!ll&&!rl&&r
000
$self->gitflux_has_master_configured and $self->gitflux_has_devel_configured and $repo->run("config", ("--get", "gitflux.branch.master")) ne $repo->run("config", ("--get", "gitflux.branch.devel"))
!ll&&!rl&&r
000
$self->gitflux_has_master_configured and $self->gitflux_has_devel_configured
!ll&&!rl&&r
000
MC/DC0%0/4
$self->gitflux_has_master_configured && $self->gitflux_has_devel_configured && $repo->run('config', ('--get', 'gitflux.branch.master')) ne $repo->run('config', ('--get', 'gitflux.branch.devel')) && $self->gitflux_has_prefixes_configured
$self->gitflux_has_master_configured $self->gitflux_has_devel_configured $repo->run("config", ("--get", "gitflux.branch.master")) ne $repo->run("config", ("--get", "gitflux.branch.devel")) $self->gitflux_has_prefixes_configured
Truth table0%0/5
A and B and C && D
A= $self->gitflux_has_master_configuredB= $self->gitflux_has_devel_configuredC= $repo->run("config", ("--get", "gitflux.branch.master")) ne $repo->run("config", ("--get", "gitflux.branch.devel"))D= $self->gitflux_has_prefixes_configured
ABCDresult
0XXX0
10XX0
110X0
11100
11111
185 $self->gitflux_has_devel_configured &&
186 (
187 $repo->run( config => qw/ --get gitflux.branch.master / ) ne
188 $repo->run( config => qw/ --get gitflux.branch.devel / )
189 ) &&
190 $self->gitflux_has_prefixes_configured;
191 }
192  
193 0 ▶sub gitflux_load_settings {
Subroutine gitflux_load_settings: not called CC=2 SCAR=17.9
Pod documented
194 0 my $self = shift;
195 0 my $repo = $self->{'repo'};
196  
197 0 $self->{'dit_git_dir'} = $repo->run( 'rev-parse' => '--git-dir' );
198  
199 0 $self->{'master_branch'} = $repo->run(
200 config => qw/ --get gitflux.branch.master /
201 );
202  
203 0 $self->{'devel_branch'} = $repo->run(
204 config => qw/ --get gitflux.branch.devel /
205 );
206  
207 0 ▶ $self->{'origin_branch'} = $repo->run(
Condition0%0/2
$repo->run('config', ('--get', 'gitflux.origin')) || 'origin'
l!l
00
MC/DC0%0/1
$repo->run('config', ('--get', 'gitflux.origin')) || 'origin'
$repo->run('config', ('--get', 'gitflux.origin'))
Truth table0%0/2
A
A= $repo->run('config', ('--get', 'gitflux.origin'))
Aresult
11
00
208 config => qw/ --get gitflux.origin /
209 ) || 'origin';
210 }
211  
212 # Inputs:
213 # $1 = name prefix to resolve
214 # $2 = branch prefix to use
215 #
216 # Searches branch names from git_local_branches() to look for a unique
217 # branch name whose name starts with the given name prefix.
218 #
219 # There are multiple exit codes possible:
220 # 0: The unambiguous full name of the branch is written to stdout
221 # (success)
222 # 1: No match is found.
223 # 2: Multiple matches found. These matches are written to stderr
224 0 ▶sub gitflux_resolve_nameprefix {
Subroutine gitflux_resolve_nameprefix: not called CC=5 SCAR=34.0
Pod documented
225 0 my $self = shift;
226 0 my ( $name, $prefix ) = @_;
227 0 my $repo = $self->{'repo'};
228  
229 # check for perfect match
230 0 ▶ if ( $self->git_local_branch_exists( $prefix . $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->git_local_branch_exists($prefix . $name)) 0 0 0
231 0 print "$name\n";
232 0 return 0;
233 }
234  
235 0 my @matches = grep { $_ =~ /^$prefix$name/ }
236 $self->git_local_branches;
237  
238  
239 0 ▶ if ( @matches == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@matches == 0) { } 0 0 0
240 0 warn "No branch matches prefix '$name'\n";
241 0 return 1;
242 } else {
243 0 ▶ if ( @matches == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches == 1) { } 0 0 0
244 0 my $match = shift @matches;
245 0 $match =~ s/^\Q$prefix\E//;
246 0 warn "$match\n";
247  
248 0 return 0;
249 } else {
250 0 warn "Multiple branches match prefix '$name':\n";
251 0 warn "- $_\n" for @matches;
252  
253 0 return 2;
254 }
255 }
256 }
257  
258 0 ▶sub require_git_repo {
Subroutine require_git_repo: not called CC=1 SCAR=6.9
Pod documented
259 0 Git::Repository->new;
260 }
261  
262 0 ▶sub require_gitflux_initialized {
Subroutine require_gitflux_initialized: not called CC=2 SCAR=17.9
Pod documented
263 0 my $self = shift;
264  
265 0 ▶ $self->gitflux_is_initialized
Branch: 1 branch
BranchTrueFalseTotal
$self->gitflux_is_initialized or die "fatal: Not a gitflux-enabled repo yet. Please run \"git flux init\" first.\n" 0 0 0
266 or die 'fatal: Not a gitflux-enabled repo yet. ' .
267 qq{Please run "git flux init" first.\n};
268 }
269  
270 0 ▶sub require_clean_working_tree {
Subroutine require_clean_working_tree: not called CC=3 SCAR=24.8
Pod documented
271 0 my $self = shift;
272 0 my $result = $self->git_is_clean_working_tree;
273  
274 0 ▶ $result eq 1
Branch: 1 branch
BranchTrueFalseTotal
$result eq 1 and die "fatal: Working tree contains unstaged changes. Aborting.\n" 0 0 0
275 and die "fatal: Working tree contains unstaged changes. Aborting.\n";
276  
277 0 ▶ $result eq 2
Branch: 1 branch
BranchTrueFalseTotal
$result eq 2 and die "fatal: Index contains uncommitted changes. Aborting.\n" 0 0 0
278 and die "fatal: Index contains uncommitted changes. Aborting.\n";
279 }
280  
281 0 ▶sub require_local_branch {
Subroutine require_local_branch: not called CC=2 SCAR=17.9
Pod documented
282 0 my $self = shift;
283 0 my $br = shift;
284  
285 0 ▶ $self->git_local_branch_exists($br)
Branch: 1 branch
BranchTrueFalseTotal
$self->git_local_branch_exists($br) or die "fatal: Local branch '${br}' does not exist and is required.\n" 0 0 0
286 or die "fatal: Local branch '$br' does not exist and is required.\n";
287 }
288  
289 0 ▶sub require_remote_branch {
Subroutine require_remote_branch: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 my $br = shift;
292  
293 0 ▶ grep { $_ eq $br } $self->git_remote_branches
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $br;} $self->git_remote_branches or die "Remote branch '${br}' does not exist and is required.\n" 0 0 0
294 or die "Remote branch '$br' does not exist and is required.\n";
295 }
296  
297 0 ▶sub require_branch {
Subroutine require_branch: not called CC=2 SCAR=17.9
Pod documented
298 0 my $self = shift;
299 0 my $br = shift;
300  
301 0 ▶ grep { $_ eq $br } $self->git_all_branches
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $br;} $self->git_all_branches or die "Branch '${br}' does not exist and is required.\n" 0 0 0
302 or die "Branch '$br' does not exist and is required.\n";
303 }
304  
305 0 ▶sub require_branch_absent {
Subroutine require_branch_absent: not called CC=2 SCAR=17.9
Pod documented
306 0 my $self = shift;
307 0 my $branch = shift;
308 0 my $repo = $self->{'repo'};
309  
310 0 ▶ grep { $_ eq $branch } $self->git_all_branches
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $branch;} $self->git_all_branches or die "Branch '${branch}' already exists. Pick another name.\n" 0 0 0
311 or die "Branch '$branch' already exists. Pick another name.\n";
312 }
313  
314 0 ▶sub require_tag_absent {
Subroutine require_tag_absent: not called CC=2 SCAR=17.9
Pod documented
315 0 my $self = shift;
316 0 my $tag = shift;
317  
318 0 ▶ grep { $_ eq $tag } $self->git_all_tags
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $tag;} $self->git_all_tags or die "Tag '${tag}' already exists. Pick another name.\n" 0 0 0
319 or die "Tag '$tag' already exists. Pick another name.\n";
320 }
321  
322 0 ▶sub require_branches_equal {
Subroutine require_branches_equal: not called CC=4 SCAR=30.0
Pod documented
323 0 my $self = shift;
324 0 my ( $br1, $br2 ) = @_;
325 0 my $repo = $self->{'repo'};
326  
327 0 my $status = $self->git_compare_branches( $br1, $br2 );
328  
329 0 ▶ if ( $status > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 0 0 0
330 0 warn "Branches '$br1' and '$br2' have diverged.\n";
331  
332 0 ▶ if ( $status == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 1) { } 0 0 0
elsif ($status == 2) { } 0 0 0
333 0 die "And branch '$br1' may be fast-forwarded.\n";
334 } elsif ( $status == 2 ) {
335 0 warn "And local branch '$br1' is ahead of '$br2'.\n";
336 } else {
337 0 die "Branches need merging first.\n";
338 }
339 }
340 }
341  
342 0 ▶sub is_interactive {
Subroutine is_interactive: not called CC=2 SCAR=17.9
Pod documented
343 0 ▶ return -t STDIN && -t STDOUT;
Condition0%0/3
-t STDIN && -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
-t STDIN && -t STDOUT
-t STDIN -t STDOUT
Truth table0%0/3
A && B
A= -t STDINB= -t STDOUT
ABresult
0X0
100
111
344 }
345  
346 6 1;
347  
348 __END__
« Flux/Command/feature.pm ↑ Summary Flux.pm »