Fix git worktree add DWIM when base exists only on remote - #62
Open
leogdion wants to merge 1 commit into
Open
Conversation
`git worktree add --no-track -b <new> <dir> <base>` DWIMs a base naming a branch that exists only on the remote: it creates a local branch tracking `origin/<base>` and overrides `-b <new>` entirely. The worktree came up on the base branch, the requested branch was never created, a stray local ref was left behind to go stale as origin advanced, and `add` exited 0. `cmd_add` now resolves the base through `_base_sha` — local commit-ish, else `origin/<base>` — and passes the sha, so there is nothing for the DWIM to latch onto and no local ref named after the base. A base that resolves to nothing is now a clear error instead of a worktree on something else. After creating the worktree, `add` asserts its HEAD really is the requested branch, so a future DWIM change cannot silently reintroduce this. Smoke coverage for the remote-only base, the explicit `origin/<base>` spelling, and an unresolvable base; README and AGENTS.md updated.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical bug where
git worktree addwith a base branch existing only on the remote would silently create a worktree on the wrong branch, leave a stray local ref, and exit successfully.Root Cause
git worktree add -b <new> <dir> <base>performs DWIM (Do What I Mean) on the base argument. When<base>is a bare name matching a branch that exists only on the remote, git interprets it as "create a local branch trackingorigin/<base>" and overrides the-b <new>flag entirely. This results in:<base>instead of<new><new>branch never created<base>ref left behind to go staleThe
--no-trackflag does not prevent this, as it only governs the upstream, not the branch name.Changes
git-trees:
_base_sha()helper function that resolves a base argument to a commit SHA by:origin/<arg>(handles remote-only branches)cmd_add()to:_base_sha()to resolve the base before creating the worktreegit worktree addinstead of the bare name (eliminates DWIM)HEADmatches the requested branch nameTests (smoke.sh):
origin/<base>commitorigin/<base>form behaves identicallyDocumentation:
-b" section explaining the issue and solutionImplementation Details
The fix uses two layers of defense:
git worktree addsees it, eliminating the DWIM triggerHEADmatches the requested branch, catching any future regressionsThis approach is robust because:
https://claude.ai/code/session_01XvKtUT9b739chxgKFLKAKL