From 58bc8c70ce96cd943424f06655ba052dac62b2c5 Mon Sep 17 00:00:00 2001 From: Stelios Sfakianakis Date: Fri, 19 Apr 2024 15:23:36 +0300 Subject: [PATCH] Allow cloning with custom git depth (#40) --- README.md | 12 +++++++++++- prefect_gitlab/repositories.py | 9 ++++++++- tests/test_repositories.py | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c65fbe7..05fcfbc 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,23 @@ public_gitlab_block.save() # specific branch or tag of a GitLab repository branch_gitlab_block = GitLabRepository( name="my-gitlab-block", - reference="branch-or-tag-name" + reference="branch-or-tag-name", repository="https://gitlab.com/testing/my-repository.git" ) branch_gitlab_block.save() +# Get all history of a specific branch or tag of a GitLab repository +branch_gitlab_block = GitLabRepository( + name="my-gitlab-block", + reference="branch-or-tag-name", + git_depth=None, + repository="https://gitlab.com/testing/my-repository.git" +) + +branch_gitlab_block.save() + # private GitLab repository private_gitlab_block = GitLabRepository( name="my-private-gitlab-block", diff --git a/prefect_gitlab/repositories.py b/prefect_gitlab/repositories.py index cb6673a..02836dc 100644 --- a/prefect_gitlab/repositories.py +++ b/prefect_gitlab/repositories.py @@ -94,6 +94,12 @@ class GitLabRepository(ReadableDeploymentStorage): default=None, description="An optional reference to pin to; can be a branch name or tag.", ) + git_depth: Optional[int] = Field( + default=1, + gte=1, + description="The number of commits that Git history is truncated to " + "during cloning. Set to None to fetch the entire history.", + ) credentials: Optional[GitLabCredentials] = Field( default=None, description="An optional GitLab Credentials block for authenticating with " @@ -188,7 +194,8 @@ async def get_directory( cmd += ["-b", self.reference] # Limit git history - cmd += ["--depth", "1"] + if self.git_depth is not None: + cmd += ["--depth", f"{self.git_depth}"] # Clone to a temporary directory and move the subdirectory over with TemporaryDirectory(suffix="prefect") as tmp_dir: diff --git a/tests/test_repositories.py b/tests/test_repositories.py index 7ed921d..cb43185 100644 --- a/tests/test_repositories.py +++ b/tests/test_repositories.py @@ -146,6 +146,29 @@ class p: ] assert mock.await_args[0][0][: len(expected_cmd)] == expected_cmd + async def test_cloning_with_custom_depth(self, monkeypatch): + """Ensure that we can retrieve the whole history, i.e. support true git clone""" # noqa: E501 + + class p: + returncode = 0 + + mock = AsyncMock(return_value=p()) + monkeypatch.setattr(prefect_gitlab.repositories, "run_process", mock) + repo = "git@gitlab.com:PrefectHQ/prefect.git" + depth = None + g = GitLabRepository( + repository=repo, + git_depth=depth, + ) + await g.get_directory() + assert mock.await_count == 1 + expected_cmd = [ + "git", + "clone", + repo, + ] + assert mock.await_args[0][0][: len(expected_cmd)] == expected_cmd + async def test_ssh_fails_with_credential(self, monkeypatch): """Ensure that credentials cannot be passed in if the URL is not in the HTTPS/HTTP format.