Elffuss BraidHub

kikocis/continue public

8eb3ae7b8d16 · 3.9 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 async getContextItems(
48 query: string,
49 extras: ContextProviderExtras,
50 ): Promise<ContextItem[]> {
51 const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
52 if (typeof lastXCommitsDepth !== "number") {
53 throw new Error("LastXCommitsDepth must be a number");
54 }
55 const topLevelDir = fileURLToPath((await extras.ide.getWorkspaceDirs())[0]);
56
57 try {
58 if (query.includes("last ")) {
59 const content = await runGitCommand(
60 [
61 "--no-pager",
62 "log",
63 '--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
64 "-p",
65 "-n",
66 lastXCommitsDepth.toString(),
67 ],
68 topLevelDir,
69 );
70 return [
71 {
72 name: query,
73 description: query,
74 content,
75 },
76 ];
77 } else {
78 const content = await runGitCommand(
79 [
80 "--no-pager",
81 "show",
82 '--pretty=format:"%H,%h,%an,%ae,%ad,%P,%s,%b"',
83 query,
84 ],
85 topLevelDir,
86 );
87 return [
88 {
89 name: query,
90 description: `commit ${query}`,
91 content,
92 },
93 ];
94 }
95 } catch (err) {
96 return [];
97 }
98 }
99
100 async loadSubmenuItems(
101 args: LoadSubmenuItemsArgs,
102 ): Promise<ContextSubmenuItem[]> {
103 const depth = this.options?.Depth ?? 50;
104 const lastXCommitsDepth = this.options?.LastXCommitsDepth ?? 10;
105
106 if (typeof depth !== "number") {
107 throw new Error("Depth must be a number");
108 }
109 if (typeof lastXCommitsDepth !== "number") {
110 throw new Error("LastXCommitsDepth must be a number");
111 }
112
113 const topLevelDir = fileURLToPath((await args.ide.getWorkspaceDirs())[0]);
114 try {
115 const gitResult = await runGitCommand(
116 [
117 "--no-pager",
118 "log",
119 '--pretty=format:"%H%x00%s"',
120 `-n`,
121 depth.toString(),
122 ],
123 topLevelDir,
124 );
125 const recentCommits = [
126 {
127 id: `last ${lastXCommitsDepth} commits`,
128 title: `last ${lastXCommitsDepth} commits`,
129 description: "recent commits",
130 },
131 ];
132 const allCommits = gitResult
133 .trim()
134 .split("\n")
135 .map((line) => {
136 let [hash, message] = line.split("\0");
137 hash = hash.replace(/"/g, "");
138 message = message.replace(/"/g, "");
139 return {
140 id: hash,
141 title: message,
142 description: hash,
143 };
144 });
145 return recentCommits.concat(allCommits);
146 } catch (err: any) {
147 //could be nice to toast the error eg. not a git repo or git is not installed
148 return [];
149 }
150 }
151}
152
153export default GitCommitContextProvider;