PS1/Plugin/Branch.pm

filter: Statementstmt 21.7% 17 / 78 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 13.6% 26 / 190 CC 41 SCAR 57.2
CC
41
Cov
11%
status
462.0
branch
306.0
git
12.0
CRAP 305.7
« PS1/Plugin/Directory.pm ↑ Summary PS1.pm »
1 package App::PS1::Plugin::Branch;
2  
3 # Created on: 2011-06-21 09:48:47
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Term::ANSIColor qw/color/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = 0.08;
16  
17 0 ▶sub branch {
Subroutine branch: not called CC=17 SCAR=57.2
Pod documented
18 0 my ($self, $options) = @_;
19 0 my ($type, $branch);
20 0 my $dir = eval { path('.')->realpath };
21 0 my $git = git();
22 0 my $cvs = cvs();
23 0 ▶ while ( $dir ne $dir->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne $dir->parent) 0 0 0
24 0 ▶ if ( -f $dir->child('.git', 'HEAD') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-f $dir->child('.git', 'HEAD')) { } 0 0 0
elsif (-f $dir->child('CVS', 'Tag')) { } 0 0 0
elsif (-f $dir->child('CVS', 'Root')) { } 0 0 0
25 0 $type = 'git';
26 0 $branch = $dir->child('.git')->child('HEAD')->slurp;
27 0 chomp $branch;
28 0 $branch =~ s/^ref: \s+ refs\/heads\/(.*)/$1/xms;
29 0 ▶ if ( length $branch == 40 && $branch =~ /^[\da-f]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $branch == 40 and $branch =~ /^[\da-f]+$/) 0 0 0
Condition0%0/3
length $branch == 40 and $branch =~ /^[\da-f]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $branch == 40 and $branch =~ /^[\da-f]+$/
length $branch == 40 $branch =~ /^[\da-f]+$/
Truth table0%0/3
A and B
A= length $branch == 40B= $branch =~ /^[\da-f]+$/
ABresult
0X0
100
111
30 0 my ($ans) = map {/^[*] [(]detached from (.*)[)]$/; $1} grep {/^[*]\s/} `$git branch --contains $branch`;
31 0 ▶ $branch = "[$ans]" if $ans;
Branch: 1 branch
BranchTrueFalseTotal
if $ans 0 0 0
32 }
33 }
34 elsif (-f $dir->child('CVS', 'Tag')) {
35 0 $type = 'cvs';
36 0 $branch = $dir->child('CVS', 'Tag')->slurp;
37 0 chomp $branch;
38 0 $branch =~ s/^N//;
39 0 $branch = "($branch)";
40 }
41 elsif (-f $dir->child('CVS', 'Root')) {
42 0 $type = 'cvs';
43 0 $branch = 'master';
44 }
45  
46 0 ▶ last if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 0 0 0
47 0 $dir = $dir->parent;
48 }
49  
50 0 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
51  
52 0 ▶ $type = $self->cols && $self->cols > 40 ? "$type " : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->cols && $self->cols > 40 ? : 0 0 0
Condition0%0/3
$self->cols and $self->cols > 40
!ll&&!rl&&r
000
MC/DC0%0/2
$self->cols and $self->cols > 40
$self->cols $self->cols > 40
Truth table0%0/3
A and B
A= $self->colsB= $self->cols > 40
ABresult
0X0
100
111
53  
54 0 ▶ my $max_branch_width = ( $self->cols || 80 ) / 3;
Condition0%0/2
$self->cols || 80
l!l
00
MC/DC0%0/1
$self->cols || 80
$self->cols
Truth table0%0/2
A
A= $self->cols
Aresult
11
00
55 0 ▶ if ($max_branch_width > 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_branch_width > 60) 0 0 0
56 0 $max_branch_width = 60;
57 }
58 0 ▶ if ( length $branch > $max_branch_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $branch > $max_branch_width) 0 0 0
59 0 ▶ if ( $options->{summarize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"summarize"}) 0 0 0
60 0 $branch =~ s{^(\w)(?:[^/]+)/}{$1/};
61 }
62 0 ▶ if ( length $branch > $max_branch_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $branch > $max_branch_width) 0 0 0
63 0 $branch = substr $branch, 0, $max_branch_width;
64 0 $branch .= '...';
65 }
66 }
67  
68 0 my ($len, $status) = status($type);
69 0 return $self->surround(
70 $len + length $type . $branch,
71 $self->colour('branch_label') . $type
72 . $self->colour('branch') . $branch
73 . $status
74 );
75 }
76  
77 0 ▶sub status {
Subroutine status: not called CC=21 SCAR=61.4
Pod documented
78 0 my ($type) = @_;
79 0 ▶ return (0, '') if $type ne 'git ';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "git " 0 0 0
80  
81 0 my %status = (
82 staged => 0,
83 unstaged => 0,
84 untracked => 0,
85 );
86 0 my @status = `git status --porcelain`;
87 0 for my $status (@status) {
88 0 my ($staged, $unstaged) = $status =~ /^(.)(.)/;
89 0 ▶ $status{staged}++ if $staged ne '?' && $staged ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $staged ne "?" and $staged ne " " 0 0 0
Condition0%0/3
$staged ne "?" and $staged ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
$staged ne "?" and $staged ne " "
$staged ne "?" $staged ne " "
Truth table0%0/3
A and B
A= $staged ne "?"B= $staged ne " "
ABresult
0X0
100
111
90 0 ▶ $status{unstaged}++ if $unstaged ne '?' && $unstaged ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $unstaged ne "?" and $unstaged ne " " 0 0 0
Condition0%0/3
$unstaged ne "?" and $unstaged ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
$unstaged ne "?" and $unstaged ne " "
$unstaged ne "?" $unstaged ne " "
Truth table0%0/3
A and B
A= $unstaged ne "?"B= $unstaged ne " "
ABresult
0X0
100
111
91 0 ▶ $status{untracked}++ if $staged eq '?' && $unstaged eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $staged eq "?" and $unstaged eq "?" 0 0 0
Condition0%0/3
$staged eq "?" and $unstaged eq "?"
!ll&&!rl&&r
000
MC/DC0%0/2
$staged eq "?" and $unstaged eq "?"
$staged eq "?" $unstaged eq "?"
Truth table0%0/3
A and B
A= $staged eq "?"B= $unstaged eq "?"
ABresult
0X0
100
111
92 }
93  
94 0 my @chars = (' ', '①','②','③','④','⑤','⑥','⑦','⑧','⑨','⑩','⑪','⑫','⑬','⑭','⑮','⑯','⑰','⑱','⑲','⑳','㉑','㉒','㉓','㉔','㉕','㉖','㉗','㉘','㉙','㉚','㉛','㉜','㉝','㉞','㉟', '∞');
95 0 my $str = '';
96 0 ▶ $str .= ' ' . color('green') . ($chars[$status{staged} ] || $chars[36]) if $status{staged};
Branch: 1 branch
BranchTrueFalseTotal
if $status{"staged"} 0 0 0
Condition0%0/3
$chars[$status{'staged'}] || $chars[36]
l!l&&r!l&&!r
000
MC/DC0%0/2
$chars[$status{'staged'}] || $chars[36]
$chars[$status{'staged'}] $chars[36]
Truth table0%0/3
A || B
A= $chars[$status{'staged'}]B= $chars[36]
ABresult
1X1
011
000
97 0 ▶ $str .= ' ' . color('red' ) . ($chars[$status{unstaged} ] || $chars[36]) if $status{unstaged};
Branch: 1 branch
BranchTrueFalseTotal
if $status{"unstaged"} 0 0 0
Condition0%0/3
$chars[$status{'unstaged'}] || $chars[36]
l!l&&r!l&&!r
000
MC/DC0%0/2
$chars[$status{'unstaged'}] || $chars[36]
$chars[$status{'unstaged'}] $chars[36]
Truth table0%0/3
A || B
A= $chars[$status{'unstaged'}]B= $chars[36]
ABresult
1X1
011
000
98 0 ▶ $str .= ' ' . color('white') . ($chars[$status{untracked}] || $chars[36]) if $status{untracked};
Branch: 1 branch
BranchTrueFalseTotal
if $status{"untracked"} 0 0 0
Condition0%0/3
$chars[$status{'untracked'}] || $chars[36]
l!l&&r!l&&!r
000
MC/DC0%0/2
$chars[$status{'untracked'}] || $chars[36]
$chars[$status{'untracked'}] $chars[36]
Truth table0%0/3
A || B
A= $chars[$status{'untracked'}]B= $chars[36]
ABresult
1X1
011
000
99  
100 return (
101 (! $status{staged} ? 0 : $status{staged} > 20 ? 3 : 2) +
102 (! $status{unstaged} ? 0 : $status{unstaged} > 20 ? 3 : 2) +
103 0 ▶ (! $status{untracked} ? 0 : $status{untracked} > 20 ? 3 : 2),
Branch: 6 branches
BranchTrueFalseTotal
!$status{'staged'} ? : 0 0 0
$status{'staged'} > 20 ? : 0 0 0
!$status{'unstaged'} ? : 0 0 0
$status{'unstaged'} > 20 ? : 0 0 0
!$status{'untracked'} ? : 0 0 0
$status{'untracked'} > 20 ? : 0 0 0
104 $str
105 );
106 }
107  
108 0 ▶sub git {
Subroutine git: not called CC=3 SCAR=24.8
Pod documented
109 0 for (split /:/, $ENV{PATH}) {
110 0 ▶ return "$_/git" if -x "$_/git";
Branch: 1 branch
BranchTrueFalseTotal
if -x "$_/git" 0 0 0
111 }
112 0 return 'git';
113 }
114  
115 0 ▶sub cvs {
Subroutine cvs: not called CC=3 SCAR=24.8
Pod documented
116 0 for (split /:/, $ENV{PATH}) {
117 0 ▶ return "$_/cvs" if -x "$_/cvs";
Branch: 1 branch
BranchTrueFalseTotal
if -x "$_/cvs" 0 0 0
118 }
119 0 return 'cvs';
120 }
121  
122 1 1;
123  
124 __END__
« PS1/Plugin/Directory.pm ↑ Summary PS1.pm »