blib/lib/Git/Repository/Plugin/Blame/Cache.pm

filter: Statementstmt 100% 46 / 46 Branchbran 81.8% 18 / 22 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 89.8% 88 / 98 CC 17 SCAR 17.6
CC
17
Cov
91%
new
9.1
set_blame_lines
6.0
get_blame_lines
4.1
CRAP 5.8
« blib/lib/Git/Repository/Plugin/Blame/Line.pm ↑ Summary Build »
1 package Git::Repository::Plugin::Blame::Cache;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Git::Repository::Plugin::Blame::Cache - Cache the output of C<< Git::Repository->blame() >>.
13  
14  
15 =head1 VERSION
16  
17 Version 1.3.0
18  
19 =cut
20  
21 7 our $VERSION = '1.3.0';
22  
23 7 my $CACHE = {};
24  
25  
26 =head1 SYNOPSIS
27  
28 use Git::Repository::Plugin::Blame::Cache;
29  
30 # Instantiate the cache for a given repository.
31 my $cache = Git::Repository::Plugin::Blame::Cache->new(
32 repository => $repository,
33 );
34  
35 my $repository = $cache->get_repository();
36  
37 # Cache blame lines.
38 $cache->set_blame_lines(
39 file => $file,
40 blame_lines => $blame_lines,
41 );
42  
43 # Retrieve blame lines from the cache.
44 my $blame_lines = $cache->get_blame_lines(
45 file => $file,
46 );
47  
48  
49 =head1 DESCRIPTION
50  
51 Cache the output of C<< Git::Repository::Plugin::Blame->blame() >> and
52 C<< Git::Repository->blame() >> by extension.
53  
54  
55 =head1 METHODS
56  
57 =head2 new()
58  
59 Return a cache object for the specified repository.
60  
61 my $cache = Git::Repository::Plugin::Blame::Cache->new(
62 repository => $repository,
63 options => $options,
64 );
65  
66 Arguments:
67  
68 =over 4
69  
70 =item * repository I<(mandatory)>
71  
72 A unique way to identify a repository. Typically, the root path of the
73 repository.
74  
75 =item * blame_args I<(optional)>
76  
77 A hashref of arguments used to generate the C<git blame> output, if applicable.
78 This avoids caching the same output for C<git blame> and C<git blame -w>, for
79 example.
80  
81 =back
82  
83 =cut
84  
85 sub new
86 {
87 10 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=9 SCAR=22.1
Pod documented
88 10 my $repository = delete( $args{'repository'} );
89 10 ▶ my $blame_args = delete( $args{'blame_args'} ) || {};
Condition100%2/2
delete $args{'blame_args'} || {}
l!l
46
MC/DC100%1/1
delete $args{'blame_args'} || {}
delete $args{'blame_args'}
Truth table100%2/2
A
A= delete $args{'blame_args'}
Aresult
11
00
90 10 ▶ croak 'The following arguments are not valid: ' . join( ',', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 10 10
91 if scalar( keys %args ) != 0;
92  
93 # Verify mandatory arguments.
94 10 ▶ croak 'The "repository" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $repository or $repository eq "" 1 9 10
Condition66%2/3
not defined $repository or $repository eq ""
l!l&&r!l&&!r
109
MC/DC50%1/2
not defined $repository or $repository eq ""
not defined $repository $repository eq ""
Truth table66%2/3
not(A) or B
A= not defined $repositoryB= $repository eq ""
ABresult
1X1
011
000
95 if !defined( $repository ) || $repository eq '';
96  
97 # Serialize the options passed, to hold a separate cache entry for each set
98 # of options.
99 9 my $serialized_blame_args = '';
100 9 ▶ if ( scalar( keys %$blame_args ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$blame_args != 0) 4 5 9
101 {
102 4 my @blame_args = ();
103 4 foreach my $blame_arg ( sort keys %$blame_args )
104 {
105 my $value = defined( $blame_args->{ $blame_arg } )
106 4 ▶ ? $blame_args->{ $blame_arg }
Branch: 1 branch
BranchTrueFalseTotal
defined $blame_args->{$blame_arg} ? : 4 0 4
107 : '';
108 4 push( @blame_args, "$blame_arg=$value" );
109 }
110 4 $serialized_blame_args = join( ',', @blame_args );
111 }
112  
113 # If the cache element does not exist, create it.
114 9 ▶ if ( !defined( $CACHE->{ $repository }->{ $serialized_blame_args } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $CACHE->{$repository}{$serialized_blame_args}) 7 2 9
115 {
116 7 $CACHE->{ $repository }->{ $serialized_blame_args } = bless(
117 {
118 repository => $repository,
119 files => {},
120 serialized_blame_args => $serialized_blame_args,
121 },
122 $class,
123 );
124 }
125  
126 # Return the corresponding cache element.
127 9 return $CACHE->{ $repository }->{ $serialized_blame_args };
128 }
129  
130  
131 =head2 get_repository()
132  
133 Return the unique identifier for the repository.
134  
135 my $repository = $cache->get_repository();
136  
137 =cut
138  
139 sub get_repository
140 {
141 1 ▶ my ( $self ) = @_;
Subroutine get_repository: called CC=1 SCAR=0.0
Pod documented
142  
143 1 return $self->{'repository'};
144 }
145  
146  
147 =head2 get_blame_lines()
148  
149 Retrieve git blame lines from the cache (if they exist) for a given file.
150  
151 my $blame_lines = $cache->get_blame_lines(
152 file => $file,
153 );
154  
155 Arguments:
156  
157 =over 4
158  
159 =item * file (mandatory)
160  
161 The file for which you want the cached C<git blame> output.
162  
163 =back
164  
165 =cut
166  
167 sub get_blame_lines
168 {
169 8 ▶ my ( $self, %args ) = @_;
Subroutine get_blame_lines: called CC=4 SCAR=14.0
Pod documented
170 8 my $file = delete( $args{'file'} );
171 8 ▶ croak 'The following arguments are not valid: ' . join( ',', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 8 8
172 if scalar( keys %args ) != 0;
173  
174 8 ▶ croak 'The "file" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 1 7 8
Condition66%2/3
not defined $file or $file eq ""
l!l&&r!l&&!r
107
MC/DC50%1/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table66%2/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
175 if !defined( $file ) || ( $file eq '' );
176  
177 7 return $self->{'files'}->{ $file };
178 }
179  
180  
181 =head2 set_blame_lines()
182  
183 Store in the cache the output of C<git blame> for a given file.
184  
185 $cache->set_blame_lines(
186 file => $file,
187 blame_lines => $blame_lines,
188 );
189  
190 Arguments:
191  
192 =over 4
193  
194 =item * file (mandatory)
195  
196 The file for which you are caching the C<git blame> output.
197  
198 =item * blame_lines (mandatory)
199  
200 The output of C<< Git::Repository::Plugin::Blame->blame() >>.
201  
202 =back
203  
204 =cut
205  
206 sub set_blame_lines
207 {
208 7 ▶ my ( $self, %args ) = @_;
Subroutine set_blame_lines: called CC=6 SCAR=18.0
Pod documented
209 7 my $file = delete( $args{'file'} );
210 7 my $blame_lines = delete( $args{'blame_lines'} );
211 7 ▶ croak 'The following arguments are not valid: ' . join( ',', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 7 7
212 if scalar( keys %args ) != 0;
213  
214 7 ▶ croak 'The "file" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 1 6 7
Condition66%2/3
not defined $file or $file eq ""
l!l&&r!l&&!r
106
MC/DC50%1/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table66%2/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
215 if !defined( $file ) || $file eq '';
216 6 ▶ croak 'The "blame_lines" argument is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $blame_lines 1 5 6
217 if !defined( $blame_lines );
218 5 ▶ croak 'The "blame_lines" argument must be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_arrayref($blame_lines) 1 4 5
219 if !Data::Validate::Type::is_arrayref( $blame_lines );
220  
221 4 $self->{'files'}->{ $file } = $blame_lines;
222  
223 4 return;
224 }
225  
226  
227 =head1 BUGS
228  
229 Please report any bugs or feature requests through the web interface at
230 L<https://github.com/guillaumeaubert/Git-Repository-Plugin-Blame/issues>.
231 I will be notified, and then you'll automatically be notified of progress on
232 your bug as I make changes.
233  
234  
235 =head1 SUPPORT
236  
237 You can find documentation for this module with the perldoc command.
238  
239 perldoc Git::Repository::Plugin::Blame
240  
241  
242 You can also look for information at:
243  
244 =over 4
245  
246 =item * GitHub (report bugs there)
247  
248 L<https://github.com/guillaumeaubert/Git-Repository-Plugin-Blame/issues>
249  
250 =item * AnnoCPAN: Annotated CPAN documentation
251  
252 L<http://annocpan.org/dist/Git-Repository-Plugin-Blame>
253  
254 =item * CPAN Ratings
255  
256 L<http://cpanratings.perl.org/d/Git-Repository-Plugin-Blame>
257  
258 =item * MetaCPAN
259  
260 L<https://metacpan.org/release/Git-Repository-Plugin-Blame>
261  
262 =back
263  
264  
265 =head1 AUTHOR
266  
267 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
268 C<< <aubertg at cpan.org> >>.
269  
270  
271 =head1 COPYRIGHT & LICENSE
272  
273 Copyright 2012-2015 Guillaume Aubert.
274  
275 This program is free software: you can redistribute it and/or modify it under
276 the terms of the GNU General Public License version 3 as published by the Free
277 Software Foundation.
278  
279 This program is distributed in the hope that it will be useful, but WITHOUT ANY
280 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
281 PARTICULAR PURPOSE. See the GNU General Public License for more details.
282  
283 You should have received a copy of the GNU General Public License along with
284 this program. If not, see http://www.gnu.org/licenses/
285  
286 =cut
287  
288 7 1;
« blib/lib/Git/Repository/Plugin/Blame/Line.pm ↑ Summary Build »