git_clean
Remove untracked files and directories from a Git repository to clean up workspace clutter. Specify repository path, directory removal, force options, or dry run to preview changes.
Instructions
Perform git clean operations.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | The path to the local Git repository | |
| directories | No | Whether to remove directories as well | |
| force | No | Whether to force clean | |
| dry_run | No | Whether to perform a dry run |
Implementation Reference
- src/handlers/other-operations.js:292-382 (handler)Implements the git clean tool logic using simpleGit. Supports dry-run previews, force removal, and directory cleaning with safety checks.export async function handleGitClean({ repo_path, directories = false, force = false, dry_run = true, }) { try { const git = simpleGit(repo_path); // At least one of force or dry_run must be true for safety if (!force && !dry_run) { return { content: [ { type: "text", text: JSON.stringify( { error: "For safety, either force or dry_run must be true" }, null, 2 ), }, ], isError: true, }; } // Build the clean command const cleanOptions = []; if (directories) { cleanOptions.push("-d"); } if (force) { cleanOptions.push("-f"); } if (dry_run) { cleanOptions.push("-n"); } // Get the files that would be removed const preview = await git.clean([ "--dry-run", ...(directories ? ["-d"] : []), ]); const filesToRemove = preview .split("\n") .filter((line) => line.startsWith("Would remove")) .map((line) => line.replace("Would remove ", "").trim()); if (!dry_run) { // Perform the actual clean await git.clean(cleanOptions); } return { content: [ { type: "text", text: JSON.stringify( { success: true, message: dry_run ? `Would remove ${filesToRemove.length} files/directories` : `Removed ${filesToRemove.length} files/directories`, files: filesToRemove, dry_run: dry_run, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to clean repository: ${error.message}` }, null, 2 ), }, ], isError: true, }; } }
- src/server.js:712-739 (schema)Defines the input schema and metadata for the 'git_clean' tool used in tool listing and validation.name: "git_clean", description: "Perform git clean operations.", inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "The path to the local Git repository", }, directories: { type: "boolean", description: "Whether to remove directories as well", default: false, }, force: { type: "boolean", description: "Whether to force clean", default: false, }, dry_run: { type: "boolean", description: "Whether to perform a dry run", default: true, }, }, required: ["repo_path"], }, },
- src/server.js:922-922 (registration)Maps the tool name 'git_clean' to its handler function in the central handlersMap for request dispatching.git_clean: handleGitClean,
- src/handlers/index.js:33-33 (registration)Imports the handleGitClean function from other-operations.js for re-export.handleGitClean,
- src/server.js:33-38 (registration)Imports handleGitClean from handlers/index.js into the server for use in handlersMap.handleGitClean, handleGitHooks, handleGitLFS, handleGitLFSFetch, handleGitRevert, } from "./handlers/index.js";