Skip to main content
Glama
bsreeram08

Git Repo Browser MCP

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
NameRequiredDescriptionDefault
repo_pathYesThe path to the local Git repository
actionYesHook action (list, get, create, enable, disable)list
hook_nameNoName of the hook (e.g., 'pre-commit', 'post-merge')
scriptNoScript content for the hook (for create action)

Implementation Reference

  • 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,
        };
      }
    }
  • 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,
  • 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,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bsreeram08/git-commands-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server