File Coverage

lib/Dist/Zilla/Util/Git/Refs.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1 1     1   1119 use strict;
  1         2  
  1         38  
2 1     1   5 use warnings;
  1         1  
  1         54  
3              
4             package Dist::Zilla::Util::Git::Refs;
5             BEGIN {
6 1     1   25 $Dist::Zilla::Util::Git::Refs::AUTHORITY = 'cpan:KENTNL';
7             }
8             {
9             $Dist::Zilla::Util::Git::Refs::VERSION = '0.002000';
10             }
11              
12             # ABSTRACT: Work with refs
13              
14 1     1   402 use Moose;
  0            
  0            
15             with 'Dist::Zilla::UtilRole::MaybeGit';
16              
17              
18             sub _for_each_ref {
19             my ( $self, $refspec, $callback ) = @_;
20              
21             my $git_dir = $self->git->dir;
22             for my $line ( $self->git->ls_remote( $git_dir, $refspec ) ) {
23             if ( $line =~ qr{ \A ([^\t]+) \t ( .+ ) \z }msx ) {
24             $callback->( $1, $2 );
25             next;
26             }
27             require Carp;
28             Carp::confess( 'Regexp failed to parse a line from `git ls-remote` :' . $line );
29             }
30             return;
31             }
32              
33              
34             sub refs {
35             my ($self) = @_;
36             return $self->get_ref('refs/**');
37             }
38              
39              
40             sub get_ref {
41             my ( $self, $refspec ) = @_;
42             my @out;
43             $self->_for_each_ref(
44             $refspec => sub {
45             my ( $sha1, $refname ) = @_;
46             push @out, $self->_mk_ref( $sha1, $refname );
47             }
48             );
49             return @out;
50             }
51              
52             sub _mk_ref {
53             my ( $self, $sha1, $name ) = @_;
54             require Dist::Zilla::Util::Git::Refs::Ref;
55             return Dist::Zilla::Util::Git::Refs::Ref->new(
56             git => $self->git,
57             name => $name,
58             );
59             }
60             __PACKAGE__->meta->make_immutable;
61             no Moose;
62              
63             1;
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Dist::Zilla::Util::Git::Refs - Work with refs
74              
75             =head1 VERSION
76              
77             version 0.002000
78              
79             =head1 SYNOPSIS
80              
81             After doing lots of work with Git::Wrapper, I found there's quite a few ways to
82             work with refs, and those ways aren't exactly all equal, or supported on all versions of git.
83              
84             This abstracts it so things can just use them.
85              
86             my $refs = Dist::Zilla::Util::Git::Refs->new( zilla => $self->zilla );
87              
88             $refs->refs(); # A ::Ref object for each entry from `git ls-remote .`
89              
90             my ( @results ) = $refs->get_ref('refs/**'); # the same thing
91              
92             my ( @results ) = $refs->get_ref('refs/heads/**'); # all branches
93              
94             my ( @results ) = $refs->get_ref('refs/tags/**'); # all tags
95              
96             my ( @results ) = $refs->get_ref('refs/remotes/**'); # all remote branches
97              
98             Note: You probably shouldn't use this module directly, and should instead use one of the C<::Util::Git> family.
99              
100             =head1 METHODS
101              
102             =head2 C<refs>
103              
104             Lists all C<refs> in the C<refs/> C<namespace>.
105              
106             my (@refs) = $reffer->refs();
107              
108             Shorthand for
109              
110             my (@refs) = $reffer->get_ref('refs/**');
111              
112             =head2 C<get_ref>
113              
114             Fetch a given C<ref>, or collection of C<ref>s, matching a specification.
115              
116             my ($ref) = $reffer->get_ref('refs/heads/master');
117             my (@branches) = $reffer->get_ref('refs/heads/**');
118             my (@tags) = $reffer->get_ref('refs/tags/**');
119              
120             Though reminder, if you're working with branches or tags, use the relevant modules =).
121              
122             =head1 AUTHOR
123              
124             Kent Fredric <kentfredric@gmail.com>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is copyright (c) 2013 by Kent Fredric <kentfredric@gmail.com>.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.
132              
133             =cut