blib/lib/App/karr/BoardStore.pm

filter: Statementstmt 8.8% 21 / 238 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 13.6% 6 / 44 Podpod 100% 35 / 35 totaltotal 12.8% 62 / 483 CC 78 SCAR 51.8
CC
78
Cov
6%
serialize_from
650.0
_same_value
182.0
ensure_gitignore
132.0
CRAP 177.8
« blib/lib/App/karr/Foundation/Overview.pm ↑ Summary blib/lib/App/karr/Cmd/Dashboard.pm »
1 # ABSTRACT: Ref-backed board storage for karr
2  
3 package App::karr::BoardStore;
4 1 our $VERSION = '0.602';
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use YAML::XS qw( DumpFile LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::karr::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::karr::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has git => (
13 is => 'ro',
14 required => 1,
15 );
16  
17  
18  
19 0 ▶sub board_exists {
Subroutine board_exists: not called CC=1 SCAR=6.9
Pod documented
20 0 my ($self) = @_;
21 0 return $self->git->ref_exists('refs/karr/config');
22 }
23  
24  
25 0 ▶sub has_board_refs {
Subroutine has_board_refs: not called CC=2 SCAR=17.9
Pod documented
26 0 my ($self) = @_;
27 0 my @refs = $self->list_karr_refs;
28 0 ▶ return @refs ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@refs ? : 0 0 0
29 }
30  
31  
32 0 ▶sub load_config_overrides {
Subroutine load_config_overrides: not called CC=2 SCAR=17.9
Pod documented
33 0 my ($self) = @_;
34 0 my $data = $self->git->read_config_ref;
35 0 ▶ return ref $data eq 'HASH' ? $data : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
36 }
37  
38  
39 0 ▶sub load_config {
Subroutine load_config: not called CC=1 SCAR=6.9
Pod documented
40 0 my ($self) = @_;
41 0 return App::karr::Config->effective_config( $self->load_config_overrides );
42 }
43  
44  
45 0 ▶sub effective_config {
Subroutine effective_config: not called CC=2 SCAR=17.9
Pod documented
46 0 my ($self) = @_;
47 0 ▶ return $self->{_effective_config} //= $self->load_config;
Condition0%0/3
$self->{"_effective_config"} //= $self->load_config
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_effective_config"} //= $self->load_config
$self->{"_effective_config"} $self->load_config
Truth table0%0/3
A //= B
A= $self->{"_effective_config"}B= $self->load_config
ABresult
1X1
011
000
48 }
49  
50  
51 0 ▶sub all_status_names {
Subroutine all_status_names: not called CC=3 SCAR=24.8
Pod documented
52 0 my ($self) = @_;
53 0 my $ec = $self->effective_config;
54 0 ▶ return map { ref $_ ? $_->{name} : $_ } @{$ec->{statuses} // []};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/2
$ec->{"statuses"} // []
l!l
00
MC/DC0%0/1
$ec->{"statuses"} // []
$ec->{"statuses"}
Truth table0%0/2
A
A= $ec->{"statuses"}
Aresult
11
00
55 }
56  
57  
58 0 ▶sub status_requires_claim {
Subroutine status_requires_claim: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($self, $status_name) = @_;
60 0 return App::karr::Config->from_merged( $self->effective_config )
61 ->status_requires_claim($status_name);
62 }
63  
64  
65 0 ▶sub is_terminal_status {
Subroutine is_terminal_status: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($self, $status_name) = @_;
67 # Through the board's own config, not the class default: an imported
68 # kanban-md board names its final column whatever it likes (ticket #67).
69 0 return App::karr::Config->from_merged( $self->effective_config )
70 ->is_terminal_status($status_name);
71 }
72  
73  
74 0 ▶sub is_held_back_status {
Subroutine is_held_back_status: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self, $status_name) = @_;
76 0 return App::karr::Config->from_merged( $self->effective_config )
77 ->is_held_back_status($status_name);
78 }
79  
80  
81 0 ▶sub foundation_enabled {
Subroutine foundation_enabled: not called CC=1 SCAR=6.9
Pod documented
82 0 my ($self) = @_;
83 0 return App::karr::Config->from_merged( $self->effective_config )
84 ->foundation_enabled;
85 }
86  
87  
88 0 ▶sub foundation_reason {
Subroutine foundation_reason: not called CC=1 SCAR=6.9
Pod documented
89 0 my ($self) = @_;
90 0 return App::karr::Config->from_merged( $self->effective_config )
91 ->foundation_reason;
92 }
93  
94  
95 0 ▶sub set_foundation_enabled {
Subroutine set_foundation_enabled: not called CC=5 SCAR=34.0
Pod documented
96 0 my ( $self, $enabled, $reason ) = @_;
97 0 my $effective = $self->effective_config;
98 0 ▶ $effective->{foundation} = {} unless ref $effective->{foundation} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $effective->{"foundation"} eq "HASH" 0 0 0
99 0 ▶ $effective->{foundation}{enabled} = $enabled ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$enabled ? : 0 0 0
100 0 ▶ if ( defined $reason && length $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason and length $reason) { } 0 0 0
Condition0%0/3
defined $reason and length $reason
!ll&&!rl&&r
000
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table0%0/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
101 0 $effective->{foundation}{reason} = $reason;
102 } else {
103 0 delete $effective->{foundation}{reason};
104 }
105 0 return $self->save_config($effective);
106 }
107  
108  
109 0 ▶sub save_config {
Subroutine save_config: not called CC=2 SCAR=17.9
Pod documented
110 0 my ( $self, $effective ) = @_;
111 # The single write choke point for refs/karr/config, so validating here is
112 # what makes `karr config set`, `karr import` and `karr disable` all refuse
113 # a broken schema (ticket #78). Callers hand in either a full effective
114 # config or the sparse contents of a config.yml, so merge first -- merging
115 # an already-effective config with the defaults is a no-op.
116 0 App::karr::Config->validate(
117 App::karr::Config->effective_config($effective) );
118 0 my $defaults = App::karr::Config->default_config;
119 0 my $overrides = _diff_hashes( $defaults, $effective );
120 0 ▶ $overrides->{version} = $effective->{version} // 1;
Condition0%0/2
$effective->{"version"} // 1
l!l
00
MC/DC0%0/1
$effective->{"version"} // 1
$effective->{"version"}
Truth table0%0/2
A
A= $effective->{"version"}
Aresult
11
00
121 0 delete $self->{_effective_config}; # invalidate cache
122 0 return $self->git->write_config_ref($overrides);
123 }
124  
125  
126 0 ▶sub peek_next_id {
Subroutine peek_next_id: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($self) = @_;
128 0 return $self->git->read_next_id_ref;
129 }
130  
131  
132 0 ▶sub allocate_next_id {
Subroutine allocate_next_id: not called CC=1 SCAR=6.9
Pod documented
133 0 my ($self) = @_;
134 0 return $self->git->allocate_next_id_ref;
135 }
136  
137  
138 0 ▶sub set_next_id {
Subroutine set_next_id: not called CC=1 SCAR=6.9
Pod documented
139 0 my ( $self, $next_id ) = @_;
140 0 return $self->git->write_next_id_ref($next_id);
141 }
142  
143  
144 0 ▶sub ensure_next_id {
Subroutine ensure_next_id: not called CC=4 SCAR=30.0
Pod documented
145 0 my ($self) = @_;
146 0 my ($max) = sort { $b <=> $a } $self->git->list_task_refs;
147 0 ▶ my $floor = defined $max ? $max + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $max ? : 0 0 0
148  
149 # A board with no counter yet gets one; a board that already has one only
150 # ever moves forward.
151 0 ▶ return $self->set_next_id($floor)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->git->ref_exists("refs/karr/meta/next-id") 0 0 0
152 unless $self->git->ref_exists('refs/karr/meta/next-id');
153 0 ▶ return 1 if $self->peek_next_id >= $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $self->peek_next_id >= $floor 0 0 0
154 0 return $self->set_next_id($floor);
155 }
156  
157  
158 0 ▶sub stamp_encoding_version {
Subroutine stamp_encoding_version: not called CC=1 SCAR=6.9
Pod documented
159 0 my ($self) = @_;
160 0 return $self->git->write_encoding_version;
161 }
162  
163  
164 0 ▶sub board_id {
Subroutine board_id: not called CC=1 SCAR=6.9
Pod documented
165 0 my ($self) = @_;
166 0 return $self->git->read_board_id_ref;
167 }
168  
169  
170 0 ▶sub ensure_board_id {
Subroutine ensure_board_id: not called CC=1 SCAR=6.9
Pod documented
171 0 my ($self) = @_;
172 0 return $self->git->ensure_board_id_ref;
173 }
174  
175  
176 # The grep is not redundant with the /data-only match in list_task_refs: a ref
177 # that exists but holds no parseable card still loads as undef, and every
178 # consumer of this list calls methods on each element. One undef in here took
179 # out list, board, materialize and pick at once (#45), so the board list is
180 # filtered at the single point that produces it rather than at each of them.
181 0 ▶sub load_tasks {
Subroutine load_tasks: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($self) = @_;
183 0 my @ids = $self->git->list_task_refs;
184 0 return grep { defined } map { $self->git->load_task_ref($_) } @ids;
185 }
186  
187  
188 0 ▶sub find_task {
Subroutine find_task: not called CC=1 SCAR=6.9
Pod documented
189 0 my ( $self, $id ) = @_;
190 0 return $self->git->load_task_ref($id);
191 }
192  
193  
194 0 ▶sub find_task_with_oid {
Subroutine find_task_with_oid: not called CC=1 SCAR=6.9
Pod documented
195 0 my ( $self, $id ) = @_;
196 0 return $self->git->load_task_ref_with_oid($id);
197 }
198  
199  
200 0 ▶sub save_task {
Subroutine save_task: not called CC=2 SCAR=17.9
Pod documented
201 0 my ( $self, $task ) = @_;
202 # Bump `updated` centrally on every mutation of an existing task, so
203 # move/edit/pick/handoff/archive get a fresh timestamp for free. A brand
204 # new task keeps its own `updated` (== created); the restore/import path in
205 # serialize_from bypasses this via git->save_task_ref to preserve stamps.
206 0 my $ref = "refs/karr/tasks/" . $task->id . "/data";
207 0 ▶ $task->updated( gmtime->datetime . 'Z' ) if $self->git->ref_exists($ref);
Branch: 1 branch
BranchTrueFalseTotal
if $self->git->ref_exists($ref) 0 0 0
208 0 return $self->git->save_task_ref($task);
209 }
210  
211  
212 0 ▶sub save_task_cas {
Subroutine save_task_cas: not called CC=1 SCAR=6.9
Pod documented
213 0 my ( $self, $task, $expected_oid ) = @_;
214 # The card came from find_task_with_oid, so the ref exists by construction
215 # and the `updated` bump is unconditional (see save_task above).
216 0 $task->updated( gmtime->datetime . 'Z' );
217 0 return $self->git->save_task_ref_cas( $task, $expected_oid );
218 }
219  
220  
221 0 ▶sub delete_task {
Subroutine delete_task: not called CC=1 SCAR=6.9
Pod documented
222 0 my ( $self, $id ) = @_;
223 0 return $self->git->delete_ref("refs/karr/tasks/$id/data");
224 }
225  
226  
227 0 ▶sub list_karr_refs {
Subroutine list_karr_refs: not called CC=1 SCAR=6.9
Pod documented
228 0 my ($self) = @_;
229 0 return $self->git->list_refs('refs/karr/');
230 }
231  
232  
233 0 ▶sub delete_all_karr_refs {
Subroutine delete_all_karr_refs: not called CC=1 SCAR=6.9
Pod documented
234 0 my ($self) = @_;
235 0 return $self->git->delete_refs('refs/karr/');
236 }
237  
238  
239 0 ▶sub materialize_to {
Subroutine materialize_to: not called CC=5 SCAR=34.0
Pod documented
240 0 my ( $self, $board_dir, %args ) = @_;
241 0 $board_dir = path($board_dir);
242 0 my $tasks_dir = $board_dir->child('tasks');
243 0 my $config_file = $board_dir->child('config.yml');
244  
245 0 my @stale = $self->_materialized_cards($tasks_dir);
246  
247 # Ticket #48: the view is written into the working tree, and `tasks/` and
248 # `config.yml` at a repository root are perfectly ordinary names for a
249 # project to already use. Overwriting or deleting a file Git tracks is data
250 # loss from a command that only reads the board, so check before writing
251 # anything at all -- not even the directories are created on this path.
252 0 ▶ unless ( $args{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"force"}) 0 0 0
253 0 my @tracked = grep { $self->git->is_tracked($_) } $config_file, @stale;
254 die "Refusing to materialize into $board_dir: "
255 . scalar(@tracked)
256 . " file(s) there are tracked by git and would be overwritten or deleted:\n"
257 0 ▶ . join( '', map { ' ' . $_->relative($board_dir) . "\n" } @tracked )
Branch: 1 branch
BranchTrueFalseTotal
if @tracked 0 0 0
258 . "The file view is disposable board state, not project content. Move those files\n"
259 . "aside, or re-run with --force to replace them.\n"
260 if @tracked;
261 }
262  
263 0 $board_dir->mkpath;
264 0 $tasks_dir->mkpath;
265  
266 {
267 # kanban-md's schema types several config keys as Go bools and rejects
268 # the integers Perl uses for them; JSON::PP is YAML::XS's mode name for
269 # "dump JSON::PP::Boolean as a real YAML boolean" (ticket #60).
270 0 local $YAML::XS::Boolean = 'JSON::PP';
271 0 DumpFile(
272 $config_file->stringify,
273 App::karr::Config->file_view_config(
274 $self->load_config,
275 next_id => $self->peek_next_id,
276 ),
277 );
278 }
279  
280 0 $_->remove for @stale;
281  
282 0 for my $task ( $self->load_tasks ) {
283 0 $task->save($tasks_dir);
284 }
285  
286 0 return $board_dir;
287 }
288  
289  
290 # The files in tasks/ that a previous materialization could have written, i.e.
291 # everything shaped like App::karr::Task::filename -- which is also kanban-md's
292 # own task-filename prefix (`^(\d+)-` in internal/task/find.go). Anything else
293 # in the directory belongs to the project, not to karr, and is never swept
294 # (ticket #48).
295 0 ▶sub _materialized_cards {
Subroutine _materialized_cards: not called CC=2 SCAR=17.9
296 0 my ( $self, $tasks_dir ) = @_;
297 0 ▶ return () unless $tasks_dir->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $tasks_dir->exists 0 0 0
298 0 return sort { $a->basename cmp $b->basename }
299 $tasks_dir->children(qr/\A\d+-.*\.md\z/);
300 }
301  
302 0 ▶sub file_view_gitignore_entries {
Subroutine file_view_gitignore_entries: not called CC=1 SCAR=6.9
Pod documented
303 # The disposable file view materialize_to writes: config.yml + tasks/*.md.
304 # These must always be gitignored -- refs/karr/* is the canonical state and
305 # the view is never committed. Mirror the exact names used by materialize_to.
306 0 return ( 'tasks/', 'config.yml' );
307 }
308  
309  
310 0 ▶sub project_owned_view_paths {
Subroutine project_owned_view_paths: not called CC=1 SCAR=6.9
Pod documented
311 0 my ( $self, $board_dir ) = @_;
312 0 $board_dir = path($board_dir);
313 0 return grep { $self->git->is_tracked_under( $board_dir->child( $_ =~ s{/\z}{}r ) ) }
314 $self->file_view_gitignore_entries;
315 }
316  
317  
318 0 ▶sub ensure_gitignore {
Subroutine ensure_gitignore: not called CC=11 SCAR=48.8
Pod documented
319 0 my ( $self, $board_dir ) = @_;
320 0 $board_dir = path($board_dir);
321 0 my $gitignore = $board_dir->child('.gitignore');
322  
323 0 my @entries = $self->file_view_gitignore_entries;
324 0 ▶ my $existing = $gitignore->exists ? $gitignore->slurp_utf8 : '';
Branch: 1 branch
BranchTrueFalseTotal
$gitignore->exists ? : 0 0 0
325  
326 # Line-exact presence (whitespace-insensitive), so we never duplicate an
327 # entry -- or our header -- that is already there.
328 0 my %present;
329 0 for my $line ( split /\n/, $existing ) {
330 0 $line =~ s/^\s+//;
331 0 $line =~ s/\s+$//;
332 0 ▶ $present{$line} = 1 if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
333 }
334  
335 0 my @missing = grep { !$present{$_} } @entries;
336 0 ▶ return () unless @missing;
Branch: 1 branch
BranchTrueFalseTotal
unless @missing 0 0 0
337  
338 0 my $header = '# karr materialized task view -- never commit';
339 0 ▶ my $header_present = $present{$header} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$present{$header} ? : 0 0 0
340  
341 # Idempotent append that keeps the existing file intact: terminate a
342 # dangling last line, separate a fresh karr block with a blank line, and
343 # only emit the header when starting one.
344 0 my $append = '';
345 0 ▶ if ( length $existing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $existing) 0 0 0
346 0 ▶ $append .= "\n" unless $existing =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $existing =~ /\n\z/ 0 0 0
347 0 ▶ $append .= "\n" unless $header_present;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_present 0 0 0
348 }
349 0 ▶ $append .= "$header\n" unless $header_present;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_present 0 0 0
350 0 $append .= "$_\n" for @missing;
351  
352 0 $gitignore->append_utf8($append);
353 0 return @missing;
354 }
355  
356  
357 0 ▶sub serialize_from {
Subroutine serialize_from: not called CC=25 SCAR=64.8
Pod documented
358 0 my ( $self, $board_dir ) = @_;
359 0 $board_dir = path($board_dir);
360  
361 # Whether this import is the board's birth or a write into one that was
362 # already here. Asked before anything below writes a ref, since every write
363 # would answer it "already here"; the encoding marker at the bottom hangs
364 # on the difference (#132).
365 0 my $born_here = !$self->has_board_refs;
366  
367 # Ticket #70: parse the entire view before a single ref is touched, so one
368 # malformed card leaves the board exactly as it was instead of half
369 # imported with the prune never reached. Reading the config here rather
370 # than writing it keeps that promise for the config too.
371 0 my $tasks_dir = $board_dir->child('tasks');
372 my @files = $tasks_dir->exists
373 0 ▶ ? sort { $a->basename cmp $b->basename } $tasks_dir->children(qr/\.md$/)
Branch: 1 branch
BranchTrueFalseTotal
$tasks_dir->exists ? : 0 0 0
374 : ();
375  
376 0 my ( @tasks, @rejected );
377 0 for my $file (@files) {
378 0 my $task = eval { App::karr::Task->from_file($file) };
379 0 ▶ if ($task) { push @tasks, $task }
Branch: 1 branch
BranchTrueFalseTotal
if ($task) { } 0 0 0
380 0 ▶ else { push @rejected, ( $@ || "unknown error\n" ) }
Condition0%0/2
$@ || "unknown error\n"
l!l
00
MC/DC0%0/1
$@ || "unknown error\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
381 }
382 # kanban-md skips malformed files and carries on, but import cannot: a
383 # skipped card's ref would be pruned below, turning an unreadable file into
384 # a deleted task. All or nothing -- and every rejected file is named, which
385 # a bare "Invalid task format" never was.
386 0 ▶ if (@rejected) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rejected) 0 0 0
387 die "Refusing to import from $board_dir: "
388 . scalar(@rejected)
389 . " of " . scalar(@files) . " task file(s) could not be parsed:\n"
390 0 . join( '', map { my $why = $_; chomp $why; " $why\n" } @rejected )
391 . "No refs were changed. Fix or remove those files and import again.\n";
392 }
393  
394 0 my $config_file = $board_dir->child('config.yml');
395 0 ▶ my $config = $config_file->exists
Branch: 1 branch
BranchTrueFalseTotal
$config_file->exists ? : 0 0 0
Condition0%0/2
LoadFile($config_file->stringify) // {}
l!l
00
MC/DC0%0/1
LoadFile($config_file->stringify) // {}
LoadFile($config_file->stringify)
Truth table0%0/2
A
A= LoadFile($config_file->stringify)
Aresult
11
00
396 ? ( LoadFile( $config_file->stringify ) // {} )
397 : undef;
398 # A config.yml that is not a mapping is a broken view, and the import path
399 # names what is wrong with a view rather than dying inside a dereference --
400 # the same promise it already makes for a malformed card above. Reaching
401 # the delete below with a list here died with a bare "Not a HASH reference"
402 # and a line number.
403 0 ▶ die "Refusing to import from $board_dir: "
Branch: 1 branch
BranchTrueFalseTotal
if defined $config and ref $config ne "HASH" 0 0 0
Condition0%0/3
defined $config and ref $config ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config and ref $config ne "HASH"
defined $config ref $config ne "HASH"
Truth table0%0/3
A and B
A= defined $configB= ref $config ne "HASH"
ABresult
0X0
100
111
404 . "its config.yml is not a mapping.\nNo refs were changed.\n"
405 if defined $config && ref $config ne 'HASH';
406  
407 0 my $view_next_id;
408 0 ▶ if ( defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 0 0 0
409 # next_id belongs to refs/karr/meta/next-id, not to the config; the
410 # seeding below owns it. materialize writes it into the view purely
411 # because kanban-md refuses a config without it (ticket #60) -- but the
412 # value still carries information, so keep it for the floor below.
413 0 my $raw = delete $config->{next_id};
414 0 ▶ $view_next_id = $raw
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw and not ref $raw and $raw =~ /\A\d+\z/ and $raw >= 1 0 0 0
Condition0%0/9
defined $raw and not ref $raw and $raw =~ /\A\d+\z/ and $raw >= 1
!ll&&!rl&&r
000
defined $raw and not ref $raw and $raw =~ /\A\d+\z/
!ll&&!rl&&r
000
defined $raw and not ref $raw
!ll&&!rl&&r
000
MC/DC0%0/4
defined $raw and not ref $raw and $raw =~ /\A\d+\z/ and $raw >= 1
defined $raw not ref $raw $raw =~ /\A\d+\z/ $raw >= 1
Truth table0%0/5
A and not(B) and C and D
A= defined $rawB= not ref $rawC= $raw =~ /\A\d+\z/D= $raw >= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
415 if defined $raw && !ref $raw && $raw =~ /\A\d+\z/ && $raw >= 1;
416 }
417  
418 # Nothing above this line wrote anything.
419 0 ▶ if ( defined $config ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $config) { } 0 0 0
elsif (not $self->board_exists) { } 0 0 0
420 # Reconcile against what refs/karr/config already says instead of
421 # replacing it (tickets #87, #88). The file view is a lossy projection:
422 # kanban-md rewrites config.yml as soon as it loads one and drops every
423 # key its Go schema does not know, so a replace silently un-did
424 # `karr disable` and recorded kanban-md's migrated defaults as
425 # deliberate per-board overrides.
426 0 $self->save_config(
427 App::karr::Config->reconcile_view_config(
428 $self->load_config_overrides, $config ) );
429 }
430 elsif ( !$self->board_exists ) {
431 # Import is a bootstrap path (#30), so it has to leave a board karr
432 # will actually write to. A kanban-md tasks/ view with no config.yml
433 # otherwise produced tasks and a counter but no refs/karr/config --
434 # exactly the half-board every write command now refuses (#62), which
435 # made `karr create` right after a successful import impossible.
436 0 $self->save_config( App::karr::Config->default_config );
437 }
438  
439 0 my %seen;
440 0 for my $task (@tasks) {
441 # Restore/import path: persist verbatim so the original `updated`
442 # timestamps survive, even when overwriting pre-existing refs.
443 0 $self->git->save_task_ref($task);
444 0 $seen{ $task->id } = 1;
445 }
446  
447 0 for my $id ( $self->git->list_task_refs ) {
448 0 ▶ next if $seen{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$id} 0 0 0
449 0 $self->delete_task($id);
450 }
451  
452 # Bootstrap fix (#30): import does not require a pre-existing board, so on a
453 # fresh repo meta/next-id is missing and a following `karr create` would
454 # re-allocate an already-imported id. Seed next-id past the highest imported
455 # id when the stored next-id is missing or stale, but never lower a next-id
456 # that is already ahead of the view (an existing healthy board is untouched).
457 #
458 # The view's own next_id is part of that floor (ticket #90). Seeding from
459 # the highest card alone retired ids the other side of the bridge had
460 # already burned: a kanban-md board whose next_id ran ahead of its highest
461 # card -- which is every board that ever lost one -- handed the next
462 # `karr create` an id kanban-md considers used. Forward only, in both
463 # directions, which is kanban-md's own rule for this value
464 # (internal/task/consistency.go, syncNextID: max(stored, highest id + 1)).
465 0 my $floor = 1;
466 0 ▶ if (%seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (%seen) 0 0 0
467 0 my ($max_id) = sort { $b <=> $a } keys %seen;
468 0 $floor = $max_id + 1;
469 }
470 0 ▶ $floor = $view_next_id if defined $view_next_id && $view_next_id > $floor;
Branch: 1 branch
BranchTrueFalseTotal
if defined $view_next_id and $view_next_id > $floor 0 0 0
Condition0%0/3
defined $view_next_id and $view_next_id > $floor
!ll&&!rl&&r
000
MC/DC0%0/2
defined $view_next_id and $view_next_id > $floor
defined $view_next_id $view_next_id > $floor
Truth table0%0/3
A and B
A= defined $view_next_idB= $view_next_id > $floor
ABresult
0X0
100
111
471 0 ▶ $self->set_next_id($floor) if $self->peek_next_id < $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $self->peek_next_id < $floor 0 0 0
472  
473 # Everything just written came from character-level file reads (LoadFile,
474 # Task->from_file) and so satisfies the current encoding contract -- but the
475 # marker speaks for the whole board, not for the refs this import happened
476 # to rewrite. On a board that was already here, import replaces the task
477 # refs and leaves the activity log under refs/karr/log/ alone (and the
478 # config too, when the view carries no config.yml), so a pre-0.403 board
479 # keeps double-encoded payloads that the marker would then declare clean:
480 # every old log entry read as mojibake and `karr repair` reported the board
481 # as up to date (#132, the same defect as `karr init` on a half-board).
482 # Only a board born in this import gets the claim; on any other, the
483 # read-path repair keeps running and `karr repair --yes` stays the one
484 # command that may stamp, because it is the one that rewrites every ref.
485 0 ▶ $self->stamp_encoding_version if $born_here;
Branch: 1 branch
BranchTrueFalseTotal
if $born_here 0 0 0
486  
487 # Import is a board-birth path like init (#30), so it stamps the board
488 # identity too (#95) rather than waiting for the first pull to notice the
489 # board has none. Read-before-write inside, same as the encoding marker
490 # above: an existing board keeps the id it has.
491 0 $self->ensure_board_id;
492  
493 0 return 1;
494 }
495  
496  
497 0 ▶sub snapshot {
Subroutine snapshot: not called CC=2 SCAR=17.9
Pod documented
498 0 my ($self) = @_;
499 0 my %snapshot;
500 0 for my $ref ( $self->list_karr_refs ) {
501 0 $snapshot{$ref} = $self->git->read_ref($ref);
502 }
503 return {
504 0 version => 1,
505 refs => \%snapshot,
506 };
507 }
508  
509  
510 0 ▶sub restore_snapshot {
Subroutine restore_snapshot: not called CC=4 SCAR=30.0
Pod documented
511 0 my ( $self, $snapshot ) = @_;
512 0 ▶ my $refs = $snapshot->{refs} || {};
Condition0%0/2
$snapshot->{'refs'} || {}
l!l
00
MC/DC0%0/1
$snapshot->{'refs'} || {}
$snapshot->{'refs'}
Truth table0%0/2
A
A= $snapshot->{'refs'}
Aresult
11
00
513 # A snapshot taken before board identities existed carries no board-id
514 # ref, and replace_board_refs makes the board exactly the snapshot -- so
515 # installing one verbatim would strip this board's identity, and the push
516 # that follows would prune it off the remote too, disarming the
517 # swapped-remote guard (#95) on every clone. Keep the standing id across
518 # such a content restore. A snapshot that does carry an id -- the board's
519 # own, or a foreign board's in a deliberate takeover -- is installed as is.
520 0 my $standing = $self->git->read_board_id_ref;
521 0 my $ok = $self->git->replace_board_refs($refs);
522 $self->git->write_board_id_ref($standing)
523 0 ▶ if defined $standing && !exists $refs->{'refs/karr/meta/board-id'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $standing and not exists $refs->{"refs/karr/meta/board-id"} 0 0 0
Condition0%0/3
defined $standing and not exists $refs->{"refs/karr/meta/board-id"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $standing and not exists $refs->{"refs/karr/meta/board-id"}
defined $standing not exists $refs->{"refs/karr/meta/board-id"}
Truth table0%0/3
A and not(B)
A= defined $standingB= not exists $refs->{"refs/karr/meta/board-id"}
ABresult
0X0
100
111
524 0 return $ok;
525 }
526  
527  
528 0 ▶sub _diff_hashes {
Subroutine _diff_hashes: not called CC=9 SCAR=45.0
529 0 my ( $defaults, $effective ) = @_;
530 0 my %diff;
531 0 ▶ for my $key ( keys %{ $effective // {} } ) {
Condition0%0/2
$effective // {}
l!l
00
MC/DC0%0/1
$effective // {}
$effective
Truth table0%0/2
A
A= $effective
Aresult
11
00
532 0 ▶ next if $key eq 'next_id';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "next_id" 0 0 0
533 0 my $have_default = exists $defaults->{$key};
534 0 my $default = $defaults->{$key};
535 0 my $value = $effective->{$key};
536  
537 0 ▶ if ( ref($value) eq 'HASH' && ref($default) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH' and ref $default eq 'HASH') { } 0 0 0
elsif (not $have_default && _same_value($default, $value)) { } 0 0 0
Condition0%0/3
ref $value eq "HASH" and ref $default eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "HASH" and ref $default eq "HASH"
ref $value eq "HASH" ref $default eq "HASH"
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= ref $default eq "HASH"
ABresult
0X0
100
111
538 0 my $nested = _diff_hashes( $default, $value );
539 0 ▶ $diff{$key} = $nested if keys %$nested;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$nested 0 0 0
540 ▶ } elsif ( !$have_default || !_same_value( $default, $value ) ) {
Condition0%0/3
$have_default && _same_value($default, $value)
!ll&&!rl&&r
000
MC/DC0%0/2
$have_default && _same_value($default, $value)
$have_default _same_value($default, $value)
Truth table0%0/3
A && B
A= $have_defaultB= _same_value($default, $value)
ABresult
0X0
100
111
541 0 $diff{$key} = $value;
542 }
543 }
544 0 return \%diff;
545 }
546  
547 0 ▶sub _same_value {
Subroutine _same_value: not called CC=13 SCAR=52.0
548 0 my ( $left, $right ) = @_;
549 0 ▶ return 0 if ref($left) ne ref($right);
Branch: 1 branch
BranchTrueFalseTotal
if ref $left ne ref $right 0 0 0
550 0 ▶ if ( ref($left) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left eq "HASH") 0 0 0
551 0 ▶ return 0 unless keys(%$left) == keys(%$right);
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$left == keys %$right 0 0 0
552 0 for my $key ( keys %$left ) {
553 0 ▶ return 0 unless exists $right->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $right->{$key} 0 0 0
554 0 ▶ return 0 unless _same_value( $left->{$key}, $right->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless _same_value($left->{$key}, $right->{$key}) 0 0 0
555 }
556 0 return 1;
557 }
558 0 ▶ if ( ref($left) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left eq "ARRAY") 0 0 0
559 0 ▶ return 0 unless @$left == @$right;
Branch: 1 branch
BranchTrueFalseTotal
unless @$left == @$right 0 0 0
560 0 for my $i ( 0 .. $#$left ) {
561 0 ▶ return 0 unless _same_value( $left->[$i], $right->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless _same_value($left->[$i], $right->[$i]) 0 0 0
562 }
563 0 return 1;
564 }
565 0 ▶ return ( defined $left ? $left : '' ) eq ( defined $right ? $right : '' );
Branch: 2 branches
BranchTrueFalseTotal
defined $left ? : 0 0 0
defined $right ? : 0 0 0
566 }
567  
568 1 1;
569  
570 __END__
« blib/lib/App/karr/Foundation/Overview.pm ↑ Summary blib/lib/App/karr/Cmd/Dashboard.pm »