Merge pull request #3491 from HD787/commitContext
Commit context provider
This commit is contained in:
commit
e16bc29c7c
5 changed files with 147 additions and 1 deletions
75
core/context/providers/GitCommitContextProvider.ts
Normal file
75
core/context/providers/GitCommitContextProvider.ts
Normal 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;
|
||||
|
|
@ -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
1
core/index.d.ts
vendored
|
|
@ -754,6 +754,7 @@ export type ContextProviderName =
|
|||
| "issue"
|
||||
| "repo-map"
|
||||
| "url"
|
||||
| "commit"
|
||||
| "web"
|
||||
| "discord"
|
||||
| "clipboard"
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue