Skip to content

fix(worktree): clean error when restoring/promoting onto an occupied branch - #3606

Closed
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-occupied-branch-worktree-restore
Closed

fix(worktree): clean error when restoring/promoting onto an occupied branch#3606
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-occupied-branch-worktree-restore

Conversation

@posthog

@posthog posthog Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Problem

Restoring a suspended task (or promoting a local task to worktree mode) failed with a raw git worktree add exited with code 128 error whenever the task's branch was still checked out in another worktree. createWorktreeForExistingBranch only verified the branch existed and the worktree name/path was free — never whether the branch was already live elsewhere — so git bailed and the raw failure escaped through the restore/promote paths into error tracking as an uncaught exception.

The workspace creation path already caught the "is already used by worktree" message and turned it into a friendly error, but the restore (suspension/unarchive) and promote paths did not. This hit the team dogfooding PostHog Code — low volume but real, and the raw exception spammed error tracking even though the restore UI already degrades to a helpful toast.

Changes

Detect the occupied branch up front in createWorktreeForExistingBranch and throw a clean, actionable message (Branch '<x>' is already used by worktree at '<path>') instead of letting the opaque git exit-128 error surface. The wording deliberately keeps the "is already used by worktree" substring, so the existing consumers that key on it — the workspace creation path and the restore toast — keep recognising the case unchanged.

Fixing it at this shared chokepoint gives the restore and promote paths the same occupied-branch handling the creation path already had, and also covers the archive/unarchive path, in one place. The check degrades gracefully: if the worktree list can't be read, it falls through to the prior behaviour rather than blocking creation.

How did you test this?

  • Added a unit test asserting createWorktreeForExistingBranch throws a clean occupied-branch error (matching is already used by worktree, and not exited with code 128) when the branch is already checked out.
  • pnpm --filter @posthog/git typecheck and biome lint on the changed files pass.
  • The git-fixture tests in this file can't execute in the automated environment (it blocks git commit, which every fixture's beforeEach seed relies on — pre-existing tests fail the same way); the new test mirrors the existing lifecycle tests and runs under CI's real git.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code from this inbox report.

…branch

createWorktreeForExistingBranch only checked that the branch existed and
that the worktree name/path was free -- never whether the branch was
already checked out in another worktree. When it was, git failed with a
raw "git worktree add exited with code 128" error that escaped through
the restore and promote paths into error tracking.

The workspace creation path already caught the "is already used by
worktree" message and surfaced a friendly error, but the restore
(suspension/archive) and promote paths did not.

Detect the occupied branch up front in createWorktreeForExistingBranch
and throw a clean, actionable message instead. The wording mirrors git's
own so existing consumers that key on the "is already used by worktree"
substring keep recognising the case. Fixing it at this shared chokepoint
covers all three callers (create, restore, promote).

Generated-By: PostHog Code
Task-Id: c5eefa8e-bed0-4490-bb76-948ba7260998
@trunk-io

trunk-io Bot commented Jul 21, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit ed1e178.

@github-actions

Copy link
Copy Markdown

This PR has had no activity for 7 days and has been marked stale. We are moving to the monorepo and tightening PR staleness in preparation, so it will be closed in 7 days if no further activity occurs.

@github-actions github-actions Bot added the stale No recent changes to PR label Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale No recent changes to PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant