Skip to content

Commit

Permalink
Add fallback to pull that also tries the HTTP URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ronald-jaepel committed Feb 11, 2025
1 parent c613200 commit df706f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 1 addition & 3 deletions cadetrdm/initialize_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,8 @@ def clone(project_url, path_to_repo: str = None, multi_options: List[str] = None
path_to_repo = project_url.split("/")[-1]
path_to_repo = path_to_repo.replace(".git", "")
print(f"Cloning {project_url} into {path_to_repo}")
git.Repo.clone_from(project_url, path_to_repo, multi_options=multi_options)

# During class instantiation, the output repo is cloned.
repo = ProjectRepo(path_to_repo)
repo = ProjectRepo.clone_from(to_path=path_to_repo, url=project_url, multi_options=multi_options)

repo.fill_data_from_cadet_rdm_json()

Expand Down
27 changes: 21 additions & 6 deletions cadetrdm/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from datetime import datetime
from pathlib import Path
from stat import S_IREAD, S_IWRITE
from typing import List
from typing import List, Optional
from urllib.request import urlretrieve

import cadetrdm
Expand All @@ -34,7 +34,6 @@ def validate_is_output_repo(path_to_repo):
if rdm_data["is_project_repo"]:
raise ValueError("Please use the URL to the output repository.")


class BaseRepo:
def __init__(self, repository_path=None, search_parent_directories=True, *args, **kwargs):
"""
Expand Down Expand Up @@ -185,6 +184,21 @@ def update(self):
traceback.print_exc()
print(f"Git command error in {self.path}: {e}")

@classmethod
def clone_from(cls, url, to_path, multi_options: Optional[List[str]] = None, **kwargs):
try:
git.Repo.clone_from(url, to_path, multi_options=multi_options, **kwargs)
except git.exc.GitCommandError as e:
if "Permission denied (publickey)" in e:
try:
git.Repo.clone_from(ssh_url_to_http_url(url), to_path, multi_options=multi_options, **kwargs)
except Exception as e_inner:
raise e_inner
else:
raise e
instance = cls(to_path)
return instance

def add_remote(self, remote_url, remote_name=None):
"""
Add a remote to the repository.
Expand Down Expand Up @@ -261,9 +275,10 @@ def import_remote_repo(self, source_repo_location, source_repo_branch, target_re

print(f"Cloning from {source_repo_location} into {target_repo_location}")
multi_options = ["--filter=blob:none", "--branch", source_repo_branch, "--single-branch"]
repo = git.Repo.clone_from(source_repo_location, target_repo_location, multi_options=multi_options)
repo.git.clear_cache()
repo.close()
repo = self.clone_from(url=source_repo_location, to_path=target_repo_location,
multi_options=multi_options)
repo._git.clear_cache()
repo._git_repo.close()

self.update_cadet_rdm_cache_json(source_repo_branch=source_repo_branch,
target_repo_location=target_repo_location,
Expand Down Expand Up @@ -750,7 +765,7 @@ def _clone_output_repo(self, multi_options: List[str] = None):
for output_remote in ssh_remotes + http_remotes:
try:
print(f"Attempting to clone {output_remote} into {output_path}")
git.Repo.clone_from(output_remote, output_path, multi_options=multi_options)
_ = self.clone_from(output_remote, output_path, multi_options=multi_options)
except Exception as e:
print(e)
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_git_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ def try_initialize_from_remote():
"test_repo_from_remote")
assert try_init_gitpython_repo("test_repo_from_remote")

repo = ProjectRepo("test_repo_from_remote")
assert hasattr(repo, "")


def test_init_over_existing_repo(monkeypatch):
path_to_repo = Path("test_repo_2")
Expand Down
9 changes: 5 additions & 4 deletions tests/test_gitlab_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from cadetrdm import initialize_repo, ProjectRepo
from cadetrdm.io_utils import delete_path
from cadetrdm.remote_integration import GitHubRemote, GitLabRemote
from cadetrdm.repositories import BaseRepo


@pytest.mark.server_api
Expand All @@ -26,15 +27,15 @@ def test_gitlab_create():

response = remote.create_remote(url=url, namespace=namespace, name=name, username="r.jaepel")

git.Repo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
BaseRepo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
delete_path("test_repo_remote")

remote.delete_remote(url=url, namespace=namespace, name=name, username="r.jaepel")

sleep(3)

with pytest.raises(git.exc.GitCommandError):
git.Repo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
BaseRepo.clone_from(response.ssh_url_to_repo, "test_repo_remote")


@pytest.mark.server_api
Expand All @@ -60,13 +61,13 @@ def test_github_create():

sleep(3)

git.Repo.clone_from(response.html_url, "test_repo_remote")
BaseRepo.clone_from(response.html_url, "test_repo_remote")
delete_path("test_repo_remote")

remote.delete_remote(namespace=namespace, name=name, username="r.jaepel")

with pytest.raises(git.exc.GitCommandError):
git.Repo.clone_from(response.ssh_url, "test_repo_remote")
BaseRepo.clone_from(response.ssh_url, "test_repo_remote")


@pytest.mark.server_api
Expand Down

0 comments on commit df706f9

Please sign in to comment.