From 932172c7a76b7e2835eebe61b885324a78e54257 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 18:15:02 -0500 Subject: [PATCH 01/12] added the commit context provider --- .../providers/GitCommitContextProvider.ts | 88 +++++++++++++++++++ core/context/providers/index.ts | 2 + core/index.d.ts | 1 + extensions/vscode/config_schema.json | 36 +++++++- 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 core/context/providers/GitCommitContextProvider.ts diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts new file mode 100644 index 000000000..09cd5a963 --- /dev/null +++ b/core/context/providers/GitCommitContextProvider.ts @@ -0,0 +1,88 @@ +import { + ContextItem, + ContextProviderDescription, + ContextProviderExtras, + ContextSubmenuItem, + LoadSubmenuItemsArgs, + } from "../../index.js"; +import { BaseContextProvider } from "../index.js"; +import childProcess from "node:child_process"; +import util from "node:util"; + +const asyncExec = util.promisify(childProcess.exec); + +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] = await extras.ide.getWorkspaceDirs(); + try{ + if(query.includes("last ")){ + return [ + { + name: query, + description: query, + content: (await asyncExec(`git --no-pager log --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" -p -n ${lastXCommitsDepth}`, {cwd: topLevelDir})).stdout, + } + ] + } + else{ + console.log('query'); + return [ + { + name: query, + description: `commit ${query}`, + content: (await asyncExec(`git --no-pager show --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" ${query}`, {cwd: topLevelDir})).stdout, + } + ] + + } + }catch(err){ + return []; + } + } + + async loadSubmenuItems( + args: LoadSubmenuItemsArgs, + ): Promise { + const depth = this.options?.Depth ?? 50; + const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; + const [topLevelDir] = await args.ide.getWorkspaceDirs(); + try{ + const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%Hx00%s" -n ${depth}`, {cwd: topLevelDir}); + if (gitResult.stderr.toLowerCase().includes('fatal')) { + console.log(gitResult.stderr); + throw new Error(gitResult.stderr); + } + return [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }] + .concat( + gitResult.stdout + .trim() + .split('\n') + .map(line => { + const [hash, message] = line.split("x00"); + console.log("line", line); + console.log(message); + if(!hash || hash === ""){console.log('empty');} + return { + id: hash, + title: message, + description: hash + }; + }) + ); + }catch(err: any){ + console.log(err.message); + //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 f5656832c..3f121058e 100644 --- a/core/context/providers/index.ts +++ b/core/context/providers/index.ts @@ -27,6 +27,7 @@ import SearchContextProvider from "./SearchContextProvider"; import TerminalContextProvider from "./TerminalContextProvider"; import URLContextProvider from "./URLContextProvider"; import WebContextProvider from "./WebContextProvider"; +import GitCommitContextProvider from "./GitCommitContextProvider" /** * Note: We are currently omitting the following providers due to bugs: @@ -62,6 +63,7 @@ export const Providers: (typeof BaseContextProvider)[] = [ GreptileContextProvider, WebContextProvider, MCPContextProvider, + GitCommitContextProvider, ]; export function contextProviderClassFromName( diff --git a/core/index.d.ts b/core/index.d.ts index f9e6aa0e7..93f814d18 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -741,6 +741,7 @@ export type ContextProviderName = | "issue" | "repo-map" | "url" + | "commit" | string; export type TemplateType = diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index f2c98642f..5e81f2aba 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1864,7 +1864,8 @@ "os", "repo-map", "greptile", - "web" + "web", + "commit" ], "markdownEnumDescriptions": [ "Reference the contents of the current changes as given by `git diff`", @@ -1890,7 +1891,8 @@ "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" ], "type": "string" }, @@ -2394,6 +2396,36 @@ }, "required": ["GreptileToken", "GithubToken"] } + }, + { + "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"] From 8eef1c55710a6799c4c5907bee27ce973daa288a Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 19:33:00 -0500 Subject: [PATCH 02/12] should be working --- core/context/providers/GitCommitContextProvider.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 09cd5a963..036261b03 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -8,6 +8,8 @@ import { import { BaseContextProvider } from "../index.js"; import childProcess from "node:child_process"; import util from "node:util"; +import { fileURLToPath} from "node:url"; + const asyncExec = util.promisify(childProcess.exec); @@ -21,7 +23,7 @@ class GitCommitContextProvider extends BaseContextProvider { async getContextItems(query: string, extras: ContextProviderExtras): Promise { const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; - const [topLevelDir] = await extras.ide.getWorkspaceDirs(); + const topLevelDir = fileURLToPath((await extras.ide.getWorkspaceDirs())[0]); try{ if(query.includes("last ")){ return [ @@ -53,9 +55,10 @@ class GitCommitContextProvider extends BaseContextProvider { ): Promise { const depth = this.options?.Depth ?? 50; const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; - const [topLevelDir] = await args.ide.getWorkspaceDirs(); + const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); + console.log(topLevelDir); try{ - const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%Hx00%s" -n ${depth}`, {cwd: topLevelDir}); + const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H:%s" -n ${depth}`, {cwd: topLevelDir}); if (gitResult.stderr.toLowerCase().includes('fatal')) { console.log(gitResult.stderr); throw new Error(gitResult.stderr); @@ -66,7 +69,7 @@ class GitCommitContextProvider extends BaseContextProvider { .trim() .split('\n') .map(line => { - const [hash, message] = line.split("x00"); + const [hash, message] = line.split(":"); console.log("line", line); console.log(message); if(!hash || hash === ""){console.log('empty');} From a8d57f78adb2b63e5da0798352bb967422212754 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 19:36:45 -0500 Subject: [PATCH 03/12] cleanup --- core/context/providers/GitCommitContextProvider.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 036261b03..3fff2ba0b 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -35,7 +35,6 @@ class GitCommitContextProvider extends BaseContextProvider { ] } else{ - console.log('query'); return [ { name: query, @@ -56,11 +55,9 @@ class GitCommitContextProvider extends BaseContextProvider { const depth = this.options?.Depth ?? 50; const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); - console.log(topLevelDir); try{ const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H:%s" -n ${depth}`, {cwd: topLevelDir}); if (gitResult.stderr.toLowerCase().includes('fatal')) { - console.log(gitResult.stderr); throw new Error(gitResult.stderr); } return [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }] @@ -70,9 +67,6 @@ class GitCommitContextProvider extends BaseContextProvider { .split('\n') .map(line => { const [hash, message] = line.split(":"); - console.log("line", line); - console.log(message); - if(!hash || hash === ""){console.log('empty');} return { id: hash, title: message, @@ -81,7 +75,6 @@ class GitCommitContextProvider extends BaseContextProvider { }) ); }catch(err: any){ - console.log(err.message); //could be nice to toast the error eg. not a git repo or git is not installed return []; } From 48ddac2504d83bea1d7723c6395093406f180e8f Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:02:07 -0500 Subject: [PATCH 04/12] added docs --- docs/docs/customize/context-providers.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/docs/customize/context-providers.md b/docs/docs/customize/context-providers.md index 256f4eab5..1ba767348 100644 --- a/docs/docs/customize/context-providers.md +++ b/docs/docs/customize/context-providers.md @@ -576,6 +576,27 @@ 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. + ### Requesting Context Providers Not seeing what you want? Create an issue [here](https://github.com/continuedev/continue/issues/new?assignees=TyDunn&labels=enhancement&projects=&template=feature-request-%F0%9F%92%AA.md&title=) to request a new Context Provider. From ece68a5431c98a01ec50f94765e310a683d0c7fe Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:08:01 -0500 Subject: [PATCH 05/12] fix doc formatting --- docs/docs/customize/context-providers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/customize/context-providers.md b/docs/docs/customize/context-providers.md index 1ba767348..66ea5dd36 100644 --- a/docs/docs/customize/context-providers.md +++ b/docs/docs/customize/context-providers.md @@ -576,7 +576,7 @@ The response 200 OK should be a JSON object with the following structure: } ``` -### @Commits +### `@Commits` Reference specific git commit metadata and diff or all of the recent commits. From 122422becd33e50090815b165e24ffd7c93b4a75 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:11:30 -0500 Subject: [PATCH 06/12] correct capitalization --- docs/docs/customize/context-providers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/customize/context-providers.md b/docs/docs/customize/context-providers.md index 66ea5dd36..1f7c3cca7 100644 --- a/docs/docs/customize/context-providers.md +++ b/docs/docs/customize/context-providers.md @@ -595,7 +595,7 @@ Reference specific git commit metadata and diff or all of the recent commits. ``` 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. +The LastXCommitsDepth is how many recent commits will be included, defaults to 10. ### Requesting Context Providers From 62cdf9142aaaa12c0f655f8450a19b0728acb71f Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:15:51 -0500 Subject: [PATCH 07/12] fixed whitespace --- core/context/providers/GitCommitContextProvider.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 3fff2ba0b..2c687336a 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -10,7 +10,6 @@ import childProcess from "node:child_process"; import util from "node:util"; import { fileURLToPath} from "node:url"; - const asyncExec = util.promisify(childProcess.exec); class GitCommitContextProvider extends BaseContextProvider { @@ -42,7 +41,6 @@ class GitCommitContextProvider extends BaseContextProvider { content: (await asyncExec(`git --no-pager show --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" ${query}`, {cwd: topLevelDir})).stdout, } ] - } }catch(err){ return []; From f93438d44ccfded228191544087ae1b513fd7958 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:27:02 -0500 Subject: [PATCH 08/12] remove unecessary throw --- core/context/providers/GitCommitContextProvider.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 2c687336a..76630418f 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -55,9 +55,6 @@ class GitCommitContextProvider extends BaseContextProvider { const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); try{ const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H:%s" -n ${depth}`, {cwd: topLevelDir}); - if (gitResult.stderr.toLowerCase().includes('fatal')) { - throw new Error(gitResult.stderr); - } return [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }] .concat( gitResult.stdout From c3e5e0fe28c952e2d18269eda72508010b5bdb0c Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:37:43 -0500 Subject: [PATCH 09/12] this feels cleaner to me --- .../providers/GitCommitContextProvider.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 76630418f..3aee08dfa 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -55,20 +55,20 @@ class GitCommitContextProvider extends BaseContextProvider { const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); try{ const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H:%s" -n ${depth}`, {cwd: topLevelDir}); - return [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }] - .concat( - gitResult.stdout - .trim() - .split('\n') - .map(line => { - const [hash, message] = line.split(":"); - return { - id: hash, - title: message, - description: hash - }; - }) + const recentCommits = [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }]; + const allCommits = gitResult.stdout + .trim() + .split('\n') + .map(line => { + const [hash, message] = line.split(":"); + 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 []; From 7ff2a791c63d4573022700ec2294484681dd3190 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 20:45:57 -0500 Subject: [PATCH 10/12] switched to null character split strings to prevent issues with commit messages containing the character the split char --- core/context/providers/GitCommitContextProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 3aee08dfa..4ef9dd37b 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -54,13 +54,13 @@ class GitCommitContextProvider extends BaseContextProvider { const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); try{ - const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H:%s" -n ${depth}`, {cwd: topLevelDir}); + const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H%x00%s" -n ${depth}`, {cwd: topLevelDir}); const recentCommits = [{ id: `last ${lastXCommitsDepth} commits`, title: `last ${lastXCommitsDepth} commits`, description: "recent commits" }]; const allCommits = gitResult.stdout .trim() .split('\n') .map(line => { - const [hash, message] = line.split(":"); + const [hash, message] = line.split("\0"); return { id: hash, title: message, From 56286c9e14017657e4835a8e635afea9f73d7d97 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 20 Dec 2024 21:48:10 -0500 Subject: [PATCH 11/12] using provided tools --- core/context/providers/GitCommitContextProvider.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 4ef9dd37b..04d0435a5 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -6,11 +6,8 @@ import { LoadSubmenuItemsArgs, } from "../../index.js"; import { BaseContextProvider } from "../index.js"; -import childProcess from "node:child_process"; -import util from "node:util"; import { fileURLToPath} from "node:url"; -const asyncExec = util.promisify(childProcess.exec); class GitCommitContextProvider extends BaseContextProvider { static description: ContextProviderDescription = { @@ -29,7 +26,7 @@ class GitCommitContextProvider extends BaseContextProvider { { name: query, description: query, - content: (await asyncExec(`git --no-pager log --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" -p -n ${lastXCommitsDepth}`, {cwd: topLevelDir})).stdout, + content: (await extras.ide.subprocess(`git --no-pager log --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" -p -n ${lastXCommitsDepth}`, topLevelDir))[0], } ] } @@ -38,7 +35,7 @@ class GitCommitContextProvider extends BaseContextProvider { { name: query, description: `commit ${query}`, - content: (await asyncExec(`git --no-pager show --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" ${query}`, {cwd: topLevelDir})).stdout, + content: (await extras.ide.subprocess(`git --no-pager show --pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b" ${query}`, topLevelDir))[0], } ] } @@ -54,9 +51,9 @@ class GitCommitContextProvider extends BaseContextProvider { const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10; const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]); try{ - const gitResult = await asyncExec(`git --no-pager log --pretty=format:"%H%x00%s" -n ${depth}`, {cwd: topLevelDir}); + 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.stdout + const allCommits = gitResult .trim() .split('\n') .map(line => { From 0bcc0584225e7d6d86e472c3f7be77e5e3c278ce Mon Sep 17 00:00:00 2001 From: henry Date: Sat, 21 Dec 2024 15:17:09 -0500 Subject: [PATCH 12/12] removed whitespace --- core/context/providers/GitCommitContextProvider.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/core/context/providers/GitCommitContextProvider.ts b/core/context/providers/GitCommitContextProvider.ts index 04d0435a5..24d2eab7b 100644 --- a/core/context/providers/GitCommitContextProvider.ts +++ b/core/context/providers/GitCommitContextProvider.ts @@ -8,7 +8,6 @@ import { import { BaseContextProvider } from "../index.js"; import { fileURLToPath} from "node:url"; - class GitCommitContextProvider extends BaseContextProvider { static description: ContextProviderDescription = { title: "commit",