Skip to content

Commit

Permalink
Merge pull request #3491 from HD787/commitContext
Browse files Browse the repository at this point in the history
Commit context provider
  • Loading branch information
sestinj authored Jan 11, 2025
2 parents e9f16ad + 6d8c6ad commit e16bc29
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
75 changes: 75 additions & 0 deletions core/context/providers/GitCommitContextProvider.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions core/context/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -63,6 +64,7 @@ export const Providers: (typeof BaseContextProvider)[] = [
GreptileContextProvider,
WebContextProvider,
MCPContextProvider,
GitCommitContextProvider,
ClipboardContextProvider,
];

Expand Down
1 change: 1 addition & 0 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ export type ContextProviderName =
| "issue"
| "repo-map"
| "url"
| "commit"
| "web"
| "discord"
| "clipboard"
Expand Down
35 changes: 35 additions & 0 deletions docs/docs/customize/context-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 34 additions & 1 deletion extensions/vscode/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,7 @@
"gitlab-mr",
"discord",
"jira",
"commit",
"clipboard"
],
"markdownEnumDescriptions": [
Expand All @@ -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"
},
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit e16bc29

Please sign in to comment.