| 1 | use 5.008; # utf8 | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Git::Wrapper::Plus::Refs; | ||
| 7 | $Git::Wrapper::Plus::Refs::VERSION = '0.004010'; | ||
| 8 | # ABSTRACT: Work with refs | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | use Moo qw( has ); | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | has 'git' => ( required => 1, is => ro => ); | ||
| 31 | has 'support' => ( is => ro =>, lazy => 1, builder => 1 ); | ||
| 32 | |||
| 33 | sub _build_support { | ||
| 34 | my ( $self, ) = @_; | ||
| 35 | require Git::Wrapper::Plus::Support; | ||
| 36 | return Git::Wrapper::Plus::Support->new( git => $self->git ); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | sub _for_each_ref { | ||
| 63 | my ( $self, $refspec, $callback ) = @_; | ||
| 64 | |||
| 65 | my $git_dir = $self->git->dir; | ||
| 66 | |||
| 67 | # git for-each-ref refs/heads/** == | ||
| 68 | # for-each-ref refs/heads/* == | ||
| 69 | # ls-remote refs/heads/* + exclude refs/heads/*/* | ||
| 70 | # | ||
| 71 | # git for-each-refs refs/heads/ == | ||
| 72 | # ls-remote refs/heads/* | ||
| 73 | # | ||
| 74 | if ( $self->support->supports_command('for-each-ref') ) { | ||
| 75 | ## no critic (Compatibility::PerlMinimumVersionAndWhy) | ||
| 76 | if ( $refspec =~ qr{\A(.*)/[*]{1,2}\z}msx ) { | ||
| 77 | $refspec = $1; | ||
| 78 | } | ||
| 79 | for my $line ( $self->git->for_each_ref($refspec) ) { | ||
| 80 | if ( $line =~ qr{ \A ([^ ]+) [^\t]+ \t ( .+ ) \z }msx ) { | ||
| 81 | $callback->( $1, $2 ); | ||
| 82 | next; | ||
| 83 | } | ||
| 84 | require Carp; | ||
| 85 | Carp::confess( 'Regexp failed to parse a line from `git for-each-ref` :' . $line ); | ||
| 86 | } | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | for my $line ( $self->git->ls_remote( $git_dir, $refspec ) ) { | ||
| 90 | ## no critic (Compatibility::PerlMinimumVersionAndWhy) | ||
| 91 | if ( $line =~ qr{ \A ([^\t]+) \t ( .+ ) \z }msx ) { | ||
| 92 | $callback->( $1, $2 ); | ||
| 93 | next; | ||
| 94 | } | ||
| 95 | require Carp; | ||
| 96 | Carp::confess( 'Regexp failed to parse a line from `git ls-remote` :' . $line ); | ||
| 97 | } | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | sub refs { | ||
| 118 | my ($self) = @_; | ||
| 119 | return $self->get_ref('refs/**'); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | sub get_ref { | ||
| 135 | my ( $self, $refspec ) = @_; | ||
| 136 | my @out; | ||
| 137 | $self->_for_each_ref( | ||
| 138 | $refspec => sub { | ||
| 139 | my ( $sha_one, $refname ) = @_; | ||
| 140 | push @out, $self->_mk_ref( $sha_one, $refname ); | ||
| 141 | }, | ||
| 142 | ); | ||
| 143 | return @out; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _mk_ref { | ||
| 147 | my ( $self, undef, $name ) = @_; | ||
| 148 | require Git::Wrapper::Plus::Ref; | ||
| 149 | return Git::Wrapper::Plus::Ref->new( | ||
| 150 | git => $self->git, | ||
| 151 | name => $name, | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | no Moo; | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | __END__ |