Guard against main repo on target branch before worktree creation#
Problem#
When the main repo is checked out on the target feature branch, git branch -D silently fails, causing git worktree add -b to fatal with "branch already exists". This was the root cause of issue #246 being stuck for 12+ hours.
Task / Link#
Closes #266
Changes#
- Added a branch guard in
WorktreeService.createWorktree()aftergit fetch origin - Detects current branch via
git rev-parse --abbrev-ref HEAD - If the main repo is on the target branch, auto-switches to
masterwith a warning log - If
git checkout masterfails, throws a descriptive error (does not swallow) - Detached HEAD state is treated as safe — no switch attempted
- Added two new unit tests: auto-switch success and checkout failure scenarios
Notes#
If the main repo has uncommitted changes when the conflict is detected, git checkout master will fail and a typed error is thrown — by design per issue scope (surface the error, do not stash).
Testing#
- 13 unit tests pass (11 existing + 2 new)
- New tests cover: auto-switch to master succeeds, and checkout failure throws descriptive error