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