Skip to content

Add worktree removal option when deleting PR branches#8559

Merged
alexr00 merged 15 commits intomainfrom
copilot/fix-worktree-merge-issue
Mar 3, 2026
Merged

Add worktree removal option when deleting PR branches#8559
alexr00 merged 15 commits intomainfrom
copilot/fix-worktree-merge-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 2, 2026

  • New setting githubPullRequests.defaultDeletionMethod.selectWorktree
  • Worktree detection via getWorktreeForBranch() using git extension API
  • Worktree removal via removeWorktree() using git extension API
  • Quick pick integration with "Remove worktree {path}" option
  • Auto-delete integration respecting selectWorktree preference
  • Ordering constraint: worktree removal before branch deletion
  • Workspace folder safety: hide worktree option when it's a workspace folder
  • Fix lint error: remove duplicate unused isWorktreeInWorkspace at top level
  • Case-insensitive path comparison for isWorktreeInWorkspace on Windows
Original prompt

This section details on the original issue you should resolve

<issue_title>Deleting a worktree after merging a PR into a branch mixes the main branch with the worktree</issue_title>
<issue_description>## Description

After merging a PR that was created from a worktree (test_1) into a branch, a button will appear for deleting the brach and its remote. Clicking on that deletes the branch and its remote that was associated with the worktree but mixes the main branch and the worktree's folder together somehow.

git worktree list

/Volumes/Work/git-projects/own/sandbox c598839 [develop]
/Volumes/Work/git-projects/own/sandbox.worktrees/test_1 1858442 [main]

Steps to Reproduce

  • Create a main branch
  • Create a develop branch from main
  • Create a worktree from develop
  • Switch to the worktree then submit some changes and push to origin
  • Create a PR from the worktree
  • Merge the PR into the develop branch
  • Click on the delete branch button
  • Delete the branch (worktree this time) and its remote
  • Run this command: git worktree list
  • The worktree folder still exists and somehow the main branch is associated with it
  • For restoring the main branch original behaviour we have to delete the worktree folder manually then prune the worktree that's folder doesn't exist anymore with this command git worktree prune -v

Expected Behavior

The worktree folder, its branch & remote are deleted and no previous branches are associated with the deleted worktree's folder.

Actual Behavior

The deleted worktree's folder remains on disk & associated with the main branch.

Environment

VS Code version: 1.109.3
Extension version: 0.128.0
OS: macOS 26.3

</issue_description>

<agent_instructions>In the dropdown to choose which branches to delete, we should also include the worktree if it is a worktree. Also add a setting githubPullRequests.defaultDeletionMethod.selectWorktree, similar to the githubPullRequests.defaultDeletionMethod.selectLocalBranch setting. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 3 commits March 2, 2026 10:32
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
- Add SELECT_WORKTREE constant in settingKeys.ts
- Add githubPullRequests.defaultDeletionMethod.selectWorktree setting in package.json
- Add NLS description in package.nls.json
- Add getWorktreeForBranch() and removeWorktree() methods to FolderRepositoryManager
- Add worktree option to SelectedAction type and quick pick dropdown
- Add worktree deletion to auto-delete flow (autoDeleteBranchesAfterMerge)
- Ensure worktree removal happens before branch deletion in performBranchDeletion

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with worktree deletion after PR merge Add worktree removal option when deleting PR branches Mar 2, 2026
Copy link
Copy Markdown
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot There are build failures:

extension:webworker:
  asset test/index.js 6.61 MiB [emitted] (name: test/index) 1 related asset
  asset extension.js 5.59 MiB [emitted] (name: extension) 1 related asset

  ERROR in ./src/github/folderRepositoryManager.ts 4:0-41
  Module not found: Error: Can't resolve 'child_process' in '/Users/runner/work/1/s/src/github'

  extension:webworker compiled with 1 error in 63667 ms

Move git worktree operations (child_process/util) into dedicated
env/node/gitWorktree.ts and env/browser/gitWorktree.ts modules,
following the existing env pattern. The webpack alias ensures the
browser/no-op version is used for the webworker target.

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot There are build failures:

extension:webworker:...

Fixed in bf66dd3. Moved the child_process and util usage out of folderRepositoryManager.ts into dedicated src/env/node/gitWorktree.ts and src/env/browser/gitWorktree.ts modules, following the existing env pattern used for net and ssh. Added the webpack alias so the webworker target uses the browser no-op version. All three webpack targets (node, webworker, webviews) now compile successfully.

Copy link
Copy Markdown
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I updated the git API. Use that, don't exec git directly.

- Use repository.state.worktrees to find worktrees for a branch
- Use repository.deleteWorktree() to remove worktrees
- Add Worktree interface to api.d.ts
- Remove env/node/gitWorktree.ts and env/browser/gitWorktree.ts
- Remove webpack alias for gitWorktree

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot I updated the git API. Use that, don't exec git directly.

Done in fb1e543. Replaced child_process exec with the git API — repository.state.worktrees for lookup and repository.deleteWorktree() for removal. Removed src/env/node/gitWorktree.ts, src/env/browser/gitWorktree.ts, and the webpack alias since they're no longer needed.

Copy link
Copy Markdown
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, only show the "delete worktree" option when the work tree isn't one of the workspace folders.

Copilot AI and others added 3 commits March 2, 2026 15:18
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
…pace check

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot, only show the "delete worktree" option when the work tree isn't one of the workspace folders.

Done in 6ec5bea. Added isWorktreeInWorkspace() helper that checks vscode.workspace.workspaceFolders — the "Remove worktree" option is now hidden in both the quick pick dialog and auto-delete path when the worktree is a workspace folder.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot, only show the "delete worktree" option when the work tree isn't one of the workspace folders.

Done in 7848e56. Added isWorktreeInWorkspace() check that compares the worktree path against vscode.workspace.workspaceFolders using isDescendant for cross-platform path handling. The "Remove worktree" option is now hidden in both the quick pick dialog and auto-delete flow when the worktree is a workspace folder.

Copy link
Copy Markdown
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot lint failure:

/Users/runner/work/1/s/src/github/pullRequestReviewCommon.ts
  32:10  error  'isWorktreeInWorkspace' is defined but never used  @typescript-eslint/no-unused-vars

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot lint failure:

Fixed in 7666cb3. Removed the duplicate unused top-level isWorktreeInWorkspace function and the isDescendant import. The inner function at the namespace level (which is the one actually called) remains.

Comment thread src/github/pullRequestReviewCommon.ts Outdated
};

function isWorktreeInWorkspace(worktreePath: string): boolean {
return !!vscode.workspace.workspaceFolders?.some(folder => folder.uri.fsPath === worktreePath);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot On windows this should be case insensitive compare

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fe73c84. Now uses process.platform === 'win32' to apply .toLowerCase() on both paths before comparing, so Windows paths like C:\Users\... and c:\users\... match correctly.

…dows

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
@alexr00 alexr00 marked this pull request as ready for review March 3, 2026 09:52
Copilot AI review requested due to automatic review settings March 3, 2026 09:52
@vs-code-engineering vs-code-engineering bot added this to the March 2026 milestone Mar 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class worktree cleanup support to the PR branch deletion flow so that deleting a merged PR branch can also remove its associated git worktree (avoiding stale worktree folders being left behind and potentially re-associated with other branches).

Changes:

  • Adds a new setting githubPullRequests.defaultDeletionMethod.selectWorktree and surfaces a “Remove worktree {path}” option in the delete-branch quick pick (hidden when the worktree is a workspace folder).
  • Implements worktree detection/removal via the git extension API (repository.state.worktrees + repository.deleteWorktree) and enforces removal-before-branch-deletion ordering.
  • Updates internal type definitions (src/api/api.d.ts) to include worktree-related API surface (plus additional unrelated VS Code proposed chat type updates).

Reviewed changes

Copilot reviewed 5 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/github/pullRequestReviewCommon.ts Adds worktree option to branch deletion quick pick, adds selectWorktree preference support, and ensures worktree removal happens before branch deletion.
src/github/folderRepositoryManager.ts Adds worktree lookup/removal helpers backed by git extension API.
src/common/settingKeys.ts Adds SELECT_WORKTREE setting key constant.
src/api/api.d.ts Extends git API typings with Worktree, RepositoryState.worktrees, and Repository.{createWorktree,deleteWorktree}.
src/@types/vscode.proposed.chatSessionsProvider.d.ts Updates proposed chat session typings (scope-unrelated to worktree feature).
src/@types/vscode.proposed.chatParticipantPrivate.d.ts Updates proposed chat participant typings (scope-unrelated to worktree feature).
src/@types/vscode.proposed.chatParticipantAdditions.d.ts Updates proposed chat participant typings (scope-unrelated to worktree feature).
package.nls.json Adds localized description for the new selectWorktree setting.
package.json Contributes the new githubPullRequests.defaultDeletionMethod.selectWorktree setting.
Comments suppressed due to low confidence (1)

src/github/pullRequestReviewCommon.ts:417

  • The new worktree deletion path can result in deletedBranchTypes containing only 'worktree', but the notification logic below only distinguishes local vs remote deletions and will currently fall into the "Deleted remote branch" message in that case. Please update the notification (and any other downstream handling) to explicitly handle worktree-only deletion and/or include worktree removal in the messaging.
		const worktreeAction = selectedActions.find(a => a.type === 'worktree');
		if (worktreeAction?.worktreePath) {
			await folderRepositoryManager.removeWorktree(worktreeAction.worktreePath);
			deletedBranchTypes.push(worktreeAction.type);
		}

Comment thread src/github/pullRequestReviewCommon.ts Outdated
Comment thread src/github/folderRepositoryManager.ts
dmitrivMS
dmitrivMS previously approved these changes Mar 3, 2026
@alexr00 alexr00 enabled auto-merge (squash) March 3, 2026 12:58
@alexr00 alexr00 merged commit d39d22b into main Mar 3, 2026
6 checks passed
@alexr00 alexr00 deleted the copilot/fix-worktree-merge-issue branch March 3, 2026 13:05
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.

Deleting a worktree after merging a PR into a branch mixes the main branch with the worktree

4 participants