Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions src/github/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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 `<repo>.worktrees/<branch>`
// 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),
Expand All @@ -89,23 +74,23 @@ 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
});
}
}
);

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');
Expand Down