| 1 | use 5.008; # utf8 | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Git::Wrapper::Plus::Tags; | ||
| 7 | $Git::Wrapper::Plus::Tags::VERSION = '0.004010'; | ||
| 8 | # ABSTRACT: Extract all tags from a repository | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | use Moo qw( has ); | ||
| 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 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | has 'git' => ( is => ro =>, required => 1 ); | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | has 'refs' => ( is => ro =>, lazy => 1, builder => 1 ); | ||
| 74 | |||
| 75 | sub _build_refs { | ||
| 76 | my ($self) = @_; | ||
| 77 | require Git::Wrapper::Plus::Refs; | ||
| 78 | return Git::Wrapper::Plus::Refs->new( git => $self->git ); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _to_tag { | ||
| 82 | my ( undef, $ref ) = @_; | ||
| 83 | require Git::Wrapper::Plus::Ref::Tag; | ||
| 84 | return Git::Wrapper::Plus::Ref::Tag->new_from_Ref($ref); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _to_tags { | ||
| 88 | my ( $self, @refs ) = @_; | ||
| 89 | return map { $self->_to_tag($_) } @refs; | ||
| 90 | } | ||
| 91 | |||
| 92 | # There's 2 types of results that come back from git ls-remote | ||
| 93 | # | ||
| 94 | # tags, and heavy tags ( usually annotations ) | ||
| 95 | # | ||
| 96 | # puretags look like | ||
| 97 | # | ||
| 98 | # abffab foo # pointer to the commit | ||
| 99 | # | ||
| 100 | # While heavy tags come in pairs | ||
| 101 | # | ||
| 102 | # fabfab foo # heavy tag pointer | ||
| 103 | # abffab foo^{} # pointer to the actual commit | ||
| 104 | # | ||
| 105 | # However, we don't really care about the second half of the latter kind. | ||
| 106 | # | ||
| 107 | sub _grep_commit_pointers { | ||
| 108 | my ( undef, @refs ) = @_; | ||
| 109 | my (@out); | ||
| 110 | for my $ref (@refs) { | ||
| 111 | next if $ref->name =~ /[^][{][}]\z/msx; | ||
| 112 | push @out, $ref; | ||
| 113 | } | ||
| 114 | return @out; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | sub tags { | ||
| 128 | my ($self) = @_; | ||
| 129 | return $self->get_tag(q[**]); | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | sub get_tag { | ||
| 157 | my ( $self, $name ) = @_; | ||
| 158 | return $self->_to_tags( $self->_grep_commit_pointers( $self->refs->get_ref( 'refs/tags/' . $name ) ) ); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | sub tag_sha1_map { | ||
| 174 | my ($self) = @_; | ||
| 175 | |||
| 176 | my %hash; | ||
| 177 | for my $tag ( $self->tags ) { | ||
| 178 | my $sha_one = $tag->sha1; | ||
| 179 | if ( not exists $hash{$sha_one} ) { | ||
| 180 | $hash{$sha_one} = []; | ||
| 181 | } | ||
| 182 | push @{ $hash{$sha_one} }, $tag; | ||
| 183 | } | ||
| 184 | return \%hash; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | sub tags_for_rev { | ||
| 200 | my ( $self, $rev ) = @_; | ||
| 201 | my (@shas) = $self->git->rev_parse($rev); | ||
| 202 | if ( scalar @shas != 1 ) { | ||
| 203 | require Carp; | ||
| 204 | Carp::croak("Could not resolve a SHA1 from rev $rev"); | ||
| 205 | } | ||
| 206 | my ($sha) = shift @shas; | ||
| 207 | my $map = $self->tag_sha1_map; | ||
| 208 | return unless exists $map->{$sha}; | ||
| 209 | return @{ $map->{$sha} }; | ||
| 210 | } | ||
| 211 | |||
| 212 | no Moo; | ||
| 213 | |||
| 214 | 1; | ||
| 215 | |||
| 216 | __END__ |