| 1 | use 5.008; # utf8 | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Git::Wrapper::Plus::Branches; | ||
| 7 | $Git::Wrapper::Plus::Branches::VERSION = '0.004010'; | ||
| 8 | # ABSTRACT: Extract branches from Git | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | use Moo qw( has ); | ||
| 55 | use Git::Wrapper::Plus::Util qw(exit_status_handler); | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | has 'git' => ( is => ro =>, required => 1 ); | ||
| 68 | has 'refs' => ( is => ro =>, lazy => 1, builder => 1 ); | ||
| 69 | |||
| 70 | sub _build_refs { | ||
| 71 | my ($self) = @_; | ||
| 72 | require Git::Wrapper::Plus::Refs; | ||
| 73 | return Git::Wrapper::Plus::Refs->new( git => $self->git ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _to_branch { | ||
| 77 | my ( undef, $ref ) = @_; | ||
| 78 | require Git::Wrapper::Plus::Ref::Branch; | ||
| 79 | return Git::Wrapper::Plus::Ref::Branch->new_from_Ref($ref); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _to_branches { | ||
| 83 | my ( $self, @refs ) = @_; | ||
| 84 | return map { $self->_to_branch($_) } @refs; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | sub branches { | ||
| 98 | my ( $self, ) = @_; | ||
| 99 | return $self->get_branch(q[**]); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | sub get_branch { | ||
| 123 | my ( $self, $name ) = @_; | ||
| 124 | return $self->_to_branches( $self->refs->get_ref( 'refs/heads/' . $name ) ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _current_branch_name { | ||
| 128 | my ($self) = @_; | ||
| 129 | my (@current_names); | ||
| 130 | return unless exit_status_handler( | ||
| 131 | sub { | ||
| 132 | (@current_names) = $self->git->symbolic_ref('HEAD'); | ||
| 133 | }, | ||
| 134 | { | ||
| 135 | 128 => sub { return }, | ||
| 136 | }, | ||
| 137 | ); | ||
| 138 | for (@current_names) { | ||
| 139 | $_ =~ s{\A refs/heads/ }{}msx; | ||
| 140 | } | ||
| 141 | return @current_names; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | sub current_branch { | ||
| 159 | my ( $self, ) = @_; | ||
| 160 | my ($ref) = $self->_current_branch_name; | ||
| 161 | return if not $ref; | ||
| 162 | my (@items) = $self->get_branch($ref); | ||
| 163 | return shift @items if 1 == @items; | ||
| 164 | require Carp; | ||
| 165 | Carp::confess( 'get_branch(' . $ref . ') returned multiple values. Cannot determine current branch' ); | ||
| 166 | } | ||
| 167 | |||
| 168 | no Moo; | ||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | __END__ |