File Coverage

blib/lib/Dist/Zilla/Role/Git/Repo.pm
Criterion Covered Total %
statement 22 22 100.0
branch 1 2 50.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 33 35 94.2


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dist-Zilla-Plugin-Git
3             #
4             # This software is copyright (c) 2009 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Dist::Zilla::Role::Git::Repo;
10             # ABSTRACT: Provide repository information for Git plugins
11              
12             our $VERSION = '2.046';
13              
14 19     19   417405 use Moose::Role;
  19         53  
  19         280  
15 19     19   100230 use Types::Standard qw(Str Maybe);
  19         51  
  19         211  
16 19     19   17307 use namespace::autoclean;
  19         58  
  19         153  
17              
18             has repo_root => (
19             is => 'ro',
20             isa => Str,
21             lazy => 1,
22             builder => '_build_repo_root',
23             );
24              
25             sub _build_repo_root
26             {
27 53     53   230 my $self = shift;
28 53         2073 $self->zilla->root->stringify;
29             }
30              
31             #pod =method current_git_branch
32             #pod
33             #pod $branch = $plugin->current_git_branch;
34             #pod
35             #pod The current branch in the repository, or C<undef> if the repository
36             #pod has a detached HEAD. Note: This value is cached; it will not
37             #pod be updated if the branch is changed during the run.
38             #pod
39             #pod =cut
40              
41             has current_git_branch => (
42             is => 'ro',
43             isa => Maybe[Str],
44             lazy => 1,
45             builder => '_build_current_git_branch',
46             init_arg => undef, # Not configurable
47             );
48              
49             sub _build_current_git_branch
50             {
51 2     2   13 my $self = shift;
52              
53             # Git 1.7+ allows "rev-parse --abbrev-ref HEAD", but we want to support 1.5.4
54 2         15 my ($branch) = $self->git->RUN(qw(symbolic-ref -q HEAD));
55              
56 19     19   4058 no warnings 'uninitialized';
  19         51  
  19         4433  
57 2 50       15724 undef $branch unless $branch =~ s!^refs/heads/!!;
58              
59 2         233 $branch;
60             } # end _build_current_git_branch
61              
62             #pod =method git
63             #pod
64             #pod $git = $plugin->git;
65             #pod
66             #pod This method returns a Git::Wrapper object for the C<repo_root>
67             #pod directory, constructing one if necessary. The object is shared
68             #pod between all plugins that consume this role (if they have the same
69             #pod C<repo_root>).
70             #pod
71             #pod =cut
72              
73             my %cached_wrapper;
74              
75             around dump_config => sub
76             {
77             my $orig = shift;
78             my $self = shift;
79              
80             my $config = $self->$orig;
81              
82             $config->{+__PACKAGE__} = {
83             repo_root => $self->repo_root,
84             git_version => $self->git->version,
85             };
86              
87             return $config;
88             };
89              
90             sub git {
91 145     145 1 6923 my $root = shift->repo_root;
92              
93 145   66     3053 $cached_wrapper{$root} ||= do {
94 44         594 require Git::Wrapper;
95 44         1014 Git::Wrapper->new( $root );
96             };
97             }
98              
99             1;
100              
101             __END__
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             Dist::Zilla::Role::Git::Repo - Provide repository information for Git plugins
110              
111             =head1 VERSION
112              
113             version 2.046
114              
115             =head1 DESCRIPTION
116              
117             This role is used within the Git plugins to get information about the
118             repository structure, and to create a Git::Wrapper object.
119              
120             =head1 ATTRIBUTES
121              
122             =head2 repo_root
123              
124             The repository root, either as a full path or relative to the distribution
125             root. The default is the distribution root (C<$zilla->root>).
126              
127             =head1 METHODS
128              
129             =head2 current_git_branch
130              
131             $branch = $plugin->current_git_branch;
132              
133             The current branch in the repository, or C<undef> if the repository
134             has a detached HEAD. Note: This value is cached; it will not
135             be updated if the branch is changed during the run.
136              
137             =head2 git
138              
139             $git = $plugin->git;
140              
141             This method returns a Git::Wrapper object for the C<repo_root>
142             directory, constructing one if necessary. The object is shared
143             between all plugins that consume this role (if they have the same
144             C<repo_root>).
145              
146             =head1 SUPPORT
147              
148             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Git>
149             (or L<bug-Dist-Zilla-Plugin-Git@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Git@rt.cpan.org>).
150              
151             There is also a mailing list available for users of this distribution, at
152             L<http://dzil.org/#mailing-list>.
153              
154             There is also an irc channel available for users of this distribution, at
155             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
156              
157             =head1 AUTHOR
158              
159             Jerome Quelin
160              
161             =head1 COPYRIGHT AND LICENCE
162              
163             This software is copyright (c) 2009 by Jerome Quelin.
164              
165             This is free software; you can redistribute it and/or modify it under
166             the same terms as the Perl 5 programming language system itself.
167              
168             =cut