compare_branches
Compare differences between two branches or commits in a GitLab project to analyze changes before merging or deploying.
Instructions
比較兩個分支或 commit 的差異
Args: project_id: 專案 ID 或路徑 from_ref: 起始分支/commit to_ref: 目標分支/commit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | ||
| from_ref | Yes | ||
| to_ref | Yes |
Implementation Reference
- src/gitlab_mcp/server.py:887-928 (handler)The tool handler that exposes `compare_branches` as an MCP tool and processes the response to format the user output.
@mcp.tool() def compare_branches(project_id: int | str, from_ref: str, to_ref: str) -> str: """比較兩個分支或 commit 的差異 Args: project_id: 專案 ID 或路徑 from_ref: 起始分支/commit to_ref: 目標分支/commit """ try: client = get_client() data = client.compare_branches(project_id, from_ref, to_ref) commits = data.get("commits", []) diffs = data.get("diffs", []) lines = [f"比較 {from_ref} → {to_ref}:"] lines.append(f"Commits: {len(commits)} 個 | 檔案變更: {len(diffs)} 個\n") if commits: lines.append("--- Commits ---") for c in commits[:20]: # 限制最多顯示 20 個 commits lines.append(f"- {c.get('short_id', 'N/A')} {c.get('title', 'N/A')}") if len(commits) > 20: lines.append(f"... 還有 {len(commits) - 20} 個 commits") if diffs: lines.append("\n--- 檔案變更 ---") for d in diffs: new_path = d.get("new_path", "") if d.get("new_file"): lines.append(f" [新增] {new_path}") elif d.get("deleted_file"): lines.append(f" [刪除] {new_path}") elif d.get("renamed_file"): lines.append(f" [重新命名] {d.get('old_path', '')} → {new_path}") else: lines.append(f" [修改] {new_path}") return "\n".join(lines) except GitLabAPIError as e: return f"比較分支失敗: {str(e)}" - The GitLab API client method that performs the network request to compare branches.
def compare_branches( self, project_id: int | str, from_ref: str, to_ref: str ) -> dict: """GET /projects/:id/repository/compare""" pid = self._resolve_project_id(project_id) params = {"from": from_ref, "to": to_ref} return self._get_json(f"/projects/{pid}/repository/compare", params=params)