Skip to content

Commit fb1e543

Browse files
Copilotalexr00
andcommitted
refactor: use git API for worktree operations instead of child_process
- 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>
1 parent 36aa0b5 commit fb1e543

File tree

6 files changed

+28
-74
lines changed

6 files changed

+28
-74
lines changed

src/api/api.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export interface Remote {
5353
readonly isReadOnly: boolean;
5454
}
5555

56+
export interface Worktree {
57+
readonly path: string;
58+
readonly name: string;
59+
readonly ref: string;
60+
readonly main: boolean;
61+
readonly detached: boolean;
62+
}
63+
5664
export { Status } from './api1';
5765

5866
export interface Change {

src/env/browser/gitWorktree.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/env/node/gitWorktree.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/github/folderRepositoryManager.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import { EventType } from '../common/timelineEvent';
6060
import { Schemes } from '../common/uri';
6161
import { AsyncPredicate, batchPromiseAll, compareIgnoreCase, formatError, Predicate } from '../common/utils';
6262
import { PULL_REQUEST_OVERVIEW_VIEW_TYPE } from '../common/webview';
63-
import { getWorktreeForBranch as envGetWorktreeForBranch, removeWorktree as envRemoveWorktree } from '../env/node/gitWorktree';
6463
import { BRANCHES_ASSOCIATED_WITH_PRS, LAST_USED_EMAIL, NEVER_SHOW_PULL_NOTIFICATION, REPO_KEYS, ReposState } from '../extensionState';
6564
import { git } from '../gitProviders/gitCommands';
6665
import { IThemeWatcher } from '../themeWatcher';
@@ -2451,18 +2450,29 @@ export class FolderRepositoryManager extends Disposable {
24512450
return await PullRequestGitHelper.getBranchNRemoteForPullRequest(this.repository, pullRequest);
24522451
}
24532452

2454-
async getWorktreeForBranch(branchName: string): Promise<string | undefined> {
2455-
try {
2456-
return await envGetWorktreeForBranch(branchName, this.repository.rootUri.fsPath);
2457-
} catch (e) {
2458-
Logger.error(`Failed to get worktree for branch ${branchName}: ${e}`, this.id);
2453+
getWorktreeForBranch(branchName: string): string | undefined {
2454+
const worktrees = this.repository.state.worktrees;
2455+
if (!worktrees) {
2456+
return undefined;
24592457
}
2460-
return undefined;
2458+
const refsHeadsPrefix = 'refs/heads/';
2459+
const worktree = worktrees.find(wt => {
2460+
if (wt.main) {
2461+
return false;
2462+
}
2463+
const ref = wt.ref.startsWith(refsHeadsPrefix) ? wt.ref.substring(refsHeadsPrefix.length) : wt.ref;
2464+
return ref === branchName;
2465+
});
2466+
return worktree?.path;
24612467
}
24622468

24632469
async removeWorktree(worktreePath: string): Promise<void> {
2470+
if (!this.repository.deleteWorktree) {
2471+
Logger.error(`deleteWorktree is not available on this repository`, this.id);
2472+
return;
2473+
}
24642474
try {
2465-
await envRemoveWorktree(worktreePath, this.repository.rootUri.fsPath);
2475+
await this.repository.deleteWorktree(worktreePath);
24662476
} catch (e) {
24672477
Logger.error(`Failed to remove worktree ${worktreePath}: ${e}`, this.id);
24682478
throw e;

src/github/pullRequestReviewCommon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export namespace PullRequestReviewCommon {
336336
});
337337
}
338338

339-
const worktreePath = await folderRepositoryManager.getWorktreeForBranch(branchInfo.branch);
339+
const worktreePath = folderRepositoryManager.getWorktreeForBranch(branchInfo.branch);
340340
if (worktreePath) {
341341
const preferredWorktreeDeletion = vscode.workspace
342342
.getConfiguration(PR_SETTINGS_NAMESPACE)
@@ -526,7 +526,7 @@ export namespace PullRequestReviewCommon {
526526
.getConfiguration(PR_SETTINGS_NAMESPACE)
527527
.get<boolean>(`${DEFAULT_DELETION_METHOD}.${SELECT_WORKTREE}`, false);
528528
if (branchInfo && deleteWorktree) {
529-
const worktreePath = await folderRepositoryManager.getWorktreeForBranch(branchInfo.branch);
529+
const worktreePath = folderRepositoryManager.getWorktreeForBranch(branchInfo.branch);
530530
if (worktreePath) {
531531
selectedActions.push({ type: 'worktree', worktreePath });
532532
}

webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ async function getExtensionConfig(target, mode, env) {
349349
'../env/node/net': path.resolve(__dirname, 'src', 'env', 'browser', 'net'),
350350
'../env/node/ssh': path.resolve(__dirname, 'src', 'env', 'browser', 'ssh'),
351351
'../../env/node/ssh': path.resolve(__dirname, 'src', 'env', 'browser', 'ssh'),
352-
'../env/node/gitWorktree': path.resolve(__dirname, 'src', 'env', 'browser', 'gitWorktree'),
353352
'./env/node/gitProviders/api': path.resolve(
354353
__dirname,
355354
'src',

0 commit comments

Comments
 (0)