diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts new file mode 100644 index 000000000..24d2eab7b --- /dev/null +++ b/core/context/providers/GitCommitContextProvider.ts @@ -0,0 +1,75 @@ +import { + ContextItem, + ContextProviderDescription, + ContextProviderExtras, + ContextSubmenuItem, + LoadSubmenuItemsArgs, + } from "../../index.js"; +import { BaseContextProvider } from "../index.js"; +import { fileURLToPath} from "node:url"; + +class GitCommitContextProvider extends BaseContextProvider { + static description: ContextProviderDescription = { + title: "commit", + displayTitle: "Commits", + description: "Type to search", + type: "submenu", + }; + + async getContextItems(query: string, extras: ContextProviderExtras): Promise { + const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; + const topLevelDir = fileURLToPath((await extras.ide.getWorkspaceDirs())[0]); + try{ + if(query.includes("last ")){ + return [ + { + name: query, + description: query, + content: (await extras.ide.subprocess(`git --no-pager log --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" -p -n ${lastXCommitsDepth}`, topLevelDir))[0], + } + ] + } + else{ + return [ + { + name: query, + description: `commit ${query}`, + content: (await extras.ide.subprocess(`git --no-pager show --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" ${query}`, topLevelDir))[0], + } + ] + } + }catch(err){ + return []; + } + } + + async loadSubmenuItems( + args: LoadSubmenuItemsArgs, + ): Promise { + const depth = this.options?.Depth ?? 50; + const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; + const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); + try{ + const [gitResult] = await args.ide.subprocess(`git --no-pager log --pretty=format:"%H%x00%s" -n ${depth}`, topLevelDir); + const recentCommits = [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }]; + const allCommits = gitResult + .trim() + .split('\n') + .map(line => { + const [hash, message] = line.split("\0"); + return { + id: hash, + title: message, + description: hash + }; + } + ); + return recentCommits.concat(allCommits); + }catch(err: any){ + //could be nice to toast the error eg. not a git repo or git is not installed + return []; + } + } +} + +export default GitCommitContextProvider; diff --git a/core/context/providers/index.ts b/core/context/providers/index.ts index 920e13687..d72d0d04c 100644 --- a/core/context/providers/index.ts +++ b/core/context/providers/index.ts @@ -12,6 +12,7 @@ import DiscordContextProvider from "./DiscordContextProvider"; import DocsContextProvider from "./DocsContextProvider"; import FileTreeContextProvider from "./FileTreeContextProvider"; import FolderContextProvider from "./FolderContextProvider"; +import GitCommitContextProvider from "./GitCommitContextProvider"; import GitHubIssuesContextProvider from "./GitHubIssuesContextProvider"; import GitLabMergeRequestContextProvider from "./GitLabMergeRequestContextProvider"; import GoogleContextProvider from "./GoogleContextProvider"; @@ -63,6 +64,7 @@ export const Providers: (typeof BaseContextProvider)[] = [ GreptileContextProvider, WebContextProvider, MCPContextProvider, + GitCommitContextProvider, ClipboardContextProvider, ]; diff --git a/core/index.d.ts b/core/index.d.ts index 132c657ad..cd6510f19 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -754,6 +754,7 @@ export type ContextProviderName = | "issue" | "repo-map" | "url" + | "commit" | "web" | "discord" | "clipboard" diff --git a/docs/docs/customize/context-providers.md b/docs/docs/customize/context-providers.md index f28824000..23d8c2253 100644 --- a/docs/docs/customize/context-providers.md +++ b/docs/docs/customize/context-providers.md @@ -618,6 +618,41 @@ The response 200 OK should be a JSON object with the following structure: } ``` +### `@Commits` + +Reference specific git commit metadata and diff or all of the recent commits. + +```json title="config.json" +{ + "contextProviders": [ + { + "name": "commit", + "params": { + "Depth": 50, + "LastXCommitsDepth": 10 + } + } + ] +} +``` + +The depth is how many commits will be loaded into the submenu, defaults to 50. +The LastXCommitsDepth is how many recent commits will be included, defaults to 10. + +### `@Clipboard` + +Reference recent clipboard items + +```json title="config.json" +{ + "contextProviders": [ + { + "name": "clipboard" + } + ] +} +``` + ### `@Greptile` Query a [Greptile](https://www.greptile.com/) index of the current repo/branch. diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index a37adc298..cf9265e67 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1909,6 +1909,7 @@ "gitlab-mr", "discord", "jira", + "commit", "clipboard" ], "markdownEnumDescriptions": [ @@ -1935,7 +1936,9 @@ "Operating system and CPU Information", "Map of files in the repository with important code highlights", "Query your greptile index of the current repo", - "Search the web for sources related to your question" + "Search the web for sources related to your question", + "Reference changes and metadata from previous git commits", + "Reference the contents of the clipboard" ], "type": "string" }, @@ -2507,6 +2510,36 @@ }, "required": ["params"] } + }, + { + "if": { + "properties": { + "name": { + "enum": ["commit"] + } + } + }, + "then": { + "properties": { + "params": { + "properties": { + "Depth": { + "title": "depth", + "description": "How many previous commits should be loaded", + "default": 50, + "type": "number" + }, + "LastXCommitsDepth": { + "title": "LastXCommitsDepth", + "description": "how many commits should be included when querying all recent commits", + "default": 10, + "type": "number" + } + } + } + }, + "required": ["params"] + } } ], "required": ["name"]