get_branch_analysis
Analyze Git repository branches to identify relationships, merging status, commit counts, and recent activity for better project management.
Instructions
Get comprehensive branch analysis using GitPython.
Provides information about:
All local and remote branches
Branch relationships and merging status
Commit counts per branch
Last activity per branch
Args: repo_path: Path to git repository
Returns: Dict containing branch analysis
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes |
Implementation Reference
- The main handler function for the 'get_branch_analysis' tool. Decorated with @mcp.tool(), it uses GitPython via CommitzenService to analyze local and remote branches, commit counts, last activity, and returns structured analysis data.@mcp.tool() @handle_errors(log_errors=True) def get_branch_analysis(repo_path: str) -> Dict[str, Any]: """ Get comprehensive branch analysis using GitPython. Provides information about: - All local and remote branches - Branch relationships and merging status - Commit counts per branch - Last activity per branch Args: repo_path: Path to git repository Returns: Dict containing branch analysis """ # Initialize service for the specified repository try: target_service = CommitzenService(repo_path=repo_path) except Exception as e: raise RepositoryError( f"Failed to initialize service for repository '{repo_path}'", repo_path=repo_path, cause=e ) if not target_service.git_enabled: # For backward compatibility with tests return { "success": False, "error": "Branch analysis requires GitPython", "repository_path": repo_path } try: repo = target_service.git_service.repo # Analyze local branches local_branches = [] for branch in repo.branches: try: last_commit = branch.commit commit_count = sum(1 for _ in repo.iter_commits(branch)) local_branches.append({ "name": branch.name, "is_current": branch == repo.active_branch, "last_commit": { "sha": last_commit.hexsha[:8], "message": last_commit.summary, "author": last_commit.author.name, "date": last_commit.committed_datetime.isoformat() }, "commit_count": commit_count }) except Exception as e: logger.warning(f"Could not analyze branch {branch.name}: {e}") # Analyze remote branches remote_branches = [] try: for remote in repo.remotes: for ref in remote.refs: if ref.name.endswith('/HEAD'): continue try: last_commit = ref.commit remote_branches.append({ "name": ref.name, "remote": remote.name, "last_commit": { "sha": last_commit.hexsha[:8], "message": last_commit.summary, "author": last_commit.author.name, "date": last_commit.committed_datetime.isoformat() } }) except Exception as e: logger.warning(f"Could not analyze remote branch {ref.name}: {e}") except Exception as e: logger.warning(f"Could not analyze remote branches: {e}") return create_success_response({ "repository_path": repo_path, "implementation": target_service.git_implementation, "branch_analysis": { "current_branch": repo.active_branch.name if repo.active_branch else "HEAD (detached)", "local_branches": { "branches": local_branches, "count": len(local_branches) }, "remote_branches": { "branches": remote_branches, "count": len(remote_branches) }, "summary": { "total_branches": len(local_branches) + len(remote_branches), "local_count": len(local_branches), "remote_count": len(remote_branches) } } }) except Exception as e: logger.error(f"Failed to get branch analysis: {e}") raise ServiceError( f"Failed to get branch analysis: {e}", service_name="get_branch_analysis", cause=e )
- src/commit_helper_mcp/mcp_server.py:54-60 (registration)Import statement in the main MCP server file that brings in the get_branch_analysis handler. Since the function is decorated with @mcp.tool(), this import registers the tool with the MCP server instance.from .server.enhanced_tools import ( analyze_repository_health, get_detailed_diff_analysis, get_branch_analysis, smart_commit_suggestion, batch_commit_analysis, )