| 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
| |||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ( -f $dir->child('.git', 'HEAD') ) { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/3
length $branch == 40 and $branch =~ /^[\da-f]+$/
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]+$/
| |||||||||||||||||||||||||||||||
| 30 | 0 | my ($ans) = map {/^[*] [(]detached from (.*)[)]$/; $1} grep {/^[*]\s/} `$git branch --contains $branch`; | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $branch = "[$ans]" if $ans; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 47 | 0 | $dir = $dir->parent; | |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return if !$type; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $type = $self->cols && $self->cols > 40 ? "$type " : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->cols and $self->cols > 40
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
| |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $max_branch_width = ( $self->cols || 80 ) / 3; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$self->cols || 80
MC/DC0%0/1
$self->cols || 80
$self->cols
Truth table0%0/2
A
A= $self->cols
| |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($max_branch_width > 60) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | 0 | $max_branch_width = 60; | |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( length $branch > $max_branch_width ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $options->{summarize} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | 0 | $branch =~ s{^(\w)(?:[^/]+)/}{$1/}; | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( length $branch > $max_branch_width ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$staged ne "?" and $staged ne " "
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 " "
| |||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $status{unstaged}++ if $unstaged ne '?' && $unstaged ne ' '; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$unstaged ne "?" and $unstaged ne " "
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 " "
| |||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $status{untracked}++ if $staged eq '?' && $unstaged eq '?'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$staged eq "?" and $unstaged eq "?"
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 "?"
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$chars[$status{'staged'}] || $chars[36]
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]
| |||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $str .= ' ' . color('red' ) . ($chars[$status{unstaged} ] || $chars[36]) if $status{unstaged}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$chars[$status{'unstaged'}] || $chars[36]
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]
| |||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $str .= ' ' . color('white') . ($chars[$status{untracked}] || $chars[36]) if $status{untracked}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$chars[$status{'untracked'}] || $chars[36]
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]
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | 0 | return 'cvs'; | |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||