Repository navigation
Make switch diagnostics describe the requested operation - #4349
Conversation
worktrunk-bot
left a comment
There was a problem hiding this comment.
codecov/patch is failing. Its only miss is the operational-failure arm of Branch::require_valid_name (the _ => Err(CommandError::from_failed_output(...)) line), which no test can easily reach. The project treats codecov/patch as a merge gate, so this needs either a different approach or an explicit maintainer waiver.
The repo already has an in-process check for the same question: is_valid_branch_name, in the same branch.rs. It is pinned against git check-ref-format by branch_name_matches_git_check_ref_format, and this PR already calls it in validate_worktree_creation. With it, switch.rs would use one validator instead of two, the extra git spawn on every --create would go away, and so would the arm that can't be covered. Then require_valid_name can be deleted. The two checks differ only on names that --branch rejects and the refs/heads/ form accepts, such as HEAD and -x. Those would still fail, but with Git's own worktree add -b error (fatal: 'HEAD' is not a valid branch name) instead of the concise message. If that precision is worth a subprocess, keep the current code and get the coverage gap waived.
worktrunk-bot
left a comment
There was a problem hiding this comment.
git check-ref-format --branch doesn't just validate. It expands @{-N} and prints the result, exiting 0. So wt switch --create @{-1} passes require_valid_name, and exists_locally("@{-1}") returns false. Git's worktree add -b @{-1} then creates a branch named after the previously checked-out branch. I reproduced this with Git directly: after checkout x; checkout master; branch -D x, git worktree add -b '@{-1}' ../wt reports new branch '@{-1}' but creates x. Worktrunk would then report and run hooks against @{-1} while the worktree holds x. When the previous branch still exists, the command fails with Git's raw "already exists" error instead of the concise one. The is_valid_branch_name version this replaces rejected the name (@{ is invalid in refs/heads/…). The inline suggestion requires Git's echoed name to match the input, so the existing subprocess also covers this.
Switch diagnostics now name the requested operation: invalid new branch names get a concise error, missing base references name
--base, and missing path selectors use the worktree selector diagnostic. Existing checkouts with directory changes disabled report their location without claiming the shell switched there. Noninteractive picker failures show the explicit branch command.Branch creation uses Git's own validation and requires the echoed name to match the requested literal name, rejecting checkout-history expressions that would create a different branch. Validation subprocess interruptions retain their native signal. The alias-decline regression exercises a real terminal prompt and checks that declining prevents execution while explicit approval permits it.
Validation: the required pre-merge gate passed on pinned Rust 1.98: 5,207 tests passed, one skipped; Clippy, lockfile, formatting, documentation and doctests passed. Focused creation checks cover invalid names, existing and deleted checkout-history targets, nested and Unicode literal names, and native cancellation without mutation. Independent source review found no remaining issues.