lib/App/FuguBench/Wiki.pm

filter: Statementstmt 73.7% 253 / 343 Branchbran 49.0% 98 / 200 Conditioncond 41.5% 22 / 53 MC/DCmcdc 14.7% 5 / 34 Subroutinesub 88.0% 44 / 50 Podpod 50.0% 2 / 4 totaltotal 61.9% 424 / 684 CC 129 SCAR 24.5
CC
129
Cov
63%
_init
42.0
_move
42.0
_rebasing
20.0
CRAP 11.6
« lib/App/FuguBench/Traces.pm ↑ Summary lib/App/FuguBench/Worktree.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::Wiki;
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 File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 117 ▶use Fugu::CLI qw(EXIT_SUCCESS EXIT_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 117 ▶use Fugu::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # App::FuguBench::Wiki - the wiki verb.
33 #
34 # The verb operates the learning library: a git repository of flat
35 # pages, cloned below the home of wiki.origin. The subcommands are
36 # init, open, note, admit, close, status, and candidates. An unknown
37 # subcommand gives the usage error.
38 #
39 # The library is <home of wiki.origin>/<wiki.dir>. A clone under
40 # Projects/ holds a .toolingrc of its own without wiki.origin, so the
41 # walk for the key reaches the workspace, and the library of the
42 # workspace serves the clone (CLI-CONFIG-2).
43 #
44 # Every capture commits, and then pushes. The commit carries the
45 # durability, and the push carries the visibility. A failed push
46 # warns, and the commit stays for the next push.
47 #
48 # The race sits in open. Two sessions of one day take one page name
49 # when each one counts the pages of a stale clone. So open fetches
50 # the origin before it reads the pages of the clone, and it renames
51 # its page when the origin holds the name already (D-08).
52 #
53 # Git runs as a child with the clone as its working directory, and
54 # every message of git goes to standard error. Standard output
55 # carries the result line of the subcommand alone, because a hook
56 # reads it.
57  
58 # The subcommands of the verb. An unknown word gives the usage error.
59 # note and admit share one body: they differ in the commit subject
60 # only, so the entry carries the subject word.
61 my %SUBCOMMAND = (
62 init => \&_init,
63 open => \&_open,
64 7 ▶ note => sub ( $app, @args ) { return _append( $app, 'note', @args ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 0 ▶ admit => sub ( $app, @args ) { return _append( $app, 'admit', @args ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 117 close => \&_close,
67 status => \&_status,
68 candidates => \&_candidates,
69 );
70  
71 # The shape of a page name (WIKI-PAGES-1, WIKI-CONFINE-1). Pages stay
72 # flat, so the name holds no slash. The leading letter refuses a name
73 # that starts with a dot, and the test of the caller refuses a parent
74 # segment.
75 117 my $PAGE = qr{\A[A-Za-z][A-Za-z0-9._-]*\z};
76  
77 # The shape of a project token and a session token (WIKI-OPEN-5). The
78 # first character is a letter or a digit: a token that starts with a
79 # dash reaches git as an option.
80 117 my $TOKEN = qr{\A[A-Za-z0-9][A-Za-z0-9._-]*\z};
81  
82 # The shape of the name of a session page (WIKI-PAGES-3). _page
83 # builds one name of that shape from a project token, a date, and an
84 # index, and this pattern reads one back.
85 117 my $SESSION =
86 qr{\ASession-[A-Za-z0-9][A-Za-z0-9._-]*-[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+[.]md\z};
87  
88 # The prefix that the prose lint skips. A page with it would never
89 # meet the prose gate (WIKI-PAGES-2).
90 117 ▶use constant SCRATCHPAD => 'SCRATCHPAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 # The tries of one push (WIKI-CAPTURE-5).
93 117 ▶use constant TRIES => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 # The page that holds the rule candidates (WIKI-STATUS-2).
96 117 ▶use constant CANDIDATES => 'Rule-candidates.md';
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 # The seconds of one day, for the age of one candidate.
99 117 ▶use constant DAY => 86_400;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 # App::FuguBench::Wiki->command($verb):
102 # The entry of the Fugu::CLI table. The module holds one verb,
103 # so it ignores the name.
104 117 ▶sub command ( $, $ )
Subroutine command: called CC=1 SCAR=0.0
Pod undocumented
105 {
106 return {
107 summary => 'operate the learning library',
108 usage => 'init | open <project> <session>'
109 . ' | note <page> <file> | admit <page> <file>'
110 . ' | close <session> | status | candidates',
111 21 ▶ run => sub ( $app, @argv ) { return run( $app, @argv ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 117 };
113 }
114  
115 # run($app, $sub, @args):
116 # Dispatch one subcommand, and return its exit code. The hook
117 # verb calls it in process, so the dispatch takes the name as an
118 # argument.
119 21 ▶sub run ( $app, $sub = undef, @args )
Subroutine run: called CC=4 SCAR=14.5
Pod undocumented
120 {
121 21 ▶ my $body = defined $sub ? $SUBCOMMAND{$sub} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 21 0 21
122 21 ▶ return $app->cli->command_usage_error('wiki') unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 21 21
123  
124 21 return $body->( $app, @args );
125 }
126  
127 # _library($app):
128 # The exit code, the library directory, and the origin URL, in
129 # that order. The code is EXIT_SUCCESS when the checkout answers,
130 # and the method reports every failure itself.
131 #
132 # The directory is undef when no .toolingrc of the walk holds
133 # wiki.origin. That key has no default, so it has no home, and
134 # the library has no path. init stops with a configuration error,
135 # and every other subcommand reports an absent clone.
136 #
137 # The home of wiki.origin anchors the directory (CLI-CONFIG-2). A
138 # clone under Projects/ is a checkout of its own, and its root
139 # holds no library.
140 #
141 # The directory must sit below that home, so a wiki.dir of . is
142 # a configuration error (CLI-CONFIG-3).
143 21 ▶sub _library ($app)
Subroutine _library: called CC=6 SCAR=21.3
144 {
145 21 ▶ my $checkout = $app->checkout
Branch: 1 branch
BranchTrueFalseTotal
my $checkout = $app->checkout or return 3 21 0 21
146 or return Fugu::CLI::EXIT_CONFIG_ERROR();
147  
148 21 my ( $origin, $home ) = $checkout->config('wiki.origin');
149 21 ▶ return EXIT_SUCCESS unless defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 0 21 21
150  
151 21 my $log = $app->cli->log;
152 21 ▶ unless ( defined $checkout->url_value($origin) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $checkout->url_value($origin)) 0 21 21
153 0 $log->error( 'wiki.origin: %s', $checkout->error );
154 0 return Fugu::CLI::EXIT_CONFIG_ERROR();
155 }
156  
157 21 my ($value) = $checkout->config('wiki.dir');
158 21 my $name = $checkout->dir_value($value);
159 21 ▶ unless ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 21 21
160 0 $log->error( 'wiki.dir: %s', $checkout->error );
161 0 return Fugu::CLI::EXIT_CONFIG_ERROR();
162 }
163  
164 # A value of . resolves to the home itself, and that home is a
165 # checkout. A checkout holds a .git, so _have would take it for
166 # the library: open would write a session page into the
167 # checkout, and the push would carry it to the origin of the
168 # checkout. The method refuses that value (CLI-CONFIG-3).
169 21 my $dir = File::Spec->catdir( $home, $name );
170 21 ▶ if ( $dir eq $home ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq $home) 0 21 21
171 0 $log->error( 'wiki.dir: the directory is the checkout: %s',
172 $name );
173 0 return Fugu::CLI::EXIT_CONFIG_ERROR();
174 }
175  
176 21 return ( EXIT_SUCCESS, $dir, $origin );
177 }
178  
179 # _have($app, $dir):
180 # True when the library answers a subcommand that needs it
181 # (WIKI-CLONE-3). An absent key and an absent clone both report
182 # on standard error and give false. The subcommand then exits
183 # zero with no result line, so no hook stops a session.
184 21 ▶sub _have ( $app, $dir )
Subroutine _have: called CC=4 SCAR=14.8
185 {
186 21 my $log = $app->cli->log;
187 21 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 21 21
188 0 $log->notice('no library, wiki.origin is unset');
189 0 return 0;
190 }
191 21 ▶ return 1 if -d $dir && -e "$dir/.git";
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir and -e "$dir/.git" 20 1 21
Condition66%2/3
-d $dir and -e "$dir/.git"
!ll&&!rl&&r
1020
MC/DC50%1/2
-d $dir and -e "$dir/.git"
-d $dir -e "$dir/.git"
Truth table66%2/3
A and B
A= -d $dirB= -e "$dir/.git"
ABresult
0X0
100
111
192  
193 1 $log->notice( 'no library at %s', $dir );
194  
195 1 return 0;
196 }
197  
198 # _init($app, @argv):
199 # Clone the library when it is absent, and write its directory
200 # to standard output (WIKI-CLONE-1). A second run reports the
201 # directory on standard error and changes nothing.
202 #
203 # init is the one subcommand that needs wiki.origin, so an absent
204 # key stops it with a configuration error that names the key
205 # (CLI-CONFIG-2).
206 #
207 # A failed clone warns and exits zero (WIKI-CLONE-2). The
208 # repository can be absent, and a checkout without network access
209 # is normal, so the session that follows must still start.
210 0 ▶sub _init ( $app, @argv )
Subroutine _init: not called CC=6 SCAR=37.4
211 {
212 0 ▶ return $app->cli->command_usage_error('wiki') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 0 0
213  
214 0 my ( $code, $dir, $origin ) = _library($app);
215 0 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 0 0
216  
217 0 my $log = $app->cli->log;
218 0 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
219 0 $log->error('wiki.origin is unset, and init needs the URL');
220 0 return Fugu::CLI::EXIT_CONFIG_ERROR();
221 }
222 0 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
223 0 $log->notice( 'already exists, nothing to do: %s', $dir );
224 0 return EXIT_SUCCESS;
225 }
226  
227 # git removes the directory that a failed clone made, so a
228 # warning is the whole answer here.
229 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app->command(["git", "clone", "--quiet", $origin, $dir])) 0 0 0
230 defined $app->command(
231 [ 'git', 'clone', '--quiet', $origin, $dir ] ) )
232 {
233 0 $log->warning( 'cannot clone %s, the library stays absent: %s',
234 $origin, $app->error );
235 0 return EXIT_SUCCESS;
236 }
237  
238 0 say $dir;
239  
240 0 return EXIT_SUCCESS;
241 }
242  
243 # _open($app, @argv):
244 # Start the session page, commit it, push it, and write the page
245 # name to standard output as the only line (WIKI-OPEN-4).
246 #
247 # A page that holds the session identifier already ends the
248 # subcommand: it writes that page and changes nothing
249 # (WIKI-OPEN-1). The identifier lives in the page and never in
250 # the name, so open stays idempotent across a resume and a
251 # compact (WIKI-PAGES-4).
252 #
253 # The fetch comes before that search and before the count
254 # (WIKI-OPEN-2). The search reads the working tree, and then the
255 # fetched branch. A clone that holds a commit of its own takes no
256 # fast-forward, so its working tree can hide the page of the
257 # session, and open would then give that session a second page.
258 # A page of the fetched branch alone reaches the working tree,
259 # because note and close read that tree. The rename settles a
260 # name that the origin took first (WIKI-OPEN-3).
261 6 ▶sub _open ( $app, @argv )
Subroutine _open: called CC=12 SCAR=29.1
262 {
263 6 ▶ return $app->cli->command_usage_error('wiki') if @argv != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 2 0 6 6
264 6 my ( $project, $session ) = @argv;
265  
266 6 ▶ return $app->cli->command_usage_error('wiki')
Branch: 1 branch
BranchTrueFalseTotal
unless _token($app, "project", $project) and _token($app, "session", $session) 0 6 6
Condition33%1/3
_token($app, "project", $project) and _token($app, "session", $session)
!ll&&!rl&&r
006
MC/DC0%0/2
_token($app, "project", $project) and _token($app, "session", $session)
_token($app, "project", $project) _token($app, "session", $session)
Truth table33%1/3
A and B
A= _token($app, "project", $project)B= _token($app, "session", $session)
ABresult
0X0
100
111
267 unless _token( $app, 'project', $project )
268 && _token( $app, 'session', $session );
269  
270 6 my ( $code, $dir ) = _library($app);
271 6 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 6 6
272 6 ▶ return EXIT_SUCCESS unless _have( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _have($app, $dir) 0 6 6
273  
274 6 my $branch = _branch( $app, $dir );
275 6 ▶ _fetch( $app, $dir, $branch ) if defined $branch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $branch 6 0 6
276  
277 6 my $log = $app->cli->log;
278 6 ▶ my $found = _page_of_session( $dir, $session )
Condition66%2/3
_page_of_session($dir, $session) // _resume($app, $dir, $branch, $session)
l!l&&r!l&&!r
105
MC/DC50%1/2
_page_of_session($dir, $session) // _resume($app, $dir, $branch, $session)
_page_of_session($dir, $session) _resume($app, $dir, $branch, $session)
Truth table66%2/3
A // B
A= _page_of_session($dir, $session)B= _resume($app, $dir, $branch, $session)
ABresult
1X1
011
000
279 // _resume( $app, $dir, $branch, $session );
280 6 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 1 5 6
281 1 $log->notice( 'already open: %s', $found );
282 1 say $found;
283 1 return EXIT_SUCCESS;
284 }
285  
286 5 my $date = strftime( '%Y-%m-%d', gmtime );
287 5 my %taken = map { $_ => 1 } _origin_pages( $app, $dir, $branch );
288 5 my $n = _free( $dir, \%taken, $project, $date );
289 5 my $page = _page( $project, $date, $n );
290  
291 # The tokens pass their own check, so the name above is flat by
292 # construction. The check runs on it because the name reaches
293 # the filesystem and git (CLI-PROGRAM-6).
294 5 ▶ my $path = _page_path( $app, $dir, $page )
Branch: 1 branch
BranchTrueFalseTotal
my $path = _page_path($app, $dir, $page) or return $app->cli->command_usage_error("wiki") 5 0 5
295 or return $app->cli->command_usage_error('wiki');
296  
297 5 my $title = _title( $project, $date, $n );
298 5 my $now = strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime );
299 5 my $text = <<"PAGE";
300 $title
301  
302 Session: $session
303 Project: $project
304 Opened: $now
305  
306 ## Observations
307 PAGE
308  
309 5 ▶ unless ( Fugu::File->write( $path, $text ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Fugu::File"->write($path, $text)) 0 5 5
310 0 $log->error( 'cannot write %s', $path );
311 0 return EXIT_ERROR;
312 }
313  
314 # The rename of WIKI-OPEN-3. A rejected push fetches the
315 # branch, and the fetched branch can hold the page of the
316 # commit. A rebase of that commit stops on an add/add conflict,
317 # so the page takes the next free index first, and the commit
318 # carries the new name and the new title.
319 #
320 # The closure answers 1 after a rename, 0 when the branch holds
321 # no page of that name, and undef after a failure. The new name
322 # reaches the result line, and a failed rename stops the
323 # subcommand.
324 0 ▶ my $rename = sub ($pages) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
325 0 ▶ return 0 unless $pages->{$page};
Branch: 1 branch
BranchTrueFalseTotal
unless $pages->{$page} 0 0 0
326  
327 0 my $index = _free( $dir, $pages, $project, $date );
328 0 my $name = _move(
329 $app, $dir, $page,
330 _page( $project, $date, $index ),
331 _title( $project, $date, $index ) );
332 0 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
333 0 $page = $name;
334  
335 0 return 1;
336 5 };
337  
338 5 $code = _save( $app, $dir, $page, "open: $page", $rename );
339 5 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 5 5
340  
341 5 say $page;
342  
343 5 return EXIT_SUCCESS;
344 }
345  
346 # _append($app, $subject, @argv):
347 # Append the text of one file to one page, commit it, push it,
348 # and write the page name to standard output (WIKI-CAPTURE-1).
349 # note and admit share this body. They differ in the word of the
350 # commit subject only, and the caller gives that word.
351 #
352 # An empty file is an error, because a capture with no text
353 # carries nothing. An absent page and an absent file are errors
354 # of the same kind: the caller names a thing that is not there.
355 7 ▶sub _append ( $app, $subject, @argv )
Subroutine _append: called CC=12 SCAR=29.3
356 {
357 7 ▶ return $app->cli->command_usage_error('wiki') if @argv != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 2 0 7 7
358 7 my ( $page, $file ) = @argv;
359  
360 7 my ( $code, $dir ) = _library($app);
361 7 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 7 7
362 7 ▶ return EXIT_SUCCESS unless _have( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _have($app, $dir) 0 7 7
363  
364 # The name reaches the filesystem and git, so it passes the
365 # shape check before the path forms (CLI-PROGRAM-6).
366 7 ▶ my $path = _page_path( $app, $dir, $page )
Branch: 1 branch
BranchTrueFalseTotal
my $path = _page_path($app, $dir, $page) or return $app->cli->command_usage_error("wiki") 3 4 7
367 or return $app->cli->command_usage_error('wiki');
368  
369 3 my $log = $app->cli->log;
370 3 ▶ unless ( -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $path) 0 3 3
371 0 $log->error( 'no such page: %s', $page );
372 0 return EXIT_ERROR;
373 }
374 3 ▶ unless ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 3 3
375 0 $log->error( 'no such file: %s', $file );
376 0 return EXIT_ERROR;
377 }
378  
379 3 my $text = Fugu::File->read($file);
380 3 ▶ unless ( defined $text && $text =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text and $text =~ /\S/u) 0 3 3
Condition33%1/3
defined $text and $text =~ /\S/u
!ll&&!rl&&r
003
MC/DC0%0/2
defined $text and $text =~ /\S/u
defined $text $text =~ /\S/u
Truth table33%1/3
A and B
A= defined $textB= $text =~ /\S/u
ABresult
0X0
100
111
381 0 $log->error( 'no text in %s', $file );
382 0 return EXIT_ERROR;
383 }
384 3 ▶ $text .= "\n" unless $text =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n\z/u 0 3 3
385  
386 3 ▶ return EXIT_ERROR unless _add( $app, $path, $text );
Branch: 1 branch
BranchTrueFalseTotal
unless _add($app, $path, $text) 0 3 3
387  
388 # The caller can leave the .md suffix out, and git needs the
389 # name of the file. The path carries that name.
390 3 my $name = ( File::Spec->splitpath($path) )[2];
391  
392 3 $code = _save( $app, $dir, $name, "$subject: $name" );
393 3 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 3 3
394  
395 3 say $name;
396  
397 3 return EXIT_SUCCESS;
398 }
399  
400 # _close($app, @argv):
401 # Append the Closed: line to the page of one session, commit it,
402 # push it, and write the page name to standard output
403 # (WIKI-CAPTURE-2, WIKI-PAGES-3).
404 #
405 # close carries no durability of its own: every observation
406 # reached a commit through note. So a page that holds the line
407 # already, and a session that has no page, both report on
408 # standard error and write no result line. A session that runs no
409 # campaign opens no page, and that is normal.
410 3 ▶sub _close ( $app, @argv )
Subroutine _close: called CC=10 SCAR=23.6
411 {
412 3 ▶ return $app->cli->command_usage_error('wiki') if @argv != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 1 0 3 3
413 3 my ($session) = @argv;
414  
415 3 ▶ return $app->cli->command_usage_error('wiki')
Branch: 1 branch
BranchTrueFalseTotal
unless _token($app, "session", $session) 0 3 3
416 unless _token( $app, 'session', $session );
417  
418 3 my ( $code, $dir ) = _library($app);
419 3 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 3 3
420 3 ▶ return EXIT_SUCCESS unless _have( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _have($app, $dir) 0 3 3
421  
422 3 my $log = $app->cli->log;
423 3 my $page = _page_of_session( $dir, $session );
424 3 ▶ unless ($page) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page) 1 2 3
425 1 $log->notice('no page for this session, nothing to do');
426 1 return EXIT_SUCCESS;
427 }
428  
429 2 my $path = "$dir/$page";
430 2 ▶ if ( ( Fugu::File->read($path) // q{} ) =~ /^Closed:/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if (("Fugu::File"->read($path) // "") =~ /^Closed:/mu) 1 1 2
Condition50%1/2
"Fugu::File"->read($path) // ""
l!l
20
MC/DC0%0/1
"Fugu::File"->read($path) // ""
"Fugu::File"->read($path)
Truth table50%1/2
A
A= "Fugu::File"->read($path)
Aresult
11
00
431 1 $log->notice( 'already closed: %s', $page );
432 1 return EXIT_SUCCESS;
433 }
434  
435 1 my $now = strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime );
436 1 ▶ return EXIT_ERROR unless _add( $app, $path, "Closed: $now\n" );
Branch: 1 branch
BranchTrueFalseTotal
unless _add($app, $path, "Closed: $now\n") 0 1 1
437  
438 1 $code = _save( $app, $dir, $page, "close: $page" );
439 1 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 1 1
440  
441 1 say $page;
442  
443 1 return EXIT_SUCCESS;
444 }
445  
446 # _status($app, @argv):
447 # Report each open session with its page, its Claim: count, and
448 # its Admitted: count, and then the count of unpushed commits
449 # (WIKI-STATUS-1).
450 #
451 # A page with no text under ## Observations is no open session.
452 # A hook opens a page for every session, and most sessions
453 # capture nothing.
454 #
455 # The consolidator writes one Admitted: line for each claim that
456 # it moves into a library page. The difference is the work that
457 # the library still waits for.
458 3 ▶sub _status ( $app, @argv )
Subroutine _status: called CC=13 SCAR=26.5
459 {
460 3 ▶ return $app->cli->command_usage_error('wiki') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 3 3
461  
462 3 my ( $code, $dir ) = _library($app);
463 3 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 3 3
464 3 ▶ return EXIT_SUCCESS unless _have( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _have($app, $dir) 0 3 3
465  
466 3 my @open;
467 3 for my $page ( _session_pages($dir) ) {
468 3 ▶ my $text = Fugu::File->read("$dir/$page") // q{};
Condition50%1/2
"Fugu::File"->read("$dir/$page") // ""
l!l
30
MC/DC0%0/1
"Fugu::File"->read("$dir/$page") // ""
"Fugu::File"->read("$dir/$page")
Truth table50%1/2
A
A= "Fugu::File"->read("$dir/$page")
Aresult
11
00
469 3 ▶ next if $text =~ /^Closed:/m;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^Closed:/mu 1 2 3
470  
471 2 my $body = __PACKAGE__->observations($text);
472 2 ▶ next unless defined $body && $body =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and $body =~ /\S/u 1 1 2
Condition66%2/3
defined $body and $body =~ /\S/u
!ll&&!rl&&r
011
MC/DC50%1/2
defined $body and $body =~ /\S/u
defined $body $body =~ /\S/u
Truth table66%2/3
A and B
A= defined $bodyB= $body =~ /\S/u
ABresult
0X0
100
111
473  
474 1 my $claims = () = $body =~ /^Claim:/mg;
475 1 my $admitted = () = $body =~ /^Admitted:/mg;
476 1 push @open,
477 sprintf( '%-44s %d claim(s), %d admitted',
478 $page, $claims, $admitted );
479 }
480  
481 3 ▶ if (@open) {
Branch: 1 branch
BranchTrueFalseTotal
if (@open) { } 1 2 3
482 1 say 'open sessions:';
483 1 say " $_" for @open;
484 }
485 else {
486 2 say 'open sessions: none';
487 }
488  
489 # A count that git does not give is unknown, and the report
490 # still ends with its last line.
491 3 my $unpushed = _capture( $app, $dir, 'rev-list', '--count', 'HEAD',
492 '--not', '--remotes' );
493 3 ▶ $unpushed = 'unknown'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unpushed and $unpushed =~ /\A[0-9]+\z/u 0 3 3
Condition33%1/3
defined $unpushed and $unpushed =~ /\A[0-9]+\z/u
!ll&&!rl&&r
003
MC/DC0%0/2
defined $unpushed and $unpushed =~ /\A[0-9]+\z/u
defined $unpushed $unpushed =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $unpushedB= $unpushed =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
494 unless defined $unpushed && $unpushed =~ /\A[0-9]+\z/;
495 3 say "unpushed commits: $unpushed";
496  
497 3 return EXIT_SUCCESS;
498 }
499  
500 # _candidates($app, @argv):
501 # Report each undelivered rule candidate with its age in days
502 # and its date (WIKI-STATUS-2).
503 #
504 # The subcommand runs inside make check, so an absent clone and
505 # an absent page both report on standard error and exit zero
506 # (WIKI-STATUS-3). A checkout without a library then passes the
507 # gate.
508 2 ▶sub _candidates ( $app, @argv )
Subroutine _candidates: called CC=9 SCAR=22.9
509 {
510 2 ▶ return $app->cli->command_usage_error('wiki') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 2 2
511  
512 2 my ( $code, $dir ) = _library($app);
513 2 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 2 2
514 2 ▶ return EXIT_SUCCESS unless _have( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _have($app, $dir) 1 1 2
515  
516 1 my $log = $app->cli->log;
517 1 my $path = "$dir/" . CANDIDATES;
518 1 ▶ unless ( -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $path) 0 1 1
519 0 $log->notice( 'no %s, nothing to report', CANDIDATES );
520 0 return EXIT_SUCCESS;
521 }
522  
523 1 my $now = time;
524 1 my $found = 0;
525 1 ▶ for my $item ( _items( Fugu::File->read($path) // q{} ) ) {
Condition50%1/2
"Fugu::File"->read($path) // ""
l!l
10
MC/DC0%0/1
"Fugu::File"->read($path) // ""
"Fugu::File"->read($path)
Truth table50%1/2
A
A= "Fugu::File"->read($path)
Aresult
11
00
526 3 ▶ next if index( $item->{text}, 'Delivered:' ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($item->{"text"}, "Delivered:") != -1 2 1 3
527  
528 1 my $age = int( ( $now - _epoch( $item->{date} ) ) / DAY );
529 1 printf "%4d d %s %s\n", $age, $item->{date}, $item->{text};
530 1 $found++;
531 }
532 1 ▶ say 'no undelivered candidate' unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 1 1
533  
534 1 return EXIT_SUCCESS;
535 }
536  
537 # _items($text):
538 # Each candidate of the page, as a hash with a date and a text
539 # (WIKI-STATUS-2). A candidate is a list item that starts with a
540 # date, and the item takes its continuation lines.
541 #
542 # The prose gate reflows the page, so Delivered: often sits on a
543 # continuation line. The join brings the whole item into one
544 # string, or the report holds a delivered candidate forever.
545 1 ▶sub _items ($text)
Subroutine _items: called CC=5 SCAR=16.1
546 {
547 1 my @items;
548 1 for my $line ( split /\n/, $text ) {
549 6 ▶ if ( $line =~ /\A-\s+([0-9]{4}-[0-9]{2}-[0-9]{2})\s+(.*)\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\A-\s+([0-9]{4}-[0-9]{2}-[0-9]{2})\s+(.*)\z/u) { } 3 3 6
elsif (@items and $line =~ /\A\s+(\S.*)\z/u) { } 1 4 5
550 3 push @items, { date => $1, text => $2 };
551 }
552 ▶ elsif ( @items && $line =~ /\A\s+(\S.*)\z/ ) {
Condition66%2/3
@items and $line =~ /\A\s+(\S.*)\z/u
!ll&&!rl&&r
201
MC/DC50%1/2
@items and $line =~ /\A\s+(\S.*)\z/u
@items $line =~ /\A\s+(\S.*)\z/u
Truth table66%2/3
A and B
A= @itemsB= $line =~ /\A\s+(\S.*)\z/u
ABresult
0X0
100
111
553 1 $items[-1]{text} .= " $1";
554 }
555 }
556  
557 1 return @items;
558 }
559  
560 # _epoch($date):
561 # The epoch second of one UTC date, by the civil algorithm of
562 # Howard Hinnant. It needs no module and no local time zone, so
563 # the age of a candidate is the same in every zone.
564 1 ▶sub _epoch ($date)
Subroutine _epoch: called CC=4 SCAR=14.3
565 {
566 1 my ( $y, $m, $d ) = split /-/, $date;
567  
568 1 ▶ my $year = $y - ( $m <= 2 ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$m <= 2 ? : 0 1 1
569 1 ▶ my $era = int( ( $year >= 0 ? $year : $year - 399 ) / 400 );
Branch: 1 branch
BranchTrueFalseTotal
$year >= 0 ? : 1 0 1
570 1 my $yoe = $year - $era * 400;
571 1 ▶ my $doy =
Branch: 1 branch
BranchTrueFalseTotal
$m > 2 ? : 1 0 1
572 int( ( 153 * ( $m + ( $m > 2 ? -3 : 9 ) ) + 2 ) / 5 ) + $d - 1;
573 1 my $doe = $yoe * 365 + int( $yoe / 4 ) - int( $yoe / 100 ) + $doy;
574  
575 1 return ( $era * 146_097 + $doe - 719_468 ) * DAY;
576 }
577  
578 # _token($app, $what, $value):
579 # True when one token holds the shape of WIKI-OPEN-5. A token
580 # that fails gives a message that names it, and the caller then
581 # returns the usage error.
582 15 ▶sub _token ( $app, $what, $value )
Subroutine _token: called CC=2 SCAR=10.5
583 {
584 15 ▶ return 1 if $value =~ $TOKEN;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$TOKEN/u 15 0 15
585  
586 0 $app->cli->log->error( 'invalid %s: %s', $what, $value );
587  
588 0 return 0;
589 }
590  
591 # _page_path($app, $dir, $page):
592 # The path of one page inside the clone, or undef with a message
593 # (WIKI-CONFINE-1). The name takes the .md suffix or leaves it
594 # out (WIKI-PAGES-1).
595 12 ▶sub _page_path ( $app, $dir, $page )
Subroutine _page_path: called CC=4 SCAR=13.9
596 {
597 12 my $log = $app->cli->log;
598 12 my $name = $page =~ s/[.]md\z//r;
599 12 ▶ unless ( $name =~ $PAGE && $name !~ m{[.][.]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$PAGE/u and not $name =~ /[.][.]/u) 3 9 12
Condition66%2/3
$name =~ /$PAGE/u and not $name =~ /[.][.]/u
!ll&&!rl&&r
309
MC/DC50%1/2
$name =~ /$PAGE/u and not $name =~ /[.][.]/u
$name =~ /$PAGE/u not $name =~ /[.][.]/u
Truth table66%2/3
A and not(B)
A= $name =~ /$PAGE/uB= not $name =~ /[.][.]/u
ABresult
0X0
100
111
600 3 $log->error( 'invalid page name: %s', $page );
601 3 return;
602 }
603 9 ▶ if ( rindex( $name, SCRATCHPAD, 0 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($name, "SCRATCHPAD", 0) == 0) 1 8 9
604 1 $log->error( 'a page name must not start with %s: %s',
605 SCRATCHPAD, $page );
606 1 return;
607 }
608  
609 8 return "$dir/$name.md";
610 }
611  
612 # App::FuguBench::Wiki->session_page($name):
613 # True when one name is the name of a session page
614 # (WIKI-PAGES-3). _page builds that name, and the doctor reads
615 # the shape here, so the two verbs never disagree.
616 0 ▶sub session_page ( $, $name )
Subroutine session_page: not called CC=2 SCAR=17.9
Pod documented
617 {
618 0 ▶ return $name =~ $SESSION ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /$SESSION/u ? : 0 0 0
619 }
620  
621 # App::FuguBench::Wiki->observations($text):
622 # The text under the ## Observations heading of one page, or
623 # undef when the page holds no such heading (WIKI-PAGES-3).
624 #
625 # The Closed: line is no observation. close appends that line at
626 # the end of the page, and the heading is the last one of the
627 # template, so the line lands under it (WIKI-CAPTURE-2).
628 #
629 # The status subcommand and the doctor verb read the body of a
630 # page here, so the two never disagree.
631 2 ▶sub observations ( $, $text )
Subroutine observations: called CC=2 SCAR=7.1
Pod documented
632 {
633 2 my ($body) = $text =~ /^[#][#] Observations$(.*)\z/ms;
634 2 ▶ return unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 2 2
635  
636 2 return $body =~ s/^Closed:[^\n]*\n?//mgr;
637 }
638  
639 # _page($project, $date, $n):
640 # The name of one session page (WIKI-PAGES-3).
641 10 ▶sub _page ( $project, $date, $n )
Subroutine _page: called CC=1 SCAR=0.0
642 {
643 10 return "Session-$project-$date-$n.md";
644 }
645  
646 # _title($project, $date, $n):
647 # The title line of one session page (WIKI-PAGES-3). The name
648 # and the title carry one index, so the rename writes this line
649 # again.
650 5 ▶sub _title ( $project, $date, $n )
Subroutine _title: called CC=1 SCAR=0.0
651 {
652 5 return "# Session $project $date $n";
653 }
654  
655 # _free($dir, $taken, $project, $date):
656 # The first free index of the day. The count takes the pages of
657 # the working tree and the pages of the fetched branch together
658 # (WIKI-OPEN-2). A stale clone alone gave two sessions one name.
659 5 ▶sub _free ( $dir, $taken, $project, $date )
Subroutine _free: called CC=3 SCAR=12.3
660 {
661 5 my $n = 1;
662 5 while (1) {
663 5 my $name = _page( $project, $date, $n );
664 5 ▶ last unless -e "$dir/$name" || $taken->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$dir/$name" or $taken->{$name} 5 0 5
Condition33%1/3
-e "$dir/$name" or $taken->{$name}
l!l&&r!l&&!r
005
MC/DC0%0/2
-e "$dir/$name" or $taken->{$name}
-e "$dir/$name" $taken->{$name}
Truth table33%1/3
A or B
A= -e "$dir/$name"B= $taken->{$name}
ABresult
1X1
011
000
665 0 $n++;
666 }
667  
668 5 return $n;
669 }
670  
671 # _session_pages($dir):
672 # Each session page of the clone, in sorted order. A directory
673 # that no read reaches gives the empty list.
674 12 ▶sub _session_pages ($dir)
Subroutine _session_pages: called CC=2 SCAR=7.0
675 {
676 12 ▶ opendir my $dh, $dir or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return () 12 0 12
677 12 my @pages = sort grep { /\ASession-.*[.]md\z/ } readdir $dh;
678 12 closedir $dh;
679  
680 12 return @pages;
681 }
682  
683 # _page_of_session($dir, $session):
684 # The page of the working tree that records one session, or
685 # undef. open and close both read this one lookup, because
686 # _resume brings the page of the fetched branch into the working
687 # tree.
688 9 ▶sub _page_of_session ( $dir, $session )
Subroutine _page_of_session: called CC=4 SCAR=13.9
689 {
690 9 for my $page ( _session_pages($dir) ) {
691 5 ▶ my $text = Fugu::File->read("$dir/$page") // q{};
Condition50%1/2
"Fugu::File"->read("$dir/$page") // ""
l!l
50
MC/DC0%0/1
"Fugu::File"->read("$dir/$page") // ""
"Fugu::File"->read("$dir/$page")
Truth table50%1/2
A
A= "Fugu::File"->read("$dir/$page")
Aresult
11
00
692 5 ▶ return $page if $text =~ /^Session: \Q$session\E$/m;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^Session: \Q$session\E$/mu 3 2 5
693 }
694  
695 6 return;
696 }
697  
698 # _add($app, $path, $text):
699 # Append the text to one page, after one blank line
700 # (WIKI-CAPTURE-1). The method returns 0 after a failure, and it
701 # reports that failure itself.
702 #
703 # Every page that this verb writes ends with a newline, so one
704 # newline in front of the text gives the blank line. The read
705 # and the write take the whole page, because a page holds a few
706 # thousand bytes.
707 4 ▶sub _add ( $app, $path, $text )
Subroutine _add: called CC=3 SCAR=15.1
708 {
709 4 my $old = Fugu::File->read($path);
710 4 ▶ return 1 if defined $old && Fugu::File->write( $path, "$old\n$text" );
Branch: 1 branch
BranchTrueFalseTotal
if defined $old and "Fugu::File"->write($path, "$old\n$text") 4 0 4
Condition33%1/3
defined $old and "Fugu::File"->write($path, "$old\n$text")
!ll&&!rl&&r
004
MC/DC0%0/2
defined $old and "Fugu::File"->write($path, "$old\n$text")
defined $old "Fugu::File"->write($path, "$old\n$text")
Truth table33%1/3
A and B
A= defined $oldB= "Fugu::File"->write($path, "$old\n$text")
ABresult
0X0
100
111
711  
712 0 $app->cli->log->error( 'cannot append to %s', $path );
713  
714 0 return 0;
715 }
716  
717 # _branch($app, $dir):
718 # The branch that the clone has checked out, or undef. A
719 # detached HEAD gives undef, and the verb then fetches nothing
720 # and pushes nothing (WIKI-CAPTURE-6).
721 15 ▶sub _branch ( $app, $dir )
Subroutine _branch: called CC=3 SCAR=12.5
722 {
723 15 my $branch = _capture( $app, $dir, 'branch', '--show-current' );
724 15 ▶ return unless defined $branch && length $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch and length $branch 0 15 15
Condition33%1/3
defined $branch and length $branch
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $branch and length $branch
defined $branch length $branch
Truth table33%1/3
A and B
A= defined $branchB= length $branch
ABresult
0X0
100
111
725  
726 15 return $branch;
727 }
728  
729 # _fetch($app, $dir, $branch):
730 # Fetch the branch of the origin, and fast-forward a local
731 # branch that holds no commit of its own (WIKI-OPEN-2).
732 #
733 # The fast-forward moves a clone that a session left behind, so
734 # its next push is a fast-forward and needs no retry. A branch
735 # with a commit of its own stays, and the push loop rebases it.
736 #
737 # A failed fetch warns. The count and the search then read the
738 # ref of the last fetch, and a checkout without network access
739 # is normal.
740 6 ▶sub _fetch ( $app, $dir, $branch )
Subroutine _fetch: called CC=4 SCAR=17.2
741 {
742 6 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "fetch", "--quiet", "origin", $branch)) 0 6 6
743 defined _git( $app, $dir, 'fetch', '--quiet', 'origin',
744 $branch ) )
745 {
746 0 $app->cli->log->warning(
747 'cannot fetch origin/%s, the count can be stale',
748 $branch );
749 0 return 0;
750 }
751  
752 6 my $own =
753 _capture( $app, $dir, 'rev-list', '--count',
754 "origin/$branch..HEAD" );
755 6 ▶ _git( $app, $dir, 'merge', '--quiet', '--ff-only', "origin/$branch" )
Branch: 1 branch
BranchTrueFalseTotal
if defined $own and $own eq 0 6 0 6
Condition33%1/3
defined $own and $own eq 0
!ll&&!rl&&r
006
MC/DC0%0/2
defined $own and $own eq 0
defined $own $own eq 0
Truth table33%1/3
A and B
A= defined $ownB= $own eq 0
ABresult
0X0
100
111
756 if defined $own && $own eq '0';
757  
758 6 return 1;
759 }
760  
761 # _origin_pages($app, $dir, $branch):
762 # Each page of the fetched branch. The list is empty when no
763 # fetch reached the origin, and the count is then local.
764 5 ▶sub _origin_pages ( $app, $dir, $branch )
Subroutine _origin_pages: called CC=3 SCAR=11.3
765 {
766 5 ▶ return () unless defined $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch 0 5 5
767  
768 5 my $out =
769 _capture( $app, $dir, 'ls-tree', '--name-only', "origin/$branch" );
770 5 ▶ return () unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 5 5
771  
772 5 return grep { /[.]md\z/ } split /\n/, $out;
773 }
774  
775 # _origin_page_of_session($app, $dir, $branch, $session):
776 # The page of the fetched branch that records one session, or
777 # undef (WIKI-OPEN-1). An undefined branch gives undef, and a
778 # branch without that page gives undef. A failed fetch leaves the
779 # ref of the last fetch, and the search then reads it.
780 #
781 # A clone that holds a commit of its own takes no fast-forward,
782 # so its working tree can miss the page that the origin holds,
783 # and open would give that session a second page (D-08). One git
784 # grep reads the branch, because the library holds a page of
785 # every session of every day. Of every call that this verb
786 # parses, git colors the output of grep alone, so it carries
787 # --no-color.
788 5 ▶sub _origin_page_of_session ( $app, $dir, $branch, $session )
Subroutine _origin_page_of_session: called CC=5 SCAR=23.1
789 {
790 5 ▶ return unless defined $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch 0 5 5
791  
792 # The token holds letters, digits, a dot, a dash, and an
793 # underscore (WIKI-OPEN-5). The dot is the one character that
794 # the pattern of git reads, so the escape takes it alone.
795 5 my $pattern = '^Session: ' . ( $session =~ s/[.]/\\./gr ) . '$';
796 5 my $out = _capture(
797 $app, $dir,
798 'grep', '--no-color',
799 '--name-only', '--extended-regexp',
800 '-e', $pattern,
801 "origin/$branch", '--',
802 'Session-*.md'
803 );
804 5 ▶ return unless defined $out && length $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out and length $out 5 0 5
Condition33%1/3
defined $out and length $out
!ll&&!rl&&r
500
MC/DC0%0/2
defined $out and length $out
defined $out length $out
Truth table33%1/3
A and B
A= defined $outB= length $out
ABresult
0X0
100
111
805  
806 # One line of the output is <ref>:<page>, and the first match
807 # answers.
808 0 my $prefix = "origin/$branch:";
809 0 my ($line) = split /\n/, $out;
810 0 ▶ return unless rindex( $line, $prefix, 0 ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless rindex($line, $prefix, 0) == 0 0 0 0
811  
812 0 return substr $line, length $prefix;
813 }
814  
815 # _resume($app, $dir, $branch, $session):
816 # The page of the fetched branch that records one session,
817 # brought into the working tree, or undef (WIKI-OPEN-2).
818 #
819 # A clone that holds a commit of its own takes no fast-forward,
820 # so the checkout gives it that page. note reads the file, and
821 # close reads the Session: line of the file. A page of the
822 # fetched branch alone serves neither one (WIKI-CAPTURE-1,
823 # WIKI-CAPTURE-2).
824 #
825 # The name comes from the tree of the origin, so it passes the
826 # shape check before it reaches the filesystem and git
827 # (CLI-PROGRAM-6). An invalid name and a failed checkout each
828 # give undef, and open then opens a page.
829 5 ▶sub _resume ( $app, $dir, $branch, $session )
Subroutine _resume: called CC=4 SCAR=25.8
830 {
831 5 ▶ my $page = _origin_page_of_session( $app, $dir, $branch, $session )
Branch: 1 branch
BranchTrueFalseTotal
my $page = _origin_page_of_session($app, $dir, $branch, $session) or return 0 5 5
832 or return;
833 0 ▶ _page_path( $app, $dir, $page ) or return;
Branch: 1 branch
BranchTrueFalseTotal
_page_path($app, $dir, $page) or return 0 0 0
834  
835 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "checkout", "origin/$branch", "--", $page)) 0 0 0
836 defined _git( $app, $dir, 'checkout', "origin/$branch", '--',
837 $page ) )
838 {
839 0 $app->cli->log->error( 'cannot check out %s: %s',
840 $page, $app->error );
841 0 return;
842 }
843  
844 0 return $page;
845 }
846  
847 # _move($app, $dir, $page, $next, $title):
848 # Rename the page of the commit, write the title of the new
849 # index into it, and amend the commit with the new name
850 # (WIKI-OPEN-3). The name and the title carry one index, so a
851 # page of the old title would contradict its own name
852 # (WIKI-PAGES-3).
853 #
854 # The new name, or undef after a failure. The caller then reports
855 # a failure of the subcommand. A failed rename changes nothing. A
856 # failed title, a failed stage, and a failed amend each leave the
857 # file and the commit apart, and no repair runs here.
858 0 ▶sub _move ( $app, $dir, $page, $next, $title )
Subroutine _move: not called CC=6 SCAR=37.4
859 {
860 0 my $log = $app->cli->log;
861 0 ▶ unless ( rename "$dir/$page", "$dir/$next" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename "$dir/$page", "$dir/$next") 0 0 0
862 0 $log->error( 'cannot rename %s to %s: %s', $page, $next, $! );
863 0 return;
864 }
865 0 $log->notice( 'the origin holds %s, renaming to %s', $page, $next );
866  
867 # The first line of every page of this verb is the title.
868 0 my $text = Fugu::File->read("$dir/$next");
869 0 ▶ unless ( defined $text
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text and "Fugu::File"->write("$dir/$next", $text =~ s/\A[^\n]*/$title/ru)) 0 0 0
Condition0%0/3
defined $text and "Fugu::File"->write("$dir/$next", $text =~ s/\A[^\n]*/$title/ru)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and "Fugu::File"->write("$dir/$next", $text =~ s/\A[^\n]*/$title/ru)
defined $text "Fugu::File"->write("$dir/$next", $text =~ s/\A[^\n]*/$title/ru)
Truth table0%0/3
A and B
A= defined $textB= "Fugu::File"->write("$dir/$next", $text =~ s/\A[^\n]*/$title/ru)
ABresult
0X0
100
111
870 && Fugu::File->write( "$dir/$next",
871 $text =~ s/\A[^\n]*/$title/r ) )
872 {
873 0 $log->error( 'cannot write the title of %s', $next );
874 0 return;
875 }
876  
877 # The pathspec covers the removal of the old name and the
878 # addition of the new one.
879 0 ▶ unless ( defined _git( $app, $dir, 'add', '-A', '--', $page, $next ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "add", "-A", "--", $page, $next)) 0 0 0
880 0 $log->error( 'cannot stage the rename: %s', $app->error );
881 0 return;
882 }
883 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "commit", "--quiet", "--amend", "-m", "open: $next")) 0 0 0
884 defined _git(
885 $app, $dir, 'commit', '--quiet',
886 '--amend', '-m', "open: $next"
887 ) )
888 {
889 0 $log->error( 'cannot amend the commit: %s', $app->error );
890 0 return;
891 }
892  
893 0 return $next;
894 }
895  
896 # _save($app, $dir, $page, $subject, $rename):
897 # Stage one page, commit it, and push it. The commit carries the
898 # durability, so a failed commit gives the failure code. The push
899 # carries the visibility, so a failed push warns alone
900 # (WIKI-CAPTURE-4).
901 #
902 # A failed rename is no failed push. It can leave the file and the
903 # commit apart, so this method gives the failure code
904 # (WIKI-OPEN-3).
905 #
906 # Every commit of the verb carries a change (WIKI-CAPTURE-3).
907 # The subcommand stops before this method when it has nothing to
908 # add, so the index of each commit here holds the page.
909 9 ▶sub _save ( $app, $dir, $page, $subject, $rename = undef )
Subroutine _save: called CC=5 SCAR=19.6
910 9 {
911 9 my $log = $app->cli->log;
912 9 ▶ unless ( defined _git( $app, $dir, 'add', '--', $page ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "add", "--", $page)) 0 9 9
913 0 $log->error( 'cannot stage %s: %s', $page, $app->error );
914 0 return EXIT_ERROR;
915 }
916  
917 9 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _git($app, $dir, "commit", "--quiet", "-m", $subject)) 0 9 9
918 defined _git( $app, $dir, 'commit', '--quiet', '-m', $subject )
919 )
920 {
921 0 $log->error( 'cannot commit %s: %s', $page, $app->error );
922 0 return EXIT_ERROR;
923 }
924  
925 9 ▶ return EXIT_ERROR unless defined _push( $app, $dir, $rename );
Branch: 1 branch
BranchTrueFalseTotal
unless defined _push($app, $dir, $rename) 0 9 9
926  
927 9 return EXIT_SUCCESS;
928 }
929  
930 # _push($app, $dir, $rename):
931 # Push the branch, and settle a lost race (WIKI-CAPTURE-5). Two
932 # checkouts push to one origin, so the loser meets a rejection.
933 # The loop pushes three times at most, and it fetches, renames,
934 # and rebases before each retry. The last try leaves no retry, so
935 # the loop ends after it. No git call carries --force, because a
936 # ruleset of the library forbids a forced push.
937 #
938 # The method answers 1 after a push, and undef after a failed
939 # rename. It answers 0 after a failed push, and after a detached
940 # HEAD that pushes nothing (WIKI-CAPTURE-6).
941 #
942 # The rename takes the pages of the fetched branch. It answers 0
943 # when that branch holds no page of the commit, and undef after a
944 # failure. A failed rename leaves a clone that no rebase settles,
945 # so the method stops there and the caller gives the failure
946 # code.
947 #
948 # After the last try the commit stays local, and the next push
949 # takes it.
950 9 ▶sub _push ( $app, $dir, $rename = undef )
Subroutine _push: called CC=10 SCAR=29.1
951 9 {
952 9 my $log = $app->cli->log;
953 9 my $branch = _branch( $app, $dir );
954 9 ▶ unless ( defined $branch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $branch) 0 9 9
955 0 $log->warning('detached HEAD, not pushing');
956 0 return 0;
957 }
958  
959 9 for my $try ( 1 .. TRIES ) {
960 10 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined _git($app, $dir, "push", "--quiet", "origin", $branch) 9 1 10
961 if defined _git( $app, $dir, 'push', '--quiet', 'origin',
962 $branch );
963 1 ▶ last if $try == TRIES;
Branch: 1 branch
BranchTrueFalseTotal
if $try == 3 0 1 1
964  
965 1 $log->notice( 'push rejected, rebasing and retrying (%d of %d)',
966 $try, TRIES );
967 last
968 1 ▶ unless defined _git( $app, $dir, 'fetch', '--quiet',
Branch: 1 branch
BranchTrueFalseTotal
unless defined _git($app, $dir, "fetch", "--quiet", "origin", $branch) 0 1 1
969 'origin', $branch );
970  
971 # The rename comes before the rebase: a rebase of two
972 # pages of one name stops on an add/add conflict.
973 1 ▶ if ($rename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rename) 0 1 1
974 0 my %pages = map { $_ => 1 }
975 _origin_pages( $app, $dir, $branch );
976 0 ▶ return unless defined $rename->( \%pages );
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$rename(\%pages) 0 0 0
977 }
978  
979 1 ▶ last unless _rebase( $app, $dir, $branch );
Branch: 1 branch
BranchTrueFalseTotal
unless _rebase($app, $dir, $branch) 0 1 1
980 }
981 0 $log->warning('push failed, the commit stays local');
982  
983 0 return 0;
984 }
985  
986 # _rebase($app, $dir, $branch):
987 # Rebase the branch onto the fetched branch. A rebase that stops
988 # aborts at once, so no stopped rebase stays behind
989 # (WIKI-OPEN-3). The method returns 0 after a failure.
990 1 ▶sub _rebase ( $app, $dir, $branch )
Subroutine _rebase: called CC=3 SCAR=19.2
991 {
992 1 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined _git($app, $dir, "rebase", "--quiet", "origin/$branch") 1 0 1
993 if
994 defined _git( $app, $dir, 'rebase', '--quiet', "origin/$branch" );
995  
996 0 $app->cli->log->error( 'the rebase stopped: %s', $app->error );
997 0 ▶ _git( $app, $dir, 'rebase', '--abort' ) if _rebasing( $app, $dir );
Branch: 1 branch
BranchTrueFalseTotal
if _rebasing($app, $dir) 0 0 0
998  
999 0 return 0;
1000 }
1001  
1002 # _rebasing($app, $dir):
1003 # True when a rebase of the clone has stopped. The abort runs
1004 # under this test alone, because an abort without a rebase in
1005 # progress reports a failure of its own.
1006 0 ▶sub _rebasing ( $app, $dir )
Subroutine _rebasing: not called CC=4 SCAR=30.0
1007 {
1008 0 my $git = _capture( $app, $dir, 'rev-parse', '--absolute-git-dir' );
1009 0 ▶ return 0 unless defined $git;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git 0 0 0
1010  
1011 0 ▶ return ( -d "$git/rebase-merge" || -d "$git/rebase-apply" ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-d "$git/rebase-merge" || -d "$git/rebase-apply" ? : 0 0 0
Condition0%0/3
-d "$git/rebase-merge" or -d "$git/rebase-apply"
l!l&&r!l&&!r
000
MC/DC0%0/2
-d "$git/rebase-merge" or -d "$git/rebase-apply"
-d "$git/rebase-merge" -d "$git/rebase-apply"
Truth table0%0/3
A or B
A= -d "$git/rebase-merge"B= -d "$git/rebase-apply"
ABresult
1X1
011
000
1012 }
1013  
1014 # _git($app, $dir, @args):
1015 # Run one git command with the clone as its working directory
1016 # (WIKI-CONFINE-2). The captured standard error of git reaches
1017 # standard error. The captured standard output stays here,
1018 # because standard output carries the result line alone
1019 # (CLI-PROGRAM-4).
1020 76 ▶sub _git ( $app, $dir, @args )
Subroutine _git: called CC=1 SCAR=0.0
1021 {
1022 76 return $app->command( [ 'git', @args ], cwd => $dir );
1023 }
1024  
1025 # _capture($app, $dir, @args):
1026 # The standard output of one git command, without the last
1027 # newline, or undef when the command fails.
1028 34 ▶sub _capture ( $app, $dir, @args )
Subroutine _capture: called CC=2 SCAR=6.9
1029 {
1030 34 my $out = _git( $app, $dir, @args );
1031 34 ▶ return unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 5 29 34
1032 29 chomp $out;
1033  
1034 29 return $out;
1035 }
1036  
1037 117 1;
« lib/App/FuguBench/Traces.pm ↑ Summary lib/App/FuguBench/Worktree.pm »