blib/lib/Git/Version/Compare.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 12 / 12 Conditioncond 53.8% 14 / 26 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 80.7% 109 / 135 CC 23 SCAR 14.5
CC
23
Cov
88%
_normalize
9.0
__ANON__
3.2
__ANON__
3.2
CRAP 4.3
↑ Summary
1 package Git::Version::Compare;
2 4 $Git::Version::Compare::VERSION = '1.005';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 my @ops = qw( lt gt le ge eq ne );
9  
10 4 our @ISA = qw(Exporter);
11 4 our @EXPORT_OK = ( looks_like_git => map "${_}_git", cmp => @ops );
12 4 our %EXPORT_TAGS = ( ops => [ map "${_}_git", @ops ], all => \@EXPORT_OK );
13  
14 # A few versions have two tags, or non-standard numbering:
15 # - the left-hand side is what `git --version` reports
16 # - the right-hand side is an internal canonical name
17 #
18 # We turn versions into strings, so we can use the fast `eq` and `gt`.
19 # The 6 elements are integers padded with 0:
20 # - the 4 parts of the dotted version (padded with as many .0 as needed)
21 # - '.000' if not an RC, or '-xxx' if an RC (- sorts before . in ascii)
22 # - the number of commits since the previous tag (for dev versions)
23 #
24 # The special cases are pre-computed below, the rest is computed as needed.
25 4 my %version_alias = (
26 '0.99.7a' => '00.99.07.01.00.0000',
27 '0.99.7b' => '00.99.07.02.00.0000',
28 '0.99.7c' => '00.99.07.03.00.0000',
29 '0.99.7d' => '00.99.07.04.00.0000',
30 '0.99.8a' => '00.99.08.01.00.0000',
31 '0.99.8b' => '00.99.08.02.00.0000',
32 '0.99.8c' => '00.99.08.03.00.0000',
33 '0.99.8d' => '00.99.08.04.00.0000',
34 '0.99.8e' => '00.99.08.05.00.0000',
35 '0.99.8f' => '00.99.08.06.00.0000',
36 '0.99.8g' => '00.99.08.07.00.0000',
37 '0.99.9a' => '00.99.09.01.00.0000',
38 '0.99.9b' => '00.99.09.02.00.0000',
39 '0.99.9c' => '00.99.09.03.00.0000',
40 '0.99.9d' => '00.99.09.04.00.0000',
41 '0.99.9e' => '00.99.09.05.00.0000',
42 '0.99.9f' => '00.99.09.06.00.0000',
43 '0.99.9g' => '00.99.09.07.00.0000',
44 '0.99.9h' => '00.99.09.08.00.0000', # 1.0.rc1
45 '1.0.rc1' => '00.99.09.08.00.0000',
46 '0.99.9i' => '00.99.09.09.00.0000', # 1.0.rc2
47 '1.0.rc2' => '00.99.09.09.00.0000',
48 '0.99.9j' => '00.99.09.10.00.0000', # 1.0.rc3
49 '1.0.rc3' => '00.99.09.10.00.0000',
50 '0.99.9k' => '00.99.09.11.00.0000',
51 '0.99.9l' => '00.99.09.12.00.0000', # 1.0.rc4
52 '1.0.rc4' => '00.99.09.12.00.0000',
53 '0.99.9m' => '00.99.09.13.00.0000', # 1.0.rc5
54 '1.0.rc5' => '00.99.09.13.00.0000',
55 '0.99.9n' => '00.99.09.14.00.0000', # 1.0.rc6
56 '1.0.rc6' => '00.99.09.14.00.0000',
57 '1.0.0a' => '01.00.01.00.00.0000',
58 '1.0.0b' => '01.00.02.00.00.0000',
59 );
60  
61 169 ▶sub looks_like_git {
Subroutine looks_like_git: called CC=1 SCAR=0.0
Pod documented
62 169 return scalar $_[0] =~
63 /^(?:v|git\ version\ )? # prefix
64 [0-9]+(?:[.-](?:0[ab]?|[1-9][0-9a-z]*|[a-zA-Z]+))* # x.y.z.*
65 (?:[.-]?[a-z]+[0-9]+)? # rc or vendor specific suffixes
66 (?:[.-](GIT|[1-9][0-9]*[.-]g[A-Fa-f0-9]+))? # devel
67 (?:\ .*)? # comment
68 $/x;
69 }
70  
71 105 ▶sub _normalize {
Subroutine _normalize: called CC=9 SCAR=22.0
72 105 my ($v) = @_;
73 105 ▶ return undef if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 104 105
74  
75 # minimal consistency check
76 104 ▶ Carp::croak "$v does not look like a Git version" if !looks_like_git($v);
Branch: 1 branch
BranchTrueFalseTotal
unless looks_like_git($v) 1 103 104
77  
78 # reformat git.git tag names, output of `git --version`
79 103 $v =~ s/^v|^git version |\.[a-zA-Z]+\..*|[\012\015]+\z//g;
80 103 $v =~ y/-/./;
81 103 $v =~ s/0rc/0.rc/;
82 103 ($v) = split / /, $v; # drop anything after the version
83  
84 # can't use exists() because the assignment in the @ops created the slot
85 103 ▶ return $version_alias{$v} if defined $version_alias{$v};
Branch: 1 branch
BranchTrueFalseTotal
if defined $version_alias{$v} 10 93 103
86  
87 # split the dotted version string
88 93 my @v = split /\./, $v;
89 93 my ( $r, $c ) = ( 0, 0 );
90  
91 # commit count since the previous tag
92 93 ▶ ($c) = ( 1, splice @v, -1 ) if $v[-1] eq 'GIT'; # before 1.4
Branch: 1 branch
BranchTrueFalseTotal
if $v[-1] eq "GIT" 2 91 93
93 93 ▶ ($c) = splice @v, -2 if substr( $v[-1], 0, 1 ) eq 'g'; # after 1.4
Branch: 1 branch
BranchTrueFalseTotal
if substr($v[-1], 0, 1) eq "g" 15 78 93
94  
95 # release candidate number
96 93 ▶ ($r) = splice @v, -1 if substr( $v[-1], 0, 2 ) eq 'rc';
Branch: 1 branch
BranchTrueFalseTotal
if substr($v[-1], 0, 2) eq "rc" 21 72 93
97 93 ▶ $r &&= do { $r =~ s/rc//; sprintf '-%02d', $r };
Condition66%2/3
$r &&= do { $r =~ s/rc//; sprintf "-%02d", $r }
!ll&&!rl&&r
72021
MC/DC50%1/2
$r &&= do { $r =~ s/rc//; sprintf "-%02d", $r }
$r do { $r =~ s/rc//; sprintf "-%02d", $r }
Truth table66%2/3
A &&= B
A= $rB= do { $r =~ s/rc//; sprintf "-%02d", $r }
ABresult
0X0
100
111
98  
99 # compute and cache normalized string
100 93 ▶ return $version_alias{$v} =
Condition100%2/2
$r || '.00'
l!l
2172
MC/DC100%1/1
$r || '.00'
$r
Truth table100%2/2
A
A= $r
Aresult
11
00
101 join( '.', map sprintf( '%02d', $_ ), ( @v, 0, 0, 0 )[ 0 .. 3 ] )
102 . ( $r || '.00' )
103 . sprintf( '.%04d', $c );
104 }
105  
106 4 for my $op (@ops) {
107 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 1884 ▶ *{"${op}_git"} = eval << "OP";
Subroutine __ANON__: called CC=3 SCAR=11.5
Subroutine __ANON__: called CC=3 SCAR=11.5
Subroutine __ANON__: called CC=3 SCAR=11.5
Subroutine __ANON__: called CC=3 SCAR=11.5
Subroutine __ANON__: called CC=3 SCAR=11.5
Subroutine __ANON__: called CC=3 SCAR=11.5
Condition44%8/18
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
3757110
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
376800
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
376800
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
376800
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
376801
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
376800
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
MC/DC50%1/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
Truth table66%2/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
109 sub {
110 my ( \$v1, \$v2 ) = \@_;
111 \$_ = \$version_alias{\$_} ||= _normalize( \$_ ) for \$v1, \$v2;
112 return \$v1 $op \$v2;
113 }
114 OP
115 }
116  
117 2670 ▶sub cmp_git ($$) {
Subroutine cmp_git: called CC=3 SCAR=11.1
Pod documented
118 2670 my ( $v1, $v2 ) = @_;
119 2670 ▶ $_ = $version_alias{$_} ||= _normalize( $_ ) for $v1, $v2;
Condition66%2/3
$version_alias{$_} ||= _normalize($_)
l!l&&r!l&&!r
5271690
MC/DC0%0/2
$version_alias{$_} ||= _normalize($_)
$version_alias{$_} _normalize($_)
Truth table66%2/3
A ||= B
A= $version_alias{$_}B= _normalize($_)
ABresult
1X1
011
000
120 2670 return $v1 cmp $v2;
121 }
122  
123 4 1;
124  
125 __END__
↑ Summary