diff --git a/backend/steps/git.py b/backend/steps/git.py index 7f0f8a0..efe5df2 100644 --- a/backend/steps/git.py +++ b/backend/steps/git.py @@ -1,8 +1,9 @@ import tempfile import git +from git.objects import Commit import logging from dataclasses import dataclass, asdict, field -from typing import List, Optional, Union, Tuple +from typing import List, Optional, Union, Tuple, Set, Iterator, Dict from steps.step import AbstractStep import tempfile import os @@ -22,9 +23,13 @@ class GitClone(AbstractStep[str]): if os.path.exists(os.path.join(self.temp_dir, ".git")): logger.info(f"Repository already cloned at {self.temp_dir}, fetching updates.") repo = git.Repo(self.temp_dir) - repo.remotes.origin.fetch() + result = repo.remotes.origin.fetch() + if not result or any(fetch_info.flags & fetch_info.ERROR for fetch_info in result): + raise BaseException(f"Failed to fetch updates for {self.env.repo}") else: git.Repo.clone_from(self.env.repo, self.temp_dir) + if not os.path.exists(os.path.join(self.temp_dir, ".git")): + raise BaseException(f"Failed to clone repository {self.env.repo} to {self.temp_dir}") return self.temp_dir @@ -39,6 +44,27 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]): self.wd = wd self.branches = branches + def _commits_tree(self, repo: git.Repo) -> Dict[Commit, List[Commit]]: + childs: Dict[Commit, List[Commit]] = {} + for c in list(repo.iter_commits('--all')): + for p in c.parents: + childs.setdefault(p, []).append(c) + return childs + + def _find_merge_childs(self, tree: Dict[Commit, List[Commit]], commit: Commit) -> List[Commit]: + + visited = [commit] + queue = [commit] + queue = [child for c in queue for child in tree.get(c, [])] + + while queue: + current = queue.pop(0) + visited.append(current) + for child in tree.get(current, []): + if len(child.parents) != 1: + queue.append(child) + + return visited def progress(self) -> Tuple[str, str]: if self.wd.result_obj is BaseException: @@ -56,20 +82,44 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]): logger.info(f"Selected branches: {self.branches}") # Extract commit ids for the selected branches - commit_ids = [] + commit_ids: Dict[Commit, str] = {} for branch in self.branches: commit = repo.commit(f'origin/{branch}') - commit_ids.append(commit.hexsha) + commit_ids[commit] = branch - # Calculate hash by commits (sorted for determinism) - sorted_commits = sorted(commit_ids) - commit_hash = hashlib.sha1(''.join(sorted_commits).encode()).hexdigest() - logger.info(f"Commit ids for branches: {dict(zip(self.branches, commit_ids))}") - logger.info(f"Hash by commits: {commit_hash}") - # Generate hash from branch names - # sorted_branches = sorted(self.branches) - # branch_hash = hashlib.sha1(''.join(sorted_branches).encode()).hexdigest() - auto_branch_name = f"auto/{commit_hash}" + tree = self._commits_tree(repo) + + merge_commit = [(c, self._find_merge_childs(tree, c)) for c in commit_ids.keys()] + result = merge_commit[0] + for c, l in merge_commit[1:]: + inters = set(result).intersection(set(l)) + if len(inters) == 0: + break + if len(result) > 0: + merge_commit = [c for c in merge_commit[0][1] if c in result][0] + logger.info(f"Common commit found {merge_commit}") + else: + merge_commit = None + logger.info(f"Common commit not found") + + if merge_commit is not None: + for head in repo.branches: + if head.commit.hexsha == merge_commit.hexsha: + logger.info(f"Merge commit {merge_commit} corresponds to branch {head}") + return (head.name, merge_commit.hexsha) + sorted_commits = sorted(commit_ids.keys(), key=lambda x: x.hexsha) + auto_branch_hash = hashlib.sha1(''.join([x.hexsha for x in sorted_commits]).encode()).hexdigest() + logger.info(f"Commit ids for branches: {commit_ids}") + + auto_branch_name = f"auto/{auto_branch_hash}" + + if merge_commit is not None: + repo.git.branch('-f', auto_branch_name, merge_commit.hexsha) + repo.git.checkout(auto_branch_name) + repo.git.push('-f', 'origin', auto_branch_name) + logger.info(f"Head is {repo.head.commit}") + logger.info(f"Pushed {merge_commit} to {auto_branch_name}") + return (auto_branch_name, merge_commit.hexsha) logger.info(f"Looking for existing auto branch: {auto_branch_name}") @@ -79,9 +129,9 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]): # Auto branch exists, fetch it logger.info(f"Found existing auto branch: {auto_branch_name}") repo.git.checkout(auto_branch_name) - commit_hash = repo.head.commit.hexsha + auto_branch_hash = repo.head.commit.hexsha - return (auto_branch_name, commit_hash) + return (auto_branch_name, auto_branch_hash) # Auto branch doesn't exist, create it logger.info(f"Creating new auto branch: {auto_branch_name}") @@ -91,7 +141,7 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]): repo.git.checkout(base_branch) # Create temporary branch for merging - temp_branch = f"temp-merge-{commit_hash}" + temp_branch = f"temp-merge-{auto_branch_hash}" # Remove temp_branch if it exists if temp_branch in repo.heads: repo.git.branch('-D', temp_branch) @@ -114,7 +164,7 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]): # Create and push the auto branch repo.git.branch('-f', auto_branch_name) repo.git.checkout(auto_branch_name) - commit_hash = repo.head.commit.hexsha + auto_branch_hash = repo.head.commit.hexsha repo.git.push('-f', 'origin', auto_branch_name) - return (auto_branch_name,commit_hash) + return (auto_branch_name,auto_branch_hash) diff --git a/backend/steps/step.py b/backend/steps/step.py index d13d668..dfd0434 100644 --- a/backend/steps/step.py +++ b/backend/steps/step.py @@ -34,7 +34,7 @@ class AbstractStep(ABC, Generic[T]): try: self._result = self.progress() except Exception as e: - logger.error(f"Job failed: {e}") + logger.error(f"Job failed",exc_info=e) self._result = e @abstractmethod