git_hooks
Manage Git hooks in repositories to automate tasks like pre-commit checks, post-merge actions, and workflow enforcement.
Instructions
Manage git hooks in the repository.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | The path to the local Git repository | |
| action | Yes | Hook action (list, get, create, enable, disable) | list |
| hook_name | No | Name of the hook (e.g., 'pre-commit', 'post-merge') | |
| script | No | Script content for the hook (for create action) |
Implementation Reference
- src/handlers/other-operations.js:11-205 (handler)Core handler function implementing git_hooks tool logic for listing, getting, and creating Git hooks in a repository.export async function handleGitHooks({ repo_path, action, hook_name = "", script = "", }) { try { // Path to the hooks directory const hooksDir = path.join(repo_path, ".git", "hooks"); switch (action) { case "list": // Get all available hooks const files = await fs.readdir(hooksDir); const hooks = []; for (const file of files) { // Filter out sample hooks if (!file.endsWith(".sample")) { const hookPath = path.join(hooksDir, file); const stats = await fs.stat(hookPath); hooks.push({ name: file, path: hookPath, size: stats.size, executable: (stats.mode & 0o111) !== 0, // Check if executable }); } } return { content: [ { type: "text", text: JSON.stringify( { success: true, hooks: hooks, }, null, 2 ), }, ], }; case "get": if (!hook_name) { return { content: [ { type: "text", text: JSON.stringify( { error: "Hook name is required for get action" }, null, 2 ), }, ], isError: true, }; } const hookPath = path.join(hooksDir, hook_name); // Check if hook exists if (!(await fs.pathExists(hookPath))) { return { content: [ { type: "text", text: JSON.stringify( { error: `Hook '${hook_name}' does not exist` }, null, 2 ), }, ], isError: true, }; } // Read hook content const hookContent = await fs.readFile(hookPath, "utf8"); const stats = await fs.stat(hookPath); return { content: [ { type: "text", text: JSON.stringify( { success: true, name: hook_name, content: hookContent, executable: (stats.mode & 0o111) !== 0, }, null, 2 ), }, ], }; case "create": if (!hook_name) { return { content: [ { type: "text", text: JSON.stringify( { error: "Hook name is required for create action" }, null, 2 ), }, ], isError: true, }; } if (!script) { return { content: [ { type: "text", text: JSON.stringify( { error: "Script content is required for create action" }, null, 2 ), }, ], isError: true, }; } const createHookPath = path.join(hooksDir, hook_name); // Write hook content await fs.writeFile(createHookPath, script); // Make hook executable await fs.chmod(createHookPath, 0o755); return { content: [ { type: "text", text: JSON.stringify( { success: true, message: `Created hook '${hook_name}'`, name: hook_name, executable: true, }, null, 2 ), }, ], }; default: return { content: [ { type: "text", text: JSON.stringify( { error: `Unknown hook action: ${action}` }, null, 2 ), }, ], isError: true, }; } } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to manage hook: ${error.message}` }, null, 2 ), }, ], isError: true, }; } }
- src/server.js:742-770 (schema)JSON schema defining the input parameters and structure for the git_hooks tool.{ name: "git_hooks", description: "Manage git hooks in the repository.", inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "The path to the local Git repository", }, action: { type: "string", description: "Hook action (list, get, create, enable, disable)", default: "list", enum: ["list", "get", "create", "enable", "disable"], }, hook_name: { type: "string", description: "Name of the hook (e.g., 'pre-commit', 'post-merge')", }, script: { type: "string", description: "Script content for the hook (for create action)", }, }, required: ["repo_path", "action"], }, },
- src/server.js:923-923 (registration)Maps the 'git_hooks' tool name to its handler function handleGitHooks in the central handlersMap.git_hooks: handleGitHooks,
- src/handlers/index.js:83-83 (registration)Re-exports the handleGitHooks function from other-operations.js for use in server.js.handleGitHooks,
- src/server.js:34-34 (registration)Imports handleGitHooks from handlers/index.js into server.js.handleGitHooks,