git_diff
Compare changes between Git branches or commits to review modifications before merging or deploying code.
Instructions
Shows differences between branches or commits
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| target | Yes | ||
| context_lines | No |
Implementation Reference
- Core handler function that computes the git diff against a specified target refdef 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)
- Pydantic input schema for the git_diff toolclass GitDiff(BaseModel): repo_path: str target: str context_lines: int = DEFAULT_CONTEXT_LINES
- src/git/src/mcp_server_git/server.py:302-306 (registration)Registration of the git_diff tool in the MCP server's list_tools methodTool( name=GitTools.DIFF, description="Shows differences between branches or commits", inputSchema=GitDiff.model_json_schema(), ),
- MCP call_tool dispatcher case that executes the git_diff tool and formats the responsecase 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}" )]