git_blame
Identify which commit introduced each line in a file to track changes and understand code evolution in Git repositories.
Instructions
Get blame information for a file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | The path to the local Git repository | |
| file_path | Yes | Path to the file | |
| rev | No | Revision to blame (default: HEAD) | HEAD |
Implementation Reference
- src/handlers/other-operations.js:485-577 (handler)The core handler function that runs `git blame --line-porcelain`, parses the detailed output, and returns structured blame information for each line including commit hash, author, time, and content.export async function handleGitBlame({ repo_path, file_path, rev = "HEAD" }) { try { const git = simpleGit(repo_path); // Run git blame const blameResult = await git.raw([ "blame", "--line-porcelain", rev, "--", file_path, ]); // Parse the output const lines = blameResult.split("\n"); const blameInfo = []; let currentCommit = null; for (let i = 0; i < lines.length; i++) { const line = lines[i]; // Start of a new blame entry if (line.match(/^[0-9a-f]{40}/)) { if (currentCommit) { blameInfo.push(currentCommit); } const parts = line.split(" "); currentCommit = { hash: parts[0], originalLine: parseInt(parts[1]), finalLine: parseInt(parts[2]), lineCount: parseInt(parts[3] || 1), author: "", authorMail: "", authorTime: 0, subject: "", content: "", }; } else if (line.startsWith("author ") && currentCommit) { currentCommit.author = line.substring(7); } else if (line.startsWith("author-mail ") && currentCommit) { currentCommit.authorMail = line.substring(12).replace(/[<>]/g, ""); } else if (line.startsWith("author-time ") && currentCommit) { currentCommit.authorTime = parseInt(line.substring(12)); } else if (line.startsWith("summary ") && currentCommit) { currentCommit.subject = line.substring(8); } else if (line.startsWith("\t") && currentCommit) { // This is the content line currentCommit.content = line.substring(1); blameInfo.push(currentCommit); currentCommit = null; } } // Add the last commit if there is one if (currentCommit) { blameInfo.push(currentCommit); } return { content: [ { type: "text", text: JSON.stringify( { success: true, file: file_path, blame: blameInfo, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to get blame information: ${error.message}` }, null, 2 ), }, ], isError: true, }; } }
- src/server.js:686-708 (schema)Tool schema definition specifying the name, description, and input parameters (repo_path, file_path, rev) for the git_blame tool, used in ListTools response.{ name: "git_blame", description: "Get blame information for a file.", inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "The path to the local Git repository", }, file_path: { type: "string", description: "Path to the file", }, rev: { type: "string", description: "Revision to blame (default: HEAD)", default: "HEAD", }, }, required: ["repo_path", "file_path"], }, },
- src/server.js:898-927 (registration)Maps the tool name 'git_blame' to its handler function handleGitBlame in the central handlersMap used by the MCP server to dispatch tool calls.this.handlersMap = { // Primary handlers git_directory_structure: handleGitDirectoryStructure, git_read_files: handleGitReadFiles, git_branch_diff: handleGitBranchDiff, git_commit_history: handleGitCommitHistory, git_commits_details: handleGitCommitsDetails, git_local_changes: handleGitLocalChanges, git_search_code: handleGitSearchCode, git_commit: handleGitCommit, git_track: handleGitTrack, git_checkout_branch: handleGitCheckoutBranch, git_delete_branch: handleGitDeleteBranch, git_merge_branch: handleGitMergeBranch, git_push: handleGitPush, git_pull: handleGitPull, git_stash: handleGitStash, git_create_tag: handleGitCreateTag, git_rebase: handleGitRebase, git_config: handleGitConfig, git_reset: handleGitReset, git_archive: handleGitArchive, git_attributes: handleGitAttributes, git_blame: handleGitBlame, git_clean: handleGitClean, git_hooks: handleGitHooks, git_lfs: handleGitLFS, git_lfs_fetch: handleGitLFSFetch, git_revert: handleGitRevert, };
- src/handlers/index.js:29-38 (registration)Imports handleGitBlame from other-operations.js into the index barrel file for centralized access.import { handleGitArchive, handleGitAttributes, handleGitBlame, handleGitClean, handleGitHooks, handleGitLFS, handleGitLFSFetch, handleGitRevert, } from "./other-operations.js";
- src/handlers/index.js:78-86 (helper)Re-exports handleGitBlame from the handlers index for convenient import in server.js.// Other operations handleGitArchive, handleGitAttributes, handleGitBlame, handleGitClean, handleGitHooks, handleGitLFS, handleGitLFSFetch, handleGitRevert,