Skip to content

Prevent branch name reuse across PR history - #250

Merged
AmethystLiang merged 1 commit into
mainfrom
worktree-no-branch-reusing-name
Apr 1, 2026
Merged

AmethystLiang merged 1 commit into
mainfrom
worktree-no-branch-reusing-name

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Problem

When creating a new worktree with a computed branch name, if that branch name had been used before (e.g., a PR was created and merged), the UI would immediately inherit the stale PR status. This happened because the UI resolves PR state by branch name alone, without checking if the PR is actually active or has been closed/merged.

Additionally, the old code would silently suffix the branch name if it already existed locally, which violated the principle of "no silent renames" and could cause confusing behavior.

Solution

  • Branch conflict detection: Check if the branch name already exists locally, remotely, or in PR history before allowing creation. If any conflict is found, reject the worktree creation with a clear error message.

    • Local check: git show-ref refs/heads/<name>
    • Remote check: git for-each-ref refs/remotes/ with proper path parsing
    • PR history check: getPRForBranch() with graceful fallback on API failures
  • Error handling improvements:

    • Wrap getPRForBranch in try/catch so GitHub API outages don't block worktree creation
    • Fix remote branch name matching to use full path comparison (refs/remotes//) instead of suffix matching, preventing false positives for branch names with / separators
    • Wrap updateWorktreeMeta in try/catch so meta-update failures don't trap the dialog in an error state for an already-created worktree
  • UI feedback: Display inline error messages when branch creation fails, allowing users to pick a different name.

Testing

  • Unit tests added for getBranchConflictKind() covering local, remote, and no-conflict scenarios
  • Dialog component now handles and displays creation errors inline

- Reject worktree creation if the computed branch name already exists locally, on a remote, or in PR history. This prevents a fresh worktree from inheriting a stale/merged PR immediately after creation.
- Wrap getPRForBranch in try/catch so GitHub API failures don't block worktree creation
- Fix remote branch matching to compare full branch name after refs/remotes/<remote>/ prefix, preventing false positives
- Wrap updateWorktreeMeta in try/catch so meta-update failures don't leave the dialog in an error state for an already-created worktree
@AmethystLiang
AmethystLiang merged commit 5b3f132 into main Apr 1, 2026
1 check passed
@AmethystLiang
AmethystLiang deleted the worktree-no-branch-reusing-name branch April 1, 2026 20:24
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
- Reject worktree creation if the computed branch name already exists locally, on a remote, or in PR history. This prevents a fresh worktree from inheriting a stale/merged PR immediately after creation.
- Wrap getPRForBranch in try/catch so GitHub API failures don't block worktree creation
- Fix remote branch matching to compare full branch name after refs/remotes/<remote>/ prefix, preventing false positives
- Wrap updateWorktreeMeta in try/catch so meta-update failures don't leave the dialog in an error state for an already-created worktree
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.

1 participant