blib/lib/Gerrit/Client/ForEach.pm

filter: Statementstmt 76.6% 250 / 326 Branchbran 61.7% 84 / 136 Conditioncond 47.6% 20 / 42 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 82.0% 41 / 50 Podpod n/a totaltotal 69.2% 400 / 578 CC 86 SCAR 27.6
CC
86
Cov
70%
_do_callback
84.0
_ensure_cmd
16.7
_ensure_git_workdir_uptodate
12.0
CRAP 15.9
« blib/lib/Gerrit/Client.pm ↑ Summary t/20-gerrit-for-each-patchset.t »
1 #############################################################################
2 ##
3 ## Copyright (C) 2012-2014 Rohan McGovern <rohan@mcgovern.id.au>
4 ##
5 ## This library is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU Lesser General Public
7 ## License as published by the Free Software Foundation; either
8 ## version 2.1 of the License, or (at your option) any later version.
9 ##
10 ## This library is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## Lesser General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU Lesser General Public
16 ## License along with this library; if not, write to the Free Software
17 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ##
19 ##
20 #############################################################################
21  
22 package Gerrit::Client::ForEach;
23 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use AnyEvent::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Capture::Tiny qw(capture_merged);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Data::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use Gerrit::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # counter of how many connections we have per server
37 6 my %CONNECTION_COUNTER;
38  
39 # counter of how many worker processes we have
40 my $WORKER_COUNTER;
41  
42 # 1 when fetching into a gitdir
43 6 my %GITDIR_FETCHING;
44  
45 155 ▶sub _giturl_counter {
Subroutine _giturl_counter: called CC=1 SCAR=0.0
46 155 my ($giturl) = @_;
47 155 my $gerriturl = Gerrit::Client::_gerrit_parse_url($giturl)->{gerrit};
48 155 return \$CONNECTION_COUNTER{$gerriturl};
49 }
50  
51 168 ▶sub _debug_print {
Subroutine _debug_print: called CC=1 SCAR=0.0
52 168 return Gerrit::Client::_debug_print(@_);
53 }
54  
55 22 ▶sub _handle_for_each_event {
Subroutine _handle_for_each_event: called CC=4 SCAR=14.0
56 22 my ( $self, $event ) = @_;
57  
58 22 ▶ return unless $event->{type} eq 'patchset-created';
Branch: 1 branch
BranchTrueFalseTotal
unless $$event{"type"} eq "patchset-created" 0 22 22
59  
60 22 ▶ if (my $wanted = $self->{args}{wanted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wanted = $$self{"args"}{"wanted"}) 22 0 22
61 22 ▶ if (!$wanted->( $event->{change}, $event->{patchSet})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$wanted($$event{"change"}, $$event{"patchSet"})) 3 19 22
62 3 return;
63 }
64 }
65  
66 19 $self->_enqueue_event($event);
67  
68 19 return $self->_dequeue_soon();
69 }
70  
71 # Git command generators; these are methods so that they can be
72 # overridden for testing
73 sub _git_bare_clone_cmd
74 0 ▶{
Subroutine _git_bare_clone_cmd: not called CC=1 SCAR=6.9
75 0 my (undef, $giturl, $gitdir) = @_;
76 0 return (@Gerrit::Client::GIT, 'clone', '--bare', $giturl, $gitdir);
77 }
78  
79 sub _git_clone_cmd
80 0 ▶{
Subroutine _git_clone_cmd: not called CC=1 SCAR=6.9
81 0 my (undef, $giturl, $gitdir) = @_;
82 0 return (@Gerrit::Client::GIT, 'clone', $giturl, $gitdir);
83 }
84  
85 sub _git_fetch_cmd
86 0 ▶{
Subroutine _git_fetch_cmd: not called CC=1 SCAR=6.9
87 0 my (undef, $giturl, $gitdir, @refs) = @_;
88 return (@Gerrit::Client::GIT, '--git-dir', $gitdir, 'fetch', '-v', $giturl,
89 0 (map { "+$_:$_" } @refs));
90 }
91  
92 sub _git_reset_cmd
93 0 ▶{
Subroutine _git_reset_cmd: not called CC=2 SCAR=17.9
94 0 my (undef, $ref, $mode) = @_;
95 0 ▶ $mode ||= '--hard';
Condition0%0/2
$mode ||= "--hard"
l!l
00
MC/DC0%0/1
$mode ||= "--hard"
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
96 0 return (@Gerrit::Client::GIT, 'reset', $mode, $ref);
97 }
98  
99 # Returns 1 iff $gitdir contains the given $ref
100 116 ▶sub _have_ref {
Subroutine _have_ref: called CC=1 SCAR=0.0
101 116 my ($gitdir, $ref) = @_;
102 116 my $status;
103 116 ▶ capture_merged {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 116 $status = system(
105 @Gerrit::Client::GIT,
106 '--git-dir', $gitdir,
107 'rev-parse', $ref
108 );
109 116 };
110 116 return ($status == 0);
111 }
112  
113 # Returns 1 iff it appears the revision for $event has already been handled
114 0 ▶sub _is_commit_reviewed {
Subroutine _is_commit_reviewed: not called CC=2 SCAR=17.9
115 0 my ( $self, $event ) = @_;
116 0 my $gitdir = $self->_gitdir($event);
117 0 ▶ return unless ( -d $gitdir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $gitdir 0 0 0
118  
119 0 my $revision = $event->{patchSet}{revision};
120 0 my $status;
121 0 ▶ my $score = capture_merged {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 $status = system(
123 @Gerrit::Client::GIT, '--no-pager',
124 '--git-dir', $gitdir,
125 'notes', '--ref',
126 'Gerrit-Client-reviews', 'show',
127 $revision
128 );
129 0 };
130 0 _debug_print "git notes for $revision: status: $status, notes: $score\n";
131 0 return (0 == $status);
132 }
133  
134 # Flag a commit as reviewed in persistent storage; it won't be
135 # reviewed again.
136 0 ▶sub _mark_commit_reviewed {
Subroutine _mark_commit_reviewed: not called CC=2 SCAR=17.9
137 0 my ( $self, $event ) = @_;
138 0 my $gitdir = $self->_gitdir($event);
139  
140 0 my $revision = $event->{patchSet}{revision};
141 0 my $status;
142 0 ▶ my $output = capture_merged {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $status =
144 system( @Gerrit::Client::GIT, '--git-dir', $gitdir, 'notes', '--ref',
145 'Gerrit-Client-reviews', 'append', '-m', '1', $revision, );
146 0 };
147 0 ▶ if ( $status != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) { } 0 0 0
148 $self->{args}{on_error}
149 0 ->( "problem writing git note for $revision\n$output\n" );
150 }
151 else {
152 0 _debug_print "marked $revision as reviewed\n";
153 }
154 0 return;
155 }
156  
157 19 ▶sub _enqueue_event {
Subroutine _enqueue_event: called CC=2 SCAR=6.9
158 19 my ( $self, $event ) = @_;
159  
160 19 ▶ return if $self->_is_commit_reviewed( $event );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_commit_reviewed($event) 5 14 19
161  
162 14 push @{ $self->{queue} }, $event;
163  
164 14 return;
165 }
166  
167 247 ▶sub _giturl {
Subroutine _giturl: called CC=1 SCAR=0.0
168 247 my ( $self, $event ) = @_;
169 247 my $project = $event->{change}{project};
170 247 return "$self->{args}{ssh_url}/$project";
171 }
172  
173 281 ▶sub _gitdir {
Subroutine _gitdir: called CC=1 SCAR=0.0
174 281 my ( $self, $event ) = @_;
175 281 my $project = $event->{change}{project};
176 281 return "$self->{args}{workdir}/$project/git";
177 }
178  
179 108 ▶sub _ensure_git_cloned {
Subroutine _ensure_git_cloned: called CC=7 SCAR=20.0
180 108 my ( $self, $event, $out ) = @_;
181  
182 108 my $ref = $event->{patchSet}{ref};
183 108 my $project = $event->{change}{project};
184  
185 108 my $gitdir = $self->_gitdir($event);
186 108 my $giturl = $self->_giturl($event);
187  
188 # ensure only one event owns the cloning process for a given dir
189 108 ▶ if ($self->{git_cloning}{$gitdir} && !$event->{_git_cloning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"git_cloning"}{$gitdir} and not $$event{"_git_cloning"}) 55 53 108
Condition100%3/3
$$self{"git_cloning"}{$gitdir} and not $$event{"_git_cloning"}
!ll&&!rl&&r
64755
MC/DC100%2/2
$$self{"git_cloning"}{$gitdir} and not $$event{"_git_cloning"}
$$self{"git_cloning"}{$gitdir} not $$event{"_git_cloning"}
Truth table100%3/3
A and not(B)
A= $$self{"git_cloning"}{$gitdir}B= not $$event{"_git_cloning"}
ABresult
0X0
100
111
190 55 ▶ if ($self->{git_cloned}{$gitdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"git_cloned"}{$gitdir}) 55 0 55
191 55 return 1;
192 }
193 0 push @{$out}, $event;
194 0 return 0;
195 }
196  
197 53 $self->{git_cloning}{$gitdir} = 1;
198 53 $event->{_git_cloning} = 1;
199  
200 my $cloned = $self->_ensure_cmd(
201 event => $event,
202 queue => $out,
203 name => 'git clone',
204 cmd => [ $self->_git_bare_clone_cmd( $giturl, $gitdir ) ],
205 6 ▶ onlyif => sub { !-d $gitdir },
Subroutine __ANON__: called CC=1 SCAR=0.0
206 53 counter => [ _giturl_counter($giturl), $Gerrit::Client::MAX_CONNECTIONS ],
207 );
208 53 ▶ return unless $cloned;
Branch: 1 branch
BranchTrueFalseTotal
unless $cloned 6 47 53
209  
210 47 ▶ if (!$self->{git_cloned}{$gitdir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"git_cloned"}{$gitdir}) 6 41 47
211 6 $self->{git_cloned}{$gitdir} = 1;
212  
213 # make sure to wake up any other event who was waiting on the clone
214 6 $self->_dequeue_soon();
215 }
216  
217 47 ▶ if ( !-d $gitdir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $gitdir) 0 47 47
218 $self->{args}{on_error}
219 0 ->( "failed to clone $giturl to $gitdir\n" );
220 0 return;
221 }
222  
223 47 return 1;
224 }
225  
226 102 ▶sub _ensure_git_fetched {
Subroutine _ensure_git_fetched: called CC=5 SCAR=17.8
227 102 my ( $self, $event, $out, $in ) = @_;
228  
229 102 my $gitdir = $self->_gitdir($event);
230 102 my $giturl = $self->_giturl($event);
231 102 my $ref = $event->{patchSet}{ref};
232  
233 102 ▶ if ($event->{_have_ref} || _have_ref($gitdir, $ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$event{"_have_ref"} or _have_ref($gitdir, $ref)) 0 102 102
Condition33%1/3
$$event{"_have_ref"} or _have_ref($gitdir, $ref)
l!l&&r!l&&!r
00102
MC/DC0%0/2
$$event{"_have_ref"} or _have_ref($gitdir, $ref)
$$event{"_have_ref"} _have_ref($gitdir, $ref)
Truth table33%1/3
A or B
A= $$event{"_have_ref"}B= _have_ref($gitdir, $ref)
ABresult
1X1
011
000
234 0 ▶ $event->{_have_ref} ||= 1;
Condition0%0/2
$$event{"_have_ref"} ||= 1
l!l
00
MC/DC0%0/1
$$event{"_have_ref"} ||= 1
$$event{"_have_ref"}
Truth table0%0/2
A
A= $$event{"_have_ref"}
Aresult
11
00
235 0 return 1;
236 }
237  
238 # If we're running a 'git fetch', we should try to find
239 # _all_ needed refs for the given giturl and fetch them at once
240 my @refs = map {
241 37 ▶ ($self->_giturl($_) eq $giturl) ? ($_->{patchSet}{ref}) : ()
Branch: 1 branch
BranchTrueFalseTotal
$self->_giturl($_) eq $giturl ? : 9 28 37
242 102 } @{$in};
243  
244 return $self->_ensure_cmd(
245 event => $event,
246 queue => $out,
247 name => 'git fetch',
248 counter => [ _giturl_counter($giturl), $Gerrit::Client::MAX_CONNECTIONS ],
249 'lock' => \$GITDIR_FETCHING{$gitdir},
250 14 ▶ onlyif => sub { !_have_ref( $gitdir, $ref ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
251 102 cmd =>
252 [ $self->_git_fetch_cmd( $giturl, $gitdir, $ref, @refs ) ],
253 );
254 }
255  
256 71 ▶sub _ensure_git_workdir_uptodate {
Subroutine _ensure_git_workdir_uptodate: called CC=10 SCAR=24.8
257 71 my ( $self, $event, $out ) = @_;
258  
259 71 my $project = $event->{change}{project};
260 71 my $ref = $event->{patchSet}{ref};
261 71 my $gitdir = $self->_gitdir($event);
262  
263 71 alias my $workdir = $event->{_workdir};
264  
265 # avoid creating temporary directory etc if we can't run processes yet
266 71 ▶ if (!$workdir && ($WORKER_COUNTER||0) >= $Gerrit::Client::MAX_FORKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $workdir and ($WORKER_COUNTER || 0) >= $Gerrit::Client::MAX_FORKS) 0 71 71
Condition80%4/5
not $workdir and ($WORKER_COUNTER || 0) >= $Gerrit::Client::MAX_FORKS
!ll&&!rl&&r
58130
$WORKER_COUNTER || 0
l!l
49
MC/DC100%2/2
not $workdir and ($WORKER_COUNTER || 0) >= $Gerrit::Client::MAX_FORKS
not $workdir $WORKER_COUNTER
Truth table100%3/3
not(A) and B
A= not $workdirB= $WORKER_COUNTER
ABresult
0X0
100
111
267 0 push @{$out}, $event;
268 0 return;
269 }
270  
271 $workdir ||=
272 71 ▶ File::Temp->newdir("$self->{args}{workdir}/$project/work.XXXXXX");
Condition66%2/3
$workdir ||= "File::Temp"->newdir(qq[$$self{"args"}{"workdir"}/$project/work.XXXXXX])
l!l&&r!l&&!r
58130
MC/DC0%0/2
$workdir ||= "File::Temp"->newdir(qq[$$self{"args"}{"workdir"}/$project/work.XXXXXX])
$workdir "File::Temp"->newdir(qq[$$self{"args"}{"workdir"}/$project/work.XXXXXX])
Truth table66%2/3
A ||= B
A= $workdirB= "File::Temp"->newdir(qq[$$self{"args"}{"workdir"}/$project/work.XXXXXX])
ABresult
1X1
011
000
273  
274 71 my $bare = !$self->{args}{git_work_tree};
275  
276 return
277 unless $self->_ensure_cmd(
278 event => $event,
279 queue => $out,
280 name => 'git clone for workdir',
281 cmd => [ $bare
282 ? $self->_git_bare_clone_cmd( $gitdir, $workdir )
283 : $self->_git_clone_cmd( $gitdir, $workdir ) ],
284 13 ▶ onlyif => sub { !-d( $bare ? "$workdir/objects" : "$workdir/.git") },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$bare ? : 0 13 13
285 71 ▶ counter => [ \$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS ],
Branch: 2 branches
BranchTrueFalseTotal
unless $self->_ensure_cmd("event", $event, "queue", $out, "name", "git clone for workdir", "cmd", [$bare ? $self->_git_bare_clone_cmd($gitdir, $workdir) : $self->_git_clone_cmd($gitdir, $workdir)], "onlyif", sub { not -d ($bare ? "$workdir/objects" : "$workdir/.git"); } , "counter", [\$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS]) 13 58 71
$bare ? : 0 71 71
286 );
287  
288 return
289 58 ▶ unless $self->_ensure_cmd(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->_ensure_cmd("event", $event, "queue", $out, "name", "git fetch for workdir", "cmd", [$self->_git_fetch_cmd("origin", $bare ? $workdir : "$workdir/.git", $ref)], "wd", $workdir, "counter", [\$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS]) 19 39 58
$bare ? : 0 58 58
290 event => $event,
291 queue => $out,
292 name => 'git fetch for workdir',
293 cmd => [ $self->_git_fetch_cmd( 'origin', $bare ? $workdir : "$workdir/.git", $ref ) ],
294 wd => $workdir,
295 counter => [ \$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS ],
296 );
297  
298 39 ▶ return $self->_ensure_cmd(
Branch: 1 branch
BranchTrueFalseTotal
$bare ? : 0 39 39
299 event => $event,
300 queue => $out,
301 name => 'git reset for workdir',
302 cmd => [ $self->_git_reset_cmd( $ref, $bare ? '--soft' : '--hard' ) ],
303 wd => $workdir,
304 counter => [ \$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS ],
305 );
306 }
307  
308 335 ▶sub _ensure_cmd {
Subroutine _ensure_cmd: called CC=15 SCAR=28.1
309 335 my ( $self, %args ) = @_;
310  
311 335 my $event = $args{event};
312 335 my $name = $args{name};
313  
314 # capture output by default so that we can include it in error messages
315 335 ▶ if ( !exists( $args{saveoutput} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"saveoutput"}) 323 12 335
316 323 $args{saveoutput} = 1;
317 }
318  
319 335 my $donekey = "_cmd_${name}_done";
320 335 my $cvkey = "_cmd_${name}_cv";
321 335 my $statuskey = "_cmd_${name}_status";
322 335 my $outputkey = "_cmd_${name}_output";
323  
324 335 ▶ return 1 if ( $event->{$donekey} );
Branch: 1 branch
BranchTrueFalseTotal
if $$event{$donekey} 242 93 335
325  
326 93 ▶ my $onlyif = $args{onlyif} || sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'onlyif'} || sub { 1; }
l!l&&r!l&&!r
50430
MC/DC0%0/2
$args{'onlyif'} || sub { 1; }
$args{'onlyif'} sub { 1; }
Truth table66%2/3
A || B
A= $args{'onlyif'}B= sub { 1; }
ABresult
1X1
011
000
327 93 my $queue = $args{queue};
328  
329 93 my $weakself = $self;
330 93 weaken($weakself);
331  
332 93 alias my $cmdcv = $event->{$cvkey};
333 93 my $cmd = $args{cmd};
334 93 my $cmdstr;
335 {
336 93 local $LIST_SEPARATOR = '] [';
337 93 $cmdstr = "[@{$cmd}]";
338 }
339  
340 93 ▶ if ( !$cmdcv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdcv) 64 29 93
341  
342 # not done and not started; only needs doing if 'onlyif' returns false
343 64 ▶ if ( !$onlyif->() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$onlyif()) 0 64 64
344 0 $event->{$donekey} = 1;
345 0 return 1;
346 }
347  
348 # Don't run the command if it counts as a connection and we'd have
349 # too many
350 64 ▶ my ($counter, $count_max) = @{ $args{counter} || [] };
Condition50%1/2
$args{'counter'} || []
l!l
640
MC/DC0%0/1
$args{'counter'} || []
$args{'counter'}
Truth table50%1/2
A
A= $args{'counter'}
Aresult
11
00
351 64 my $uncounter;
352 64 ▶ if ($counter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counter) 64 0 64
353 64 ▶ if ( ($$counter||0) >= $count_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($$counter || 0) >= $count_max) 0 64 64
Condition100%2/2
$$counter || 0
l!l
2341
MC/DC100%1/1
$$counter || 0
$$counter
Truth table100%2/2
A
A= $$counter
Aresult
11
00
354 0 _debug_print(
355 "$cmdstr: delaying execution, would surpass limit\n");
356 0 push @{$queue}, $event;
357 0 return;
358 }
359 64 $$counter++;
360 64 ▶ $uncounter = guard { $$counter-- };
Subroutine __ANON__: called CC=1 SCAR=0.0
361 }
362  
363 64 my $lock = $args{lock};
364 64 my $unlock;
365 64 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) 14 50 64
366 14 ▶ if ($$lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$lock) 1 13 14
367 1 _debug_print(
368 "$cmdstr: delaying execution, lock held elsewhere\n");
369 1 push @{$queue}, $event;
370 1 return;
371 }
372 13 $$lock++;
373 13 ▶ $unlock = guard { $$lock-- };
Subroutine __ANON__: called CC=1 SCAR=0.0
374 }
375  
376 63 ▶ my $printoutput = sub { _debug_print( "$cmdstr: ", @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
377 63 my $handleoutput = $printoutput;
378  
379 63 ▶ if ( $args{saveoutput} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"saveoutput"}) 58 5 63
380 154 ▶ $handleoutput = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
381 154 $printoutput->(@_);
382 154 ▶ $event->{$outputkey} .= $_[0] if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 38 116 154
383 58 };
384 }
385  
386 63 my %run_cmd_args = (
387 '>' => $handleoutput,
388 '2>' => $handleoutput,
389 );
390  
391 63 ▶ if ( $args{wd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"wd"}) 31 32 63
392 0 ▶ $run_cmd_args{on_prepare} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
393 0 ▶ chdir( $args{wd} ) || warn __PACKAGE__ . ": chdir $args{wd}: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $args{"wd"} or warn 'Gerrit::Client::ForEach' . qq[: chdir $args{"wd"}: $!] 0 0 0
394 31 };
395 }
396  
397 63 $cmdcv = AnyEvent::Util::run_cmd( $cmd, %run_cmd_args, );
398 $cmdcv->cb(
399 62 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=18.3
400 62 my ($cv) = @_;
401 62 undef $uncounter;
402 62 undef $unlock;
403 62 ▶ return unless $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless $weakself 0 62 62
404  
405 62 my $status = $cv->recv();
406 62 ▶ if ( $status && !$args{allownonzero} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and not $args{'allownonzero'}) { } 0 62 62
Condition33%1/3
$status and not $args{"allownonzero"}
!ll&&!rl&&r
6200
MC/DC0%0/2
$status and not $args{"allownonzero"}
$status not $args{"allownonzero"}
Truth table33%1/3
A and not(B)
A= $statusB= not $args{"allownonzero"}
ABresult
0X0
100
111
407 $self->{args}{on_error}->( "$name exited with status $status\n"
408 0 ▶ . ( $event->{$outputkey} ? $event->{$outputkey} : q{} ) );
Branch: 1 branch
BranchTrueFalseTotal
$$event{$outputkey} ? : 0 0 0
409 }
410 else {
411 62 $event->{$donekey} = 1;
412 }
413 62 $event->{$statuskey} = $status;
414 62 $weakself->_dequeue_soon();
415 }
416 63 );
417 63 push @{$queue}, $event;
418 63 return;
419 }
420  
421 29 ▶ if ( !$cmdcv->ready ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdcv->ready) 29 0 29
422 29 push @{$queue}, $event;
423 29 return;
424 }
425  
426 0 $self->{args}{on_error}->("dropped event due to failed command: $cmdstr\n");
427 0 return;
428 }
429  
430 4 ▶sub _do_cb_sub {
Subroutine _do_cb_sub: called CC=2 SCAR=7.0
431 4 my ( $self, $sub, $event ) = @_;
432  
433 4 my $returned;
434 4 ▶ my $run = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
435 4 local $CWD = $event->{_workdir};
436 4 $returned = $sub->( $event->{change}, $event->{patchSet} );
437 4 };
438  
439 4 my $output;
440 4 ▶ if ($self->{args}{review}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{'args'}{'review'}) { } 0 4 4
441 0 $output = &capture_merged( $run );
442 } else {
443 4 $run->();
444 }
445  
446 return {
447 4 returned => $returned,
448 output => $output
449 };
450 }
451  
452 7 ▶sub _do_cb_forksub {
Subroutine _do_cb_forksub: called CC=3 SCAR=11.9
453 7 my ( $self, $sub, $event, $queue ) = @_;
454  
455 7 my $weakself = $self;
456 7 weaken($weakself);
457  
458 7 ▶ if ($event->{_forksub_result}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$event{"_forksub_result"}) 3 4 7
459 3 return $event->{_forksub_result};
460 }
461  
462 4 ▶ if ( $event->{_forked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$event{"_forked"}) 0 4 4
463 0 push @{$queue}, $event;
464 0 return;
465 }
466  
467 4 $event->{_forked} = 1;
468 &fork_call(
469 \&_do_cb_sub,
470 $self,
471 $sub,
472 $event,
473 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=16.6
474 4 ▶ return unless $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless $weakself 1 3 4
475  
476 3 my ($result) = $_[0];
477 3 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 3 3
478 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
479 0 $result = {output => $@};
480 } else {
481 0 $result = {output => $!};
482 }
483 }
484 3 $event->{_forksub_result} = $result;
485 3 $weakself->_dequeue_soon();
486 }
487 4 );
488 4 push @{$queue}, $event;
489 4 return;
490 }
491  
492 12 ▶sub _do_cb_cmd {
Subroutine _do_cb_cmd: called CC=9 SCAR=23.5
493 12 my ( $self, $cmd, $event, $out ) = @_;
494  
495 12 ▶ return if ( $event->{_done} );
Branch: 1 branch
BranchTrueFalseTotal
if $$event{"_done"} 0 12 12
496  
497 12 my $project = $event->{change}{project};
498 12 my $ref = $event->{patchSet}{ref};
499  
500 12 ▶ if ( !$event->{_cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$event{"_cmd"}) 5 7 12
501 5 ▶ if ( $cmd && ref($cmd) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd and ref $cmd eq "CODE") 0 5 5
Condition33%1/3
$cmd and ref $cmd eq "CODE"
!ll&&!rl&&r
050
MC/DC0%0/2
$cmd and ref $cmd eq "CODE"
$cmd ref $cmd eq "CODE"
Truth table33%1/3
A and B
A= $cmdB= ref $cmd eq "CODE"
ABresult
0X0
100
111
502 0 $cmd = [ $cmd->( $event->{change}, $event->{patchSet} ) ];
503 }
504 5 $event->{_cmd} = $cmd;
505 5 local $LIST_SEPARATOR = '] [';
506 5 _debug_print "on_patchset_cmd for $project $ref: [@{$cmd}]\n";
507 }
508  
509 return unless $self->_ensure_cmd(
510 event => $event,
511 queue => $out,
512 name => 'on_patchset_cmd',
513 cmd => $event->{_cmd},
514 wd => $event->{_workdir},
515 saveoutput => $self->{args}{review},
516 12 ▶ allownonzero => 1,
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ensure_cmd("event", $event, "queue", $out, "name", "on_patchset_cmd", "cmd", $$event{"_cmd"}, "wd", $$event{"_workdir"}, "saveoutput", $$self{"args"}{"review"}, "allownonzero", 1, "counter", [\$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS]) 8 4 12
517 counter => [ \$WORKER_COUNTER, $Gerrit::Client::MAX_FORKS ],
518 );
519  
520 4 my $score = 0;
521 4 my $output = $event->{_cmd_on_patchset_cmd_output};
522 4 my $status = $event->{_cmd_on_patchset_cmd_status};
523  
524 4 ▶ if ($status == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == -1) { } 0 4 4
elsif ($status & 127) { } 0 4 4
525 # exited abnormally; treat as neutral score
526 } elsif ($status & 127) {
527 # exited due to signal; treat as neutral score,
528 # append signal to output
529 0 $output .= "\n[exited due to signal ".($status&127)."]\n";
530 } else {
531 # exited normally; exit code is score
532 4 $score = $status >> 8;
533 # interpret exit code as signed
534 4 ▶ if ($score > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > 127) 0 4 4
535 0 $score = $score - 256;
536 }
537 }
538  
539 return {
540 4 score => $score,
541 output => $output
542 };
543 }
544  
545 23 ▶sub _do_callback {
Subroutine _do_callback: called CC=16 SCAR=44.3
546 23 my ( $self, $event, $out ) = @_;
547  
548 23 my $ref;
549 my $result;
550  
551 23 ▶ if ( $ref = $self->{args}{on_patchset} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref = $$self{'args'}{'on_patchset'}) { } 4 19 23
elsif ($ref = $$self{'args'}{'on_patchset_fork'}) { } 7 12 19
elsif ($ref = $$self{'args'}{'on_patchset_cmd'}) { } 12 0 12
552 4 $result = $self->_do_cb_sub( $ref, $event );
553 }
554 elsif ( $ref = $self->{args}{on_patchset_fork} ) {
555 7 $result = $self->_do_cb_forksub( $ref, $event, $out );
556 }
557 elsif ( $ref = $self->{args}{on_patchset_cmd} ) {
558 12 $result = $self->_do_cb_cmd( $ref, $event, $out );
559 }
560  
561 23 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 12 11 23
562  
563 11 ▶ if ($Gerrit::Client::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Gerrit::Client::DEBUG) 0 11 11
564 0 _debug_print 'callback result: ' . Dumper($result);
565 }
566  
567 # Ensure we shan't review it again
568 11 $self->_mark_commit_reviewed($event);
569  
570 11 my $review = $self->{args}{review};
571 11 ▶ return unless $review;
Branch: 1 branch
BranchTrueFalseTotal
unless $review 11 0 11
572  
573 0 ▶ if ( $review =~ m{\A\d+\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($review =~ /\A\d+\z/) 0 0 0
574 0 $review = 'code_review';
575 }
576  
577 0 ▶ if ( my $cb = $self->{args}{on_review} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $$self{"args"}{"on_review"}) 0 0 0
578 return
579 unless $cb->(
580 $event->{change}, $event->{patchSet},
581 $result->{output}, $result->{score},
582 $result->{returned}
583 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless &$cb($$event{"change"}, $$event{"patchSet"}, $$result{"output"}, $$result{"score"}, $$result{"returned"}) 0 0 0
584 }
585  
586 0 ▶ if ( !$result->{output} && !$result->{score} && !$result->{returned}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$result{"output"} or $$result{"score"} or $$result{"returned"}) 0 0 0
Condition0%0/6
$$result{"output"} or $$result{"score"} or $$result{"returned"}
l!l&&r!l&&!r
000
$$result{"output"} or $$result{"score"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$$result{"output"} or $$result{"score"} or $$result{"returned"}
$$result{"output"} $$result{"score"} $$result{"returned"}
Truth table0%0/4
A or B or C
A= $$result{"output"}B= $$result{"score"}C= $$result{"returned"}
ABCresult
1XX1
01X1
0011
0000
587 # no review to be done
588 0 return;
589 }
590  
591 my (%review_args) = (
592 message => $result->{output},
593 project => $event->{change}{project},
594 branch => $event->{change}{branch},
595 change => $event->{change}{id},
596 0 );
597  
598 0 for my $arg (qw(ssh_url http_url http_auth_cb)) {
599 0 $review_args{$arg} = $self->{args}{$arg};
600 }
601  
602 0 ▶ if ($result->{returned}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$result{"returned"}) 0 0 0
603 0 ▶ if (ref($result->{returned}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$result{'returned'} eq 'HASH') { } 0 0 0
604 0 $review_args{reviewInput} = $result->{returned};
605 } else {
606 0 $review_args{$review} = $result->{returned};
607 }
608 }
609  
610 Gerrit::Client::review(
611 $event->{patchSet}{revision},
612 0 %review_args
613 );
614 }
615  
616 90 ▶sub _dequeue_soon {
Subroutine _dequeue_soon: called CC=2 SCAR=7.0
617 90 my ($self) = @_;
618 90 my $weakself = $self;
619 90 weaken($weakself);
620 $self->{_dequeue_timer} ||= AE::timer( .1, 0,
621 73 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
622 73 ▶ return unless $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless $weakself 0 73 73
623 73 delete $weakself->{_dequeue_timer};
624 73 $weakself->_dequeue();
625 }
626 90 ▶ );
Condition66%2/3
$$self{"_dequeue_timer"} ||= &AE::timer("0.1", 0, sub { return unless $weakself; delete $weakself->{"_dequeue_timer"}; $weakself->_dequeue; } )
l!l&&r!l&&!r
16740
MC/DC0%0/2
$$self{"_dequeue_timer"} ||= &AE::timer("0.1", 0, sub { return unless $weakself; delete $weakself->{"_dequeue_timer"}; $weakself->_dequeue; } )
$$self{"_dequeue_timer"} &AE::timer("0.1", 0, sub { return unless $weakself; delete $weakself->{"_dequeue_timer"}; $weakself->_dequeue; } )
Truth table66%2/3
A ||= B
A= $$self{"_dequeue_timer"}B= &AE::timer("0.1", 0, sub { return unless $weakself; delete $weakself->{"_dequeue_timer"}; $weakself->_dequeue; } )
ABresult
1X1
011
000
627 }
628  
629 73 ▶sub _dequeue {
Subroutine _dequeue: called CC=8 SCAR=21.1
630 73 my ($self) = @_;
631  
632 73 ▶ if ($Gerrit::Client::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Gerrit::Client::DEBUG) 0 73 73
633 0 _debug_print 'queue before processing: ', Dumper( $self->{queue} );
634 }
635  
636 73 my $weakself = $self;
637 73 weaken($weakself);
638  
639 73 ▶ my @queue = @{ $self->{queue} || [] };
Condition50%1/2
$$self{'queue'} || []
l!l
730
MC/DC0%0/1
$$self{'queue'} || []
$$self{'queue'}
Truth table50%1/2
A
A= $$self{'queue'}
Aresult
11
00
640 73 my @newqueue;
641 73 ▶ while (my $event = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = shift @queue) 108 73 181
642 108 ▶ next unless $self->_ensure_git_cloned( $event, \@newqueue );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ensure_git_cloned($event, \@newqueue) 6 102 108
643 102 ▶ next unless $self->_ensure_git_fetched( $event, \@newqueue, \@queue );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ensure_git_fetched($event, \@newqueue, \@queue) 31 71 102
644 71 ▶ next unless $self->_ensure_git_workdir_uptodate( $event, \@newqueue );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ensure_git_workdir_uptodate($event, \@newqueue) 48 23 71
645 23 $self->_do_callback( $event, \@newqueue );
646 }
647  
648 73 $self->{queue} = \@newqueue;
649  
650 73 ▶ if ($Gerrit::Client::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Gerrit::Client::DEBUG) 0 73 73
651 0 _debug_print 'queue after processing: ', Dumper( $self->{queue} );
652 }
653  
654 73 return;
655 }
656  
657 6 1;
« blib/lib/Gerrit/Client.pm ↑ Summary t/20-gerrit-for-each-patchset.t »