Skip to content

refactor(wt): move wtclean's logic into the script - #85

Merged
radiosilence merged 5 commits into
mainfrom
feat/wtclean-inline
Aug 7, 2026
Merged

refactor(wt): move wtclean's logic into the script#85
radiosilence merged 5 commits into
mainfrom
feat/wtclean-inline

Conversation

@radiosilence

Copy link
Copy Markdown
Owner

What this does

_wt_clean, _wt_pr_cache and _wt_fan lived in the plugin with bin/wtclean as their only caller — about 150 lines that every interactive shell parsed and never ran. They move into the script. Only the helpers something else actually shares stay behind: _wt_root, _wt_base, _wt_named, _wt_pr_state, used by wtrm, _wt_core and _wt_branches.

Why

The split forced arguments through a round trip. zarg parsed --dry-run into $dry_run, then the script re-encoded it so a function could parse it a second time:

# Not ${dry_run:+-n}: zarg sets flags to the string 0 or 1, and 0 is non-empty.
local -a args; (( dry_run )) && args=(-n)
_wt_clean $args

That comment documents a gotcha which only exists because of the file boundary. With the body in the script it reads $dry_run directly and both the array and the comment disappear.

The justification in the old header — "the logic lives in the plugin next door so wtrm can share _wt_pr_state" — didn't hold up. _wt_pr_state stays in the plugin either way; that was never an argument for the body being there.

The one thing that had to stay put

_wt_prs keeps its typeset -gA beside _wt_pr_state, not with _wt_pr_cache which fills it. Moving it broke wtrm for every branch with a slash in the name:

_wt_pr_state:1: division by zero

An undeclared associative array makes ${_wt_prs[feat/x]} an arithmetic subscript, so zsh evaluates feat/x as a division of two unset variables. Silent for flat branch names, fatal for feat/…. Found by testing wtrm's path after the move, not by reading the diff.

How to confirm

wtclean -n
wtclean --dyr-run     # rc=2

Verified: identical output through the script and through the plugin shim; unknown flags still refused by zarg; and _wt_pr_state resolves feat/wt-zarg, feat/linux-bootstrap and flat names correctly with only the plugin loaded, which is the path wtrm takes.

Merged main in to pick up #83.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SdK2s5sJBVLc5Edhm3cZ3H

radiosilence and others added 5 commits August 7, 2026 13:03
wtclean moved to zarg in #82 and the other four were left hand-rolled,
which is exactly the drift zarg exists to prevent. One declaration now
yields the parser, --help and completions for all five.

Branch arguments reuse _wt_branches, the function already backing the wt
and wtrm compdefs, rather than a second way to list worktrees.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SdK2s5sJBVLc5Edhm3cZ3H
The plugin defined _wt_clean, _wt_pr_cache and _wt_fan, and bin/wtclean
was their only caller — every interactive shell parsed ~150 lines it
would never run. Worse, zarg parsed --dry-run into $dry_run and the
script re-encoded it back into -n so the function could parse it again,
with a comment explaining the gotcha that round trip created.

The genuinely shared helpers (_wt_root, _wt_base, _wt_named,
_wt_pr_state) stay in the plugin, where wtrm and the picker use them.

_wt_prs keeps its declaration next to _wt_pr_state rather than moving
with the code that fills it: an undeclared associative array makes
${_wt_prs[feat/x]} an arithmetic subscript, so "feat/x" is evaluated as
a division and any slashed branch name dies with "division by zero".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SdK2s5sJBVLc5Edhm3cZ3H
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SdK2s5sJBVLc5Edhm3cZ3H
@radiosilence
radiosilence merged commit 148ffcc into main Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant