Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Allow cloning with custom git depth (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsfak authored Apr 19, 2024
1 parent 15d5c35 commit 58bc8c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion prefect_gitlab/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]: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.
Expand Down

0 comments on commit 58bc8c7

Please sign in to comment.