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

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