Which/Plugin/Git.pm

filter: Statementstmt 27.2% 52 / 191 Branchbran 15.7% 11 / 70 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 50.0% 12 / 24 Podpod 100% 10 / 10 totaltotal 25.0% 85 / 339 CC 48 SCAR 39.7
CC
48
Cov
22%
cat
156.0
status
132.0
uptodate
20.0
CRAP 53.2
↑ Summary Which/Plugin/Subversion.pm »
1 package VCS::Which::Plugin::Git;
2  
3 # Created on: 2009-05-16 16:58:22
4 # Create by: ivan
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Contextual::Return;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 extends 'VCS::Which::Plugin';
21  
22 2 our $VERSION = version->new('0.6.9');
23 2 our $name = 'Git';
24 2 our $exe = 'git';
25 2 our $meta = '.git';
26  
27 6 ▶sub installed {
Subroutine installed: called CC=4 SCAR=13.9
Pod documented
28 6 my ($self) = @_;
29  
30 6 ▶ return $self->_installed if defined $self->_installed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_installed 5 1 6
31  
32 1 for my $path (split /[:;]/, $ENV{PATH}) {
33 8 ▶ next if !-x "$path/$exe";
Branch: 1 branch
BranchTrueFalseTotal
unless -x "$path/$VCS::Which::Plugin::Git::exe" 7 1 8
34  
35 1 return $self->_installed( 1 );
36 }
37  
38 0 return $self->_installed( 0 );
39 }
40  
41 21 ▶sub used {
Subroutine used: called CC=6 SCAR=18.8
Pod documented
42 21 my ( $self, $dir ) = @_;
43  
44 21 ▶ if (-f $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dir) 0 21 21
45 0 $dir = path($dir)->parent;
46 }
47  
48 21 ▶ croak "$dir is not a directory!" if !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 20 21
49  
50 20 my $current_dir = path($dir)->absolute;
51 20 my $level = 1;
52  
53 20 ▶ while ($current_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_dir) 131 0 131
54 131 ▶ if ( -d "$current_dir/$meta" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$current_dir/$VCS::Which::Plugin::Git::meta") 0 131 131
55 0 $self->_base( $current_dir );
56 0 return $level;
57 }
58  
59 131 $level++;
60  
61 # check that we still have a parent directory
62 131 ▶ last if $current_dir eq $current_dir->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $current_dir eq $current_dir->parent 20 111 131
63  
64 111 $current_dir = $current_dir->parent;
65 }
66  
67 20 return 0;
68 }
69  
70 0 ▶sub uptodate {
Subroutine uptodate: not called CC=4 SCAR=30.0
Pod documented
71 0 my ( $self, $dir ) = @_;
72  
73 0 ▶ $dir ||= $self->_base;
Condition0%0/3
$dir ||= $self->_base
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir ||= $self->_base
$dir $self->_base
Truth table0%0/3
A ||= B
A= $dirB= $self->_base
ABresult
1X1
011
000
74  
75 0 ▶ croak "'$dir' is not a directory!" if !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
76  
77 0 local $CWD = path($dir)->absolute;
78 0 my $ans = `$exe status`;
79  
80 0 ▶ return $ans =~ /nothing \s to \s commit/xms ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ans =~ /nothing \s to \s commit/msx ? : 0 0 0
81 }
82  
83 0 ▶sub pull {
Subroutine pull: not called CC=3 SCAR=24.8
Pod documented
84 0 my ( $self, $dir ) = @_;
85  
86 0 ▶ $dir ||= $self->_base;
Condition0%0/3
$dir ||= $self->_base
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir ||= $self->_base
$dir $self->_base
Truth table0%0/3
A ||= B
A= $dirB= $self->_base
ABresult
1X1
011
000
87  
88 0 ▶ croak "'$dir' is not a directory!" if !-e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
89  
90 0 local $CWD = $dir;
91 0 return !system "$exe pull > /dev/null 2> /dev/null";
92 }
93  
94 0 ▶sub push {
Subroutine push: not called CC=3 SCAR=24.8
Pod documented
95 0 my ( $self, $dir ) = @_;
96  
97 0 ▶ $dir ||= $self->_base;
Condition0%0/3
$dir ||= $self->_base
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir ||= $self->_base
$dir $self->_base
Truth table0%0/3
A ||= B
A= $dirB= $self->_base
ABresult
1X1
011
000
98  
99 0 ▶ croak "'$dir' is not a directory!" if !-e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
100  
101 0 local $CWD = $dir;
102 0 return !system "$exe push origin master > /dev/null 2> /dev/null";
103 }
104  
105 0 ▶sub cat {
Subroutine cat: not called CC=12 SCAR=50.5
Pod documented
106 0 my ($self, $file, $revision) = @_;
107  
108 # git expects $file to be relative to the base of the git repo not the
109 # current directory so we change it to being relative to the repo if nessesary
110 0 ▶ my $repo_dir = path($self->_base) or confess "How did I get here with out a base directory?\n";
Branch: 1 branch
BranchTrueFalseTotal
my $repo_dir = path($self->_base) or confess("How did I get here with out a base directory?\n") 0 0 0
111 0 my $cwd = path('.')->absolute;
112 0 local $CWD = $CWD;
113  
114 0 ▶ if ( -f $file && $cwd ne $repo_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and $cwd ne $repo_dir) 0 0 0
Condition0%0/3
-f $file and $cwd ne $repo_dir
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and $cwd ne $repo_dir
-f $file $cwd ne $repo_dir
Truth table0%0/3
A and B
A= -f $fileB= $cwd ne $repo_dir
ABresult
0X0
100
111
115 # get relavie directory of $cwd to $repo_dir
116 0 my ($relative) = $cwd =~ m{^ $repo_dir / (.*) $}xms;
117 0 my $old = $file;
118 0 $file = path("$relative/$file");
119 0 ▶ warn "Using repo absolute file $file from $old\n" if $ENV{VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE"} 0 0 0
120 0 $CWD = $repo_dir;
121 }
122  
123 0 ▶ if ( $revision && $revision =~ /^-?\d+$|[^0-9a-fA-F]/xms ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($revision and $revision =~ /^-?\d+$|[^0-9a-fA-F]/msx) { } 0 0 0
elsif (not defined $revision) { } 0 0 0
Condition0%0/3
$revision and $revision =~ /^-?\d+$|[^0-9a-fA-F]/msx
!ll&&!rl&&r
000
MC/DC0%0/2
$revision and $revision =~ /^-?\d+$|[^0-9a-fA-F]/msx
$revision $revision =~ /^-?\d+$|[^0-9a-fA-F]/msx
Truth table0%0/3
A and B
A= $revisionB= $revision =~ /^-?\d+$|[^0-9a-fA-F]/msx
ABresult
0X0
100
111
124 0 eval { require Git };
125 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VCS::Which::Plugin::Git::EVAL_ERROR) 0 0 0
126 0 die "Git.pm is not installed only propper revision names can be used\n";
127 }
128  
129 0 my $repo = Git->repository(Directory => $self->_base);
130 0 my @revs = reverse $repo->command('log', '--format=format:%H', '--', $file);
131 0 ▶ $revision = $revision =~ /^[-]?\d+$/xms && $revs[$revision] ? $revs[$revision] : $revision;
Branch: 1 branch
BranchTrueFalseTotal
$revision =~ /^[-]?\d+$/msx && $revs[$revision] ? : 0 0 0
Condition0%0/3
$revision =~ /^[-]?\d+$/msx and $revs[$revision]
!ll&&!rl&&r
000
MC/DC0%0/2
$revision =~ /^[-]?\d+$/msx and $revs[$revision]
$revision =~ /^[-]?\d+$/msx $revs[$revision]
Truth table0%0/3
A and B
A= $revision =~ /^[-]?\d+$/msxB= $revs[$revision]
ABresult
0X0
100
111
132 }
133 elsif ( !defined $revision ) {
134 0 $revision = '';
135 }
136 0 ▶ warn "$exe show $revision\:$file\n" if $ENV{VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE"} 0 0 0
137  
138 0 return `$exe show $revision\:$file`;
139 }
140  
141 0 ▶sub log {
Subroutine log: not called CC=4 SCAR=30.0
Pod documented
142 0 my ($self, @args) = @_;
143 0 local $CWD = $CWD;
144  
145 0 my $dir;
146 0 ▶ if ( defined $args[0] && -d $args[0] && $args[0] =~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and -d $args[0] and $args[0] =~ m[^/]) 0 0 0
Condition0%0/6
defined $args[0] and -d $args[0] and $args[0] =~ m[^/]
!ll&&!rl&&r
000
defined $args[0] and -d $args[0]
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args[0] and -d $args[0] and $args[0] =~ m[^/]
defined $args[0] -d $args[0] $args[0] =~ m[^/]
Truth table0%0/4
A and B and C
A= defined $args[0]B= -d $args[0]C= $args[0] =~ m[^/]
ABCresult
0XX0
10X0
1100
1111
147 0 $dir = shift @args;
148 0 $CWD = $dir;
149 }
150 0 my $args = join ' ', grep {defined $_} @args;
151  
152 return
153 0 ▶ SCALAR { scalar `$exe log $args` }
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 ▶ ARRAYREF {
Subroutine __ANON__: not called CC=4 SCAR=30.0
155 0 my @raw_log = `$exe log $args`;
156 0 my @log;
157 0 my $line = '';
158 0 for my $raw (@raw_log) {
159 0 ▶ if ( $raw =~ /^commit / && $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /^commit / and $line) { } 0 0 0
Condition0%0/3
$raw =~ /^commit / and $line
!ll&&!rl&&r
000
MC/DC0%0/2
$raw =~ /^commit / and $line
$raw =~ /^commit / $line
Truth table0%0/3
A and B
A= $raw =~ /^commit /B= $line
ABresult
0X0
100
111
160 0 CORE::push @log, $line;
161 0 $line = $raw;
162 }
163 else {
164 0 $line .= $raw;
165 }
166  
167 }
168 0 return \@log;
169 }
170 0 ▶ HASHREF {
Subroutine __ANON__: not called CC=2 SCAR=17.9
171 0 my $logs = `$exe log $args`;
172 0 my @logs = split /^commit\s*/xms, $logs;
173 0 shift @logs;
174 0 my $num = @logs;
175 0 my %log;
176 0 for my $log (@logs) {
177 0 $log{$num--} = $self->_log_expand($log);
178 }
179 0 return \%log;
180 }
181 0 }
182  
183 0 ▶sub _log_expand {
Subroutine _log_expand: not called CC=2 SCAR=17.9
184 0 my ($self, $log) = @_;
185  
186 # split the commit from the reset of the message
187 0 my ($ver, $rest) = split /\n/, $log, 2;
188  
189 # split log details and the description
190 0 my ($details, $description) = split /\n\n\s*/, $rest, 2;
191  
192 # remove excess whitespace at the end of the description
193 0 $description =~ s/\s+\Z//xms;
194 0 my ($conflicts) = $description =~ /\s+Conflicts:\s+(.*)\Z/xms;
195 0 $description =~ s/\s+Conflicts:\s+(.*)\Z//xms;
196  
197 # split up the details
198 0 my %log = map {split /:\s*/, $_, 2} split /\n/, $details;
199  
200 # add in the description
201 0 $log{description} = $description;
202  
203 # add in the revision
204 0 $log{rev} = $ver;
205  
206 # add conflicts if any
207 0 ▶ $log{conflicts} = [ split /\n\s+/, $conflicts ] if $conflicts;
Branch: 1 branch
BranchTrueFalseTotal
if $conflicts 0 0 0
208  
209 0 return \%log;
210 }
211  
212 0 ▶sub versions {
Subroutine versions: not called CC=2 SCAR=17.9
Pod documented
213 0 my ($self, $file, $oldest, $newest, $max) = @_;
214  
215 0 eval { require Git };
216 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VCS::Which::Plugin::Git::EVAL_ERROR) 0 0 0
217 0 die "Git.pm is not installed only propper revision names can be used\n";
218 }
219  
220 0 my $repo = Git->repository(Directory => $self->_base);
221 0 my @revs = reverse $repo->command('rev-list', '--all', '--', path($file)->absolute);
222  
223 0 return @revs;
224 }
225  
226 0 ▶sub status {
Subroutine status: not called CC=11 SCAR=48.8
Pod documented
227 0 my ($self, $dir) = @_;
228 0 my %status;
229 0 my $name = '';
230 0 ▶ if ( -f $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dir) 0 0 0
231 0 $name = path($dir)->absolute->basename;
232 }
233 0 ▶ local $CWD = -f $dir ? path($dir)->absolute->parent : path($dir)->absolute;
Branch: 1 branch
BranchTrueFalseTotal
-f $dir ? : 0 0 0
234 0 my $status = `$exe status $name`;
235 0 $status =~ s/^no \s+ changes (.*?) $//xms;
236 0 chomp $status;
237  
238 0 my @both = split /\n?[#]?\s+both\s+modified:\s+/, $status;
239 0 ▶ if ( @both > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@both > 1) 0 0 0
240 0 shift @both;
241 0 $both[-1] =~ s/\n.*//xms;
242 0 $status{both} = \@both;
243 }
244  
245 0 my @modified = split /\n?[#]?\s+modified:\s+/, $status;
246 0 ▶ if ( @modified > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modified > 1) 0 0 0
247 0 shift @modified;
248 0 $modified[-1] =~ s/\n.*//xms;
249 0 $status{modified} = \@modified;
250 }
251  
252 0 my @added = split /\n?[#]?\s+new\sfile:\s+/, $status;
253 0 ▶ if ( @added > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@added > 1) 0 0 0
254 0 shift @added;
255 0 $added[-1] =~ s/\n.*//xms;
256 0 $status{added} = \@added;
257 }
258  
259 0 my @committed = split /Changes to be committed:\n/, $status;
260 0 ▶ if (@committed > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@committed > 1) 0 0 0
261 0 my $new = pop @committed;
262 0 $status{committed} = [ $new =~ /^\t[^:]+:\s+(.*?)\n/gxms ];
263 }
264  
265 0 my @untracked = split /Untracked files:\n/, $status;
266 0 ▶ if ( @untracked > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@untracked > 1) 0 0 0
267 0 my $untracked = pop @untracked;
268 0 ▶ if ($untracked =~ s/^\s+[(]use \s+ "git \s+ add \s+ [^"]+" \s+ [^)]+\)\n\n//xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untracked =~ s/^\s+[(]use \s+ "git \s+ add \s+ [^"]+" \s+ [^)]+\)\n\n//msx) { } 0 0 0
269 0 chomp $untracked;
270 }
271 else {
272 0 $untracked =~ s/^[#].*?\n//gxms;
273 }
274  
275 0 ▶ if ($untracked =~ /^[#]/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untracked =~ /^[#]/msx) { } 0 0 0
276 0 $status{untracked} = [ grep {$_} map {chomp; $_} split /\n?[#]\s+/, $untracked ];
277 }
278 else {
279 0 $status{untracked} = [ $untracked =~ /^\t(.*?)\n/gxms ];
280 }
281 }
282  
283 0 ▶ if ($status =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($status =~ / You \s+ have \s+ unmerged \s+ paths[.]$ | All \s+ conflicts \s+ fixed \s+ but \s+ you \s+ are \s+ still \s+ merging[.]$ /msx) 0 0 0
284 You \s+ have \s+ unmerged \s+ paths[.]$
285 |
286 All \s+ conflicts \s+ fixed \s+ but \s+ you \s+ are \s+ still \s+ merging[.]$
287 /xms) {
288 0 $status{merge} = 1;
289 }
290  
291 0 return \%status;
292 }
293  
294 0 ▶sub checkout {
Subroutine checkout: not called CC=3 SCAR=24.8
Pod documented
295 0 my ($self, $dir, @extra) = @_;
296 0 my $name = '';
297 0 ▶ if ( -f $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dir) 0 0 0
298 0 $name = path($dir)->absolute->basename;
299 }
300 0 ▶ local $CWD = -f $dir ? path($dir)->absolute->parent : path($dir)->absolute;
Branch: 1 branch
BranchTrueFalseTotal
-f $dir ? : 0 0 0
301 0 my $extra = join ' ', @extra;
302 0 `$exe checkout $extra $name`;
303  
304 0 return;
305 }
306  
307 2 1;
308  
309 __END__
↑ Summary Which/Plugin/Subversion.pm »