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

filter: Statementstmt 100% 51 / 51 Branchbran 72.2% 13 / 18 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 93.9% 78 / 83 CC 12 SCAR 20.0
CC
12
Cov
93%
blame
12.2
BEGIN
1.0
BEGIN
1.0
CRAP 7.4
↑ Summary blib/lib/Git/Repository/Plugin/Blame/Line.pm »
1 package Git::Repository::Plugin::Blame;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Git::Repository::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @ISA = qw( Git::Repository::Plugin );
9 1 ▶sub _keywords { return qw( blame ) } ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Subroutine _keywords: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Class::Load qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl6::Slurp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Git::Repository::Plugin::Blame::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 Git::Repository::Plugin::Blame - Add a blame() method to L<Git::Repository>.
20  
21  
22 =head1 VERSION
23  
24 Version 1.2.3
25  
26 =cut
27  
28 2 our $VERSION = '1.2.3';
29  
30  
31 =head1 SYNOPSIS
32  
33 # Load the plugin.
34 use Git::Repository 'Blame';
35  
36 my $repository = Git::Repository->new();
37  
38 # Get the git blame information.
39 my $blame_lines = $repository->blame( $file );
40  
41  
42 =head1 DESCRIPTION
43  
44 This module adds a new C<blame()> method to L<Git::Repository>, which can be
45 used to determine what the last change for each line in a file is.
46  
47  
48 =head1 METHODS
49  
50 =head2 blame()
51  
52 Return the git blame information for a given file as an arrayref of
53 L<Git::Repository::Plugin::Blame::Line> objects.
54  
55 my $blame_lines = $repository->blame(
56 $file,
57 use_cache => $boolean, # default 0
58 );
59  
60 Arguments:
61  
62 =over 4
63  
64 =item * use_cache (default: 0)
65  
66 Cache the git blame output.
67  
68 =back
69  
70 =cut
71  
72 sub blame
73 {
74 2 ▶ my ( $repository, $file, %args ) = @_;
Subroutine blame: called CC=12 SCAR=25.0
Pod documented
75 2 ▶ my $use_cache = delete( $args{'use_cache'} ) || 0;
Condition100%2/2
delete $args{'use_cache'} || 0
l!l
11
MC/DC100%1/1
delete $args{'use_cache'} || 0
delete $args{'use_cache'}
Truth table100%2/2
A
A= delete $args{'use_cache'}
Aresult
11
00
76 2 ▶ croak 'The following arguments are not valid: ' . join( ', ' , keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 2 2
77 if scalar( keys %args ) != 0;
78  
79 # Check if the cache is enabled and if the file has already been parsed.
80 2 my $cache;
81 2 ▶ if ( $use_cache )
Branch: 1 branch
BranchTrueFalseTotal
if ($use_cache) 1 1 2
82 {
83 1 my $class = Class::Load::load_class( 'Git::Repository::Plugin::Blame::Cache' );
84 1 $cache = $class->new( repository => $repository->work_tree() );
85 1 ▶ croak 'Failed to initialize cache for repository ' . $repository->work_tree()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache 0 1 1
86 if !defined( $cache );
87  
88 1 my $blame_lines = $cache->get_blame_lines( file => $file );
89 1 ▶ return $blame_lines
Branch: 1 branch
BranchTrueFalseTotal
if defined $blame_lines 0 1 1
90 if defined( $blame_lines );
91 }
92  
93 # Run the command.
94 2 my $command = $repository->command( 'blame', '--porcelain', $file );
95 2 my @output = $command->final_output();
96  
97 # Parse the output.
98 2 my ( $commit_id, $original_line_number, $final_line_number, $lines_count_in_group );
99 2 my $commit_attributes = {};
100 2 my $lines = [];
101 2 foreach my $line ( @output )
102 {
103 60 ▶ if ( $line =~ /^\t(.*)$/x )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\t(.*)$/x) { } 8 52 60
104 {
105 # It's a line from the file we git blamed.
106 push(
107 @$lines,
108 Git::Repository::Plugin::Blame::Line->new(
109 line_number => $final_line_number,
110 line => defined( $1 ) ? $1 : '',
111 8 ▶ commit_attributes => $commit_attributes->{ $commit_id },
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 8 0 8
112 commit_id => $commit_id,
113 )
114 );
115 }
116 else
117 {
118 # It's a git header line.
119 52 ▶ if ( $line =~ /^([0-9a-f]+)\s(\d+)\s(\d+)\s*(\d*)$/x )
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^([0-9a-f]+)\s(\d+)\s(\d+)\s*(\d*)$/x) { } 8 44 52
elsif ($line =~ /^([\w\-]+)\s*(.*)$/x) { } 44 0 44
120 {
121 8 ( $commit_id, $original_line_number, $final_line_number, $lines_count_in_group ) = ( $1, $2, $3, $4 );
122 }
123 elsif ( $line =~ m/^([\w\-]+)\s*(.*)$/x )
124 {
125 44 $commit_attributes->{ $commit_id }->{ $1 } = $2;
126 }
127 }
128 }
129  
130 # If we have a cache object, cache the output.
131 2 ▶ if ( defined( $cache ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache) 1 1 2
132 {
133 1 $cache->set_blame_lines(
134 file => $file,
135 blame_lines => $lines,
136 );
137 }
138  
139 2 return $lines;
140 }
141  
142  
143 =head1 BUGS
144  
145 Please report any bugs or feature requests through the web interface at
146 L<https://github.com/guillaumeaubert/Git-Repository-Plugin-Blame/issues>.
147 I will be notified, and then you'll automatically be notified of progress on
148 your bug as I make changes.
149  
150  
151 =head1 SUPPORT
152  
153 You can find documentation for this module with the perldoc command.
154  
155 perldoc Git::Repository::Plugin::Blame
156  
157  
158 You can also look for information at:
159  
160 =over 4
161  
162 =item * GitHub (report bugs there)
163  
164 L<https://github.com/guillaumeaubert/Git-Repository-Plugin-Blame/issues>
165  
166 =item * AnnoCPAN: Annotated CPAN documentation
167  
168 L<http://annocpan.org/dist/Git-Repository-Plugin-Blame>
169  
170 =item * CPAN Ratings
171  
172 L<http://cpanratings.perl.org/d/Git-Repository-Plugin-Blame>
173  
174 =item * MetaCPAN
175  
176 L<https://metacpan.org/release/Git-Repository-Plugin-Blame>
177  
178 =back
179  
180  
181 =head1 AUTHOR
182  
183 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
184 C<< <aubertg at cpan.org> >>.
185  
186  
187 =head1 COPYRIGHT & LICENSE
188  
189 Copyright 2012-2014 Guillaume Aubert.
190  
191 This program is free software: you can redistribute it and/or modify it under
192 the terms of the GNU General Public License version 3 as published by the Free
193 Software Foundation.
194  
195 This program is distributed in the hope that it will be useful, but WITHOUT ANY
196 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
197 PARTICULAR PURPOSE. See the GNU General Public License for more details.
198  
199 You should have received a copy of the GNU General Public License along with
200 this program. If not, see http://www.gnu.org/licenses/
201  
202 =cut
203  
204 2 1;
↑ Summary blib/lib/Git/Repository/Plugin/Blame/Line.pm »