Path: blob/main/src/vs/platform/agentHost/node/copilot/copilotGitProject.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 { Schemas } from '../../../../base/common/network.js';6import { basename } from '../../../../base/common/path.js';7import { URI } from '../../../../base/common/uri.js';8import type { IAgentSessionProjectInfo } from '../../common/agentService.js';9import type { IAgentHostGitService } from '../agentHostGitService.js';1011export interface ICopilotSessionContext {12readonly cwd?: string;13readonly gitRoot?: string;14readonly repository?: string;15}1617export async function resolveGitProject(workingDirectory: URI | undefined, gitService: IAgentHostGitService): Promise<IAgentSessionProjectInfo | undefined> {18if (!workingDirectory || workingDirectory.scheme !== Schemas.file) {19return undefined;20}2122if (!await gitService.isInsideWorkTree(workingDirectory)) {23return undefined;24}2526const uri = (await gitService.getWorktreeRoots(workingDirectory))[0]27?? await gitService.getRepositoryRoot(workingDirectory);28if (!uri) {29return undefined;30}31return { uri, displayName: basename(uri.fsPath) || uri.toString() };32}3334export function projectFromRepository(repository: string): IAgentSessionProjectInfo | undefined {35const uri = repository.includes('://') ? URI.parse(repository) : URI.parse(`https://github.com/${repository}`);36const rawDisplayName = basename(uri.path) || repository.split('/').filter(Boolean).pop() || repository;37const displayName = rawDisplayName.endsWith('.git') ? rawDisplayName.slice(0, -'.git'.length) : rawDisplayName;38return { uri, displayName };39}4041export async function projectFromCopilotContext(context: ICopilotSessionContext | undefined, gitService: IAgentHostGitService): Promise<IAgentSessionProjectInfo | undefined> {42const workingDirectory = typeof context?.cwd === 'string'43? URI.file(context.cwd)44: typeof context?.gitRoot === 'string'45? URI.file(context.gitRoot)46: undefined;47const gitProject = await resolveGitProject(workingDirectory, gitService);48if (gitProject) {49return gitProject;50}5152if (context?.repository) {53return projectFromRepository(context.repository);54}5556return undefined;57}585960