| 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::Checkout; | ||||||||||||||||||||||||||||
| 19 | 118 | our $VERSION = '0.1.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 118 | ▶ | use v5.34; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 118 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 118 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 118 | ▶ | no feature qw(indirect multidimensional bareword_filehandles); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 118 | ▶ | use File::Basename qw(basename dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 118 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 118 | ▶ | use Fugu::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # App::FuguBench::Checkout - the checkout walk and the configuration | ||||||||||||||||||||||||||||
| 32 | # reader. | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # The walk has two stops. The first stop is the nearest directory | ||||||||||||||||||||||||||||
| 35 | # with .toolingrc, and that directory is the root. The second stop is | ||||||||||||||||||||||||||||
| 36 | # the nearest .toolingrc on the walk that holds a key. The directory | ||||||||||||||||||||||||||||
| 37 | # of that file is the home of the key, and a verb anchors a relative | ||||||||||||||||||||||||||||
| 38 | # value at a home or at the root. | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | # A clone under Projects/ holds its own .toolingrc without a | ||||||||||||||||||||||||||||
| 41 | # wiki.origin key, so the walk for the library reaches the workspace. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 118 | ▶ | use constant CONFIG_FILE => '.toolingrc'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # The keys of CLI-CONFIG, with the default of each one. The default | ||||||||||||||||||||||||||||
| 46 | # is a code reference, because wiki.project reads the root. A key | ||||||||||||||||||||||||||||
| 47 | # outside the table belongs to another tool, and config() gives | ||||||||||||||||||||||||||||
| 48 | # nothing for it. A key with no default stops the verb that needs it. | ||||||||||||||||||||||||||||
| 49 | my %KEY = ( | ||||||||||||||||||||||||||||
| 50 | 22 | ▶ | 'wiki.dir' => sub ($) { return 'Wiki' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | 'wiki.origin' => sub ($) { return }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | 'wiki.project' => sub ($self) { return basename( $self->root ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | 'wiki.projects' => sub ($) { return 'Projects' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 55 | ▶ | 'worktree.base' => sub ($) { return '.claude/worktrees' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 118 | ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # App::FuguBench::Checkout->new(start => $dir): | ||||||||||||||||||||||||||||
| 58 | # Walk up from the start and record each directory that holds a | ||||||||||||||||||||||||||||
| 59 | # .toolingrc, nearest first. The method returns undef when the | ||||||||||||||||||||||||||||
| 60 | # walk reaches the filesystem root with none. | ||||||||||||||||||||||||||||
| 61 | 111 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | my $dir = File::Spec->canonpath( | ||||||||||||||||||||||||||||
| 64 | 111 | ▶ | File::Spec->rel2abs( $args{start} // q{.} ) ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"start"} // "."
MC/DC0%0/1
$args{"start"} // "."
$args{"start"}
Truth table50%1/2
A
A= $args{"start"}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 111 | my @walk; | |||||||||||||||||||||||||||
| 67 | 111 | while (1) { | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # The file name goes in a variable first. On perl 5.34 a | ||||||||||||||||||||||||||||
| 70 | # file test reads the class name of a method call as a | ||||||||||||||||||||||||||||
| 71 | # bareword filehandle. | ||||||||||||||||||||||||||||
| 72 | 388 | my $file = File::Spec->catfile( $dir, CONFIG_FILE ); | |||||||||||||||||||||||||||
| 73 | 388 | ▶ | push @walk, $dir if -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 388 | my $parent = dirname($dir); | |||||||||||||||||||||||||||
| 76 | 388 | ▶ | last if $parent eq $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 277 | $dir = $parent; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 111 | ▶ | return unless @walk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 105 | return bless { | |||||||||||||||||||||||||||
| 82 | root => $walk[0], | ||||||||||||||||||||||||||||
| 83 | walk => \@walk, | ||||||||||||||||||||||||||||
| 84 | values => {}, | ||||||||||||||||||||||||||||
| 85 | error => undef, | ||||||||||||||||||||||||||||
| 86 | }, $class; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # $self->root: | ||||||||||||||||||||||||||||
| 90 | # The directory of the first .toolingrc on the walk. | ||||||||||||||||||||||||||||
| 91 | 89 | ▶ | sub root ($self) | ||||||||||||||||||||||||||
|
Subroutine
root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 89 | return $self->{root}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # $self->error: | ||||||||||||||||||||||||||||
| 97 | # The reason of the last failed shape check, or undef. | ||||||||||||||||||||||||||||
| 98 | 7 | ▶ | sub error ($self) | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 7 | return $self->{error}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # $self->config($key): | ||||||||||||||||||||||||||||
| 104 | # The value of one key and its home, as a two-element list. The | ||||||||||||||||||||||||||||
| 105 | # value comes from the nearest .toolingrc on the walk that holds | ||||||||||||||||||||||||||||
| 106 | # the key, and the home is the directory of that file. A key | ||||||||||||||||||||||||||||
| 107 | # that no file holds takes its default, with the root as its | ||||||||||||||||||||||||||||
| 108 | # home. A key with no default, and a key of another tool, give | ||||||||||||||||||||||||||||
| 109 | # the empty list. | ||||||||||||||||||||||||||||
| 110 | 119 | ▶ | sub config ( $self, $key ) | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 119 | ▶ | my $default = $KEY{$key} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 118 | for my $dir ( @{ $self->{walk} } ) { | |||||||||||||||||||||||||||
| 115 | 124 | my $values = $self->_values($dir); | |||||||||||||||||||||||||||
| 116 | 124 | ▶ | next unless exists $values->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 37 | return ( $values->{$key}, $dir ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 81 | my $value = $default->($self); | |||||||||||||||||||||||||||
| 121 | 81 | ▶ | return unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 80 | return ( $value, $self->{root} ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # $self->_values($dir): | ||||||||||||||||||||||||||||
| 127 | # The keys of the .toolingrc of one directory. A line holds a | ||||||||||||||||||||||||||||
| 128 | # key, whitespace, and a value. A line that starts with # is a | ||||||||||||||||||||||||||||
| 129 | # comment, and a line with no value holds no key. | ||||||||||||||||||||||||||||
| 130 | 124 | ▶ | sub _values ( $self, $dir ) | ||||||||||||||||||||||||||
|
Subroutine
_values: called CC=9 SCAR=23.4
| |||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | 124 | ▶ | return $self->{values}{$dir} if $self->{values}{$dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 98 | my $text = Fugu::File->read( File::Spec->catfile( $dir, CONFIG_FILE ) ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 98 | my %values; | |||||||||||||||||||||||||||
| 137 | 98 | ▶ | for my $line ( split /\n/, $text // q{} ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||
| 138 | 48 | my $trimmed = $line =~ s/^\s+|\s+\z//gr; | |||||||||||||||||||||||||||
| 139 | 48 | ▶ | next if $trimmed eq q{} || $trimmed =~ /^#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$trimmed eq "" or $trimmed =~ /^#/u
MC/DC50%1/2
$trimmed eq "" or $trimmed =~ /^#/u
$trimmed eq ""
$trimmed =~ /^#/u
Truth table66%2/3
A or B
A= $trimmed eq ""B= $trimmed =~ /^#/u
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 43 | my ( $key, $value ) = split /\s+/, $trimmed, 2; | |||||||||||||||||||||||||||
| 142 | 43 | ▶ | next unless defined $value && $value ne q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value ne ""
MC/DC0%0/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 143 | 43 | ▶ | $values{$key} //= $value; | ||||||||||||||||||||||||||
|
Condition33%1/3
$values{$key} //= $value
MC/DC0%0/2
$values{$key} //= $value
$values{$key}
$value
Truth table33%1/3
A //= B
A= $values{$key}B= $value
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 98 | return $self->{values}{$dir} = \%values; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # $self->dir_value($value): | ||||||||||||||||||||||||||||
| 150 | # The shape check of a directory value: a relative path with no | ||||||||||||||||||||||||||||
| 151 | # parent segment. A value that fails gives undef with the reason | ||||||||||||||||||||||||||||
| 152 | # in error(). | ||||||||||||||||||||||||||||
| 153 | 92 | ▶ | sub dir_value ( $self, $value ) | ||||||||||||||||||||||||||
|
Subroutine
dir_value: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 92 | $self->{error} = undef; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 92 | ▶ | unless ( defined $value && $value ne q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value ne ""
MC/DC0%0/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 158 | 0 | $self->{error} = 'the directory is empty'; | |||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 92 | ▶ | if ( File::Spec->file_name_is_absolute($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | $self->{error} = "the directory is absolute: $value"; | |||||||||||||||||||||||||||
| 163 | 1 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 91 | ▶ | if ( grep { $_ eq '..' } split m{/}, $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 5 | $self->{error} = "the directory leaves the tree: $value"; | |||||||||||||||||||||||||||
| 167 | 5 | return; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 86 | return $value; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # $self->url_value($value): | ||||||||||||||||||||||||||||
| 174 | # The shape check of a URL value: a scheme and an authority. A | ||||||||||||||||||||||||||||
| 175 | # value that fails gives undef with the reason in error(). | ||||||||||||||||||||||||||||
| 176 | 23 | ▶ | sub url_value ( $self, $value ) | ||||||||||||||||||||||||||
|
Subroutine
url_value: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||
| 178 | 23 | $self->{error} = undef; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 23 | ▶ | unless ( defined $value && $value ne q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value ne ""
MC/DC0%0/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 181 | 0 | $self->{error} = 'the URL is empty'; | |||||||||||||||||||||||||||
| 182 | 0 | return; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 23 | ▶ | unless ( $value =~ m{\A[A-Za-z][A-Za-z0-9+.-]*://\S} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | $self->{error} = "the URL holds no scheme: $value"; | |||||||||||||||||||||||||||
| 186 | 1 | return; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 22 | return $value; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 118 | 1; | |||||||||||||||||||||||||||