Skip to main content
Glama

git_attributes

Manage Git attributes for files in repositories to control how Git handles specific file patterns, enabling configuration of text conversion, diff behavior, and merge strategies.

Instructions

Manage git attributes for files.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
repo_pathYesThe path to the local Git repository
actionYesAction (get, set, list)list
patternNoFile pattern
attributeNoAttribute to set

Implementation Reference

  • Core implementation of the `handleGitAttributes` function, which handles list, get, and set actions for the .gitattributes file in a Git repository.
    export async function handleGitAttributes({ repo_path, action, pattern = "", attribute = "", }) { try { const attributesPath = path.join(repo_path, ".gitattributes"); switch (action) { case "list": // Check if .gitattributes exists if (!(await fs.pathExists(attributesPath))) { return { content: [ { type: "text", text: JSON.stringify( { success: true, attributes: [], message: ".gitattributes file does not exist", }, null, 2 ), }, ], }; } // Read and parse .gitattributes const content = await fs.readFile(attributesPath, "utf8"); const lines = content .split("\n") .filter((line) => line.trim() && !line.startsWith("#")); const attributes = lines.map((line) => { const parts = line.trim().split(/\s+/); return { pattern: parts[0], attributes: parts.slice(1), }; }); return { content: [ { type: "text", text: JSON.stringify( { success: true, attributes: attributes, }, null, 2 ), }, ], }; case "get": if (!pattern) { return { content: [ { type: "text", text: JSON.stringify( { error: "Pattern is required for get action" }, null, 2 ), }, ], isError: true, }; } // Check if .gitattributes exists if (!(await fs.pathExists(attributesPath))) { return { content: [ { type: "text", text: JSON.stringify( { success: true, pattern: pattern, attributes: [], message: ".gitattributes file does not exist", }, null, 2 ), }, ], }; } // Read and find pattern const getContent = await fs.readFile(attributesPath, "utf8"); const getLines = getContent.split("\n"); const matchingLines = getLines.filter((line) => { const parts = line.trim().split(/\s+/); return parts[0] === pattern; }); if (matchingLines.length === 0) { return { content: [ { type: "text", text: JSON.stringify( { success: true, pattern: pattern, attributes: [], message: `No attributes found for pattern '${pattern}'`, }, null, 2 ), }, ], }; } // Parse attributes const patternAttributes = matchingLines .map((line) => { const parts = line.trim().split(/\s+/); return parts.slice(1); }) .flat(); return { content: [ { type: "text", text: JSON.stringify( { success: true, pattern: pattern, attributes: patternAttributes, }, null, 2 ), }, ], }; case "set": if (!pattern) { return { content: [ { type: "text", text: JSON.stringify( { error: "Pattern is required for set action" }, null, 2 ), }, ], isError: true, }; } if (!attribute) { return { content: [ { type: "text", text: JSON.stringify( { error: "Attribute is required for set action" }, null, 2 ), }, ], isError: true, }; } // Check if .gitattributes exists, create if not if (!(await fs.pathExists(attributesPath))) { await fs.writeFile(attributesPath, ""); } // Read current content const setContent = await fs.readFile(attributesPath, "utf8"); const setLines = setContent.split("\n"); // Check if pattern already exists const patternIndex = setLines.findIndex((line) => { const parts = line.trim().split(/\s+/); return parts[0] === pattern; }); if (patternIndex !== -1) { // Update existing pattern const parts = setLines[patternIndex].trim().split(/\s+/); // Check if attribute already exists if (!parts.includes(attribute)) { parts.push(attribute); setLines[patternIndex] = parts.join(" "); } } else { // Add new pattern setLines.push(`${pattern} ${attribute}`); } // Write back to file await fs.writeFile( attributesPath, setLines.filter(Boolean).join("\n") + "\n" ); return { content: [ { type: "text", text: JSON.stringify( { success: true, message: `Set attribute '${attribute}' for pattern '${pattern}'`, pattern: pattern, attribute: attribute, }, null, 2 ), }, ], }; default: return { content: [ { type: "text", text: JSON.stringify( { error: `Unknown attributes action: ${action}` }, null, 2 ), }, ], isError: true, }; } } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to manage git attributes: ${error.message}` }, null, 2 ), }, ], isError: true, }; } }
  • Input schema definition for the 'git_attributes' tool, specifying parameters like repo_path, action, pattern, and attribute.
    { name: "git_attributes", description: "Manage git attributes for files.", inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "The path to the local Git repository", }, action: { type: "string", description: "Action (get, set, list)", default: "list", enum: ["get", "set", "list"], }, pattern: { type: "string", description: "File pattern", }, attribute: { type: "string", description: "Attribute to set", }, }, required: ["repo_path", "action"], }, },
  • src/server.js:920-920 (registration)
    Registration of the 'git_attributes' handler in the server's handlersMap, mapping the tool name to its implementation function.
    git_attributes: handleGitAttributes,
  • Import of handleGitAttributes from other-operations.js into the handlers index module.
    import { handleGitArchive, handleGitAttributes,
  • Re-export of handleGitAttributes from handlers/index.js for centralized access.
    handleGitAttributes,

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