diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 3dd210dfcf2..aa825b87587 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -2938,7 +2938,25 @@ export class CommandCenter { } if (err.gitErrorCode === GitErrorCodes.WorktreeBranchAlreadyUsed) { - this.handleWorktreeBranchAlreadyUsed(err); + // Not checking out in a worktree (use standard error handling) + if (!repository.dotGit.commonPath) { + await this.handleWorktreeBranchAlreadyUsed(err); + return false; + } + + // 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) + const commonPath = path.dirname(repository.dotGit.commonPath); + if (workspace.workspaceFolders && workspace.workspaceFolders.some(folder => pathEquals(folder.uri.fsPath, commonPath))) { + const mainRepository = this.model.getRepository(commonPath); + if (mainRepository && item.refName && item.refName.replace(`${item.refRemote}/`, '') === mainRepository.HEAD?.name) { + const message = l10n.t('Branch "{0}" is already checked out in the current window.', item.refName); + await window.showErrorMessage(message, { modal: true }); + return false; + } + } + + // Check out in a worktree, (branch is already checked out in existing worktree) + await this.handleWorktreeBranchAlreadyUsed(err); return false; } @@ -3744,7 +3762,6 @@ export class CommandCenter { return; } - @command('git.deleteWorktree', { repository: true, repositoryFilter: ['worktree'] }) async deleteWorktree(repository: Repository): Promise { if (!repository.dotGit.commonPath) {