fix git commit context provider subprocess calls

This commit is contained in:
Nate 2025-03-30 23:27:12 -07:00
parent 95bba8b12b
commit ac423eb3c5
3 changed files with 76 additions and 18 deletions

View file

@ -1,3 +1,4 @@
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import {
ContextItem,
@ -8,6 +9,33 @@ import {
} from "../../index.js";
import { BaseContextProvider } from "../index.js";
const runGitCommand = (args: string[], cwd: string): Promise<string> => {
return new Promise((resolve, reject) => {
// First argument is the command, rest are arguments
const gitProcess = spawn("git", args, {
cwd,
});
let stdout = "";
let stderr = "";
gitProcess.stdout.on("data", (data) => {
stdout += data.toString();
});
gitProcess.stderr.on("data", (data) => {
stderr += data.toString();
});
gitProcess.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Git command failed: ${stderr}`));
return;
}
resolve(stdout);
});
});
};
class GitCommitContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
title: "commit",
@ -21,32 +49,46 @@ class GitCommitContextProvider extends BaseContextProvider {
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
if (typeof lastXCommitsDepth !== "number") {
throw new Error("LastXCommitsDepth must be a number");
}
const topLevelDir = fileURLToPath((await extras.ide.getWorkspaceDirs())[0]);
try {
if (query.includes("last ")) {
const content = await runGitCommand(
[
"--no-pager",
"log",
'--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
"-p",
"-n",
lastXCommitsDepth.toString(),
],
topLevelDir,
);
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],
content,
},
];
} else {
const content = await runGitCommand(
[
"--no-pager",
"show",
'--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
query,
],
topLevelDir,
);
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],
content,
},
];
}
@ -60,10 +102,24 @@ class GitCommitContextProvider extends BaseContextProvider {
): Promise<ContextSubmenuItem[]> {
const depth = this.options?.Depth ?? 50;
const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
if (typeof depth !== "number") {
throw new Error("Depth must be a number");
}
if (typeof lastXCommitsDepth !== "number") {
throw new Error("LastXCommitsDepth must be a number");
}
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}`,
const gitResult = await runGitCommand(
[
"--no-pager",
"log",
'--pretty=format:"%H%x00%s"',
`-n`,
depth.toString(),
],
topLevelDir,
);
const recentCommits = [
@ -77,7 +133,9 @@ class GitCommitContextProvider extends BaseContextProvider {
.trim()
.split("\n")
.map((line) => {
const [hash, message] = line.split("\0");
let [hash, message] = line.split("\0");
hash = hash.replace(/"/g, "");
message = message.replace(/"/g, "");
return {
id: hash,
title: message,

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "1.1.17",
"version": "1.1.18",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "1.1.17",
"version": "1.1.18",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/fetch": "^1.0.3",

View file

@ -2,7 +2,7 @@
"name": "continue",
"icon": "media/icon.png",
"author": "Continue Dev, Inc",
"version": "1.1.17",
"version": "1.1.18",
"repository": {
"type": "git",
"url": "https://github.com/continuedev/continue"