Path: blob/main/extensions/copilot/src/extension/chatSessions/common/workspaceInfo.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type * as vscode from 'vscode';6import { extUriBiasedIgnorePathCase } from '../../../util/vs/base/common/resources';7import { RepositoryProperties } from './chatSessionMetadataStore';8import { ChatSessionWorktreeProperties } from './chatSessionWorktreeService';910export interface IWorkspaceInfo {11/**12* The folder URI selected for this session.13* This could be a workspace folder or a git repository root.14*/15readonly folder: vscode.Uri | undefined;1617/**18* The git repository root URI if the selected folder contains a git repository.19* `undefined` if the folder is not a git repository.20*/21readonly repository: vscode.Uri | undefined;2223/**24* The git repository properties associated with this session.25*/26readonly repositoryProperties?: RepositoryProperties;2728/**29* The worktree path if a worktree was created for this session.30* `undefined` if no worktree exists (e.g., plain folder or worktree creation failed).31*/32readonly worktree: vscode.Uri | undefined;3334/**35* The worktree properties associated with this session.36*/37readonly worktreeProperties: ChatSessionWorktreeProperties | undefined;38}3940export function getWorkingDirectory(workspaceInfo: IWorkspaceInfo): vscode.Uri | undefined {41// Give the folder higher priority over repository, as the user may have selected the folder directly,42// & if we don't create a worktree, then the folder is the working directory.43return workspaceInfo.worktree ?? workspaceInfo.folder ?? workspaceInfo.repository;44}4546export function isIsolationEnabled(workspaceInfo: IWorkspaceInfo): boolean {47return !!workspaceInfo.worktreeProperties;48}4950export function emptyWorkspaceInfo(): IWorkspaceInfo {51return {52folder: undefined,53repository: undefined,54repositoryProperties: undefined,55worktree: undefined,56worktreeProperties: undefined,57};58}5960/**61* Given a file URI, finds which workspace (primary or additional) owns it.62* Returns the matching IWorkspaceInfo or undefined if no match.63*/64export function findOwningWorkspace(65file: vscode.Uri,66primaryWorkspace: IWorkspaceInfo,67additionalWorkspaces: IWorkspaceInfo[]68): IWorkspaceInfo | undefined {69for (const ws of [primaryWorkspace, ...additionalWorkspaces]) {70const wd = getWorkingDirectory(ws);71if (wd && extUriBiasedIgnorePathCase.isEqualOrParent(file, wd)) {72return ws;73}74if (ws.folder && extUriBiasedIgnorePathCase.isEqualOrParent(file, ws.folder)) {75return ws;76}77if (ws.worktree && ws.repository && extUriBiasedIgnorePathCase.isEqualOrParent(file, ws.repository)) {78return ws;79}80}81return undefined;82}838485