Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional parameters fed to git fetch #291

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions gitman/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,22 @@ def is_sha(rev):
return re.match("^[0-9a-f]{7,40}$", rev) is not None


def fetch(type, repo, path, rev=None): # pylint: disable=unused-argument
def fetch(
type, repo, path, rev=None, fetch_params=None
): # pylint: disable=unused-argument
"""Fetch the latest changes from the remote repository."""

if fetch_params is None:
fetch_params = []

if type == "git-svn":
# deep clone happens in update function
return

assert type == "git"

git("remote", "set-url", "origin", repo)
args = ["fetch", "--tags", "--force", "--prune", "origin"]
args = ["fetch", "--tags", "--force", "--prune", *fetch_params, "origin"]
if rev:
if is_sha(rev):
pass # fetch only works with a SHA if already present locally
Expand Down
20 changes: 17 additions & 3 deletions gitman/models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Source:

type: str = "git"
params: Optional[str] = None
fetch_params: Optional[str] = None
sparse_paths: List[str] = field(default_factory=list)
links: List[Link] = field(default_factory=list)

Expand Down Expand Up @@ -101,10 +102,17 @@ def __lt__(self, other):
return self.name < other.name

def clone_params_if_any(self):
self.clone_params_var_if_any(self.params)

def clone_fetch_params_if_any(self):
self.clone_params_var_if_any(self.fetch_params)

@staticmethod
def clone_params_var_if_any(params):
# sanitize params strings by splitting on spaces
if self.params:
if params:
params_list = []
for param in self.params.split(" "):
for param in params.split(" "):
if len(param) > 0:
params_list.append(param)
return params_list
Expand Down Expand Up @@ -187,7 +195,13 @@ def update_files(

# Fetch the desired revision
if fetch or git.is_fetch_required(self.type, self.rev):
git.fetch(self.type, self.repo, self.name, rev=self.rev)
git.fetch(
self.type,
self.repo,
self.name,
rev=self.rev,
fetch_params=self.clone_fetch_params_if_any(),
)

# Update the working tree to the desired revision
git.update(
Expand Down
11 changes: 11 additions & 0 deletions gitman/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def test_fetch(self, mock_call):
],
)

def test_fetch_params(self, mock_call):
"""Verify the commands to fetch from a Git repository w/ params."""
git.fetch("git", "mock.git", "mock/path", fetch_params=["--depth", "1"])
check_calls(
mock_call,
[
"git remote set-url origin mock.git",
"git fetch --tags --force --prune --depth 1 origin",
],
)

def test_fetch_rev(self, mock_call):
"""Verify the commands to fetch from a Git repository w/ rev."""
git.fetch("git", "mock.git", "mock/path", "mock-rev")
Expand Down
8 changes: 6 additions & 2 deletions gitman/tests/test_models_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def test_update_files(
"git", "repo", "name", rev="rev", sparse_paths=[], user_params=None
)
mock_is_fetch_required.assert_called_once_with("git", "rev")
mock_fetch.assert_called_once_with("git", "repo", "name", rev="rev")
mock_fetch.assert_called_once_with(
"git", "repo", "name", rev="rev", fetch_params=None
)
mock_update.assert_called_once_with(
"git", "repo", "name", clean=True, fetch=False, rev="rev"
)
Expand Down Expand Up @@ -125,7 +127,9 @@ def test_update_files_rebuild_git(
mock_clone.assert_not_called()
mock_rebuild.assert_called_once_with("git", "repo")
mock_is_fetch_required.assert_not_called()
mock_fetch.assert_called_once_with("git", "repo", "name", rev="rev")
mock_fetch.assert_called_once_with(
"git", "repo", "name", rev="rev", fetch_params=None
)
mock_update.assert_called_once_with(
"git", "repo", "name", clean=True, fetch=True, rev="rev"
)
Expand Down
Loading