Skip to content

Commit 04c416a

Browse files
Copilotalexr00
andcommitted
refactor: use git extension API for worktree creation instead of shell execution
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 103da71 commit 04c416a

File tree

2 files changed

+13
-38
lines changed

2 files changed

+13
-38
lines changed

src/api/api.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ export interface Repository {
209209
add(paths: string[]): Promise<void>;
210210
merge(ref: string): Promise<void>;
211211
mergeAbort(): Promise<void>;
212+
213+
createWorktree?(options?: { path?: string; commitish?: string; branch?: string }): Promise<string>;
214+
deleteWorktree?(path: string, options?: { force?: boolean }): Promise<void>;
212215
}
213216

214217
/**

src/commands.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -896,50 +896,22 @@ export function registerCommands(
896896

897897
const worktreePath = worktreeUri.fsPath;
898898

899-
// Create the worktree using a VS Code task
899+
// Create the worktree using the git extension API
900900
progress.report({ message: vscode.l10n.t('Creating worktree at {0}...', worktreePath) });
901901

902902
const trackedBranchName = `${remoteName}/${branchName}`;
903-
const localBranchName = branchName;
904903

905904
try {
906-
// Create a VS Code task to execute the git worktree command
907-
const taskDefinition: vscode.TaskDefinition = {
908-
type: 'shell'
909-
};
910-
911-
const shellExecution = new vscode.ShellExecution('git', [
912-
'worktree', 'add',
913-
'-b', { value: localBranchName, quoting: vscode.ShellQuoting.Strong },
914-
{ value: worktreePath, quoting: vscode.ShellQuoting.Strong },
915-
{ value: trackedBranchName, quoting: vscode.ShellQuoting.Strong }
916-
], {
917-
cwd: repoRootPath
918-
});
919-
920-
const task = new vscode.Task(
921-
taskDefinition,
922-
vscode.TaskScope.Workspace,
923-
vscode.l10n.t('Create Worktree for Pull Request #{0}', pullRequestModel.number),
924-
'git',
925-
shellExecution
926-
);
905+
// Check if the createWorktree API is available
906+
if (!repositoryToUse.createWorktree) {
907+
throw new Error(vscode.l10n.t('Git worktree API is not available. Please update VS Code to the latest version.'));
908+
}
927909

928-
// Execute the task and wait for completion
929-
const taskExecution = await vscode.tasks.executeTask(task);
930-
931-
// Wait for task to complete
932-
await new Promise<void>((resolve, reject) => {
933-
const disposable = vscode.tasks.onDidEndTaskProcess(e => {
934-
if (e.execution === taskExecution) {
935-
disposable.dispose();
936-
if (e.exitCode === 0) {
937-
resolve();
938-
} else {
939-
reject(new Error(vscode.l10n.t('Git worktree command failed with exit code {0}', e.exitCode?.toString() ?? 'unknown')));
940-
}
941-
}
942-
});
910+
// Use the git extension's createWorktree API
911+
await repositoryToUse.createWorktree({
912+
path: worktreePath,
913+
commitish: trackedBranchName,
914+
branch: branchName
943915
});
944916

945917
// Ask user if they want to open the worktree

0 commit comments

Comments
 (0)