| 1 | from __future__ import annotations |
| 2 | |
| 3 | from helpers.api import ApiHandler, Request, Response |
| 4 | from plugins._time_travel.helpers.time_travel import ( |
| 5 | TimeTravelError, |
| 6 | TimeTravelService, |
| 7 | WorkspaceRejectedError, |
| 8 | resolve_workspace, |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | class HistoryRevert(ApiHandler): |
| 13 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 14 | context_id = str(input.get("context_id") or "").strip() |
| 15 | workspace_id = str(input.get("workspace_id") or "").strip() |
| 16 | try: |
| 17 | workspace = resolve_workspace( |
| 18 | context_id, |
| 19 | workspace_id=workspace_id, |
| 20 | context_loader=self.use_context, |
| 21 | ) |
| 22 | return TimeTravelService(workspace).revert( |
| 23 | commit_hash=str(input.get("commit_hash") or ""), |
| 24 | metadata=input.get("metadata") if isinstance(input.get("metadata"), dict) else {}, |
| 25 | ) |
| 26 | except WorkspaceRejectedError as exc: |
| 27 | return {"ok": False, "locked": True, "error": str(exc)} |
| 28 | except TimeTravelError as exc: |
| 29 | details = getattr(exc, "stderr", "") or str(exc) |
| 30 | return {"ok": False, "error": _human_conflict_summary(str(exc)), "technical_details": details} |
| 31 | |
| 32 | |
| 33 | def _human_conflict_summary(message: str) -> str: |
| 34 | text = str(message or "").strip() |
| 35 | if not text: |
| 36 | return "Revert could not be applied cleanly." |
| 37 | first = text.splitlines()[0] |
| 38 | if "does not match index" in text or "patch failed" in text.lower() or "error:" in text.lower(): |
| 39 | return "Revert could not be applied cleanly because the current workspace has conflicting changes." |
| 40 | return first |