| 1 | # ABSTRACT: Initialize a new karr board | ||
| 2 | |||
| 3 | package App::karr::Cmd::Init; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | usage_string => 'USAGE: karr init [--name TEXT] [--statuses LIST] [--claude-skill] ' | |
| 9 | . '[--new-board]', | ||
| 10 | 2 | ); | |
| 11 | 2 | ▶ | use App::karr::Error qw( user_error clean_error ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use App::karr::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use App::karr::Role::BoardDiscovery; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use App::karr::Role::CliArgs; | ||
| 15 | use App::karr::Role::Output; | ||
| 16 | use App::karr::Role::SkillFile; | ||
| 17 | |||
| 18 | # SkillFile: _skill_files and _write_skill_files, shared with `karr skill`, | ||
| 19 | # which installs the same directories --claude-skill installs (tickets #145, | ||
| 20 | # #146, #285). | ||
| 21 | with 'App::karr::Role::BoardDiscovery', 'App::karr::Role::SkillFile'; | ||
| 22 | with 'App::karr::Role::CliArgs'; | ||
| 23 | with 'App::karr::Role::Output'; | ||
| 24 | |||
| 25 | |||
| 26 | option name => ( | ||
| 27 | is => 'ro', | ||
| 28 | format => 's', | ||
| 29 | doc => 'Board name', | ||
| 30 | ); | ||
| 31 | |||
| 32 | option statuses => ( | ||
| 33 | is => 'ro', | ||
| 34 | format => 's', | ||
| 35 | doc => 'Comma-separated status list', | ||
| 36 | ); | ||
| 37 | |||
| 38 | option new_board => ( | ||
| 39 | is => 'ro', | ||
| 40 | doc => 'Start a board here even though the remote already has one', | ||
| 41 | ); | ||
| 42 | |||
| 43 | option claude_skill => ( | ||
| 44 | is => 'ro', | ||
| 45 | doc => 'Install the Claude Code skills for karr', | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub execute { | ||
| 49 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 50 | |||
| 51 | # git_root honours --dir (both call forms) and dies loudly if the target is | ||
| 52 | # not a Git repository, instead of hardcoding the current directory. | ||
| 53 | my $root = $self->git_root; | ||
| 54 | my $store = $self->store; | ||
| 55 | die "Board already exists in refs/karr/\n" if $store->board_exists; | ||
| 56 | |||
| 57 | # Before the first ref write, and before $born_here below: whether this | ||
| 58 | # repository already has a board is a question the remote answers too, not | ||
| 59 | # the local refs alone (#182). | ||
| 60 | $self->_refuse_if_remote_has_board unless $self->new_board; | ||
| 61 | |||
| 62 | # Every file --claude-skill is going to install, looked up before anything | ||
| 63 | # is written, the way `karr skill install` looks up the whole set before its | ||
| 64 | # first write. The lookup can fail -- an install whose share dir is missing, | ||
| 65 | # as the single binary's was until k308 -- and init used to find that out | ||
| 66 | # only after writing the board refs and the .gitignore entries and creating | ||
| 67 | # the first skill's directory: exit 1, an initialized board, an empty | ||
| 68 | # .claude/skills/NAME (or one skill written and the next one empty), and a | ||
| 69 | # retry that only said "Board already exists" (k316). Asked here, a failed | ||
| 70 | # lookup leaves the repository exactly as it found it. Since #146 the lookup | ||
| 71 | # is App::karr::Role::SkillFile's and since #285 a skill is a directory, | ||
| 72 | # SKILL.md plus references/*.md, which _skill_files hands out whole. | ||
| 73 | my %shipped = $self->claude_skill | ||
| 74 | ? map { ( $_ => { $self->_skill_files($_) } ) } $self->_skill_names | ||
| 75 | : (); | ||
| 76 | |||
| 77 | # Asked before the first ref write below, which would make any repository | ||
| 78 | # look like it already held something. This is what tells a board born here | ||
| 79 | # apart from a half-board this run is completing (#62), and the encoding | ||
| 80 | # marker further down hangs on the difference (#132). | ||
| 81 | my $born_here = !$store->has_board_refs; | ||
| 82 | |||
| 83 | my $overrides = { version => 1 }; | ||
| 84 | $overrides->{board} = { name => $self->name } if defined $self->name; | ||
| 85 | |||
| 86 | if ($self->statuses) { | ||
| 87 | my @statuses = split /,/, $self->statuses; | ||
| 88 | $overrides->{statuses} = \@statuses; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $effective = App::karr::Config->effective_config($overrides); | ||
| 92 | $store->save_config($effective); | ||
| 93 | # Not set_next_id(1): init now also completes a board that a stray write | ||
| 94 | # command left half-built (#62), and resetting the counter under tasks that | ||
| 95 | # are already there would hand the next `karr create` an id it would then | ||
| 96 | # overwrite. | ||
| 97 | $store->ensure_next_id; | ||
| 98 | # A board born here is written under the current encoding contract, so mark | ||
| 99 | # it and spare it the legacy-mojibake repair (ticket #53) -- but only one | ||
| 100 | # actually born here. The task refs of a half-board this run is completing | ||
| 101 | # were written by some earlier karr, quite possibly 0.402 or older, and | ||
| 102 | # stamping asserts the opposite of what they carry: the read-path repair | ||
| 103 | # stops running, every old card turns to mojibake, and `karr repair` then | ||
| 104 | # reports the board as up to date and declines to fix it (#132). Say nothing | ||
| 105 | # instead, which leaves both the repair on read and `karr repair --yes` | ||
| 106 | # available. | ||
| 107 | $store->stamp_encoding_version if $born_here; | ||
| 108 | # And stamp its identity, the thing a pull compares against the remote's to | ||
| 109 | # recognise a swapped board (#95). ensure_, not set_: init also completes | ||
| 110 | # half-boards (#62), and re-keying one that already carries an id would | ||
| 111 | # make every other clone read this board as a foreign one. | ||
| 112 | $store->ensure_board_id; | ||
| 113 | |||
| 114 | print "Initialized karr board in refs/karr/\n" unless $self->json; | ||
| 115 | |||
| 116 | # Completing a half-board is a different event from creating one, and the | ||
| 117 | # user has to be told which one just happened: the tasks that were already | ||
| 118 | # there are still there, and the board is still on the old encoding contract | ||
| 119 | # because this run had no business claiming otherwise (#132). | ||
| 120 | if ( !$born_here ) { | ||
| 121 | my @ids = $store->git->list_task_refs; # returns through sort: no scalar context | ||
| 122 | my $tasks = scalar @ids; | ||
| 123 | unless ($self->json) { | ||
| 124 | print $tasks == 1 | ||
| 125 | ? "Completed a half-board: the 1 task ref already here was kept.\n" | ||
| 126 | : "Completed a half-board: the $tasks task refs already here were kept.\n"; | ||
| 127 | print "Left refs/karr/meta/encoding unstamped, so those refs keep being read the way\n" | ||
| 128 | . "they were written; 'karr repair' says whether they need migrating.\n" | ||
| 129 | if $store->git->board_is_legacy_encoded; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | # The materialized file view (config.yml + tasks/) is a disposable view of the | ||
| 134 | # canonical refs and must never be committed. Ensure the board-root .gitignore | ||
| 135 | # covers it, appending idempotently -- kanban-md does the same at init time. | ||
| 136 | # | ||
| 137 | # Unless the project got there first: `tasks/` and `config.yml` at a | ||
| 138 | # repository root are perfectly ordinary names for a project to already use, | ||
| 139 | # and git applies no ignore rule to a file it already tracks. The entry would | ||
| 140 | # therefore change nothing at all while telling every later reader that karr | ||
| 141 | # owns a path the project owns -- and it would say so right where `karr | ||
| 142 | # materialize` refuses to write, for that very reason (tickets #48, #89). Say | ||
| 143 | # nothing rather than something untrue. | ||
| 144 | my @owned = $store->project_owned_view_paths($root); | ||
| 145 | my @ignored; | ||
| 146 | if (@owned) { | ||
| 147 | print "Left .gitignore alone: git already tracks content at " | ||
| 148 | . join( ', ', @owned ) . ".\n" | ||
| 149 | . "Those paths belong to the project, not to karr's file view, so karr is " | ||
| 150 | . "not\nclaiming them here.\n" | ||
| 151 | unless $self->json; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | @ignored = $store->ensure_gitignore( $root->stringify ); | ||
| 155 | print "Added .gitignore entries for the file view: " . join( ', ', @ignored ) . "\n" | ||
| 156 | if @ignored && !$self->json; | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($self->claude_skill) { | ||
| 160 | $self->_install_claude_skill( $root, \%shipped ); | ||
| 161 | } | ||
| 162 | |||
| 163 | # --json reports the board name and the .gitignore entries this run added, | ||
| 164 | # and nothing else on stdout (ticket #268). The remote-probe warning above | ||
| 165 | # goes to STDERR, so the JSON stream stays clean. | ||
| 166 | if ($self->json) { | ||
| 167 | $self->print_json( | ||
| 168 | { board => { name => $effective->{board}{name} }, gitignore => \@ignored } ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # `git clone` does not carry refs/karr/*, so a fresh clone of a repository | ||
| 173 | # whose board lives on origin holds nothing under refs/karr/ -- exactly what a | ||
| 174 | # repository that never had a board looks like. init used to answer that by | ||
| 175 | # writing a second board with its own refs/karr/meta/board-id, and the next | ||
| 176 | # sync then hit the board-identity guard (#95) with "the remote is a different | ||
| 177 | # board": true, unhelpful, and too late, since the user is by then holding two | ||
| 178 | # boards and has to work out which one is theirs (#182). | ||
| 179 | # | ||
| 180 | # So ask the remote first, through the same bounded ls-remote probe the read | ||
| 181 | # path takes before it refuses (App::karr::Git/remote_has_board, reached from | ||
| 182 | # App::karr::Role::BoardDiscovery/_autofetch_board). Ask, and only ask: the | ||
| 183 | # read path fetches at this point because a read has nothing to lose and no | ||
| 184 | # other way to answer the question it was given, while init writes, and a | ||
| 185 | # writing command that pulls a whole board in unasked is a bigger surprise | ||
| 186 | # than the one it prevents. Refusing and naming `karr sync` leaves that fetch | ||
| 187 | # where the user can see it. | ||
| 188 | # | ||
| 189 | # Only a remote that actually advertises refs/karr/* refuses. Every other | ||
| 190 | # answer -- no remote configured, an unreachable one, no git CLI, no answer | ||
| 191 | # inside the probe's budget -- lets init through, because init has to work | ||
| 192 | # offline and a question that could not be put is not evidence of a board. | ||
| 193 | # KARR_NO_AUTO_FETCH is deliberately not consulted: it switches off an | ||
| 194 | # implicit fetch on the read path, this fetches nothing, and someone who set | ||
| 195 | # it so that reads stay local has not thereby asked for a second board. | ||
| 196 | # --new-board is the opt-out here, and it skips the round trip as well. | ||
| 197 | # | ||
| 198 | # Board refs already here do not change the answer, half-board (#62) included. | ||
| 199 | # A half-board plus a board on the remote is not a half-board to complete: | ||
| 200 | # completing it stamps refs/karr/meta/board-id, this repository's board -- the | ||
| 201 | # one on origin -- already carries one, and the next sync is back in #95. | ||
| 202 | # `karr sync` brings in the config those stray refs are missing and merges the | ||
| 203 | # id counter forward (#172), which completes the same board without minting a | ||
| 204 | # rival identity for it. | ||
| 205 | sub _refuse_if_remote_has_board { | ||
| 206 | my ($self) = @_; | ||
| 207 | |||
| 208 | my $git = $self->store->git; | ||
| 209 | my $advertised = $git->remote_has_board; | ||
| 210 | |||
| 211 | if ( !defined $advertised ) { | ||
| 212 | # The probe could not ask, and init is about to create a board that may | ||
| 213 | # turn out to be the second one. The user waited for this round trip, so | ||
| 214 | # say in one line what it could not establish -- on STDERR, in front of | ||
| 215 | # init's own output on STDOUT. | ||
| 216 | my $why = $git->last_error // 'unknown error'; | ||
| 217 | $why =~ s/\s*\n.*//s; | ||
| 218 | print STDERR "karr: could not ask the remote whether it already has a board: $why\n"; | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | return unless $advertised; | ||
| 222 | |||
| 223 | user_error( | ||
| 224 | "This repository already has a board on origin: 'git clone' does not fetch\n" | ||
| 225 | . "refs/karr/*, so a fresh clone looks like it has none until the board is\n" | ||
| 226 | . "brought in. Run 'karr sync' to fetch it.\n" | ||
| 227 | . "Initializing here would start a second board beside it, and the next sync\n" | ||
| 228 | . "would refuse the two as different boards. Run 'karr init --new-board' if an\n" | ||
| 229 | . "independent board in this clone is really what you want." | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | # $shipped is what execute looked up before its first write: skill name => | ||
| 234 | # { relative path => content }, the shape `karr skill install` builds too. | ||
| 235 | sub _install_claude_skill { | ||
| 236 | my ($self, $root, $shipped) = @_; | ||
| 237 | my $base = $root->child('.claude/skills'); | ||
| 238 | my @installed; | ||
| 239 | |||
| 240 | # Every skill of the set, as App::karr::Role::SkillFile lists them -- the | ||
| 241 | # same list `karr skill install` walks, so the two cannot install different | ||
| 242 | # sets. | ||
| 243 | for my $name ($self->_skill_names) { | ||
| 244 | my $skill_dir = $base->child($name); | ||
| 245 | # An unwritable .claude is the project's layout, not a karr bug: Path::Tiny | ||
| 246 | # would otherwise report this file and line at the user (#77). Kept here | ||
| 247 | # rather than left to the mkpath inside _write_skill_files, which would | ||
| 248 | # report the same failure as "Could not write .../SKILL.md": at that point | ||
| 249 | # nothing has been written and nothing could be, because the directory is | ||
| 250 | # what karr could not create. Saying so is this command's own contract | ||
| 251 | # (t/120). | ||
| 252 | eval { $skill_dir->mkpath; 1 } | ||
| 253 | or user_error( "Could not create $skill_dir: ", clean_error($@) ); | ||
| 254 | |||
| 255 | # Through App::karr::Role::SkillFile, not spew_utf8: this is the same | ||
| 256 | # directory `karr skill install --agent claude-code` writes, and in a | ||
| 257 | # checkout wired up by manage-skills its SKILL.md is one link of a hardlink | ||
| 258 | # chain. spew_utf8 renames a temp file over the target, which breaks this | ||
| 259 | # project out of that chain and leaves every other one on the old inode | ||
| 260 | # with the old text -- the bug fixed in `karr skill` as ticket #142 and | ||
| 261 | # left standing here until #145. The role is also where the read-only | ||
| 262 | # fallback and its warning live, so there is one description of how a | ||
| 263 | # skill gets written rather than two that drift. | ||
| 264 | $self->_write_skill_files( $skill_dir, $shipped->{$name} ); | ||
| 265 | push @installed, $skill_dir->child('SKILL.md'); | ||
| 266 | } | ||
| 267 | |||
| 268 | # The retired single skill the set replaces goes, as `karr skill install` | ||
| 269 | # removes it -- after the new ones are written, so a failed write leaves the | ||
| 270 | # old skill rather than none. | ||
| 271 | my @removed = $self->_remove_retired_skills($base); | ||
| 272 | |||
| 273 | return if $self->json; | ||
| 274 | # The paths it wrote, not a fixed relative string: this installs into the | ||
| 275 | # root of the repository being initialized, which --dir can put in a | ||
| 276 | # different tree than the one the caller stands in, and ".claude/skills/..." | ||
| 277 | # is true of every tree at once. `karr skill install` printed the same | ||
| 278 | # non-answer and was fixed with it (#226, point 3). | ||
| 279 | print "Installed Claude Code skill to $_\n" for @installed; | ||
| 280 | print "Removed retired Claude Code skill $_\n" for @removed; | ||
| 281 | } | ||
| 282 | |||
| 283 | 1; | ||
| 284 | |||
| 285 | __END__ | ||