Skilletor/Source/Git.pm

filter: Statementstmt 97.2% 208 / 214 Branchbran 84.6% 88 / 104 Conditioncond 64.3% 56 / 87 MC/DCmcdc 42.0% 21 / 50 Subroutinesub 100% 37 / 37 Podpod 100% 7 / 7 totaltotal 83.5% 417 / 499 CC 88 SCAR 19.9
CC
88
Cov
87%
_run
15.8
sweep_cache
11.7
remote_commit
10.3
CRAP 7.3
« Skilletor/Gitignore.pm ↑ Summary Skilletor/JSString.pm »
1 package Skilletor::Source::Git;
2 # ABSTRACT: A git source: shallow fetch into the shared cache, refs, SHA pins, offline reuse
3  
4 234 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 234 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 234 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 234 ▶use Skilletor::Cache qw( git_cache_dir CACHE_NAME_RE );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 234 ▶use Skilletor::Error::CacheRejected;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 234 ▶use Skilletor::Error::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 234 ▶use Skilletor::FSUtil qw( decode_utf8_lossy path_join path_resolve );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 234 ▶use Skilletor::GitEnv qw( git_env );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 234 ▶use Skilletor::JSString qw( js_lc js_trim );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 234 ▶use Skilletor::Process qw( run_command );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 234 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 234 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 234 with 'Skilletor::Role::Source';
18  
19 234 our $VERSION = '0.001';
20  
21 # Git for Windows checks out with core.autocrlf=true; the cache holds the repository's bytes
22 # there as everywhere, and longpaths lifts the 260-character limit.
23 234 ▶my @CHECKOUT_AS_IS = $^O eq 'MSWin32' ? ( '-c', 'core.autocrlf=false', '-c', 'core.eol=lf', '-c', 'core.longpaths=true' ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 234 234
24  
25 # The fetch refspec `git remote add` gives origin.
26 234 my $ORIGIN_REFSPEC = '+refs/heads/*:refs/remotes/origin/*';
27  
28 # git's order for a short ref name (ref_rev_parse_rules): the ref `git fetch` takes.
29 my @REF_RULES = (
30 1199 ▶ sub { $_[0] }, sub { 'refs/'.$_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
31 461 ▶ sub { 'refs/tags/'.$_[0] }, sub { 'refs/heads/'.$_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
32 123 ▶ sub { 'refs/remotes/'.$_[0] }, sub { 'refs/remotes/'.$_[0].'/HEAD' },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
33 234 );
34  
35 # The lock files, relative to .git, that git takes for what resolve runs.
36 234 my @LOCK_FILES = ( 'index.lock', 'shallow.lock', 'config.lock', 'HEAD.lock', 'ORIG_HEAD.lock', 'packed-refs.lock', 'reftable/tables.list.lock' );
37  
38 # How old a lock file must be to count as a dead git's: the sync lock's stale age.
39 234 my $STALE_LOCK_SECONDS = 5 * 60;
40  
41 234 has url => (
42 is => 'ro',
43 required => 1,
44 );
45  
46 234 has ref => (
47 is => 'ro',
48 );
49  
50 234 has cache_root => (
51 is => 'ro',
52 required => 1,
53 );
54  
55 has timeout_ms => (
56 is => 'ro',
57 3048 ▶ default => sub { 60_000 },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 234 );
59  
60 3898 ▶sub cache_dir {
Subroutine cache_dir: called CC=1 SCAR=0.0
Pod documented
61 3898 my ( $self ) = @_;
62 3898 return git_cache_dir( $self->cache_root, $self->url, $self->ref );
63 }
64  
65 3727 ▶sub is_commitish {
Subroutine is_commitish: called CC=3 SCAR=11.1
Pod documented
66 3727 my ( $self, $ref ) = @_;
67 3727 ▶ return ( defined $ref && $ref =~ /\A[0-9a-f]{7,40}\z/i ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && $ref =~ /\A[0-9a-f]{7,40}\z/i ? : 2316 1411 3727
Condition66%2/3
defined $ref and $ref =~ /\A[0-9a-f]{7,40}\z/i
!ll&&!rl&&r
014112316
MC/DC50%1/2
defined $ref and $ref =~ /\A[0-9a-f]{7,40}\z/i
defined $ref $ref =~ /\A[0-9a-f]{7,40}\z/i
Truth table66%2/3
A and B
A= defined $refB= $ref =~ /\A[0-9a-f]{7,40}\z/i
ABresult
0X0
100
111
68 }
69  
70 7620 ▶sub _pinned { my ( $self ) = @_; my $ref = $self->ref; return ( defined $ref && length $ref && $self->is_commitish($ref) ) ? 1 : 0 }
Subroutine _pinned: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && length $ref && $self->is_commitish($ref) ? : 2316 5304 7620
Condition83%5/6
defined $ref and length $ref and $self->is_commitish($ref)
!ll&&!rl&&r
389314112316
defined $ref and length $ref
!ll&&!rl&&r
389303727
MC/DC66%2/3
defined $ref and length $ref and $self->is_commitish($ref)
defined $ref length $ref $self->is_commitish($ref)
Truth table75%3/4
A and B and C
A= defined $refB= length $refC= $self->is_commitish($ref)
ABCresult
0XX0
10X0
1100
1111
71  
72 # One git run: no shell, the repository-local variables dropped, never a repository above the
73 # cache. Fails with git's stderr – or Node's "Command failed: …" when git wrote none.
74 20398 ▶sub _run {
Subroutine _run: called CC=13 SCAR=27.6
75 20398 my ( $self, $cwd, $args, $timeout_ms, $env ) = @_;
76 20398 my @cmd = ( 'git', @CHECKOUT_AS_IS, @$args );
77 20398 ▶ my %extra = ( ( length( $cwd // '' ) ? ( GIT_CEILING_DIRECTORIES => path_resolve( $cwd, '..' ) ) : () ), %{ $env // {} } );
Branch: 1 branch
BranchTrueFalseTotal
length($cwd // '') ? : 19246 1152 20398
Condition75%3/4
$cwd // ""
l!l
203980
$env // {}
l!l
44319955
MC/DC0%0/1
$cwd // ""
$cwd
MC/DC100%1/1
$env // {}
$env
Truth table50%1/2
A
A= $cwd
Aresult
11
00
Truth table100%2/2
A
A= $env
Aresult
11
00
78 20398 ▶ my $r = run_command(
Branch: 1 branch
BranchTrueFalseTotal
length($cwd // '') ? : 19246 1152 20398
Condition60%3/5
$cwd // ""
l!l
203980
$timeout_ms // $self->timeout_ms
l!l&&r!l&&!r
1152192460
MC/DC0%0/1
$cwd // ""
$cwd
MC/DC0%0/2
$timeout_ms // $self->timeout_ms
$timeout_ms $self->timeout_ms
Truth table50%1/2
A
A= $cwd
Aresult
11
00
Truth table66%2/3
A // B
A= $timeout_msB= $self->timeout_ms
ABresult
1X1
011
000
79 cmd => \@cmd,
80 cwd => ( length( $cwd // '' ) ? $cwd : undef ),
81 env => git_env(%extra),
82 timeout_ms => $timeout_ms // $self->timeout_ms,
83 );
84 20176 my $what = 'git '.join( ' ', @$args );
85 20176 ▶ if ( my $spawn = $r->{spawn_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spawn = $r->{"spawn_error"}) 0 20176 20176
86 0 ▶ Skilletor::Error::Source->throw( $what.': spawn git '.( $spawn->{errno} == 2 ? 'ENOENT' : $spawn->{message} ) );
Branch: 1 branch
BranchTrueFalseTotal
$spawn->{'errno'} == 2 ? : 0 0 0
87 }
88 20176 ▶ if ( $r->{timed_out} || !defined $r->{code} || $r->{code} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"timed_out"} or not defined $r->{"code"} or $r->{"code"} != 0) 2161 18015 20176
Condition50%3/6
$r->{"timed_out"} or not defined $r->{"code"} or $r->{"code"} != 0
l!l&&r!l&&!r
0216118015
$r->{"timed_out"} or not defined $r->{"code"}
l!l&&r!l&&!r
0020176
MC/DC33%1/3
$r->{"timed_out"} or not defined $r->{"code"} or $r->{"code"} != 0
$r->{"timed_out"} not defined $r->{"code"} $r->{"code"} != 0
Truth table50%2/4
A or not(B) or C
A= $r->{"timed_out"}B= not defined $r->{"code"}C= $r->{"code"} != 0
ABCresult
1XX1
01X1
0011
0000
89 2161 my $stderr = js_trim( decode_utf8_lossy( $r->{stderr} ) );
90 2161 ▶ Skilletor::Error::Source->throw( $what.': '.( length $stderr ? $stderr : 'Command failed: '.join( ' ', @cmd ) ) );
Branch: 1 branch
BranchTrueFalseTotal
length $stderr ? : 1944 217 2161
91 }
92 18015 return decode_utf8_lossy( $r->{stdout} );
93 }
94  
95 3357 ▶sub _ok {
Subroutine _ok: called CC=2 SCAR=6.9
96 3357 my ( $self, @args ) = @_;
97 3357 my $out;
98 3357 ▶ return eval { $out = $self->_run(@args); 1 } ? ( 1, $out ) : ( 0, $@ );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $out = $self->_run(@args); 1 } } ? : 2645 672 3317
99 }
100  
101 938 ▶sub _msg { my ( $err ) = @_; return blessed $err && $err->can('message') ? $err->message : js_trim("$err") }
Subroutine _msg: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->can('message') ? : 938 0 938
Condition33%1/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
00938
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
102  
103 4288 ▶sub _is_repo { -e path_join( $_[1], '.git' ) ? 1 : 0 }
Subroutine _is_repo: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
-e path_join($_[1], '.git') ? : 1834 2454 4288
104  
105 3232 ▶sub resolve {
Subroutine resolve: called CC=10 SCAR=23.0
Pod documented
106 3232 my ( $self ) = @_;
107 3232 my $dir = $self->cache_dir;
108 3232 my $ref = $self->ref;
109 3232 my $result = eval {
110 3232 ▶ if ( !$self->_is_repo($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_is_repo($dir)) { } 2382 850 3232
111 2382 path($dir)->mkpath;
112 2382 $self->_run( $dir, [ 'init', '-q' ] );
113 2360 $self->_run( $dir, [ 'remote', 'add', '--', 'origin', $self->url ] );
114 }
115 else {
116 850 $self->_repair($dir);
117 }
118 3172 my $reset_target = 'FETCH_HEAD';
119 3172 ▶ if ( $self->_pinned ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pinned) { } 1049 2123 3172
120 1049 my ($fetched) = $self->_ok( $dir, [ 'fetch', '--depth', '1', '--', 'origin', $ref ] );
121 1040 ▶ unless ($fetched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fetched) 455 585 1040
122 455 $self->_run( $dir, [ 'fetch', 'origin' ] );
123 122 $reset_target = $ref;
124 }
125 }
126 else {
127 2123 ▶ $self->_run( $dir, [ 'fetch', '--depth', '1', '--', 'origin', ( defined $ref ? $ref : 'HEAD' ) ] );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref ? : 542 1581 2123
128 }
129 2196 $self->_run( $dir, [ 'reset', '--hard', $reset_target ] );
130 # reset --hard leaves every file its index does not track.
131 2173 $self->_run( $dir, [ 'clean', '-ffdxq' ] );
132 2150 +{ dir => $dir, version => $self->_version($dir) };
133 };
134 3053 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 2115 938 3053
135 938 my $err = _msg($@);
136 938 ▶ if ( $self->_is_repo($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_repo($dir)) 938 0 938
137 938 my $served = eval {
138 938 my $version = $self->_version($dir);
139 401 $self->_restore($dir);
140 393 +{ dir => $dir, version => $version, warning => 'git fetch failed for '.$self->url.', using cache ('.$err.')' };
141 };
142 914 ▶ return $served if $served;
Branch: 1 branch
BranchTrueFalseTotal
if $served 393 521 914
143 521 my $cache_error = $@;
144 # A repo without a commit is no cache; only a refused pin or checkout is worth naming.
145 521 ▶ if ( blessed $cache_error && $cache_error->isa('Skilletor::Error::CacheRejected') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($cache_error) and $cache_error->isa("Skilletor::Error::CacheRejected")) 104 417 521
Condition66%2/3
builtin::blessed($cache_error) and $cache_error->isa("Skilletor::Error::CacheRejected")
!ll&&!rl&&r
0417104
MC/DC50%1/2
builtin::blessed($cache_error) and $cache_error->isa("Skilletor::Error::CacheRejected")
builtin::blessed($cache_error) $cache_error->isa("Skilletor::Error::CacheRejected")
Truth table66%2/3
A and B
A= builtin::blessed($cache_error)B= $cache_error->isa("Skilletor::Error::CacheRejected")
ABresult
0X0
100
111
146 104 Skilletor::Error::Source->throw( 'git source '.$self->url.' failed: '.$err.'; cache rejected ('.$cache_error->message.')' );
147 }
148 }
149 417 Skilletor::Error::Source->throw( 'git source '.$self->url.' failed: '.$err );
150 }
151  
152 # The cache as it is, without the network: what resolve serves when a fetch fails, without the
153 # warning – the checkout brought back to its HEAD, a pin checked against it.
154 118 ▶sub cached {
Subroutine cached: called CC=5 SCAR=20.4
Pod documented
155 118 my ( $self ) = @_;
156 118 my $dir = $self->cache_dir;
157 118 ▶ Skilletor::Error::Source->throw( 'git source '.$self->url.' is not in the cache (no fetch)' ) unless $self->_is_repo($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_repo($dir) 72 46 118
158 46 my $served = eval {
159 46 my $version = $self->_version($dir);
160 42 $self->_restore($dir);
161 38 +{ dir => $dir, version => $version };
162 };
163 38 ▶ return $served if $served;
Branch: 1 branch
BranchTrueFalseTotal
if $served 38 0 38
164 0 my $err = $@;
165 0 ▶ if ( blessed $err && $err->isa('Skilletor::Error::CacheRejected') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($err) and $err->isa("Skilletor::Error::CacheRejected")) 0 0 0
Condition0%0/3
builtin::blessed($err) and $err->isa("Skilletor::Error::CacheRejected")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::CacheRejected")
builtin::blessed($err) $err->isa("Skilletor::Error::CacheRejected")
Truth table0%0/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::CacheRejected")
ABresult
0X0
100
111
166 0 Skilletor::Error::Source->throw( 'git source '.$self->url.': cache rejected ('.$err->message.')' );
167 }
168 0 Skilletor::Error::Source->throw( 'git source '.$self->url.' is not in the cache (no fetch; '._msg($err).')' );
169 }
170  
171 # Make a cache a killed run left half-made fit to fetch into, keeping what it holds.
172 850 ▶sub _repair {
Subroutine _repair: called CC=5 SCAR=16.1
173 850 my ( $self, $dir ) = @_;
174 850 my ( $ok, $out ) = $self->_ok( $dir, [ 'config', '--local', '-z', '--get-regexp', '^remote\\.origin\\.(url|fetch)$' ] );
175 838 ▶ my %origin = $ok ? %{ $self->_config_entries($out) } : ();
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 809 29 838
176 838 ▶ unless ( exists $origin{'remote.origin.url'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $origin{"remote.origin.url"}) 29 809 838
177 29 $self->_run( $dir, [ 'init', '-q' ] );
178 28 $self->_run( $dir, [ 'remote', 'add', '--', 'origin', $self->url ] );
179 27 return;
180 }
181 809 ▶ $self->_run( $dir, [ 'remote', 'set-url', '--', 'origin', $self->url ] ) if $origin{'remote.origin.url'} ne $self->url;
Branch: 1 branch
BranchTrueFalseTotal
if $origin{"remote.origin.url"} ne $self->url 47 762 809
182 808 ▶ $self->_run( $dir, [ 'config', '--local', 'remote.origin.fetch', $ORIGIN_REFSPEC ] ) unless exists $origin{'remote.origin.fetch'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $origin{"remote.origin.fetch"} 35 773 808
183 }
184  
185 # Bring the checkout of a cache about to be served offline back to exactly its HEAD commit.
186 443 ▶sub _restore {
Subroutine _restore: called CC=4 SCAR=14.1
187 443 my ( $self, $dir ) = @_;
188 443 my ( $ok, $out ) = $self->_ok( $dir, [ 'status', '--porcelain', '--ignored', '--untracked-files=normal' ], undef, { GIT_OPTIONAL_LOCKS => '0' } );
189 433 ▶ return if $ok && $out eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ok and $out eq "" 420 13 433
Condition66%2/3
$ok and $out eq ""
!ll&&!rl&&r
013420
MC/DC50%1/2
$ok and $out eq ""
$ok $out eq ""
Truth table66%2/3
A and B
A= $okB= $out eq ""
ABresult
0X0
100
111
190 13 my $fixed = eval {
191 13 $self->_run( $dir, [ 'reset', '--hard', 'HEAD' ] );
192 12 $self->_run( $dir, [ 'clean', '-ffdxq' ] );
193 11 1;
194 };
195 11 ▶ return if $fixed;
Branch: 1 branch
BranchTrueFalseTotal
if $fixed 11 0 11
196 0 Skilletor::Error::CacheRejected->throw( 'its checkout cannot be brought back to HEAD: '._msg($@) );
197 }
198  
199 3134 ▶sub _version {
Subroutine _version: called CC=7 SCAR=19.5
200 3134 my ( $self, $dir ) = @_;
201 3134 my $ref = $self->ref;
202 3134 ▶ if ( $self->_pinned ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pinned) 1024 2110 3134
203 # Validate even on fallback: a repo's existence is no proof it holds this pin.
204 1024 my $head = js_trim( $self->_run( $dir, [ 'rev-parse', '--verify', 'HEAD^{commit}' ] ) );
205 1015 my ( $ok, $out ) = $self->_ok( $dir, [ 'rev-parse', '--verify', '--quiet', $ref.'^{commit}' ] );
206 1006 ▶ my $pin = $ok ? js_trim($out) : '';
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 818 188 1006
207 # A prefix of HEAD must resolve to HEAD unambiguously; any other ref is taken as a name
208 # unless it names another commit here – or is a full SHA, never read as a name.
209 1006 ▶ my $by_name = length($ref) < 40 && ( $pin eq '' || $pin eq $head );
Condition66%4/6
length $ref < 40 && ($pin eq '' || $pin eq $head)
!ll&&!rl&&r
7000306
$pin eq "" or $pin eq $head
l!l&&r!l&&!r
1881180
MC/DC33%1/3
length $ref < 40 && ($pin eq '' || $pin eq $head)
length $ref < 40 $pin eq "" $pin eq $head
Truth table75%3/4
A && B or C
A= length $ref < 40B= $pin eq ""C= $pin eq $head
ABCresult
0XX0
1000
11X1
1011
210 1006 ▶ my $held = index( $head, js_lc($ref) ) == 0 ? $pin eq $head : $by_name;
Branch: 1 branch
BranchTrueFalseTotal
index($head, js_lc($ref)) == 0 ? : 714 292 1006
211 1006 ▶ Skilletor::Error::CacheRejected->throw( 'cached commit '.$head.' does not match requested pin '.$ref ) unless $held;
Branch: 1 branch
BranchTrueFalseTotal
unless $held 104 902 1006
212 }
213 3012 my $sha = js_trim( $self->_run( $dir, [ 'rev-parse', '--short', 'HEAD' ] ) );
214 2558 return 'git:'.$sha;
215 }
216  
217 1648 ▶sub check {
Subroutine check: called CC=8 SCAR=20.8
Pod documented
218 1648 my ( $self, $cached_version ) = @_;
219 1648 ▶ return 1 unless defined $cached_version && length $cached_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cached_version and length $cached_version 334 1314 1648
Condition100%3/3
defined $cached_version and length $cached_version
!ll&&!rl&&r
1671671314
MC/DC100%2/2
defined $cached_version and length $cached_version
defined $cached_version length $cached_version
Truth table100%3/3
A and B
A= defined $cached_versionB= length $cached_version
ABresult
0X0
100
111
220 1314 my $ref = $self->ref;
221 1314 ( my $cached = $cached_version ) =~ s/\Agit://;
222 1314 $cached = js_lc($cached);
223 # A SHA pin the lock already holds never moves: decided without the remote.
224 1314 ▶ return 0 if $self->_pinned && _same_prefix( $cached, js_lc($ref) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_pinned and _same_prefix($cached, js_lc($ref)) 162 1152 1314
Condition100%3/3
$self->_pinned and _same_prefix($cached, js_lc($ref))
!ll&&!rl&&r
107181162
MC/DC100%2/2
$self->_pinned and _same_prefix($cached, js_lc($ref))
$self->_pinned _same_prefix($cached, js_lc($ref))
Truth table100%3/3
A and B
A= $self->_pinnedB= _same_prefix($cached, js_lc($ref))
ABresult
0X0
100
111
225 # The commit the ref names upstream; nothing counts as changed.
226 1152 ▶ my $name = defined $ref ? $ref : 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
defined $ref ? : 419 733 1152
227 1152 my @patterns = map { ( $_, $_.'^{}' ) } $name, $name.'/HEAD';
228 1152 my $out = $self->_run( '', [ 'ls-remote', '--', $self->url, @patterns ], $self->timeout_ms );
229 1007 my $remote = $self->remote_commit( $out, $name );
230 1007 ▶ return ( length $cached && index( $remote, $cached ) == 0 ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $cached && index($remote, $cached) == 0 ? : 477 530 1007
Condition66%2/3
length $cached and index($remote, $cached) == 0
!ll&&!rl&&r
0530477
MC/DC50%1/2
length $cached and index($remote, $cached) == 0
length $cached index($remote, $cached) == 0
Truth table66%2/3
A and B
A= length $cachedB= index($remote, $cached) == 0
ABresult
0X0
100
111
231 }
232  
233 # The commit a ref names in `git ls-remote` output: of the refs listed, the one git ranks
234 # first, through its peeled ^{} line when it is a tag object. "" when none is listed.
235 1199 ▶sub remote_commit {
Subroutine remote_commit: called CC=10 SCAR=23.3
Pod documented
236 1199 my ( $self, $ls_remote, $ref ) = @_;
237 1199 my %shas;
238 1199 for my $line ( split /\n/, $ls_remote, -1 ) {
239 2985 my ( $sha, $name ) = split /\t/, $line, -1;
240 2985 ▶ $shas{$name} = js_lc($sha) if defined $sha && length $sha && defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sha and length $sha and defined $name and length $name 1845 1140 2985
Condition66%6/9
defined $sha and length $sha and defined $name and length $name
!ll&&!rl&&r
114001845
defined $sha and length $sha and defined $name
!ll&&!rl&&r
114001845
defined $sha and length $sha
!ll&&!rl&&r
114001845
MC/DC25%1/4
defined $sha and length $sha and defined $name and length $name
defined $sha length $sha defined $name length $name
Truth table40%2/5
A and B and C and D
A= defined $shaB= length $shaC= defined $nameD= length $name
ABCDresult
0XXX0
10XX0
110X0
11100
11111
241 }
242 1199 for my $rule (@REF_RULES) {
243 2490 my $name = $rule->($ref);
244 2490 ▶ my $sha = $shas{ $name.'^{}' } // $shas{$name};
Condition100%3/3
$shas{$name . '^{}'} // $shas{$name}
l!l&&r!l&&!r
1938831414
MC/DC100%2/2
$shas{$name . '^{}'} // $shas{$name}
$shas{$name . '^{}'} $shas{$name}
Truth table100%3/3
A // B
A= $shas{$name . '^{}'}B= $shas{$name}
ABresult
1X1
011
000
245 2490 ▶ return $sha if defined $sha && length $sha;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sha and length $sha 1076 1414 2490
Condition66%2/3
defined $sha and length $sha
!ll&&!rl&&r
141401076
MC/DC50%1/2
defined $sha and length $sha
defined $sha length $sha
Truth table66%2/3
A and B
A= defined $shaB= length $sha
ABresult
0X0
100
111
246 }
247 123 return '';
248 }
249  
250 243 ▶sub _same_prefix {
Subroutine _same_prefix: called CC=4 SCAR=15.6
251 243 my ( $a, $b ) = @_;
252 243 ▶ return length $a && length $b && ( index( $a, $b ) == 0 || index( $b, $a ) == 0 );
Condition55%5/9
length $a && length $b && (index($a, $b) == 0 || index($b, $a) == 0)
!ll&&!rl&&r
081162
length $a and length $b
!ll&&!rl&&r
00243
index($a, $b) == 0 or index($b, $a) == 0
l!l&&r!l&&!r
016281
MC/DC25%1/4
length $a && length $b && (index($a, $b) == 0 || index($b, $a) == 0)
length $a length $b index($a, $b) == 0 index($b, $a) == 0
Truth table40%2/5
A and B && C or D
A= length $aB= length $bC= index($a, $b) == 0D= index($b, $a) == 0
ABCDresult
0XXX0
10XX0
11000
111X1
11011
253 }
254  
255 # `git config -z` output – key\nvalue\0 per entry – as key => last value.
256 809 ▶sub _config_entries {
Subroutine _config_entries: called CC=5 SCAR=16.3
257 809 my ( $self, $out ) = @_;
258 809 my %entries;
259 809 for my $entry ( split /\0/, $out, -1 ) {
260 2392 ▶ next if $entry eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "" 809 1583 2392
261 1583 my $nl = index $entry, "\n";
262 1583 ▶ $entries{ $nl < 0 ? $entry : substr( $entry, 0, $nl ) } = $nl < 0 ? '' : substr( $entry, $nl + 1 );
Branch: 2 branches
BranchTrueFalseTotal
$nl < 0 ? : 0 1583 1583
$nl < 0 ? : 0 1583 1583
263 }
264 809 return \%entries;
265 }
266  
267 # Remove the lock files a git killed midway left in the git caches of a cache root – each once
268 # older than the sync lock's stale age. Only safe while no resolve runs: the caller holds the
269 # sync lock. Never throws.
270 351 ▶sub sweep_cache {
Subroutine sweep_cache: called CC=11 SCAR=24.6
Pod documented
271 351 my ( $class, $cache_root, $now ) = @_;
272 351 ▶ $now //= Time::HiRes::time();
Condition33%1/3
$now //= &Time::HiRes::time()
l!l&&r!l&&!r
03510
MC/DC0%0/2
$now //= &Time::HiRes::time()
$now &Time::HiRes::time()
Truth table33%1/3
A //= B
A= $nowB= &Time::HiRes::time()
ABresult
1X1
011
000
273 351 ▶ opendir( my $dh, $cache_root ) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $cache_root or return 68 283 351
274 68 my @names = readdir $dh;
275 68 closedir $dh;
276 68 for my $name (@names) {
277 205 ▶ next unless $name =~ CACHE_NAME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[0-9a-f]{16}\z/ 137 68 205
278 68 my $git_dir = path_join( $cache_root, $name, '.git' );
279 68 ▶ next unless lstat($git_dir) && -d _; # a symlink or a gitfile: not one git init made
Branch: 1 branch
BranchTrueFalseTotal
unless lstat $git_dir and -d _ 0 68 68
Condition33%1/3
lstat $git_dir and -d _
!ll&&!rl&&r
0068
MC/DC0%0/2
lstat $git_dir and -d _
lstat $git_dir -d _
Truth table33%1/3
A and B
A= lstat $git_dirB= -d _
ABresult
0X0
100
111
280 68 for my $file ( ( map { path_join( $git_dir, $_ ) } @LOCK_FILES ), $class->_ref_locks( path_join( $git_dir, 'refs' ) ) ) {
281 477 ▶ my @st = lstat $file or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = lstat $file or next 5 472 477
282 5 ▶ next unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 5 5
283 5 ▶ unlink $file if $now - $st[9] > $STALE_LOCK_SECONDS;
Branch: 1 branch
BranchTrueFalseTotal
if $now - $st[9] > $STALE_LOCK_SECONDS 4 1 5
284 }
285 }
286 68 return;
287 }
288  
289 203 ▶sub _ref_locks {
Subroutine _ref_locks: called CC=8 SCAR=20.9
290 203 my ( $class, $dir ) = @_;
291 203 ▶ opendir( my $dh, $dir ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return () 203 0 203
292 203 ▶ my @names = grep { $_ ne '.' && $_ ne '..' } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
203203203
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
293 203 closedir $dh;
294 203 my @out;
295 203 for my $name (@names) {
296 203 my $p = path_join( $dir, $name );
297 203 ▶ lstat $p or next;
Branch: 1 branch
BranchTrueFalseTotal
lstat $p or next 203 0 203
298 203 ▶ if ( -d _ ) { push @out, $class->_ref_locks($p) }
Branch: 2 branches
BranchTrueFalseTotal
if (-d _) { } 135 68 203
elsif (-f _ and $name =~ /\.lock\z/) { } 1 67 68
299 1 ▶ elsif ( -f _ && $name =~ /\.lock\z/ ) { push @out, $p }
Condition66%2/3
-f _ and $name =~ /\.lock\z/
!ll&&!rl&&r
0671
MC/DC50%1/2
-f _ and $name =~ /\.lock\z/
-f _ $name =~ /\.lock\z/
Truth table66%2/3
A and B
A= -f _B= $name =~ /\.lock\z/
ABresult
0X0
100
111
300 }
301 203 return @out;
302 }
303  
304 234 1;
305  
306 __END__
« Skilletor/Gitignore.pm ↑ Summary Skilletor/JSString.pm »