git_diff_branches
Compare changes between two branches in a Git repository to identify differences, enabling clear review and analysis of code updates.
Instructions
Show differences between two branches
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| base_branch | Yes | ||
| compare_branch | Yes | ||
| repo_path | Yes |
Implementation Reference
- The core handler function git_diff_branches that performs the git diff operation between two specified branches, including branch validation, stat-only mode, truncation for large diffs, and error handling.def git_diff_branches( repo: Repo, base_branch: str, compare_branch: str, stat_only: bool = False, max_lines: int | None = None, ) -> str: """Show differences between two branches with size limiting options""" try: # Verify branches exist all_branches = [branch.name for branch in repo.branches] + [ ref.name.split("/")[-1] for ref in repo.remote().refs ] if base_branch not in all_branches: return f"❌ Base branch '{base_branch}' not found" if compare_branch not in all_branches: return f"❌ Compare branch '{compare_branch}' not found" # Build diff command arguments diff_range = f"{base_branch}...{compare_branch}" if stat_only: # Return only file statistics diff_output = repo.git.diff("--stat", diff_range) if not diff_output.strip(): return f"No differences between {base_branch} and {compare_branch}" return f"Diff statistics between {base_branch} and {compare_branch}:\n{diff_output}" # Get full diff diff_output = repo.git.diff(diff_range) if not diff_output.strip(): return f"No differences between {base_branch} and {compare_branch}" # Apply line limit if specified if max_lines and max_lines > 0: lines = diff_output.split("\n") if len(lines) > max_lines: truncated_output = "\n".join(lines[:max_lines]) truncated_output += ( f"\n\n... [Truncated: showing {max_lines} of {len(lines)} lines]" ) truncated_output += "\nUse --stat flag for summary or increase max_lines for more content" return truncated_output # Check if output is extremely large and warn if len(diff_output) > 50000: # 50KB threshold lines_count = len(diff_output.split("\n")) warning = f"⚠️ Large diff detected ({lines_count} lines, ~{len(diff_output) // 1000}KB)\n" warning += "Consider using stat_only=true for summary or max_lines parameter to limit output\n\n" return warning + diff_output return diff_output except GitCommandError as e: return f"❌ Diff failed: {str(e)}" except Exception as e: return f"❌ Diff error: {str(e)}"
- Pydantic BaseModel defining the input schema/parameters for the git_diff_branches tool.class GitDiffBranches(BaseModel): repo_path: str base_branch: str compare_branch: str stat_only: bool | None = False max_lines: int | None = None
- src/mcp_server_git/core/handlers.py:116-119 (registration)Registration of the git_diff_branches handler in the CallToolHandler._get_git_handlers() method, wrapping the core function with error handling and repo validation."git_diff_branches": self._create_git_handler( git_diff_branches, requires_repo=True, extra_args=["base_branch", "compare_branch"],
- src/mcp_server_git/core/tools.py:286-292 (registration)ToolDefinition registration in ToolRegistry.initialize_default_tools(), associating the tool name, schema, and category.ToolDefinition( name=GitTools.DIFF_BRANCHES, category=ToolCategory.GIT, description="Show differences between two branches", schema=GitDiffBranches, handler=placeholder_handler, requires_repo=True,
- src/mcp_server_git/git/__init__.py:36-36 (registration)Export of the git_diff_branches function from the git module for use in handlers.git_diff_branches,