Skip to content

Commit df706f9

Browse files
committed
Add fallback to pull that also tries the HTTP URL
1 parent c613200 commit df706f9

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

cadetrdm/initialize_repo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,8 @@ def clone(project_url, path_to_repo: str = None, multi_options: List[str] = None
242242
path_to_repo = project_url.split("/")[-1]
243243
path_to_repo = path_to_repo.replace(".git", "")
244244
print(f"Cloning {project_url} into {path_to_repo}")
245-
git.Repo.clone_from(project_url, path_to_repo, multi_options=multi_options)
246-
247245
# During class instantiation, the output repo is cloned.
248-
repo = ProjectRepo(path_to_repo)
246+
repo = ProjectRepo.clone_from(to_path=path_to_repo, url=project_url, multi_options=multi_options)
249247

250248
repo.fill_data_from_cadet_rdm_json()
251249

cadetrdm/repositories.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from datetime import datetime
1111
from pathlib import Path
1212
from stat import S_IREAD, S_IWRITE
13-
from typing import List
13+
from typing import List, Optional
1414
from urllib.request import urlretrieve
1515

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

37-
3837
class BaseRepo:
3938
def __init__(self, repository_path=None, search_parent_directories=True, *args, **kwargs):
4039
"""
@@ -185,6 +184,21 @@ def update(self):
185184
traceback.print_exc()
186185
print(f"Git command error in {self.path}: {e}")
187186

187+
@classmethod
188+
def clone_from(cls, url, to_path, multi_options: Optional[List[str]] = None, **kwargs):
189+
try:
190+
git.Repo.clone_from(url, to_path, multi_options=multi_options, **kwargs)
191+
except git.exc.GitCommandError as e:
192+
if "Permission denied (publickey)" in e:
193+
try:
194+
git.Repo.clone_from(ssh_url_to_http_url(url), to_path, multi_options=multi_options, **kwargs)
195+
except Exception as e_inner:
196+
raise e_inner
197+
else:
198+
raise e
199+
instance = cls(to_path)
200+
return instance
201+
188202
def add_remote(self, remote_url, remote_name=None):
189203
"""
190204
Add a remote to the repository.
@@ -261,9 +275,10 @@ def import_remote_repo(self, source_repo_location, source_repo_branch, target_re
261275

262276
print(f"Cloning from {source_repo_location} into {target_repo_location}")
263277
multi_options = ["--filter=blob:none", "--branch", source_repo_branch, "--single-branch"]
264-
repo = git.Repo.clone_from(source_repo_location, target_repo_location, multi_options=multi_options)
265-
repo.git.clear_cache()
266-
repo.close()
278+
repo = self.clone_from(url=source_repo_location, to_path=target_repo_location,
279+
multi_options=multi_options)
280+
repo._git.clear_cache()
281+
repo._git_repo.close()
267282

268283
self.update_cadet_rdm_cache_json(source_repo_branch=source_repo_branch,
269284
target_repo_location=target_repo_location,
@@ -750,7 +765,7 @@ def _clone_output_repo(self, multi_options: List[str] = None):
750765
for output_remote in ssh_remotes + http_remotes:
751766
try:
752767
print(f"Attempting to clone {output_remote} into {output_path}")
753-
git.Repo.clone_from(output_remote, output_path, multi_options=multi_options)
768+
_ = self.clone_from(output_remote, output_path, multi_options=multi_options)
754769
except Exception as e:
755770
print(e)
756771
else:

tests/test_git_adapter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ def try_initialize_from_remote():
159159
"test_repo_from_remote")
160160
assert try_init_gitpython_repo("test_repo_from_remote")
161161

162+
repo = ProjectRepo("test_repo_from_remote")
163+
assert hasattr(repo, "")
164+
162165

163166
def test_init_over_existing_repo(monkeypatch):
164167
path_to_repo = Path("test_repo_2")

tests/test_gitlab_api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from cadetrdm import initialize_repo, ProjectRepo
77
from cadetrdm.io_utils import delete_path
88
from cadetrdm.remote_integration import GitHubRemote, GitLabRemote
9+
from cadetrdm.repositories import BaseRepo
910

1011

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

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

29-
git.Repo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
30+
BaseRepo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
3031
delete_path("test_repo_remote")
3132

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

3435
sleep(3)
3536

3637
with pytest.raises(git.exc.GitCommandError):
37-
git.Repo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
38+
BaseRepo.clone_from(response.ssh_url_to_repo, "test_repo_remote")
3839

3940

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

6162
sleep(3)
6263

63-
git.Repo.clone_from(response.html_url, "test_repo_remote")
64+
BaseRepo.clone_from(response.html_url, "test_repo_remote")
6465
delete_path("test_repo_remote")
6566

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

6869
with pytest.raises(git.exc.GitCommandError):
69-
git.Repo.clone_from(response.ssh_url, "test_repo_remote")
70+
BaseRepo.clone_from(response.ssh_url, "test_repo_remote")
7071

7172

7273
@pytest.mark.server_api

0 commit comments

Comments
 (0)