From 24f211ae6756dfe7a5f635d76d9520134f2d55bc Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Wed, 5 Mar 2025 14:18:01 +1100 Subject: [PATCH] repo: pass default_branch to SCM if set (bug 1951843) --- src/lando/main/models/repo.py | 6 +++++- src/lando/main/scm/abstract_scm.py | 2 +- src/lando/main/scm/git.py | 2 +- src/lando/main/scm/hg.py | 2 +- src/lando/main/tests/test_models.py | 11 +++++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/lando/main/models/repo.py b/src/lando/main/models/repo.py index 47e8aa10..9a5fab97 100644 --- a/src/lando/main/models/repo.py +++ b/src/lando/main/models/repo.py @@ -154,7 +154,11 @@ def scm(self) -> AbstractSCM: """Return the SCM implementation associated with this Repository""" if not self._scm: if impl := SCM_IMPLEMENTATIONS.get(self.scm_type): - self._scm = impl(self.path) + kwargs = {} + if self.default_branch: + kwargs["default_branch"] = self.default_branch + + self._scm = impl(self.path, **kwargs) else: raise Exception(f"Repository type not supported: {self.scm_type}") return self._scm diff --git a/src/lando/main/scm/abstract_scm.py b/src/lando/main/scm/abstract_scm.py index aec866ba..f442c623 100644 --- a/src/lando/main/scm/abstract_scm.py +++ b/src/lando/main/scm/abstract_scm.py @@ -12,7 +12,7 @@ class AbstractSCM: # The path to the repository. path: str - def __init__(self, path: str): + def __init__(self, path: str, **kwargs): self.path = path def __str__(self): diff --git a/src/lando/main/scm/git.py b/src/lando/main/scm/git.py index b87b2c03..d4c19524 100644 --- a/src/lando/main/scm/git.py +++ b/src/lando/main/scm/git.py @@ -59,7 +59,7 @@ class GitSCM(AbstractSCM): default_branch: str - def __init__(self, path: str, default_branch: str = "main"): + def __init__(self, path: str, default_branch: str = "main", **kwargs): self.default_branch = default_branch super().__init__(path) diff --git a/src/lando/main/scm/hg.py b/src/lando/main/scm/hg.py index 7abeb9d5..91ce71f0 100644 --- a/src/lando/main/scm/hg.py +++ b/src/lando/main/scm/hg.py @@ -162,7 +162,7 @@ class HgSCM(AbstractSCM): hg_repo: hglib.client.hgclient - def __init__(self, path: str, config: Optional[dict] = None): + def __init__(self, path: str, config: Optional[dict] = None, **kwargs): self.config = copy.copy(self.DEFAULT_CONFIGS) # Somewhere to store patch headers for testing. diff --git a/src/lando/main/tests/test_models.py b/src/lando/main/tests/test_models.py index 81a67035..b40c4728 100644 --- a/src/lando/main/tests/test_models.py +++ b/src/lando/main/tests/test_models.py @@ -156,3 +156,14 @@ def test__models__Revision__metadata(): assert r.commit_message == commit_message assert r.timestamp == timestamp assert r.diff == DIFF_ONLY + + +@pytest.mark.parametrize( + "branch,expected_branch", [(None, "main"), ("non-default", "non-default")] +) +def test_repo_default_branch_to_scm(branch: str, expected_branch: str): + repo_path = "some_repo" + repo = Repo(pull_path=repo_path, scm_type=SCM_TYPE_GIT, default_branch=branch) + + # repo.scm here is a GitSCM + assert repo.scm.default_branch == expected_branch