Skip to content

Make switch diagnostics describe the requested operation - #4349

Merged
max-sixty merged 5 commits into
mainfrom
codex/switch-operation-messages
Oct 10, 2026
Merged

max-sixty merged 5 commits into
mainfrom
codex/switch-operation-messages

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

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.

This was written by Codex on behalf of max-sixty

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/commands/worktree/switch.rs Resolved

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/git/repository/branch.rs Resolved
@max-sixty
max-sixty merged commit becca5f into main Oct 10, 2026
39 checks passed
@max-sixty
max-sixty deleted the codex/switch-operation-messages branch October 10, 2026 22:45
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.

2 participants