Skip to content

Commit 211615d

Browse files
Git: Fix worktree checkout default actions (microsoft#262414)
* fix worktree checkout default action * remove comments * clean up * fix returns * change action to 'cancel'
1 parent 351b578 commit 211615d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

extensions/git/src/commands.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,7 +2938,25 @@ export class CommandCenter {
29382938
}
29392939

29402940
if (err.gitErrorCode === GitErrorCodes.WorktreeBranchAlreadyUsed) {
2941-
this.handleWorktreeBranchAlreadyUsed(err);
2941+
// Not checking out in a worktree (use standard error handling)
2942+
if (!repository.dotGit.commonPath) {
2943+
await this.handleWorktreeBranchAlreadyUsed(err);
2944+
return false;
2945+
}
2946+
2947+
// Check out in a worktree (check if worktree's main repository is open in workspace and if branch is already checked out in main repository)
2948+
const commonPath = path.dirname(repository.dotGit.commonPath);
2949+
if (workspace.workspaceFolders && workspace.workspaceFolders.some(folder => pathEquals(folder.uri.fsPath, commonPath))) {
2950+
const mainRepository = this.model.getRepository(commonPath);
2951+
if (mainRepository && item.refName && item.refName.replace(`${item.refRemote}/`, '') === mainRepository.HEAD?.name) {
2952+
const message = l10n.t('Branch "{0}" is already checked out in the current window.', item.refName);
2953+
await window.showErrorMessage(message, { modal: true });
2954+
return false;
2955+
}
2956+
}
2957+
2958+
// Check out in a worktree, (branch is already checked out in existing worktree)
2959+
await this.handleWorktreeBranchAlreadyUsed(err);
29422960
return false;
29432961
}
29442962

@@ -3744,7 +3762,6 @@ export class CommandCenter {
37443762
return;
37453763
}
37463764

3747-
37483765
@command('git.deleteWorktree', { repository: true, repositoryFilter: ['worktree'] })
37493766
async deleteWorktree(repository: Repository): Promise<void> {
37503767
if (!repository.dotGit.commonPath) {

0 commit comments

Comments
 (0)