git-diff
Generate a diff between the current HEAD and a specified ancestor commit or branch to review code changes and understand modifications.
Instructions
Get a diff between the HEAD and the ancestor branch or commit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ancestor | Yes | The ancestor commit hash or branch name |
Implementation Reference
- src/git_prompts_mcp_server/server.py:304-307 (registration)Registration of the 'git-diff' tool using FastMCP's @APP.tool decorator.@APP.tool( name="git-diff", description="Get a diff between the HEAD and the ancestor branch or commit", )
- Thin wrapper handler function for the 'git-diff' tool that calls the GitMethodCollection's get_diff_data method.async def git_diff_tool( ancestor: str = Field(..., description="The ancestor commit hash or branch name"), ) -> list[dict]: return await GIT_METHOD_COLLETION.get_diff_data(ancestor)
- Core logic for fetching and formatting the git diff data between the ancestor commit and HEAD.async def get_diff_data(self, ancestor: str) -> list[dict]: if not ancestor: raise ValueError("Ancestor argument required") diff_results = _get_diff_results(self.repo.commit(ancestor), self.repo.head.commit, self.excludes) return _get_diff_results_as_list_of_dict(diff_results)
- Helper function that computes the raw git diff results and filters out excluded paths.def _get_diff_results( source_commit: git.Commit, target_commit: git.Commit | None, excludes: list[str] ) -> list[git.Diff]: if target_commit is None: # Note: source_commit.diff() compares source with the index (staged changes) # source_commit.diff(None) compares source with the working tree diff_results = source_commit.diff(create_patch=True) else: diff_results = source_commit.diff(target_commit, create_patch=True) for exclude_pattern in excludes: diff_results = [ item for item in diff_results if not fnmatch(item.a_path or "", exclude_pattern) and not fnmatch(item.b_path or "", exclude_pattern) ] return diff_results
- Helper function to convert git diff objects into a structured list of dictionaries.def _get_diff_results_as_list_of_dict(diff_results: list[git.Diff]) -> list[dict]: return [ { "a_path": item.a_path or "New Addition", "b_path": item.b_path or "Deleted", "diff": cast(bytes, item.diff).decode("utf-8"), } for item in diff_results ]