From af83bf732921f921fd5c556585808f0c3815cd2d Mon Sep 17 00:00:00 2001 From: aliang Date: Thu, 19 Dec 2024 12:40:02 +0700 Subject: [PATCH] feat(vscode): introduce API to read git repositories in workspace (#3593) * feat(vscode): introduce API to read git repositories in workspace * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- clients/vscode/src/chat/WebviewHelper.ts | 32 +++++++++++++++++++++++- clients/vscode/src/chat/chatPanel.ts | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/clients/vscode/src/chat/WebviewHelper.ts b/clients/vscode/src/chat/WebviewHelper.ts index bb514c66a4ce..3b2789748b4f 100644 --- a/clients/vscode/src/chat/WebviewHelper.ts +++ b/clients/vscode/src/chat/WebviewHelper.ts @@ -13,6 +13,7 @@ import { commands, Location, LocationLink, + workspace, } from "vscode"; import type { ServerApi, @@ -23,6 +24,7 @@ import type { LookupSymbolHint, SymbolInfo, FileLocation, + GitRepository, } from "tabby-chat-panel"; import { TABBY_CHAT_PANEL_API_VERSION } from "tabby-chat-panel"; import hashObject from "object-hash"; @@ -33,7 +35,7 @@ import { GitProvider } from "../git/GitProvider"; import { createClient } from "./chatPanel"; import { Client as LspClient } from "../lsp/Client"; import { isBrowser } from "../env"; -import { getFileContextFromSelection, showFileContext, openTextDocument } from "./fileContext"; +import { getFileContextFromSelection, showFileContext, openTextDocument, buildFilePathParams } from "./fileContext"; import { localUriToChatPanelFilepath, chatPanelFilepathToLocalUri, @@ -698,6 +700,34 @@ export class WebviewHelper { return false; } }, + readWorkspaceGitRepositories: async (): Promise => { + const activeTextEditor = window.activeTextEditor; + const infoList: GitRepository[] = []; + let activeGitUrl: string | undefined; + if (activeTextEditor) { + const pathParams = await buildFilePathParams(activeTextEditor.document.uri, this.gitProvider); + if (pathParams.gitRemoteUrl) { + activeGitUrl = pathParams.gitRemoteUrl; + infoList.push({ + url: activeGitUrl, + }); + } + } + + const workspaceFolder = workspace.workspaceFolders || []; + for (const folder of workspaceFolder) { + const repo = this.gitProvider.getRepository(folder.uri); + if (repo) { + const gitRemoteUrl = this.gitProvider.getDefaultRemoteUrl(repo); + if (gitRemoteUrl && gitRemoteUrl !== activeGitUrl) { + infoList.push({ + url: gitRemoteUrl, + }); + } + } + } + return infoList; + }, }); } } diff --git a/clients/vscode/src/chat/chatPanel.ts b/clients/vscode/src/chat/chatPanel.ts index 0d3c50de1582..356a85a2d343 100644 --- a/clients/vscode/src/chat/chatPanel.ts +++ b/clients/vscode/src/chat/chatPanel.ts @@ -35,6 +35,7 @@ export function createClient(webview: Webview, api: ClientApiMethods): ServerApi onKeyboardEvent: api.onKeyboardEvent, lookupSymbol: api.lookupSymbol, openInEditor: api.openInEditor, + readWorkspaceGitRepositories: api.readWorkspaceGitRepositories, }, }); }