lib/App/FuguBench/Worktree.pm

filter: Statementstmt 89.3% 301 / 337 Branchbran 78.6% 151 / 192 Conditioncond 60.4% 52 / 86 MC/DCmcdc 43.1% 22 / 51 Subroutinesub 100% 46 / 46 Podpod 0.0% 0 / 1 totaltotal 80.2% 572 / 713 CC 133 SCAR 22.3
CC
133
Cov
82%
_clone_repo
20.1
_remove
18.2
_copy_file
13.1
CRAP 9.3
« lib/App/FuguBench/Wiki.pm ↑ Summary lib/App/FuguBench/Fetch.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguBench::Worktree;
19 117 our $VERSION = '0.1.1';
20  
21 117 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 117 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 117 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 117 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 117 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 117 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 117 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 117 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 117 ▶use Fugu::CLI qw(EXIT_SUCCESS EXIT_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 117 ▶use Fugu::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # App::FuguBench::Worktree - the worktree verb.
37 #
38 # The verb makes, removes, and lists the worktrees of one checkout,
39 # and it clones the gitignored trees of that checkout into a
40 # worktree. The subcommands are create, remove, list, and clone. An
41 # unknown subcommand gives the usage error.
42 #
43 # The root of the verb is the main checkout, and -C names it. A
44 # linked worktree holds a .git file, not a directory, so the verb
45 # refuses such a root. No worktree then nests under another one.
46 #
47 # Create makes the branch first, as its own step. That step is the
48 # lock against a parallel create of one name. After that step the
49 # verb owns all that it makes, so a failure and a signal both run
50 # one cleanup.
51 #
52 # A second create of a name whose worktree exists runs the bootstrap
53 # again and writes the path again. A second create must not fail,
54 # and HOOK-WORKTREE-3 gives the reason.
55 #
56 # Only an operator runs remove, and no hook calls it (D-06). A
57 # session captures its work in the clones inside its worktree, so
58 # remove refuses a worktree that holds work at risk. The option
59 # --force overrides that refusal.
60 #
61 # Clone is the tool of a bootstrap target. It copies the gitignored
62 # paths of the main checkout into the current directory, with no
63 # network. A destination that exists stays as it is, so a second run
64 # repairs a bootstrap that stopped early.
65  
66 # The subcommands of the verb. An unknown word gives the usage error.
67 117 my %SUBCOMMAND = (
68 create => \&_create,
69 remove => \&_remove,
70 list => \&_list,
71 clone => \&_clone,
72 );
73  
74 # The shape of a worktree name (WT-CREATE-2). The first character is
75 # a letter or a digit: a name that starts with a dash reaches git as
76 # an option.
77 117 my $NAME = qr{\A[A-Za-z0-9][A-Za-z0-9._/-]*\z};
78  
79 # The shape of a path of clone (WT-CLONE-5). A gitignored path can
80 # start with a dot, such as .env, so the first character takes a dot
81 # and an underscore too. It takes no dash, which reaches git as an
82 # option.
83 117 my $PATH = qr{\A[A-Za-z0-9._][A-Za-z0-9._/-]*\z};
84  
85 # The format of one line of the listing (WT-LIST-1): the name, the
86 # age in days, and the state.
87 117 ▶use constant LINE => '%-40s %4s d %s';
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 # The makefiles that make reads, in the order of make itself.
90 117 ▶use constant MAKEFILES => qw(GNUmakefile Makefile makefile);
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 # App::FuguBench::Worktree->command($verb):
93 # The entry of the Fugu::CLI table. The module holds one verb,
94 # so it ignores the name.
95 117 ▶sub command ( $, $ )
Subroutine command: called CC=1 SCAR=0.0
Pod undocumented
96 {
97 return {
98 summary => 'create, remove, list, or clone into a worktree',
99 usage => 'create <name> | remove [--force] <name> | list'
100 . ' | clone <path>...',
101 options => {
102 force => 'remove a worktree that holds work at risk'
103 },
104 69 ▶ run => sub ( $app, @argv ) { return _run( $app, @argv ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 117 };
106 }
107  
108 # _run($app, @argv):
109 # The body of the verb. It reads the subcommand as its first
110 # argument. Each subcommand counts its own arguments before it
111 # looks for the root, so a usage error comes before a
112 # configuration error.
113 69 ▶sub _run ( $app, @argv )
Subroutine _run: called CC=3 SCAR=11.4
114 {
115 69 my $word = shift @argv;
116 69 ▶ my $sub = defined $word ? $SUBCOMMAND{$word} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $word ? : 69 0 69
117 69 ▶ return $app->cli->command_usage_error('worktree') unless $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 69 69
118  
119 69 return $sub->( $app, @argv );
120 }
121  
122 # _root($app):
123 # The exit code and the main checkout, in that order. The code
124 # is EXIT_SUCCESS when the root holds a value, and the method
125 # reports every failure itself.
126 #
127 # The path of the root comes from abs_path, because git reports
128 # the resolved path of a worktree. The two must agree, or the
129 # listing finds no worktree of the base.
130 69 ▶sub _root ($app)
Subroutine _root: called CC=4 SCAR=14.0
131 {
132 69 ▶ my $checkout = $app->checkout
Branch: 1 branch
BranchTrueFalseTotal
my $checkout = $app->checkout or return 3 69 0 69
133 or return Fugu::CLI::EXIT_CONFIG_ERROR();
134 69 my $root = Cwd::abs_path( $checkout->root );
135  
136 # A linked worktree holds a .git file, and the main checkout
137 # holds a .git directory.
138 69 ▶ unless ( defined $root && -d "$root/.git" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root and -d "$root/.git") 1 68 69
Condition66%2/3
defined $root and -d "$root/.git"
!ll&&!rl&&r
0168
MC/DC50%1/2
defined $root and -d "$root/.git"
defined $root -d "$root/.git"
Truth table66%2/3
A and B
A= defined $rootB= -d "$root/.git"
ABresult
0X0
100
111
139 1 $app->cli->log->error( 'not the main checkout: %s',
140 $checkout->root );
141 1 return EXIT_ERROR;
142 }
143  
144 68 return ( EXIT_SUCCESS, $root );
145 }
146  
147 # _setup($app):
148 # The exit code, the main checkout, and the worktree base, in
149 # that order. Create, remove, list, and clone all read the base.
150 #
151 # The base is the worktree.base key, and it resolves against the
152 # root (CLI-CONFIG-2). A clone under Projects/ can inherit the
153 # key from the workspace, and its worktrees belong to the clone.
154 #
155 # The base must name a directory below the root. A value of .
156 # resolves to the root itself, and the base then holds every
157 # path of the checkout. The containment guard of remove admits
158 # each one, so a plain directory of the checkout reaches
159 # remove_tree. The method refuses that value (CLI-CONFIG-3).
160 69 ▶sub _setup ($app)
Subroutine _setup: called CC=4 SCAR=13.9
161 {
162 69 my ( $code, $root ) = _root($app);
163 69 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 1 68 69
164  
165 68 my $checkout = $app->checkout;
166 68 my ($value) = $checkout->config('worktree.base');
167 68 my $dir = $checkout->dir_value($value);
168 68 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 4 64 68
169 4 $app->cli->log->error( 'worktree.base: %s', $checkout->error );
170 4 return Fugu::CLI::EXIT_CONFIG_ERROR();
171 }
172  
173 64 my $base = File::Spec->catdir( $root, $dir );
174 64 ▶ if ( $base eq $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base eq $root) 5 59 64
175 5 $app->cli->log->error(
176 'worktree.base: the directory is the root: %s', $dir );
177 5 return Fugu::CLI::EXIT_CONFIG_ERROR();
178 }
179  
180 59 return ( EXIT_SUCCESS, $root, $base );
181 }
182  
183 # _nested($root, $base):
184 # The pattern of a nested worktree directory (WT-REMOVE-3,
185 # WT-CLONE-3). The directory is the worktree.base value, a
186 # relative path under a checkout root. So the pattern matches it
187 # as a whole segment at the end of a path.
188 #
189 # _setup refuses a base that equals the root, and every caller
190 # reads the base from _setup. So the base is longer than the
191 # root, and the substr stays inside the string.
192 25 ▶sub _nested ( $root, $base )
Subroutine _nested: called CC=1 SCAR=0.0
193 {
194 25 my $dir = substr $base, length($root) + 1;
195  
196 25 return qr{(?:\A|/)\Q$dir\E\z};
197 }
198  
199 # _known($dir):
200 # True when git knows the directory as a checkout of its own. A
201 # linked worktree holds a .git file, and a main checkout holds a
202 # .git directory.
203 #
204 # Debris from a killed create holds neither. The discovery of
205 # git then walks up from it to the checkout above it, so every
206 # answer of `git -C <debris>` is an answer about that checkout.
207 # No caller must trust one.
208 40 ▶sub _known ($dir)
Subroutine _known: called CC=2 SCAR=6.9
209 {
210 40 ▶ return -e "$dir/.git" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e "$dir/.git" ? : 29 11 40
211 }
212  
213 # _create($app, @argv):
214 # Make one worktree of the checkout, and write its path to
215 # standard output as the only line (WT-CREATE-5).
216 #
217 # The branch step comes first, and the cleanup starts after it.
218 # A failure and a signal then remove all that the verb made: the
219 # worktree, the branch, and each empty parent directory.
220 #
221 # A name whose worktree exists goes to _again, which repeats the
222 # bootstrap and the path line (WT-CREATE-7).
223 26 ▶sub _create ( $app, @argv )
Subroutine _create: called CC=10 SCAR=23.2
224 {
225 26 ▶ return $app->cli->command_usage_error('worktree') if @argv != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 1 0 26 26
226 26 my ($name) = @argv;
227  
228 26 my ( $code, $root, $base ) = _setup($app);
229 26 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 2 24 26
230  
231 24 my $log = $app->cli->log;
232 24 ▶ unless ( $name =~ $NAME && $name !~ m{[.][.]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$NAME/u and not $name =~ /[.][.]/u) 3 21 24
Condition100%3/3
$name =~ /$NAME/u and not $name =~ /[.][.]/u
!ll&&!rl&&r
2121
MC/DC100%2/2
$name =~ /$NAME/u and not $name =~ /[.][.]/u
$name =~ /$NAME/u not $name =~ /[.][.]/u
Truth table100%3/3
A and not(B)
A= $name =~ /$NAME/uB= not $name =~ /[.][.]/u
ABresult
0X0
100
111
233 3 $log->error( 'invalid worktree name: %s', $name );
234 3 return EXIT_ERROR;
235 }
236  
237 21 my $wt = File::Spec->catdir( $base, $name );
238 21 ▶ return _again( $app, $root, $name, $wt ) if -e $wt;
Branch: 1 branch
BranchTrueFalseTotal
if -e $wt 4 17 21
239 17 ▶ return EXIT_ERROR unless _outside( $app, $base, $name, $wt );
Branch: 1 branch
BranchTrueFalseTotal
unless _outside($app, $base, $name, $wt) 1 16 17
240  
241 # The branch step is the lock against a parallel create of one
242 # name: one create is successful, and the others stop here and
243 # change nothing (WT-CREATE-3).
244 16 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app->command(["git", "-C", $root, "branch", $name], "group", 1)) 1 15 16
245 defined $app->command(
246 [ 'git', '-C', $root, 'branch', $name ],
247 group => 1
248 ) )
249 {
250 1 $log->error( 'cannot make the branch %s: %s',
251 $name, $app->error );
252 1 return EXIT_ERROR;
253 }
254  
255 15 ▶ my $cleanup = sub { _cleanup( $app, $root, $base, $name, $wt ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
256 1 ▶ my $signal = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
257 1 $SIG{INT} = $SIG{TERM} = 'IGNORE';
258 1 $log->error('interrupted, cleaning up');
259 1 ▶ Fugu::Process->terminate( $app->child, group => 1 )
Branch: 1 branch
BranchTrueFalseTotal
if defined $app->child 1 0 1
260 if defined $app->child;
261 1 $cleanup->();
262 1 exit EXIT_ERROR;
263 15 };
264 15 local $SIG{INT} = $signal;
265 15 local $SIG{TERM} = $signal;
266  
267 15 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app->command(["git", "-C", $root, "worktree", "add", $wt, $name], "group", 1)) 0 15 15
268 defined $app->command(
269 [ 'git', '-C', $root, 'worktree', 'add', $wt, $name ],
270 group => 1
271 ) )
272 {
273 0 $log->error( 'cannot add the worktree %s: %s',
274 $wt, $app->error );
275 0 $cleanup->();
276 0 return EXIT_ERROR;
277 }
278  
279 15 ▶ unless ( _bootstrap( $app, $root, $wt ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_bootstrap($app, $root, $wt)) 1 13 14
280 1 $log->error( 'the bootstrap of %s failed: %s',
281 $wt, $app->error );
282 1 $cleanup->();
283 1 return EXIT_ERROR;
284 }
285  
286 13 say $wt;
287  
288 13 return EXIT_SUCCESS;
289 }
290  
291 # _again($app, $root, $name, $wt):
292 # The result of a second create of one name (WT-CREATE-7). A
293 # second create must not fail, so the method runs the bootstrap
294 # again and writes the path again. Each clone step of the
295 # bootstrap skips what exists, so the run repairs a bootstrap
296 # that stopped early.
297 #
298 # A path that is no worktree of the name is debris from a killed
299 # create, and only remove clears it. The cleanup of _create must
300 # not run here, because the worktree belongs to the create before
301 # this one.
302 4 ▶sub _again ( $app, $root, $name, $wt )
Subroutine _again: called CC=5 SCAR=16.5
303 {
304 4 my $log = $app->cli->log;
305 4 ▶ my $branch =
Branch: 1 branch
BranchTrueFalseTotal
_known($wt) ? : 2 2 4
306 _known($wt)
307 ? _capture( $app, 'git', '-C', $wt, 'branch', '--show-current' )
308 : undef;
309 4 ▶ unless ( defined $branch && $branch eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $branch and $branch eq $name) 2 2 4
Condition66%2/3
defined $branch and $branch eq $name
!ll&&!rl&&r
202
MC/DC50%1/2
defined $branch and $branch eq $name
defined $branch $branch eq $name
Truth table66%2/3
A and B
A= defined $branchB= $branch eq $name
ABresult
0X0
100
111
310 2 $log->error( 'already exists, not a worktree of %s: %s',
311 $name, $wt );
312 2 $log->error( 'remove it with: %s worktree remove %s',
313 $app->cli->name, $name );
314 2 return EXIT_ERROR;
315 }
316  
317 2 ▶ unless ( _bootstrap( $app, $root, $wt ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_bootstrap($app, $root, $wt)) 0 2 2
318 0 $log->error( 'the bootstrap of %s failed: %s',
319 $wt, $app->error );
320 0 return EXIT_ERROR;
321 }
322  
323 2 say $wt;
324  
325 2 return EXIT_SUCCESS;
326 }
327  
328 # _outside($app, $base, $name, $wt):
329 # True when no existing worktree holds the new one
330 # (WT-CREATE-2). The removal of an outer worktree destroys an
331 # inner one, and it reports nothing. An empty parent directory
332 # of a sibling holds no .git, so two worktrees under one plain
333 # parent are permitted.
334 17 ▶sub _outside ( $app, $base, $name, $wt )
Subroutine _outside: called CC=3 SCAR=11.0
335 {
336 17 my $dir = dirname($wt);
337 17 ▶ while ( length($dir) > length($base) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dir > length $base) 5 16 21
338 5 ▶ if ( -e "$dir/.git" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/.git") 1 4 5
339 1 $app->cli->log->error(
340 '%s nests inside the worktree %s',
341 $name, $dir );
342 1 return 0;
343 }
344 4 $dir = dirname($dir);
345 }
346  
347 16 return 1;
348 }
349  
350 # _bootstrap($app, $root, $wt):
351 # Run the bootstrap target of the worktree (WT-CREATE-4). The
352 # target belongs to the repository, and it names the paths to
353 # clone. A worktree with no makefile needs no bootstrap.
354 17 ▶sub _bootstrap ( $app, $root, $wt )
Subroutine _bootstrap: called CC=2 SCAR=6.9
355 {
356 17 ▶ return 1 unless grep { -f "$wt/$_" } MAKEFILES;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {-f "$wt/$_";} ("GNUmakefile", "Makefile", "makefile") 12 5 17
357  
358 return
359 5 defined $app->command(
360 [ 'make', '-C', $wt, 'bootstrap', "MAIN=$root" ],
361 group => 1 );
362 }
363  
364 # _cleanup($app, $root, $base, $name, $wt):
365 # Remove all that create made (WT-CREATE-6). Every step runs,
366 # also after a step that fails: the worktree removal, the
367 # directory removal, the branch deletion, the prune of the git
368 # records, and the prune of each empty parent.
369 2 ▶sub _cleanup ( $app, $root, $base, $name, $wt )
Subroutine _cleanup: called CC=5 SCAR=21.6
370 {
371 2 ▶ $app->command( [
Branch: 1 branch
BranchTrueFalseTotal
if -d $wt 2 0 2
372 'git', '-C', $root, 'worktree',
373 'remove', '--force', '--force', $wt
374 ] ) if -d $wt;
375  
376 2 ▶ if ( -d $wt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $wt) 0 2 2
377 0 File::Path::remove_tree( $wt, { error => \my $failed } );
378 $app->cli->log->error( 'cannot remove %s', $wt )
379 0 ▶ if @{ $failed // [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$failed // [];} 0 0 0
Condition0%0/2
$failed // []
l!l
00
MC/DC0%0/1
$failed // []
$failed
Truth table0%0/2
A
A= $failed
Aresult
11
00
380 }
381  
382 2 _delete_branch( $app, $root, $name );
383 2 $app->command( [ 'git', '-C', $root, 'worktree', 'prune' ] );
384 2 _prune_parents( $base, $wt );
385  
386 2 return;
387 }
388  
389 # _remove($app, @argv):
390 # Remove one worktree of the checkout, and delete its branch
391 # (WT-REMOVE-1). Only an operator runs it, and no hook calls it.
392 #
393 # Without --force the subcommand refuses a worktree that holds
394 # work at risk, and it reports each cause (WT-REMOVE-2).
395 #
396 # The path resolves first, and the resolved path must sit under
397 # the base (WT-REMOVE-6). A symbolic link in the base must not
398 # permit a removal outside the base.
399 25 ▶sub _remove ( $app, @argv )
Subroutine _remove: called CC=17 SCAR=29.0
400 {
401 25 ▶ return $app->cli->command_usage_error('worktree') if @argv != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 1 0 25 25
402 25 my ($name) = @argv;
403  
404 25 my ( $code, $root, $base ) = _setup($app);
405 25 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 3 22 25
406  
407 22 my $log = $app->cli->log;
408 22 ▶ unless ( $name =~ $NAME && $name !~ m{[.][.]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$NAME/u and not $name =~ /[.][.]/u) 0 22 22
Condition33%1/3
$name =~ /$NAME/u and not $name =~ /[.][.]/u
!ll&&!rl&&r
0022
MC/DC0%0/2
$name =~ /$NAME/u and not $name =~ /[.][.]/u
$name =~ /$NAME/u not $name =~ /[.][.]/u
Truth table33%1/3
A and not(B)
A= $name =~ /$NAME/uB= not $name =~ /[.][.]/u
ABresult
0X0
100
111
409 0 $log->error( 'invalid worktree name: %s', $name );
410 0 return EXIT_ERROR;
411 }
412  
413 22 my $wt = File::Spec->catdir( $base, $name );
414  
415 # A parallel remove can take the directory between the test and
416 # the resolution, and abs_path then gives undef. Both results
417 # reach the branch below, which is no error (WT-REMOVE-6).
418 22 ▶ my $resolved = -d $wt ? Cwd::abs_path($wt) : undef;
Branch: 1 branch
BranchTrueFalseTotal
-d $wt ? : 20 2 22
419 22 ▶ unless ( defined $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resolved) 2 20 22
420  
421 # The worktree is gone, or a user removed it by hand.
422 # The prune and the branch deletion free the name again
423 # (WT-REMOVE-4).
424 2 $app->command( [ 'git', '-C', $root, 'worktree', 'prune' ] );
425 2 my $deleted = _delete_branch( $app, $root, $name );
426 2 _prune_parents( $base, $wt );
427  
428 2 ▶ return $deleted ? EXIT_SUCCESS : EXIT_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$deleted ? : 2 0 2
429 }
430  
431 20 ▶ unless ( rindex( $resolved, "$base/", 0 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rindex($resolved, "$base/", 0) == 0) 1 19 20
432 1 $log->error( 'refusing to remove %s: it resolves outside %s',
433 $name, $base );
434 1 return EXIT_ERROR;
435 }
436  
437 # The guard of D-06: the work inside the worktree must survive.
438 # Debris from a killed create is no checkout, so the walk reads
439 # no state of it. A clone below it is a repository of its own,
440 # and the walk reads that one.
441 19 ▶ unless ( $app->cli->option('force') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app->cli->option("force")) 15 4 19
442 15 my @risk = _risks( $app, $resolved, _nested( $root, $base ) );
443 15 ▶ if (@risk) {
Branch: 1 branch
BranchTrueFalseTotal
if (@risk) 5 10 15
444 5 $log->error( '%s', $_ ) for @risk;
445 5 $log->error(
446 'refusing to remove %s: it holds work at '
447 . 'risk; --force overrides',
448 $name
449 );
450 5 return EXIT_ERROR;
451 }
452 }
453  
454 # The branch that the worktree has checked out. git knows no
455 # debris of a killed create, so it reads the branch of the
456 # checkout above it. The name that create gives the branch is
457 # the right one there.
458 14 ▶ my $branch =
Branch: 1 branch
BranchTrueFalseTotal
_known($resolved) ? : 10 4 14
459 _known($resolved)
460 ? _capture( $app, 'git', '-C', $resolved, 'branch',
461 '--show-current' )
462 : undef;
463 14 ▶ $branch = $name unless defined $branch && length $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch and length $branch 4 10 14
Condition66%2/3
defined $branch and length $branch
!ll&&!rl&&r
4010
MC/DC50%1/2
defined $branch and length $branch
defined $branch length $branch
Truth table66%2/3
A and B
A= defined $branchB= length $branch
ABresult
0X0
100
111
464  
465 14 ▶ return EXIT_ERROR unless _take( $app, $root, $resolved );
Branch: 1 branch
BranchTrueFalseTotal
unless _take($app, $root, $resolved) 0 14 14
466  
467 14 my $deleted = _delete_branch( $app, $root, $branch );
468 14 _prune_parents( $base, $wt );
469  
470 14 ▶ return $deleted ? EXIT_SUCCESS : EXIT_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$deleted ? : 14 0 14
471 }
472  
473 # _take($app, $root, $wt):
474 # Take the directory of one worktree. The second --force takes a
475 # locked worktree too (WT-REMOVE-4). A directory that git does
476 # not know is debris inside the base, so the method takes the
477 # directory itself. It returns 0 when the directory stays.
478 14 ▶sub _take ( $app, $root, $wt )
Subroutine _take: called CC=3 SCAR=11.4
479 {
480 14 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $app->command(["git", "-C", $root, "worktree", "remove", "--force", "--force", $wt]) 10 4 14
481 if defined $app->command( [
482 'git', '-C', $root, 'worktree',
483 'remove', '--force', '--force', $wt
484 ] );
485  
486 4 my $log = $app->cli->log;
487 4 $log->error('git refused, deleting the directory');
488  
489 # The error key stops remove_tree from dying, and the directory
490 # itself is the result: a parallel remove that takes it first is
491 # no error (WT-REMOVE-6).
492 4 File::Path::remove_tree( $wt, { error => \my $failed } );
493 4 $app->command( [ 'git', '-C', $root, 'worktree', 'prune' ] );
494 4 ▶ return 1 unless -d $wt;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $wt 4 0 4
495  
496 0 $log->error( 'cannot remove %s', $wt );
497  
498 0 return 0;
499 }
500  
501 # _list($app, @argv):
502 # Report each worktree of the base with its name, its age in
503 # days, and its state (WT-LIST-1). An operator reads the state
504 # to see which worktree is safe to remove, because no hook
505 # removes one.
506 10 ▶sub _list ( $app, @argv )
Subroutine _list: called CC=12 SCAR=25.0
507 {
508 10 ▶ return $app->cli->command_usage_error('worktree') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 10 10
509  
510 10 my ( $code, $root, $base ) = _setup($app);
511 10 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 3 7 10
512  
513 7 my $out = $app->command(
514 [ 'git', '-C', $root, 'worktree', 'list', '--porcelain' ] );
515 7 ▶ unless ( defined $out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 0 7 7
516 0 $app->cli->log->error( 'cannot list the worktrees: %s',
517 $app->error );
518 0 return EXIT_ERROR;
519 }
520  
521 7 my @paths;
522 7 for my $line ( split /\n/, $out ) {
523 46 ▶ next unless $line =~ /\Aworktree (.+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\Aworktree (.+)\z/u 33 13 46
524 13 my $path = $1;
525 13 ▶ push @paths, $path if rindex( $path, "$base/", 0 ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if rindex($path, "$base/", 0) == 0 6 7 13
526 }
527 7 ▶ unless (@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 1 6 7
528 1 say 'no worktrees';
529 1 return EXIT_SUCCESS;
530 }
531  
532 6 my $nested = _nested( $root, $base );
533 6 for my $path ( sort @paths ) {
534  
535 # The gitfile records the creation, and later work
536 # leaves it alone, so its mtime is the age.
537 6 my @stat = stat "$path/.git";
538 6 ▶ my $age = @stat ? int( ( time - $stat[9] ) / 86_400 ) : -1;
Branch: 1 branch
BranchTrueFalseTotal
@stat ? : 5 1 6
539 6 my @risk = _risks( $app, $path, $nested );
540 6 ▶ my $state = @risk ? join( '; ', @risk ) : 'clean';
Branch: 1 branch
BranchTrueFalseTotal
@risk ? : 2 4 6
541 6 ▶ say sprintf LINE, substr( $path, length($base) + 1 ),
Branch: 1 branch
BranchTrueFalseTotal
$age < 0 ? : 1 5 6
542 $age < 0 ? '?' : $age, $state;
543 }
544  
545 6 return EXIT_SUCCESS;
546 }
547  
548 # _risks($app, $wt, $nested):
549 # Each reason why one worktree holds work at risk
550 # (WT-REMOVE-2). An empty list names a worktree that is safe to
551 # remove. Each string names one repository and one cause.
552 21 ▶sub _risks ( $app, $wt, $nested )
Subroutine _risks: called CC=12 SCAR=25.6
553 {
554 21 my @risk;
555 21 for my $repo ( _repos_in( $wt, $nested ) ) {
556 17 ▶ my $rel = $repo eq $wt ? '.' : substr( $repo, length($wt) + 1 );
Branch: 1 branch
BranchTrueFalseTotal
$repo eq $wt ? : 16 1 17
557  
558 17 my $dirty = _capture( $app, 'git', '-C', $repo, 'status',
559 '--porcelain' );
560 17 ▶ push @risk, "$rel: uncommitted change"
Branch: 1 branch
BranchTrueFalseTotal
if defined $dirty and length $dirty 4 13 17
Condition66%2/3
defined $dirty and length $dirty
!ll&&!rl&&r
0134
MC/DC50%1/2
defined $dirty and length $dirty
defined $dirty length $dirty
Truth table66%2/3
A and B
A= defined $dirtyB= length $dirty
ABresult
0X0
100
111
561 if defined $dirty && length $dirty;
562  
563 # Without a remote, a commit that the main branch
564 # holds is safe: the merge to main is where the work
565 # lands.
566 #
567 # A linked worktree shares one ref store with its main
568 # checkout, so --branches counts the branch of every
569 # other worktree too. Its own HEAD is the one ref that
570 # it owns. A nested clone is a repository of its own,
571 # so --branches is right for it.
572 17 my $linked = -f "$repo/.git";
573 17 my $remotes = _capture( $app, 'git', '-C', $repo, 'remote' );
574 17 my @not = ('--remotes');
575 17 ▶ push @not, 'main'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $remotes && length $remotes and _branch_exists($app, $repo, "main") 16 1 17
Condition50%3/6
not defined $remotes && length $remotes and _branch_exists($app, $repo, "main")
!ll&&!rl&&r
0116
defined $remotes && length $remotes
!ll&&!rl&&r
0170
MC/DC33%1/3
not defined $remotes && length $remotes and _branch_exists($app, $repo, "main")
defined $remotes length $remotes _branch_exists($app, $repo, "main")
Truth table40%2/5
not(A && B) and C
A= defined $remotesB= length $remotesC= _branch_exists($app, $repo, "main")
ABCresult
11X0
0X00
1000
0X11
1011
576 if !( defined $remotes && length $remotes )
577 && _branch_exists( $app, $repo, 'main' );
578  
579 17 ▶ my $count =
Branch: 1 branch
BranchTrueFalseTotal
$linked ? : 16 1 17
580 _capture( $app, 'git', '-C', $repo, 'rev-list', '--count',
581 $linked ? 'HEAD' : '--branches',
582 '--not', @not );
583 17 ▶ push @risk, "$rel: $count commit(s) that no remote holds"
Branch: 1 branch
BranchTrueFalseTotal
if defined $count and $count =~ /\A\d+\z/u and $count > 0 3 14 17
Condition50%3/6
defined $count and $count =~ /\A\d+\z/u and $count > 0
!ll&&!rl&&r
0143
defined $count and $count =~ /\A\d+\z/u
!ll&&!rl&&r
0017
MC/DC33%1/3
defined $count and $count =~ /\A\d+\z/u and $count > 0
defined $count $count =~ /\A\d+\z/u $count > 0
Truth table50%2/4
A and B and C
A= defined $countB= $count =~ /\A\d+\z/uC= $count > 0
ABCresult
0XX0
10X0
1100
1111
584 if defined $count && $count =~ /\A\d+\z/ && $count > 0;
585 }
586  
587 21 return @risk;
588 }
589  
590 # _repos_in($wt, $nested):
591 # Each git repository in one worktree: the worktree itself, and
592 # each repository below it, such as a clone under Projects/ or
593 # the library at Wiki/ (WT-REMOVE-3). The walk stops at each
594 # repository that it finds, because the content of a clone is
595 # the business of that clone. It skips scratch/ and a nested
596 # worktree directory, which hold no session work.
597 #
598 # Debris from a killed create is no repository, so the list
599 # holds it only when git knows it. A clone that a partial
600 # bootstrap left inside it is a repository, and the walk finds
601 # that one.
602 21 ▶sub _repos_in ( $wt, $nested )
Subroutine _repos_in: called CC=2 SCAR=6.9
603 {
604 21 ▶ my @repos = _known($wt) ? ($wt) : ();
Branch: 1 branch
BranchTrueFalseTotal
_known($wt) ? : 16 5 21
605 File::Find::find( {
606 no_chdir => 1,
607 21 ▶ preprocess => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
608 my @kept = sort
609 21 ▶ grep { $_ ne '.git' && $_ ne 'scratch' } @_;
Condition66%2/3
$_ ne '.git' && $_ ne 'scratch'
!ll&&!rl&&r
160102
MC/DC50%1/2
$_ ne '.git' && $_ ne 'scratch'
$_ ne '.git' $_ ne 'scratch'
Truth table66%2/3
A && B
A= $_ ne '.git'B= $_ ne 'scratch'
ABresult
0X0
100
111
610  
611 21 return @kept;
612 },
613 81 ▶ wanted => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
614 81 my $name = $File::Find::name;
615 81 ▶ return if $name eq $wt;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $wt 21 60 81
616 60 ▶ return unless -d $name && !-l $name;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $name and not -l $name 58 2 60
Condition66%2/3
-d $name and not -l $name
!ll&&!rl&&r
5802
MC/DC50%1/2
-d $name and not -l $name
-d $name not -l $name
Truth table66%2/3
A and not(B)
A= -d $nameB= not -l $name
ABresult
0X0
100
111
617 2 ▶ if ( $name =~ $nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$nested/u) 1 1 2
618 1 $File::Find::prune = 1;
619 1 return;
620 }
621 1 ▶ return unless _known($name);
Branch: 1 branch
BranchTrueFalseTotal
unless _known($name) 0 1 1
622 1 push @repos, $name;
623 1 $File::Find::prune = 1;
624  
625 1 return;
626 },
627 },
628 21 $wt
629 );
630  
631 21 return @repos;
632 }
633  
634 # _clone($app, @argv):
635 # Copy the gitignored paths of the main checkout into the
636 # current directory, with no network and no gh (WT-CLONE-1). A
637 # bootstrap target of a worktree names the paths.
638 #
639 # Each path passes the shape check before any work starts, so
640 # one bad path stops the run and changes nothing (CLI-PROGRAM-6).
641 # The subcommand writes inside the current directory only, and
642 # in the main checkout itself it changes nothing (WT-CLONE-6).
643 8 ▶sub _clone ( $app, @argv )
Subroutine _clone: called CC=9 SCAR=22.0
644 {
645 8 ▶ return $app->cli->command_usage_error('worktree') unless @argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 0 8 8
646  
647 8 my ( $code, $root, $base ) = _setup($app);
648 8 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 2 6 8
649  
650 6 my $log = $app->cli->log;
651 6 my @paths;
652 6 for my $path (@argv) {
653 8 $path =~ s{/+\z}{};
654 8 ▶ unless ( _valid_path($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_valid_path($path)) 1 7 8
655 1 $log->error( 'invalid path: %s', $path );
656 1 return EXIT_ERROR;
657 }
658 7 push @paths, $path;
659 }
660  
661 5 my $here = Cwd::abs_path(q{.});
662 5 ▶ if ( defined $here && $here eq $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $here and $here eq $root) 1 4 5
Condition66%2/3
defined $here and $here eq $root
!ll&&!rl&&r
041
MC/DC50%1/2
defined $here and $here eq $root
defined $here $here eq $root
Truth table66%2/3
A and B
A= defined $hereB= $here eq $root
ABresult
0X0
100
111
663 1 $log->notice('already the main checkout, nothing to do');
664 1 return EXIT_SUCCESS;
665 }
666  
667 4 my $nested = _nested( $root, $base );
668 4 for my $path (@paths) {
669 6 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _clone_path($app, $root, $path, $nested) 0 6 6
670 unless _clone_path( $app, $root, $path, $nested );
671 }
672  
673 4 return EXIT_SUCCESS;
674 }
675  
676 # _valid_path($path):
677 # True when one path of clone holds the shape of WT-CLONE-5: a
678 # relative path, with no .. segment, and not the current
679 # directory.
680 8 ▶sub _valid_path ($path)
Subroutine _valid_path: called CC=3 SCAR=11.7
681 {
682 8 ▶ return $path =~ $PATH && $path !~ m{[.][.]} && $path ne q{.};
Condition66%4/6
$path =~ /$PATH/u && !($path =~ /[.][.]/u) && $path ne '.'
!ll&&!rl&&r
107
$path =~ /$PATH/u and not $path =~ /[.][.]/u
!ll&&!rl&&r
017
MC/DC33%1/3
$path =~ /$PATH/u && !($path =~ /[.][.]/u) && $path ne '.'
$path =~ /$PATH/u not $path =~ /[.][.]/u $path ne '.'
Truth table50%2/4
A and not(B) && C
A= $path =~ /$PATH/uB= not $path =~ /[.][.]/uC= $path ne '.'
ABCresult
0XX0
10X0
1100
1111
683 }
684  
685 # _clone_path($app, $root, $path, $nested):
686 # Take one path of clone (WT-CLONE-2). A repository gives a
687 # local clone, a directory gives one clone of each child, and a
688 # plain file gives a copy. An absent path gives a message and no
689 # failure, because a consumer names a path that its own tree can
690 # omit (WT-CLONE-5). The method returns 0 after a failure.
691 6 ▶sub _clone_path ( $app, $root, $path, $nested )
Subroutine _clone_path: called CC=10 SCAR=24.0
692 {
693 6 my $log = $app->cli->log;
694 6 my $src = File::Spec->catdir( $root, $path );
695  
696 6 ▶ return _clone_repo( $app, $src, $path, $nested )
Branch: 1 branch
BranchTrueFalseTotal
if -d $src and -e "$src/.git" 1 5 6
Condition100%3/3
-d $src and -e "$src/.git"
!ll&&!rl&&r
321
MC/DC100%2/2
-d $src and -e "$src/.git"
-d $src -e "$src/.git"
Truth table100%3/3
A and B
A= -d $srcB= -e "$src/.git"
ABresult
0X0
100
111
697 if -d $src && -e "$src/.git";
698  
699 5 ▶ if ( -d $src ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $src) 2 3 5
700 2 ▶ opendir my $dh, $src or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $src or do { $log->error("cannot read %s: %s", $src, $!); return 0 } 2 0 2
701 0 $log->error( 'cannot read %s: %s', $src, $! );
702 0 return 0;
703 };
704 my @names =
705 2 ▶ sort grep { !m{\A[.]} && -d "$src/$_" } readdir $dh;
Condition66%2/3
!/\A[.]/u && -d "$src/$_"
!ll&&!rl&&r
403
MC/DC50%1/2
!/\A[.]/u && -d "$src/$_"
!/\A[.]/u -d "$src/$_"
Truth table66%2/3
not(A) && B
A= !/\A[.]/uB= -d "$src/$_"
ABresult
0X0
100
111
706 2 closedir $dh;
707  
708 2 for my $name (@names) {
709 3 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _clone_repo($app, "$src/$name", "$path/$name", $nested) 0 3 3
710 unless _clone_repo( $app, "$src/$name",
711 "$path/$name", $nested );
712 }
713  
714 2 return 1;
715 }
716  
717 3 ▶ return _copy_file( $app, $src, $path ) if -f $src;
Branch: 1 branch
BranchTrueFalseTotal
if -f $src 2 1 3
718  
719 1 ▶ unless ( -e $src ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $src) 1 0 1
720 1 $log->notice( 'missing in main checkout, skipped: %s', $path );
721 1 return 1;
722 }
723  
724 0 $log->error( 'not a repository, directory or file: %s', $src );
725  
726 0 return 0;
727 }
728  
729 # _clone_repo($app, $src, $dst, $nested):
730 # Make one local clone of a repository of the main checkout, and
731 # set its origin to the origin URL of the source (WT-CLONE-2). A
732 # destination that exists stays as it is (WT-CLONE-4).
733 #
734 # A project that no clone reaches gives an incomplete worktree,
735 # and no message names it later. So a failure stops the run.
736 4 ▶sub _clone_repo ( $app, $src, $dst, $nested )
Subroutine _clone_repo: called CC=11 SCAR=30.0
737 {
738 4 my $log = $app->cli->log;
739 4 ▶ if ( -e $dst ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dst) 1 3 4
740 1 $log->notice( 'already exists, skipped: %s', $dst );
741 1 return 1;
742 }
743  
744 3 ▶ unless ( -r $src && -x $src && -e "$src/.git" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $src and -x $src and -e "$src/.git") 0 3 3
Condition33%2/6
-r $src and -x $src and -e "$src/.git"
!ll&&!rl&&r
003
-r $src and -x $src
!ll&&!rl&&r
003
MC/DC0%0/3
-r $src and -x $src and -e "$src/.git"
-r $src -x $src -e "$src/.git"
Truth table25%1/4
A and B and C
A= -r $srcB= -x $srcC= -e "$src/.git"
ABCresult
0XX0
10X0
1100
1111
745 0 $log->error( 'unreadable or not a git repository: %s', $src );
746 0 return 0;
747 }
748  
749 3 my $parent = dirname($dst);
750 3 ▶ unless ( -d $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $parent) 2 1 3
751 2 File::Path::make_path($parent);
752 2 ▶ unless ( -d $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $parent) 0 2 2
753 0 $log->error( 'cannot make the directory %s', $parent );
754 0 return 0;
755 }
756 }
757  
758 3 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app->command(["git", "clone", "--quiet", $src, $dst])) 0 3 3
759 defined $app->command(
760 [ 'git', 'clone', '--quiet', $src, $dst ] ) )
761 {
762 0 $log->error( 'cannot clone %s: %s', $src, $app->error );
763 0 return 0;
764 }
765  
766 # A source with no origin gives undef here, and the clone keeps
767 # the source itself as its origin.
768 3 my $origin =
769 _capture( $app, 'git', '-C', $src, 'remote', 'get-url', 'origin' );
770 3 ▶ if ( defined $origin && length $origin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $origin and length $origin) 3 0 3
Condition33%1/3
defined $origin and length $origin
!ll&&!rl&&r
003
MC/DC0%0/2
defined $origin and length $origin
defined $origin length $origin
Truth table33%1/3
A and B
A= defined $originB= length $origin
ABresult
0X0
100
111
771 3 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app->command(["git", "-C", $dst, "remote", "set-url", "origin", $origin])) 0 3 3
772 defined $app->command( [
773 'git', '-C',
774 $dst, 'remote',
775 'set-url', 'origin',
776 $origin
777 ] ) )
778 {
779 0 $log->error( 'cannot set the origin of %s: %s',
780 $dst, $app->error );
781 0 return 0;
782 }
783 }
784  
785 3 return _copy_env_tree( $app, $src, $dst, $nested );
786 }
787  
788 # _copy_env_tree($app, $src, $dst, $nested):
789 # Copy each regular .env file of one source tree, at any depth,
790 # into the clone (WT-CLONE-3). The files are gitignored, so the
791 # clone above holds none of them. The walk skips .git and a
792 # nested worktree directory, and it copies no symbolic link: a
793 # .env link is content of the repository, and it stays there.
794 3 ▶sub _copy_env_tree ( $app, $src, $dst, $nested )
Subroutine _copy_env_tree: called CC=1 SCAR=0.0
795 {
796 3 my $ok = 1;
797 File::Find::find( {
798 no_chdir => 1,
799 15 ▶ wanted => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
800 15 my $name = $File::Find::name;
801 15 my $base = basename($name);
802 15 ▶ my $skip = $base eq '.git'
Condition100%6/6
$base eq '.git' || $name ne $src && $name =~ /$nested/u
l!l&&r!l&&!r
3111
$name ne $src and $name =~ /$nested/u
!ll&&!rl&&r
381
MC/DC100%3/3
$base eq '.git' || $name ne $src && $name =~ /$nested/u
$base eq '.git' $name ne $src $name =~ /$nested/u
Truth table100%4/4
A || B and C
A= $base eq '.git'B= $name ne $srcC= $name =~ /$nested/u
ABCresult
1XX1
0111
00X0
0100
803 || ( $name ne $src && $name =~ $nested );
804 15 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 4 11 15
805 4 $File::Find::prune = 1;
806 4 return;
807 }
808 11 ▶ return unless $base eq '.env';
Branch: 1 branch
BranchTrueFalseTotal
unless $base eq ".env" 8 3 11
809 3 ▶ return if -l $name || !-f $name;
Branch: 1 branch
BranchTrueFalseTotal
if -l $name or not -f $name 1 2 3
Condition66%2/3
-l $name or not -f $name
l!l&&r!l&&!r
102
MC/DC50%1/2
-l $name or not -f $name
-l $name not -f $name
Truth table66%2/3
A or not(B)
A= -l $nameB= not -f $name
ABresult
1X1
011
000
810  
811 # A gitignored parent directory is absent
812 # in the clone, so the copy makes it.
813 2 my $rel = substr $name, length($src) + 1;
814 2 my $parent = dirname("$dst/$rel");
815 2 ▶ File::Path::make_path($parent)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $parent 2 0 2
816 unless -d $parent;
817 2 ▶ $ok = 0
Branch: 1 branch
BranchTrueFalseTotal
unless _copy_file($app, $name, "$dst/$rel") 0 2 2
818 unless _copy_file( $app, $name,
819 "$dst/$rel" );
820  
821 2 return;
822 },
823 },
824 3 $src
825 );
826  
827 3 return $ok;
828 }
829  
830 # _copy_file($app, $src, $dst):
831 # Copy one file with the mode of the source. The method returns
832 # 0 after a failure.
833 4 ▶sub _copy_file ( $app, $src, $dst )
Subroutine _copy_file: called CC=7 SCAR=25.7
834 {
835 4 my $log = $app->cli->log;
836  
837 # A project can leave a symbolic link at the destination, and
838 # the copy must not write through it. A regular file that
839 # exists stays, so a second run keeps a local change
840 # (WT-CLONE-4).
841 4 ▶ unlink $dst if -l $dst;
Branch: 1 branch
BranchTrueFalseTotal
if -l $dst 1 3 4
842 4 ▶ if ( -e $dst ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dst) 0 4 4
843 0 $log->notice( 'already exists, skipped: %s', $dst );
844 0 return 1;
845 }
846  
847 # A .env file holds credentials, so the copy takes the mode of
848 # the source and not the default of the umask. The stat runs
849 # before the copy: a stat after it can follow a parallel
850 # removal of the source, and the chmod then gives the copy the
851 # mode 0.
852 4 my @stat = stat $src;
853 4 ▶ unless ( File::Copy::copy( $src, $dst ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Copy::copy($src, $dst)) 0 4 4
854 0 $log->error( 'cannot copy %s -> %s: %s', $src, $dst, $! );
855 0 return 0;
856 }
857 4 ▶ if ( @stat && chmod( $stat[2] & 07777, $dst ) != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat and chmod($stat[2] & 4095, $dst) != 1) 0 4 4
Condition33%1/3
@stat and chmod($stat[2] & 4095, $dst) != 1
!ll&&!rl&&r
040
MC/DC0%0/2
@stat and chmod($stat[2] & 4095, $dst) != 1
@stat chmod($stat[2] & 4095, $dst) != 1
Truth table33%1/3
A and B
A= @statB= chmod($stat[2] & 4095, $dst) != 1
ABresult
0X0
100
111
858 0 $log->error( 'cannot set the mode of %s: %s', $dst, $! );
859  
860 # A copy of a credential file at the mode of the umask
861 # is wider than the source, so it must not stay.
862 0 ▶ unlink $dst
Branch: 1 branch
BranchTrueFalseTotal
unlink $dst or $log->error("cannot remove %s: %s", $dst, $!) 0 0 0
863 or $log->error( 'cannot remove %s: %s', $dst, $! );
864  
865 0 return 0;
866 }
867 4 $log->notice( 'copied %s -> %s', $src, $dst );
868  
869 4 return 1;
870 }
871  
872 # _delete_branch($app, $root, $branch):
873 # Delete one branch, and report nothing about a branch that is
874 # gone. The method never deletes main, and never the branch that
875 # the main checkout has checked out (WT-REMOVE-5). A parallel
876 # remove that deletes the branch first is no error. The method
877 # returns 0 when the branch stays.
878 18 ▶sub _delete_branch ( $app, $root, $branch )
Subroutine _delete_branch: called CC=9 SCAR=25.7
879 {
880 18 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch and length $branch and $branch ne "main" 1 17 18
Condition50%3/6
defined $branch and length $branch and $branch ne "main"
!ll&&!rl&&r
0117
defined $branch and length $branch
!ll&&!rl&&r
0018
MC/DC33%1/3
defined $branch and length $branch and $branch ne "main"
defined $branch length $branch $branch ne "main"
Truth table50%2/4
A and B and C
A= defined $branchB= length $branchC= $branch ne "main"
ABCresult
0XX0
10X0
1100
1111
881 unless defined $branch && length $branch && $branch ne 'main';
882  
883 17 my $head = _capture( $app, 'git', '-C', $root, 'symbolic-ref',
884 '--quiet', '--short', 'HEAD' );
885 17 ▶ return 1 if defined $head && $branch eq $head;
Branch: 1 branch
BranchTrueFalseTotal
if defined $head and $branch eq $head 1 16 17
Condition66%2/3
defined $head and $branch eq $head
!ll&&!rl&&r
0161
MC/DC50%1/2
defined $head and $branch eq $head
defined $head $branch eq $head
Truth table66%2/3
A and B
A= defined $headB= $branch eq $head
ABresult
0X0
100
111
886 16 ▶ return 1 unless _branch_exists( $app, $root, $branch );
Branch: 1 branch
BranchTrueFalseTotal
unless _branch_exists($app, $root, $branch) 3 13 16
887  
888 13 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $app->command(["git", "-C", $root, "branch", "-D", $branch]) 13 0 13
889 if defined $app->command(
890 [ 'git', '-C', $root, 'branch', '-D', $branch ] );
891 0 ▶ return 1 unless _branch_exists( $app, $root, $branch );
Branch: 1 branch
BranchTrueFalseTotal
unless _branch_exists($app, $root, $branch) 0 0 0
892  
893 0 $app->cli->log->error( 'cannot delete the branch %s', $branch );
894  
895 0 return 0;
896 }
897  
898 # _branch_exists($app, $repo, $branch):
899 # True when the repository holds the branch.
900 33 ▶sub _branch_exists ( $app, $repo, $branch )
Subroutine _branch_exists: called CC=1 SCAR=0.0
901 {
902 33 return defined $app->command( [
903 'git', '-C',
904 $repo, 'show-ref',
905 '--verify', '--quiet',
906 "refs/heads/$branch"
907 ] );
908 }
909  
910 # _prune_parents($base, $wt):
911 # Remove each empty parent directory of one worktree, up to the
912 # base (WT-REMOVE-7). A name with a slash sits in a nested
913 # directory, and the first parent that holds a file stops the
914 # walk.
915 18 ▶sub _prune_parents ( $base, $wt )
Subroutine _prune_parents: called CC=4 SCAR=13.9
916 {
917 18 my $dir = dirname($wt);
918 18 ▶ while ( length($dir) > length($base)
Branch: 1 branch
BranchTrueFalseTotal
if (length $dir > length $base and rindex($dir, "$base/", 0) == 0) 4 17 21
Condition66%2/3
length $dir > length $base and rindex($dir, "$base/", 0) == 0
!ll&&!rl&&r
1704
MC/DC50%1/2
length $dir > length $base and rindex($dir, "$base/", 0) == 0
length $dir > length $base rindex($dir, "$base/", 0) == 0
Truth table66%2/3
A and B
A= length $dir > length $baseB= rindex($dir, "$base/", 0) == 0
ABresult
0X0
100
111
919 && rindex( $dir, "$base/", 0 ) == 0 )
920 {
921 4 ▶ rmdir $dir or last;
Branch: 1 branch
BranchTrueFalseTotal
rmdir $dir or last 3 1 4
922 3 $dir = dirname($dir);
923 }
924  
925 18 return;
926 }
927  
928 # _capture($app, @cmd):
929 # The standard output of one child, without the last newline,
930 # or undef when the child fails.
931 83 ▶sub _capture ( $app, @cmd )
Subroutine _capture: called CC=2 SCAR=7.0
932 {
933 83 my $out = $app->command( \@cmd );
934 83 ▶ return unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 83 83
935 83 chomp $out;
936  
937 83 return $out;
938 }
939  
940 117 1;
« lib/App/FuguBench/Wiki.pm ↑ Summary lib/App/FuguBench/Fetch.pm »