DescribeVersion.pm

filter: Statementstmt 88.1% 97 / 110 Branchbran 81.8% 36 / 44 Conditioncond 51.8% 14 / 27 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 100% 16 / 16 Podpod 63.6% 7 / 11 totaltotal 77.3% 174 / 225 CC 36 SCAR 17.6
CC
36
Cov
81%
git
13.1
parse_version
9.0
new
7.1
CRAP 5.8
↑ Summary DescribeVersion/App.pm »
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of Git-DescribeVersion
4 #
5 # This software is copyright (c) 2010 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Git::DescribeVersion;
14 {
15 10 $Git::DescribeVersion::VERSION = '1.015';
16 }
17 # git description: v1.014-6-g81d7192
18  
19 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 $Git::DescribeVersion::AUTHORITY = 'cpan:RWSTAUNER';
21 }
22 # ABSTRACT: Use git-describe to show a repo's version
23  
24 10 ▶use Carp (); # core
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use version 0.82 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 10 our %Defaults = (
28 first_version => 'v0.1',
29 match_pattern => 'v[0-9]*',
30 format => 'decimal',
31 version_regexp => '([0-9._]+)'
32 );
33  
34 # Git::Repository is easier to install than Git::Wrapper
35 10 my @delegators = qw(
36 git_repository
37 git_wrapper
38 git_backticks
39 );
40  
41  
42 1248 ▶sub new {
Subroutine new: called CC=7 SCAR=19.7
Pod documented
43 1248 my $class = shift;
44 # accept a hash or hashref
45 1248 ▶ my %opts = ref($_[0]) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1216 32 1248
46 my $self = {
47 %Defaults,
48 # restrict accepted arguments
49 1248 map { $_ => $opts{$_} } grep { exists($opts{$_}) } keys %Defaults
50 };
51  
52 1248 ▶ $self->{directory} = $opts{directory} || '.';
Condition50%1/2
$opts{'directory'} || '.'
l!l
01248
MC/DC0%0/1
$opts{'directory'} || '.'
$opts{'directory'}
Truth table50%1/2
A
A= $opts{'directory'}
Aresult
11
00
53 1248 bless $self, $class;
54  
55 # accept a Git::Repository or Git::Wrapper object (or command to exec)
56 # or a simple '1' (true value) to indicate which one is desired
57 1248 foreach my $mod ( @delegators ){
58 3744 ▶ if( $opts{$mod} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$mod}) 1231 2513 3744
59 1231 $self->{git} = $mod;
60 # if it's just a true value leave it blank so we create later
61 # TODO: should this be checking ref?
62 $self->{$mod} = $opts{$mod}
63 1231 ▶ unless $opts{$mod} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{$mod} eq 1 1229 2 1231
64 # test that requested method "works"
65 1231 eval { $self->$mod('--version') };
66 1231 ▶ if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1231 1231
67 0 Carp::carp qq[Failed to execute $mod (will attempt other methods): $@];
68 0 delete @$self{(git => $mod)};
69 }
70 }
71 }
72 1248 return $self;
73 }
74  
75  
76 2312 ▶sub format_version {
Subroutine format_version: called CC=3 SCAR=11.0
Pod documented
77 2312 my ($self, $vobject) = @_;
78 2312 ▶ my $format = $self->{format} =~ /dot|normal|v|string/ ? 'normal' : 'numify';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'format'} =~ /dot|normal|v|string/ ? : 1538 774 2312
79 2312 my $version = $vobject->$format;
80 2312 ▶ $version =~ s/^v// if $self->{format} =~ /no.?v/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} =~ /no.?v/ 769 1543 2312
81 2312 return $version;
82 }
83  
84  
85 # NOTE: the git* subs are called in list context
86  
87 2708 ▶sub git {
Subroutine git: called CC=5 SCAR=25.7
Pod documented
88 2708 my ($self) = @_;
89 2708 ▶ unless( $self->{git} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"git"}) 0 2708 2708
90 0 for my $method ( @delegators ){
91 0 ▶ $self->{git} ||= eval {
Condition0%0/3
$self->{"git"} ||= eval { do { $self->$method("--version"); $method } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"git"} ||= eval { do { $self->$method("--version"); $method } }
$self->{"git"} eval { do { $self->$method("--version"); $method } }
Truth table0%0/3
A ||= B
A= $self->{"git"}B= eval { do { $self->$method("--version"); $method } }
ABresult
1X1
011
000
92 # confirm method works (without dying)
93 0 $self->$method('--version');
94 0 $method;
95 };
96 }
97 Carp::croak("All git methods failed. Is `git` installed?\n".
98 "Consider installing Git::Repository or Git::Wrapper.\n")
99 0 ▶ unless $self->{git};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"git"} 0 0 0
100 }
101 2708 goto &{$self->{git}};
102 }
103  
104 7 ▶sub git_backticks {
Subroutine git_backticks: called CC=6 SCAR=18.5
Pod undocumented
105 7 my ($self, $command, @args) = @_;
106 warn("'directory' attribute not supported when using backticks.\n" .
107 "Consider installing Git::Repository or Git::Wrapper.\n")
108 7 ▶ if $self->{directory} && $self->{directory} ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"directory"} and $self->{"directory"} ne "." 0 7 7
Condition33%1/3
$self->{"directory"} and $self->{"directory"} ne "."
!ll&&!rl&&r
070
MC/DC0%0/2
$self->{"directory"} and $self->{"directory"} ne "."
$self->{"directory"} $self->{"directory"} ne "."
Truth table33%1/3
A and B
A= $self->{"directory"}B= $self->{"directory"} ne "."
ABresult
0X0
100
111
109  
110 7 ▶ @args = map { ref $_ ? @$_ : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 3 7 10
111  
112 7 ▶ @args = map { quotemeta } @args
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 7 0 7
113 unless $^O eq 'MSWin32';
114  
115 my $exec = join(' ',
116 # the external app to run
117 7 ▶ ($self->{git_backticks} ||= 'git'),
Condition100%2/2
$self->{"git_backticks"} ||= "git"
l!l
52
MC/DC100%1/1
$self->{"git_backticks"} ||= "git"
$self->{"git_backticks"}
Truth table100%2/2
A
A= $self->{"git_backticks"}
Aresult
11
00
118 $command,
119 @args
120 );
121  
122 7 return (`$exec`);
123 }
124  
125 3 ▶sub git_repository {
Subroutine git_repository: called CC=3 SCAR=12.3
Pod undocumented
126 3 my ($self, $command, @args) = @_;
127 # Git::Repository 1.22 fails with alternate $/ (rt-71621)
128 3 local $/ = "\n";
129 (
130 $self->{git_repository} ||=
131 do {
132 0 require Git::Repository;
133 Git::Repository->new(work_tree => $self->{directory})
134 0 }
135 )
136 ->run($command,
137 3 ▶ map { ref $_ ? @$_ : $_ } @args
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 3 4
Condition33%1/3
$self->{"git_repository"} ||= do { require Git::Repository; "Git::Repository"->new("work_tree", $self->{"directory"}) }
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"git_repository"} ||= do { require Git::Repository; "Git::Repository"->new("work_tree", $self->{"directory"}) }
$self->{"git_repository"} do { require Git::Repository; "Git::Repository"->new("work_tree", $self->{"directory"}) }
Truth table33%1/3
A ||= B
A= $self->{"git_repository"}B= do { require Git::Repository; "Git::Repository"->new("work_tree", $self->{"directory"}) }
ABresult
1X1
011
000
138 );
139 }
140  
141 3929 ▶sub git_wrapper {
Subroutine git_wrapper: called CC=3 SCAR=12.0
Pod undocumented
142 3929 my ($self, $command, @args) = @_;
143 3929 $command =~ tr/-/_/;
144 (
145 $self->{git_wrapper} ||=
146 do {
147 0 require Git::Wrapper;
148 Git::Wrapper->new($self->{directory})
149 0 }
150 )
151 ->$command({
152 7204 map { ($$_[0] =~ /^-{0,2}(.+)$/, $$_[1]) }
153 3929 ▶ map { ref $_ ? $_ : [$_ => 1] } @args
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2251 4953 7204
Condition33%1/3
$self->{"git_wrapper"} ||= do { require Git::Wrapper; "Git::Wrapper"->new($self->{"directory"}) }
l!l&&r!l&&!r
392900
MC/DC0%0/2
$self->{"git_wrapper"} ||= do { require Git::Wrapper; "Git::Wrapper"->new($self->{"directory"}) }
$self->{"git_wrapper"} do { require Git::Wrapper; "Git::Wrapper"->new($self->{"directory"}) }
Truth table33%1/3
A ||= B
A= $self->{"git_wrapper"}B= do { require Git::Wrapper; "Git::Wrapper"->new($self->{"directory"}) }
ABresult
1X1
011
000
154 });
155 }
156  
157  
158  
159 2306 ▶sub parse_version {
Subroutine parse_version: called CC=9 SCAR=22.0
Pod documented
160 2306 my ($self, $prefix, $count) = @_;
161  
162 # This is unlikely as it should mean that both git commands
163 # returned unexpected output. If it does happen, don't die
164 # trying to parse it, default to first_version.
165 $prefix = $self->{first_version}
166 2306 ▶ unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 24 2282 2306
167 2306 ▶ $count ||= 0;
Condition100%2/2
$count ||= 0
l!l
2054252
MC/DC100%1/1
$count ||= 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
168  
169 # If still undef (first_version explicitly set to undef)
170 # don't die trying to parse it, just return nothing.
171 2306 ▶ unless( defined $prefix ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix) 3 2303 2306
172 3 warn("Version could not be determined.\n");
173 3 return;
174 }
175  
176 # s//$1/ requires the regexp to be anchored.
177 # Doing a match and then assigning to $1 does not.
178 2303 ▶ if( $self->{version_regexp} && $prefix =~ /$self->{version_regexp}/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version_regexp"} and $prefix =~ /$self->{"version_regexp"}/) 2297 6 2303
Condition66%2/3
$self->{"version_regexp"} and $prefix =~ /$self->{"version_regexp"}/
!ll&&!rl&&r
062297
MC/DC50%1/2
$self->{"version_regexp"} and $prefix =~ /$self->{"version_regexp"}/
$self->{"version_regexp"} $prefix =~ /$self->{"version_regexp"}/
Truth table66%2/3
A and B
A= $self->{"version_regexp"}B= $prefix =~ /$self->{"version_regexp"}/
ABresult
0X0
100
111
179 2297 $prefix = $1;
180 }
181  
182 2303 my $vstring = "v$prefix.$count";
183  
184 # quote 'version' to reference the module and not call the local sub
185 2303 my $vobject = eval {
186 # don't even try to parse it if it doesn't look like a version
187 2303 ▶ 'version'->parse($vstring)
Branch: 1 branch
BranchTrueFalseTotal
if version::is_lax($vstring) 2294 9 2303
188 if version::is_lax($vstring);
189 };
190  
191 # Don't die if it's not parseable, just return nothing.
192 2303 ▶ if( my $error = $@ || !$vobject ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@ || !$vobject) 9 2294 2303
Condition66%2/3
$@ || !$vobject
l!l&&r!l&&!r
092294
MC/DC50%1/2
$@ || !$vobject
$@ !$vobject
Truth table66%2/3
A || not(B)
A= $@B= !$vobject
ABresult
1X1
011
000
193 9 $error = $self->prepare_warning($error);
194 9 warn("'$vstring' is not a valid version string.\n$error");
195 9 return;
196 }
197  
198 2294 return $self->format_version($vobject);
199 }
200  
201 # normalize error message
202  
203 9 ▶sub prepare_warning {
Subroutine prepare_warning: called CC=2 SCAR=7.0
Pod undocumented
204 9 my ($self, $error) = @_;
205 9 ▶ return '' unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 9 9
206 9 $error =~ s/ at \S+?\.pm line \d+\.?\s*$//;
207 9 chomp($error);
208 9 return $error . "\n";
209 }
210  
211  
212 2252 ▶sub version {
Subroutine version: called CC=2 SCAR=7.1
Pod documented
213 2252 my ($self) = @_;
214 2252 ▶ return $self->version_from_describe() ||
Condition66%2/3
$self->version_from_describe || $self->version_from_count_objects
l!l&&r!l&&!r
18024500
MC/DC0%0/2
$self->version_from_describe || $self->version_from_count_objects
$self->version_from_describe $self->version_from_count_objects
Truth table66%2/3
A || B
A= $self->version_from_describeB= $self->version_from_count_objects
ABresult
1X1
011
000
215 $self->version_from_count_objects();
216 }
217  
218  
219 2255 ▶sub version_from_describe {
Subroutine version_from_describe: called CC=3 SCAR=11.3
Pod documented
220 2255 my ($self) = @_;
221 2255 my ($ver) = eval {
222 $self->git('describe',
223 2255 ['--match' => $self->{match_pattern}], qw(--tags --long)
224 );
225 };
226 # usually you'll expect a tag to be found, so warn if it isn't
227 2255 ▶ if( my $error = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2255 2255
228 0 $error = $self->prepare_warning($error);
229 0 warn("git-describe: $error");
230 }
231  
232 # return nothing so we know to move on to count-objects
233 2255 ▶ return unless $ver;
Branch: 1 branch
BranchTrueFalseTotal
unless $ver 450 1805 2255
234  
235 # ignore the -gSHA
236 1805 my ($tag, $count) = ($ver =~ /^(.+?)-(\d+)-(g[0-9a-f]+)$/);
237  
238 1805 return $self->parse_version($tag, $count);
239 }
240  
241  
242 453 ▶sub version_from_count_objects {
Subroutine version_from_count_objects: called CC=3 SCAR=11.0
Pod documented
243 453 my ($self) = @_;
244 453 my @counts = $self->git(qw(count-objects -v));
245 453 my $count = 0;
246 453 local $_;
247 453 foreach (@counts){
248 1206 ▶ /(count|in-pack): (\d+)/ and $count += $2;
Branch: 1 branch
BranchTrueFalseTotal
/(count|in-pack): (\d+)/ and $count += $2 750 456 1206
249 }
250 453 return $self->parse_version($self->{first_version}, $count);
251 }
252  
253 10 1;
254  
255  
256 __END__
↑ Summary DescribeVersion/App.pm »