diff --git a/src/github/worktree.ts b/src/github/worktree.ts index 3c6cb7db0f..d95543a2fa 100644 --- a/src/github/worktree.ts +++ b/src/github/worktree.ts @@ -3,7 +3,6 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import * as path from 'path'; import * as vscode from 'vscode'; import { FolderRepositoryManager } from './folderRepositoryManager'; import { PullRequestModel } from './pullRequestModel'; @@ -41,26 +40,8 @@ export async function checkoutPRInWorktree( */ telemetry.sendTelemetryEvent('pr.checkoutInWorktree'); - // Prepare for operations - const repoRootPath = repositoryToUse.rootUri.fsPath; - const parentDir = path.dirname(repoRootPath); - const repoFolderName = path.basename(repoRootPath); - const defaultWorktreePath = path.join(parentDir, `${repoFolderName}.worktrees`, `pr-${pullRequestModel.number}`); const branchName = prHead.ref; const remoteName = pullRequestModel.remote.remoteName; - - // Ask user for worktree location first (not in progress) - const worktreeUri = await vscode.window.showSaveDialog({ - defaultUri: vscode.Uri.file(defaultWorktreePath), - title: vscode.l10n.t('Select Worktree Location'), - saveLabel: vscode.l10n.t('Create Worktree'), - }); - - if (!worktreeUri) { - return; // User cancelled - } - - const worktreePath = worktreeUri.fsPath; const trackedBranchName = `${remoteName}/${branchName}`; try { @@ -69,8 +50,12 @@ export async function checkoutPRInWorktree( throw new Error(vscode.l10n.t('Git worktree API is not available. Please update VS Code to the latest version.')); } - // Start progress for fetch and worktree creation - await vscode.window.withProgress( + // Fetch the PR branch and create the worktree. The Git extension's + // createWorktree API picks the worktree path itself (using the user's + // saved worktree root or the default `.worktrees/` + // convention) and returns the resulting path, so we don't need any + // custom picker UI here. + const worktreePath = await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, title: vscode.l10n.t('Creating worktree for Pull Request #{0}...', pullRequestModel.number), @@ -89,16 +74,14 @@ export async function checkoutPRInWorktree( branchExists = false; } - // Use the git extension's createWorktree API - // If branch already exists, don't specify the branch parameter to avoid "branch already exists" error + // Use the git extension's createWorktree API. If branch already exists, + // don't specify the branch parameter to avoid "branch already exists" error. if (branchExists) { - await repositoryToUse.createWorktree!({ - path: worktreePath, + return await repositoryToUse.createWorktree!({ commitish: branchName }); } else { - await repositoryToUse.createWorktree!({ - path: worktreePath, + return await repositoryToUse.createWorktree!({ commitish: trackedBranchName, branch: branchName }); @@ -106,6 +89,8 @@ export async function checkoutPRInWorktree( } ); + const worktreeUri = vscode.Uri.file(worktreePath); + // Ask user how they want to open the worktree (modal dialog) const openInNewWindow = vscode.l10n.t('New Window'); const openInCurrentWindow = vscode.l10n.t('Current Window');