From 91be3803b6a48229a880a6ba3e669be2cd955fda Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 28 Aug 2025 15:18:05 +0200 Subject: [PATCH] refs #2506 -- refactoring --- ogclient/lib/python3/GitLib/__init__.py | 67 +++++++------------------ 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/ogclient/lib/python3/GitLib/__init__.py b/ogclient/lib/python3/GitLib/__init__.py index b0a8352..c86afc1 100755 --- a/ogclient/lib/python3/GitLib/__init__.py +++ b/ogclient/lib/python3/GitLib/__init__.py @@ -1491,67 +1491,36 @@ class OpengnsysGitLibrary: if branch: + # We've got a nearby branch, start from that + self.logger.debug("Cloning repo %s, branch %s", repo_url, branch) repo = git.Repo.clone_from(repo_url, destination_dir, branch = branch, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + else: + # Start from main instead + self.logger.debug("Cloning repo %s", repo_url) + repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + self.logger.debug("Checking out indicated branch %s", branch) remote_branch_ref = repo.heads[branch] if branch in repo.heads: - self.logger.debug("Removing existing local branch %s", branch) - repo.delete_head(branch) + self.logger.debug("Local branch %s exists, checking it out first", branch) + repo.git.checkout(branch) - if ref: - self.logger.debug("Local branch adjusted to ref %s", ref) - local_ref = repo.create_head(branch, ref) + self.logger.debug("Resetting branch %s to ref %s", branch, ref) + repo.git.reset("--hard", ref) else: - self.logger.debug("Local branch is set to remote branch %s", remote_branch_ref) - local_ref = repo.create_head(branch, remote_branch_ref) - local_ref.set_tracking_branch(remote_branch_ref) + if ref: + self.logger.debug("Local branch adjusted to ref %s", ref) + local_ref = repo.create_head(branch, ref) + else: + self.logger.debug("Local branch is set to remote branch %s", remote_branch_ref) + local_ref = repo.create_head(branch, remote_branch_ref) + local_ref.set_tracking_branch(remote_branch_ref) self.logger.debug("Checking out local branch %s", branch) local_ref.checkout() - else: - repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) - - branches_with_commit = repo.git.branch("-r", "--contains", ref).split("\n") - self.logger.debug("Branches with commit: %s", branches_with_commit) - - if len(branches_with_commit) > 0: - remote_branch = branches_with_commit[0].strip() - if "->" in remote_branch: - # Git returned something like: - # origin/HEAD -> origin/main - # - # so take the second part. - remote_branch = remote_branch.split("->")[1].strip() - - parts = remote_branch.split("/") - self.logger.info(f"Branch: {remote_branch}, parts: {parts}") - local_branch_name = parts[1] - - if local_branch_name in repo.heads: - #self.logger.debug("Removing existing local branch %s", local_branch_name) - #repo.git.reset("--hard") - #repo.delete_head(local_branch_name) - self.logger.debug("Local branch %s exists, checking it out first", local_branch_name) - repo.git.checkout(local_branch_name) - - self.logger.debug("Resetting branch %s to ref %s", local_branch_name, ref) - repo.git.reset("--hard", ref) - else: - self.logger.info("Checking out containing remote branch %s, as %s", remote_branch, local_branch_name) - repo.git.checkout("-b", local_branch_name, remote_branch) - - try: - self.logger.info("Setting upstream: %s", local_branch_name) - repo.git.branch(f"--set-upstream-to={remote_branch}", local_branch_name) - except Exception as ex: - self.logger.error("Setting upstream failed: %s", ex) - - else: - self.logger.info("Checking out REF %s", ref) - repo.git.checkout(ref) self.fs.mklostandfound(destination_dir) self._restore_metadata(destination_dir, set_device_uuids=True)