git_branch_diff
Compare changes between two branches in a Git repository to identify file differences, with an option to include detailed diff patches for precise analysis.
Instructions
Compare two branches and show files changed between them.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_url | Yes | The URL of the Git repository | |
| show_patch | No | Whether to include the actual diff patches | |
| source_branch | Yes | The source branch name | |
| target_branch | Yes | The target branch name |
Implementation Reference
- src/handlers/branch-operations.js:12-77 (handler)Core handler function that executes the git_branch_diff tool: clones repo, fetches branches if needed, computes diff summary or patch, gets commit count, and formats response.export async function handleGitBranchDiff({ repo_url, source_branch, target_branch, show_patch = false, }) { try { const repoPath = await cloneRepo(repo_url); const git = simpleGit(repoPath); // Make sure both branches exist locally const branches = await git.branch(); if (!branches.all.includes(source_branch)) { await git.fetch("origin", source_branch); await git.checkout(source_branch); } if (!branches.all.includes(target_branch)) { await git.fetch("origin", target_branch); } // Get the diff between branches const diffOptions = ["--name-status"]; if (show_patch) { diffOptions.push("--patch"); } const diff = await git.diff([ ...diffOptions, `${target_branch}...${source_branch}`, ]); // Get commit range information const logSummary = await git.log({ from: target_branch, to: source_branch, }); const result = { commits_count: logSummary.total, diff_summary: diff, }; return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to get branch diff: ${error.message}` }, null, 2 ), }, ], isError: true, }; }
- src/server.js:133-158 (schema)Input schema definition for git_branch_diff tool registration, specifying parameters and validation rules.name: "git_branch_diff", description: "Compare two branches and show files changed between them.", inputSchema: { type: "object", properties: { repo_url: { type: "string", description: "The URL of the Git repository", }, source_branch: { type: "string", description: "The source branch name", }, target_branch: { type: "string", description: "The target branch name", }, show_patch: { type: "boolean", description: "Whether to include the actual diff patches", default: false, }, }, required: ["repo_url", "source_branch", "target_branch"], },
- src/server.js:902-902 (registration)Maps the tool name 'git_branch_diff' to its handler function in the central handlersMap used by the MCP server.git_branch_diff: handleGitBranchDiff,
- src/server.js:13-13 (registration)Imports the handleGitBranchDiff handler from handlers/index.js for use in server.js registrations.handleGitBranchDiff,
- src/handlers/index.js:55-55 (registration)Re-exports handleGitBranchDiff from branch-operations.js to centralize imports in server.js.handleGitBranchDiff,