rebase rework
This commit is contained in:
@@ -20,6 +20,7 @@ import shutil
|
||||
from git import Repo
|
||||
from git.exc import GitCommandError
|
||||
from git.exc import InvalidGitRepositoryError, NoSuchPathError
|
||||
from git.objects.blob import Blob
|
||||
from fastapi import HTTPException
|
||||
from typing import Any, Dict, List, Optional
|
||||
from datetime import datetime
|
||||
@@ -834,6 +835,204 @@ class GitService:
|
||||
# @PURPOSE: Pull changes from remote.
|
||||
# @PRE: Repository exists and has an 'origin' remote.
|
||||
# @POST: Changes from origin are pulled and merged into the active branch.
|
||||
def _read_blob_text(self, blob: Blob) -> str:
|
||||
with belief_scope("GitService._read_blob_text"):
|
||||
if blob is None:
|
||||
return ""
|
||||
try:
|
||||
return blob.data_stream.read().decode("utf-8", errors="replace")
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def _get_unmerged_file_paths(self, repo: Repo) -> List[str]:
|
||||
with belief_scope("GitService._get_unmerged_file_paths"):
|
||||
try:
|
||||
return sorted(list(repo.index.unmerged_blobs().keys()))
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def _build_unfinished_merge_payload(self, repo: Repo) -> Dict[str, Any]:
|
||||
with belief_scope("GitService._build_unfinished_merge_payload"):
|
||||
merge_head_path = os.path.join(repo.git_dir, "MERGE_HEAD")
|
||||
merge_head_value = ""
|
||||
merge_msg_preview = ""
|
||||
current_branch = "unknown"
|
||||
try:
|
||||
merge_head_value = Path(merge_head_path).read_text(encoding="utf-8").strip()
|
||||
except Exception:
|
||||
merge_head_value = "<unreadable>"
|
||||
try:
|
||||
merge_msg_path = os.path.join(repo.git_dir, "MERGE_MSG")
|
||||
if os.path.exists(merge_msg_path):
|
||||
merge_msg_preview = (
|
||||
Path(merge_msg_path).read_text(encoding="utf-8").strip().splitlines()[:1] or [""]
|
||||
)[0]
|
||||
except Exception:
|
||||
merge_msg_preview = "<unreadable>"
|
||||
try:
|
||||
current_branch = repo.active_branch.name
|
||||
except Exception:
|
||||
current_branch = "detached_or_unknown"
|
||||
|
||||
conflicts_count = len(self._get_unmerged_file_paths(repo))
|
||||
return {
|
||||
"error_code": "GIT_UNFINISHED_MERGE",
|
||||
"message": (
|
||||
"В репозитории есть незавершённое слияние. "
|
||||
"Завершите или отмените слияние вручную."
|
||||
),
|
||||
"repository_path": repo.working_tree_dir,
|
||||
"git_dir": repo.git_dir,
|
||||
"current_branch": current_branch,
|
||||
"merge_head": merge_head_value,
|
||||
"merge_message_preview": merge_msg_preview,
|
||||
"conflicts_count": conflicts_count,
|
||||
"next_steps": [
|
||||
"Откройте локальный репозиторий по пути repository_path",
|
||||
"Проверьте состояние: git status",
|
||||
"Разрешите конфликты и выполните commit, либо отмените: git merge --abort",
|
||||
"После завершения/отмены слияния повторите Pull из интерфейса",
|
||||
],
|
||||
"manual_commands": [
|
||||
"git status",
|
||||
"git add <resolved-files>",
|
||||
"git commit -m \"resolve merge conflicts\"",
|
||||
"git merge --abort",
|
||||
],
|
||||
}
|
||||
|
||||
def get_merge_status(self, dashboard_id: int) -> Dict[str, Any]:
|
||||
with belief_scope("GitService.get_merge_status"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
merge_head_path = os.path.join(repo.git_dir, "MERGE_HEAD")
|
||||
if not os.path.exists(merge_head_path):
|
||||
current_branch = "unknown"
|
||||
try:
|
||||
current_branch = repo.active_branch.name
|
||||
except Exception:
|
||||
current_branch = "detached_or_unknown"
|
||||
return {
|
||||
"has_unfinished_merge": False,
|
||||
"repository_path": repo.working_tree_dir,
|
||||
"git_dir": repo.git_dir,
|
||||
"current_branch": current_branch,
|
||||
"merge_head": None,
|
||||
"merge_message_preview": None,
|
||||
"conflicts_count": 0,
|
||||
}
|
||||
payload = self._build_unfinished_merge_payload(repo)
|
||||
return {
|
||||
"has_unfinished_merge": True,
|
||||
"repository_path": payload["repository_path"],
|
||||
"git_dir": payload["git_dir"],
|
||||
"current_branch": payload["current_branch"],
|
||||
"merge_head": payload["merge_head"],
|
||||
"merge_message_preview": payload["merge_message_preview"],
|
||||
"conflicts_count": int(payload.get("conflicts_count") or 0),
|
||||
}
|
||||
|
||||
def get_merge_conflicts(self, dashboard_id: int) -> List[Dict[str, Any]]:
|
||||
with belief_scope("GitService.get_merge_conflicts"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
conflicts = []
|
||||
unmerged = repo.index.unmerged_blobs()
|
||||
for file_path, stages in unmerged.items():
|
||||
mine_blob = None
|
||||
theirs_blob = None
|
||||
for stage, blob in stages:
|
||||
if stage == 2:
|
||||
mine_blob = blob
|
||||
elif stage == 3:
|
||||
theirs_blob = blob
|
||||
conflicts.append(
|
||||
{
|
||||
"file_path": file_path,
|
||||
"mine": self._read_blob_text(mine_blob) if mine_blob else "",
|
||||
"theirs": self._read_blob_text(theirs_blob) if theirs_blob else "",
|
||||
}
|
||||
)
|
||||
return sorted(conflicts, key=lambda item: item["file_path"])
|
||||
|
||||
def resolve_merge_conflicts(self, dashboard_id: int, resolutions: List[Dict[str, Any]]) -> List[str]:
|
||||
with belief_scope("GitService.resolve_merge_conflicts"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
resolved_files: List[str] = []
|
||||
repo_root = os.path.abspath(str(repo.working_tree_dir or ""))
|
||||
if not repo_root:
|
||||
raise HTTPException(status_code=500, detail="Repository working tree directory is unavailable")
|
||||
|
||||
for item in resolutions or []:
|
||||
file_path = str(item.get("file_path") or "").strip()
|
||||
strategy = str(item.get("resolution") or "").strip().lower()
|
||||
content = item.get("content")
|
||||
if not file_path:
|
||||
raise HTTPException(status_code=400, detail="resolution.file_path is required")
|
||||
if strategy not in {"mine", "theirs", "manual"}:
|
||||
raise HTTPException(status_code=400, detail=f"Unsupported resolution strategy: {strategy}")
|
||||
|
||||
if strategy == "mine":
|
||||
repo.git.checkout("--ours", "--", file_path)
|
||||
elif strategy == "theirs":
|
||||
repo.git.checkout("--theirs", "--", file_path)
|
||||
else:
|
||||
abs_target = os.path.abspath(os.path.join(repo_root, file_path))
|
||||
if abs_target != repo_root and not abs_target.startswith(repo_root + os.sep):
|
||||
raise HTTPException(status_code=400, detail=f"Invalid conflict file path: {file_path}")
|
||||
os.makedirs(os.path.dirname(abs_target), exist_ok=True)
|
||||
with open(abs_target, "w", encoding="utf-8") as file_obj:
|
||||
file_obj.write(str(content or ""))
|
||||
|
||||
repo.git.add(file_path)
|
||||
resolved_files.append(file_path)
|
||||
|
||||
return resolved_files
|
||||
|
||||
def abort_merge(self, dashboard_id: int) -> Dict[str, Any]:
|
||||
with belief_scope("GitService.abort_merge"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
try:
|
||||
repo.git.merge("--abort")
|
||||
except GitCommandError as e:
|
||||
details = str(e)
|
||||
lowered = details.lower()
|
||||
if "there is no merge to abort" in lowered or "no merge to abort" in lowered:
|
||||
return {"status": "no_merge_in_progress"}
|
||||
raise HTTPException(status_code=409, detail=f"Cannot abort merge: {details}")
|
||||
return {"status": "aborted"}
|
||||
|
||||
def continue_merge(self, dashboard_id: int, message: Optional[str] = None) -> Dict[str, Any]:
|
||||
with belief_scope("GitService.continue_merge"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
unmerged_files = self._get_unmerged_file_paths(repo)
|
||||
if unmerged_files:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"error_code": "GIT_MERGE_CONFLICTS_REMAIN",
|
||||
"message": "Невозможно завершить merge: остались неразрешённые конфликты.",
|
||||
"unresolved_files": unmerged_files,
|
||||
},
|
||||
)
|
||||
try:
|
||||
normalized_message = str(message or "").strip()
|
||||
if normalized_message:
|
||||
repo.git.commit("-m", normalized_message)
|
||||
else:
|
||||
repo.git.commit("--no-edit")
|
||||
except GitCommandError as e:
|
||||
details = str(e)
|
||||
lowered = details.lower()
|
||||
if "nothing to commit" in lowered:
|
||||
return {"status": "already_clean"}
|
||||
raise HTTPException(status_code=409, detail=f"Cannot continue merge: {details}")
|
||||
|
||||
commit_hash = ""
|
||||
try:
|
||||
commit_hash = repo.head.commit.hexsha
|
||||
except Exception:
|
||||
commit_hash = ""
|
||||
return {"status": "committed", "commit_hash": commit_hash}
|
||||
|
||||
def pull_changes(self, dashboard_id: int):
|
||||
with belief_scope("GitService.pull_changes"):
|
||||
repo = self.get_repo(dashboard_id)
|
||||
@@ -841,23 +1040,46 @@ class GitService:
|
||||
# Check for unfinished merge (MERGE_HEAD exists)
|
||||
merge_head_path = os.path.join(repo.git_dir, "MERGE_HEAD")
|
||||
if os.path.exists(merge_head_path):
|
||||
logger.warning(f"[pull_changes][Action] Unfinished merge detected for dashboard {dashboard_id}")
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail=(
|
||||
"В репозитории есть незавершённое слияние. "
|
||||
"Пожалуйста, завершите или отмените слияние вручную: "
|
||||
"1) Разрешите конфликты и выполните коммит, или "
|
||||
"2) Выполните 'git merge --abort' для отмены слияния."
|
||||
),
|
||||
payload = self._build_unfinished_merge_payload(repo)
|
||||
|
||||
logger.warning(
|
||||
"[pull_changes][Action] Unfinished merge detected for dashboard %s "
|
||||
"(repo_path=%s git_dir=%s branch=%s merge_head=%s merge_msg=%s)",
|
||||
dashboard_id,
|
||||
payload["repository_path"],
|
||||
payload["git_dir"],
|
||||
payload["current_branch"],
|
||||
payload["merge_head"],
|
||||
payload["merge_message_preview"],
|
||||
)
|
||||
raise HTTPException(status_code=409, detail=payload)
|
||||
|
||||
try:
|
||||
origin = repo.remote(name='origin')
|
||||
current_branch = repo.active_branch.name
|
||||
try:
|
||||
origin_urls = list(origin.urls)
|
||||
except Exception:
|
||||
origin_urls = []
|
||||
|
||||
logger.info(
|
||||
"[pull_changes][Action] Pull diagnostics dashboard=%s repo_path=%s branch=%s origin_urls=%s",
|
||||
dashboard_id,
|
||||
repo.working_tree_dir,
|
||||
current_branch,
|
||||
origin_urls,
|
||||
)
|
||||
|
||||
origin.fetch(prune=True)
|
||||
remote_ref = f"origin/{current_branch}"
|
||||
has_remote_branch = any(ref.name == remote_ref for ref in repo.refs)
|
||||
logger.info(
|
||||
"[pull_changes][Action] Pull remote branch check dashboard=%s branch=%s remote_ref=%s exists=%s",
|
||||
dashboard_id,
|
||||
current_branch,
|
||||
remote_ref,
|
||||
has_remote_branch,
|
||||
)
|
||||
if not has_remote_branch:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
|
||||
Reference in New Issue
Block a user