Merge pull request #3491 from HD787/commitContext

Commit context provider
This commit is contained in:
Nate Sesti 2025-01-10 22:07:09 -08:00 • committed by GitHub
commit e16bc29c7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 147 additions and 1 deletions

View file

@ -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<ContextItem[]> {
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<ContextSubmenuItem[]> {
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;

View file

@ -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,
];

1
core/index.d.ts vendored
View file

@ -754,6 +754,7 @@ export type ContextProviderName =
| "issue"
| "repo-map"
| "url"
| "commit"
| "web"
| "discord"
| "clipboard"

View file

@ -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.

View file

@ -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"]