Skip to content

Commit e24e264

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

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
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: 24 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,24 @@ 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+
# prevent git terminal prompts from interrupting the process.
190+
os.environ["GIT_TERMINAL_PROMPT"] = "0"
191+
192+
try:
193+
git.Repo.clone_from(url, to_path, multi_options=multi_options, **kwargs)
194+
except git.exc.GitCommandError as e:
195+
if "Host key verification failed." in e.stderr:
196+
try:
197+
git.Repo.clone_from(ssh_url_to_http_url(url), to_path, multi_options=multi_options, **kwargs)
198+
except Exception as e_inner:
199+
raise e_inner
200+
else:
201+
raise e
202+
instance = cls(to_path)
203+
return instance
204+
188205
def add_remote(self, remote_url, remote_name=None):
189206
"""
190207
Add a remote to the repository.
@@ -261,9 +278,10 @@ def import_remote_repo(self, source_repo_location, source_repo_branch, target_re
261278

262279
print(f"Cloning from {source_repo_location} into {target_repo_location}")
263280
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()
281+
repo = self.clone_from(url=source_repo_location, to_path=target_repo_location,
282+
multi_options=multi_options)
283+
repo._git.clear_cache()
284+
repo._git_repo.close()
267285

268286
self.update_cadet_rdm_cache_json(source_repo_branch=source_repo_branch,
269287
target_repo_location=target_repo_location,
@@ -750,7 +768,7 @@ def _clone_output_repo(self, multi_options: List[str] = None):
750768
for output_remote in ssh_remotes + http_remotes:
751769
try:
752770
print(f"Attempting to clone {output_remote} into {output_path}")
753-
git.Repo.clone_from(output_remote, output_path, multi_options=multi_options)
771+
_ = self.clone_from(output_remote, output_path, multi_options=multi_options)
754772
except Exception as e:
755773
print(e)
756774
else:

tests/test_git_adapter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,13 @@ def try_add_remote(path_to_repo):
155155
def try_initialize_from_remote():
156156
if Path("test_repo_from_remote").exists():
157157
delete_path("test_repo_from_remote")
158-
clone("https://jugit.fz-juelich.de/IBG-1/ModSim/cadet/rdm-examples-fraunhofer-ime-aachen",
158+
clone("[email protected]:ronald-jaepel/rdm_testing_template.git",
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, "output_path")
164+
162165

163166
def test_init_over_existing_repo(monkeypatch):
164167
path_to_repo = Path("test_repo_2")
@@ -275,7 +278,7 @@ def test_cadet_rdm(path_to_repo):
275278
# because these depend on one-another and there is no native support afaik for sequential tests
276279
# these tests are called sequentially here as try_ functions.
277280
try_initialize_git_repo(path_to_repo)
278-
# try_initialize_from_remote()
281+
try_initialize_from_remote()
279282

280283
try_add_remote(path_to_repo)
281284
# try_add_submodule(path_to_repo)

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)