Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/node/copilot/copilotGitProject.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Schemas } from '../../../../base/common/network.js';
7
import { basename } from '../../../../base/common/path.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import type { IAgentSessionProjectInfo } from '../../common/agentService.js';
10
import type { IAgentHostGitService } from '../agentHostGitService.js';
11
12
export interface ICopilotSessionContext {
13
readonly cwd?: string;
14
readonly gitRoot?: string;
15
readonly repository?: string;
16
}
17
18
export async function resolveGitProject(workingDirectory: URI | undefined, gitService: IAgentHostGitService): Promise<IAgentSessionProjectInfo | undefined> {
19
if (!workingDirectory || workingDirectory.scheme !== Schemas.file) {
20
return undefined;
21
}
22
23
if (!await gitService.isInsideWorkTree(workingDirectory)) {
24
return undefined;
25
}
26
27
const uri = (await gitService.getWorktreeRoots(workingDirectory))[0]
28
?? await gitService.getRepositoryRoot(workingDirectory);
29
if (!uri) {
30
return undefined;
31
}
32
return { uri, displayName: basename(uri.fsPath) || uri.toString() };
33
}
34
35
export function projectFromRepository(repository: string): IAgentSessionProjectInfo | undefined {
36
const uri = repository.includes('://') ? URI.parse(repository) : URI.parse(`https://github.com/${repository}`);
37
const rawDisplayName = basename(uri.path) || repository.split('/').filter(Boolean).pop() || repository;
38
const displayName = rawDisplayName.endsWith('.git') ? rawDisplayName.slice(0, -'.git'.length) : rawDisplayName;
39
return { uri, displayName };
40
}
41
42
export async function projectFromCopilotContext(context: ICopilotSessionContext | undefined, gitService: IAgentHostGitService): Promise<IAgentSessionProjectInfo | undefined> {
43
const workingDirectory = typeof context?.cwd === 'string'
44
? URI.file(context.cwd)
45
: typeof context?.gitRoot === 'string'
46
? URI.file(context.gitRoot)
47
: undefined;
48
const gitProject = await resolveGitProject(workingDirectory, gitService);
49
if (gitProject) {
50
return gitProject;
51
}
52
53
if (context?.repository) {
54
return projectFromRepository(context.repository);
55
}
56
57
return undefined;
58
}
59
60