git_branch_diff
Compare two Git branches to identify files changed between them. Use this tool to analyze differences in code, track modifications, and review changes before merging branches in a repository.
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 | |
| source_branch | Yes | The source branch name | |
| target_branch | Yes | The target branch name | |
| show_patch | No | Whether to include the actual diff patches |
Implementation Reference
- src/handlers/branch-operations.js:12-77 (handler)The main handler function that executes the git_branch_diff tool. It clones the repository, ensures branches are available, computes the diff between source and target branches (with optional patch), gets commit count, and returns a JSON-formatted result or error.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)The MCP tool schema definition for 'git_branch_diff', including description and input schema with properties repo_url, source_branch, target_branch, and optional show_patch.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)Registers the tool name 'git_branch_diff' mapped to the handleGitBranchDiff handler function in the server's handlersMap.git_branch_diff: handleGitBranchDiff,
- src/handlers/index.js:14-19 (registration)Imports the handleGitBranchDiff handler from branch-operations.js into the handlers index module, facilitating its use in the server.import { handleGitBranchDiff, handleGitCheckoutBranch, handleGitDeleteBranch, handleGitMergeBranch, } from "./branch-operations.js";