git_diff
Compare differences between Git branches or commits. Specify a repository path and target branch or commit to view line-by-line changes.
Instructions
Shows differences between branches or commits
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| target | Yes | ||
| context_lines | No |
Implementation Reference
- The git_diff function that executes the diff logic. Uses GitPython's repo.git.diff() to show differences between branches or commits, with security checks against flag injection (rejects targets starting with '-') and validates the target is a real git ref via repo.rev_parse().
def git_diff(repo: git.Repo, target: str, context_lines: int = DEFAULT_CONTEXT_LINES) -> str: # Defense in depth: reject targets starting with '-' to prevent flag injection, # even if a malicious ref with that name exists (e.g. via filesystem manipulation) if target.startswith("-"): raise BadName(f"Invalid target: '{target}' - cannot start with '-'") repo.rev_parse(target) # Validates target is a real git ref, throws BadName if not return repo.git.diff(f"--unified={context_lines}", target) - GitDiff Pydantic BaseModel schema defining input parameters: repo_path (str), target (str), and optional context_lines (int, default 3).
class GitDiff(BaseModel): repo_path: str target: str context_lines: int = DEFAULT_CONTEXT_LINES - src/git/src/mcp_server_git/server.py:342-352 (registration)Tool registration under the name 'git_diff' (from GitTools.DIFF enum value 'git_diff') with description 'Shows differences between branches or commits', using GitDiff.model_json_schema() for input validation and readOnlyHint=True.
Tool( name=GitTools.DIFF, description="Shows differences between branches or commits", inputSchema=GitDiff.model_json_schema(), annotations=ToolAnnotations( readOnlyHint=True, destructiveHint=False, idempotentHint=True, openWorldHint=False, ), ), - src/git/src/mcp_server_git/server.py:504-509 (registration)The call_tool handler that dispatches the 'git_diff' tool name to the git_diff function, passing arguments['target'] and context_lines.
case GitTools.DIFF: diff = git_diff(repo, arguments["target"], arguments.get("context_lines", DEFAULT_CONTEXT_LINES)) return [TextContent( type="text", text=f"Diff with {arguments['target']}:\n{diff}" )] - GitTools enum defining DIFF = 'git_diff' as the constant used for the tool name throughout the code.
class GitTools(str, Enum): STATUS = "git_status" DIFF_UNSTAGED = "git_diff_unstaged" DIFF_STAGED = "git_diff_staged" DIFF = "git_diff" COMMIT = "git_commit" ADD = "git_add" RESET = "git_reset" LOG = "git_log" CREATE_BRANCH = "git_create_branch" CHECKOUT = "git_checkout" SHOW = "git_show" BRANCH = "git_branch"