Elffuss BraidHub

kikocis/continue public

d1cdc8b8ac2b · 1.6 KiBRawPermalink
1import * as child_process from "child_process";
2import * as fs from "fs";
3import * as util from "util";
4
5import { Tool } from "./types.js";
6
7const execPromise = util.promisify(child_process.exec);
8
9export const viewDiffTool: Tool = {
10 name: "Diff",
11 displayName: "Diff",
12 description: "View all uncommitted changes in the git repository",
13 parameters: {
14 path: {
15 type: "string",
16 description:
17 "The path to the git repository (defaults to current directory)",
18 required: false,
19 },
20 },
21 readonly: true,
22 isBuiltIn: true,
23 preprocess: async (args) => {
24 return {
25 args,
26 preview: [
27 {
28 type: "text",
29 content: "Will show git diff",
30 },
31 ],
32 };
33 },
34 run: async (args: { path?: string }): Promise<string> => {
35 try {
36 const repoPath = args.path || process.cwd();
37 if (!fs.existsSync(repoPath)) {
38 return `Error: Path does not exist: ${repoPath}`;
39 }
40
41 try {
42 await execPromise("git rev-parse --is-inside-work-tree", {
43 cwd: repoPath,
44 });
45 } catch {
46 return `Error: The specified path is not a git repository: ${repoPath}`;
47 }
48
49 const { stdout, stderr } = await execPromise("git diff", {
50 cwd: repoPath,
51 });
52
53 if (stderr) {
54 return `Error executing git diff: ${stderr}`;
55 }
56
57 if (!stdout.trim()) {
58 return "No changes detected in the git repository.";
59 }
60
61 return `Git diff for repository at ${repoPath}:\n\n${stdout}`;
62 } catch (error) {
63 return `Error running git diff: ${
64 error instanceof Error ? error.message : String(error)
65 }`;
66 }
67 },
68};