Elffuss BraidHub

kikocis/continue public

main · 4.1 KiBRawPermalink
1import { spawn } from "node:child_process";
2import { fileURLToPath } from "node:url";
3import {
4 ContextItem,
5 ContextProviderDescription,
6 ContextProviderExtras,
7 ContextSubmenuItem,
8 LoadSubmenuItemsArgs,
9} from "../../index.js";
10import { BaseContextProvider } from "../index.js";
11
12const runGitCommand = (args: string[], cwd: string): Promise<string> => {
13 return new Promise((resolve, reject) => {
14 // First argument is the command, rest are arguments
15 const gitProcess = spawn("git", args, {
16 cwd,
17 });
18 let stdout = "";
19 let stderr = "";
20
21 gitProcess.stdout.on("data", (data) => {
22 stdout += data.toString();
23 });
24
25 gitProcess.stderr.on("data", (data) => {
26 stderr += data.toString();
27 });
28
29 gitProcess.on("close", (code) => {
30 if (code !== 0) {
31 reject(new Error(`Git command failed: ${stderr}`));
32 return;
33 }
34 resolve(stdout);
35 });
36 });
37};
38
39class GitCommitContextProvider extends BaseContextProvider {
40 static description: ContextProviderDescription = {
41 title: "commit",
42 displayTitle: "Commits",
43 description: "Type to search",
44 type: "submenu",
45 };
46
47 get deprecationMessage() {
48 return "The git commits context provider is now deprecated and may be removed in a later version. Please consider using the Git MCP (https://hub.continue.dev/docker/mcp-git) instead.";
49 }
50
51 async getContextItems(
52 query: string,
53 extras: ContextProviderExtras,
54 ): Promise<ContextItem[]> {
55 const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
56 if (typeof lastXCommitsDepth !== "number") {
57 throw new Error("LastXCommitsDepth must be a number");
58 }
59 const topLevelDir = fileURLToPath((await extras.ide.getWorkspaceDirs())[0]);
60
61 try {
62 if (query.includes("last ")) {
63 const content = await runGitCommand(
64 [
65 "--no-pager",
66 "log",
67 '--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
68 "-p",
69 "-n",
70 lastXCommitsDepth.toString(),
71 ],
72 topLevelDir,
73 );
74 return [
75 {
76 name: query,
77 description: query,
78 content,
79 },
80 ];
81 } else {
82 const content = await runGitCommand(
83 [
84 "--no-pager",
85 "show",
86 '--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
87 query,
88 ],
89 topLevelDir,
90 );
91 return [
92 {
93 name: query,
94 description: `commit ${query}`,
95 content,
96 },
97 ];
98 }
99 } catch (err) {
100 return [];
101 }
102 }
103
104 async loadSubmenuItems(
105 args: LoadSubmenuItemsArgs,
106 ): Promise<ContextSubmenuItem[]> {
107 const depth = this.options?.Depth ?? 50;
108 const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
109
110 if (typeof depth !== "number") {
111 throw new Error("Depth must be a number");
112 }
113 if (typeof lastXCommitsDepth !== "number") {
114 throw new Error("LastXCommitsDepth must be a number");
115 }
116
117 const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]);
118 try {
119 const gitResult = await runGitCommand(
120 [
121 "--no-pager",
122 "log",
123 '--pretty=format:"%H%x00%s"',
124 `-n`,
125 depth.toString(),
126 ],
127 topLevelDir,
128 );
129 const recentCommits = [
130 {
131 id: `last ${lastXCommitsDepth} commits`,
132 title: `last ${lastXCommitsDepth} commits`,
133 description: "recent commits",
134 },
135 ];
136 const allCommits = gitResult
137 .trim()
138 .split("\n")
139 .map((line) => {
140 let [hash, message] = line.split("\0");
141 hash = hash.replace(/"/g, "");
142 message = message.replace(/"/g, "");
143 return {
144 id: hash,
145 title: message,
146 description: hash,
147 };
148 });
149 return recentCommits.concat(allCommits);
150 } catch (err: any) {
151 //could be nice to toast the error eg. not a git repo or git is not installed
152 return [];
153 }
154 }
155}
156
157export default GitCommitContextProvider;