""" Git 深度集成服务 ================ 【功能说明】 Git 仓库深度操作:状态查看、diff 对比、分支管理、日志、blame、自动提交。 对应用户需求:Agent 改码后自动 commit + 可视化 diff。 【技术选型理由】 1. 复用已有 git_tools(只读+写操作),统一入口; 2. 支持按任务分支隔离,改码自动 commit; 3. diff 对比供前端可视化展示。 """ from __future__ import annotations import logging import subprocess from pathlib import Path from app.tools.path_guard import ensure_under_root logger = logging.getLogger("agent.git_deep") def git_status(repo_path: str) -> dict: """查看工作区状态。""" try: root = ensure_under_root(Path(repo_path), "Git 操作") result = subprocess.run( ["git", "status", "--short"], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10, ) return {"success": True, "output": result.stdout or "(工作区干净)"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)} def git_diff(repo_path: str, file_path: str = "") -> dict: """查看工作区 diff(相对 HEAD)。""" try: root = ensure_under_root(Path(repo_path), "Git diff") cmd = ["git", "diff", "HEAD"] if file_path: cmd.append(file_path) result = subprocess.run( cmd, cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15, ) return {"success": True, "diff": result.stdout[:10000] or "(无差异)"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)} def git_commit(repo_path: str, message: str, task_id: str = "") -> dict: """提交工作区改动(自动 add -A + commit)。""" try: root = ensure_under_root(Path(repo_path), "Git 提交") subprocess.run(["git", "add", "-A"], cwd=str(root), capture_output=True, timeout=15) result = subprocess.run( ["git", "commit", "-m", message], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15, ) if result.returncode == 0: commit_hash = subprocess.run( ["git", "rev-parse", "--short", "HEAD"], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=5, ).stdout.strip() return {"success": True, "commit_hash": commit_hash, "message": f"提交成功:{commit_hash}"} return {"success": False, "message": result.stderr or "提交失败"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)} def git_log(repo_path: str, limit: int = 20) -> dict: """查看提交日志。""" try: root = ensure_under_root(Path(repo_path), "Git log") result = subprocess.run( ["git", "log", f"--max-count={limit}", "--oneline", "--graph"], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10, ) return {"success": True, "output": result.stdout or "(无提交记录)"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)} def git_branch(repo_path: str) -> dict: """查看分支列表。""" try: root = ensure_under_root(Path(repo_path), "Git branch") result = subprocess.run( ["git", "branch", "-a"], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10, ) return {"success": True, "output": result.stdout or "(无分支信息)"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)} def git_blame(repo_path: str, file_path: str) -> dict: """查看文件逐行变更记录(blame)。""" try: root = ensure_under_root(Path(repo_path), "Git blame") result = subprocess.run( ["git", "blame", file_path], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15, ) return {"success": True, "output": result.stdout[:5000] or "(无记录)"} except Exception as exc: # noqa: BLE001 return {"success": False, "message": str(exc)}