""" Git Operations Tool - Version control for the Agent """ import subprocess import json from pathlib import Path from typing import Dict, Any, Optional import asyncio class GitOperationsTool: """Git version control operations""" def __init__(self, workspace_root: str = "./workspace"): self.workspace_root = Path(workspace_root).resolve() async def _run_git(self, args: list) -> Dict[str, Any]: """Run a git command""" try: cmd = ['git'] + args process = await asyncio.create_subprocess_exec( *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=str(self.workspace_root) ) stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=30) return { "success": process.returncode == 0, "output": stdout.decode('utf-8', errors='replace'), "error": stderr.decode('utf-8', errors='replace') if process.returncode != 0 else None, "command": ' '.join(cmd) } except asyncio.TimeoutError: return {"success": False, "error": "Git command timed out"} except Exception as e: return {"success": False, "error": f"Git error: {str(e)}"} async def status(self) -> Dict[str, Any]: """Get git status""" return await self._run_git(['status', '--porcelain']) async def diff(self, file_path: Optional[str] = None) -> Dict[str, Any]: """Get git diff""" args = ['diff'] if file_path: args.append(file_path) return await self._run_git(args) async def add(self, files: list) -> Dict[str, Any]: """Stage files""" return await self._run_git(['add'] + files) async def commit(self, message: str) -> Dict[str, Any]: """Create a commit""" return await self._run_git(['commit', '-m', message]) async def log(self, limit: int = 10) -> Dict[str, Any]: """Get commit history""" return await self._run_git(['log', '--oneline', f'-{limit}']) async def branch(self, name: Optional[str] = None) -> Dict[str, Any]: """List or create branches""" if name: return await self._run_git(['checkout', '-b', name]) return await self._run_git(['branch', '-a']) async def checkout(self, branch: str) -> Dict[str, Any]: """Checkout a branch""" return await self._run_git(['checkout', branch]) async def init(self) -> Dict[str, Any]: """Initialize git repository""" return await self._run_git(['init'])