From 2d7bd5f9fb3edec01a2505e1728cd0aa41a541e1 Mon Sep 17 00:00:00 2001 From: Haitham-AbdelKarim Date: Wed, 13 Mar 2024 00:19:18 +0200 Subject: [PATCH] Make repo download function handle existing repos --- .../gh_processor/github_downloader.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/project_processor/gh_processor/github_downloader.py b/project_processor/gh_processor/github_downloader.py index 51ea2d7..242dfa3 100644 --- a/project_processor/gh_processor/github_downloader.py +++ b/project_processor/gh_processor/github_downloader.py @@ -27,8 +27,23 @@ def download_github_repo(repo_url: str, branch: str = "main") -> str: """ repo_name = repo_url.split("/")[-1].split(".")[0] repo_path = os.path.abspath(repo_name) + + if os.path.exists(repo_path): + repo = Repo(repo_path) + origin = repo.remote('origin') + + origin.fetch() + + head_ref = f"refs/remotes/origin/{branch}" + if repo.commit() != repo.commit(head_ref) or repo.is_dirty(untracked_files=True): + # Reset the local repository to match the last version on GitHub + repo.git.reset('--hard', 'HEAD') + logger.info(f"Repository {repo_name} is now synced with the last version on GitHub.") + else: + logger.info(f"Repository {repo_name} is already up-to-date.") + + else: + Repo.clone_from(repo_url, repo_name, branch=branch) + logger.info(f"Repository '{repo_name}' downloaded successfully!") - Repo.clone_from(repo_url, repo_name, branch=branch) - - logger.info(f"Repository '{repo_name}' downloaded successfully!") return repo_path