Skip to content

Commit cbadbe9

Browse files
committed
Using already cloned repos as a reference, if the reference is not provided by the user
1 parent 9135c3f commit cbadbe9

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

arca/_arca.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ def pull_again(self, repo: Optional[str]=None, branch: Optional[str]=None) -> No
249249
except KeyError:
250250
pass
251251

252+
def get_reference_repository(self, reference: Optional[Path], repo: str) -> Optional[Path]:
253+
"""
254+
Returns a repository to use in clone command, if there is one to be referenced.
255+
Either provided by the user of generated from already cloned branches (master is preferred).
256+
257+
:param reference: Path to a local repository provided by the user or None.
258+
:param repo: Reference for which remote repository.
259+
"""
260+
if reference is not None:
261+
return reference.absolute()
262+
263+
repo_path = self.get_path_to_repo(repo)
264+
265+
if not repo_path.exists():
266+
return None
267+
268+
master = repo_path / "master"
269+
270+
if master.exists() and master.is_dir():
271+
return master
272+
273+
for existing_branch in repo_path.iterdir():
274+
if not existing_branch.is_dir():
275+
continue
276+
277+
return existing_branch.resolve()
278+
279+
return None
280+
252281
def _pull(self, *, repo_path: Path=None, git_repo: Repo=None, repo: str=None, branch: str=None,
253282
depth: Optional[int]=None,
254283
reference: Optional[Path]=None
@@ -271,8 +300,10 @@ def _pull(self, *, repo_path: Path=None, git_repo: Repo=None, repo: str=None, br
271300
if depth is not None:
272301
kwargs["depth"] = depth
273302

303+
reference = self.get_reference_repository(reference, repo)
304+
274305
if reference is not None:
275-
kwargs["reference-if-able"] = str(reference.absolute())
306+
kwargs["reference-if-able"] = str(reference)
276307
kwargs["dissociate"] = True
277308

278309
try:

tests/test_arca_class.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,30 @@ def test_reference_validate(temp_repo_static, reference, valid):
307307
arca.static_filename(temp_repo_static.url, temp_repo_static.branch, relative_path, reference=reference)
308308

309309

310+
def test_get_reference_repository(temp_repo_static):
311+
"""
312+
Test that the :meth:`Arca.get_reference_repository` works when reference is not provided by the user
313+
or when branch `master` is not pulled first (as it is in other tests).
314+
"""
315+
temp_repo_static.file_path.write_text("master")
316+
temp_repo_static.repo.index.add([str(temp_repo_static.file_path)])
317+
temp_repo_static.repo.index.commit("Initial")
318+
319+
for branch in "branch1", "branch2", "branch3":
320+
temp_repo_static.repo.create_head(branch)
321+
temp_repo_static.repo.branches[branch].checkout()
322+
temp_repo_static.file_path.write_text(branch)
323+
temp_repo_static.repo.index.add([str(temp_repo_static.file_path)])
324+
temp_repo_static.repo.index.commit(branch)
325+
326+
arca = Arca(base_dir=BASE_DIR)
327+
328+
for branch in "branch1", "branch2", "master", "branch3":
329+
_, path = arca.get_files(temp_repo_static.url, branch)
330+
331+
assert (path / temp_repo_static.file_path.name).read_text() == branch
332+
333+
310334
def test_pull_error():
311335
arca = Arca(base_dir=BASE_DIR)
312336

0 commit comments

Comments
 (0)