fix(worktree): allow reusing an existing branch for a new session#3262
fix(worktree): allow reusing an existing branch for a new session#3262terrytangyuan wants to merge 1 commit into
Conversation
The worktree pre-check rejected any branch that already existed, even when no worktree had it checked out. This blocked users from starting a new session on a branch from a previous (ended) session. Narrow the check to only reject branches actively checked out in another worktree. Signed-off-by: Yuan Tang <terrytangyuan@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts the host-side git worktree creation logic to allow starting a new session on an already-existing branch as long as that branch is not currently checked out in any worktree, unblocking the “reuse branch after prior session ended” workflow.
Changes:
- Add a worktree lookup helper to detect whether a branch is currently checked out in another worktree.
- Update
create_worktree()to reuse an existing branch (no-b) when safe, and error only when the branch is actively checked out elsewhere. - Update unit tests to cover the new reuse behavior and the still-invalid “already checked out” case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
omnigent/host/git_worktree.py |
Reworks the branch pre-check to allow reusing existing branches when not checked out, and adds a helper to find where a branch is checked out. |
tests/host/test_git_worktree.py |
Updates/extends tests to validate reuse of an existing branch and reuse after a worktree is removed. |
| if base_branch is not None: | ||
| _ensure_base_resolvable(repo_root, base_branch) |
|
/review |
|
Related issue
N/A
Summary
The worktree pre-check in
create_worktree()rejected any branch that already existed, even when no worktree had it checked out. This blocked users from starting a new session on a branch from a previous (ended) session — they had to pick a different name every time.This narrows the check: only reject when the branch is actively checked out in another worktree (which would cause two sessions to clobber each other). When the branch exists but isn't checked out anywhere, reuse it with
git worktree add <path> <branch>(no-b).Test Plan
python -m pytest tests/host/test_git_worktree.py -v— all 35 tests pass.test_create_worktree_duplicate_branch_checked_out_fails— still rejects when branch is checked out in another worktree.test_create_worktree_existing_branch_no_worktree_reuses— a branch with no worktree is reused successfully.test_create_worktree_reuses_after_worktree_removed— covers the exact user scenario (create → remove worktree keeping branch → create again on same branch).Demo
N/A
Type of change
Test coverage
Coverage notes
Changelog
Starting a new session on a branch from a previous session no longer fails with "branch already exists"