Skip to content

dulwich: do not add files from submodules #431

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

Merged
merged 2 commits into from
Aug 6, 2025
Merged
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
4 changes: 4 additions & 0 deletions src/scmrepo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ def close(self):

def _reset(self) -> None:
pass

def list_submodules(self) -> list[str]:
"""Returns a list of submodules in the repo."""
return []
1 change: 1 addition & 0 deletions src/scmrepo/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def fetch_refspecs(
check_attr = partialmethod(_backend_func, "check_attr")

get_tree_obj = partialmethod(_backend_func, "get_tree_obj")
list_submodules = partialmethod(_backend_func, "list_submodules")

def branch_revs(self, branch: str, end_rev: Optional[str] = None) -> Iterable[str]:
"""Iterate over revisions in a given branch (from newest to oldest).
Expand Down
18 changes: 3 additions & 15 deletions src/scmrepo/git/backend/dulwich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ def __init__( # pylint:disable=W0231
self._submodules: dict[str, str] = self._find_submodules()
self._stashes: dict = {}

def list_submodules(self) -> list[str]:
raise NotImplementedError

def _find_submodules(self) -> dict[str, str]:
"""Return dict mapping submodule names to submodule paths.

Expand Down Expand Up @@ -349,21 +352,6 @@ def add(

def _expand_paths(self, paths: list[str], force: bool = False) -> Iterator[str]:
for path in paths:
if not os.path.isabs(path) and self._submodules:
# NOTE: If path is inside a submodule, Dulwich expects the
# staged paths to be relative to the submodule root (not the
# parent git repo root). We append path to root_dir here so
# that the result of relpath(path, root_dir) is actually the
# path relative to the submodule root.
fs_path = relpath(path, self.root_dir)
for sm_path in self._submodules.values():
assert self.root_dir
if fs_path.startswith(sm_path):
path = os.path.join(
self.root_dir,
relpath(fs_path, sm_path),
)
break
if os.path.isdir(path):
for root, _, fs in os.walk(path):
for fpath in fs:
Expand Down
3 changes: 3 additions & 0 deletions src/scmrepo/git/backend/gitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,6 @@ def check_attr(
if info == "unset":
return False
return info

def list_submodules(self) -> list[str]:
raise NotImplementedError
8 changes: 8 additions & 0 deletions src/scmrepo/git/backend/pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ def release_odb_handles(self):
# can be reacquired later as needed.
self.repo.free()

def list_submodules(self) -> list[str]:
return self.repo.listall_submodules()

@classmethod
def clone(
cls,
Expand Down Expand Up @@ -865,6 +868,11 @@ def reset(self, hard: bool = False, paths: Optional[Iterable[str]] = None):
from pygit2 import IndexEntry
from pygit2.enums import ResetMode

if self.list_submodules():
raise NotImplementedError(
"pygit2 seems to remove files from submodules on reset"
)

self.repo.index.read(False) # type: ignore[attr-defined]
if paths is not None:
tree = self.repo.revparse_single("HEAD").tree # type: ignore[attr-defined]
Expand Down
Loading