Skip to content

Commit fb6e0b4

Browse files
authored
dulwich: remove support for adding files from submodules (#431)
1 parent a83279e commit fb6e0b4

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/scmrepo/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ def close(self):
103103

104104
def _reset(self) -> None:
105105
pass
106+
107+
def list_submodules(self) -> list[str]:
108+
"""Returns a list of submodules in the repo."""
109+
return []

src/scmrepo/git/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ def fetch_refspecs(
412412
check_attr = partialmethod(_backend_func, "check_attr")
413413

414414
get_tree_obj = partialmethod(_backend_func, "get_tree_obj")
415+
list_submodules = partialmethod(_backend_func, "list_submodules")
415416

416417
def branch_revs(self, branch: str, end_rev: Optional[str] = None) -> Iterable[str]:
417418
"""Iterate over revisions in a given branch (from newest to oldest).

src/scmrepo/git/backend/dulwich/__init__.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def __init__( # pylint:disable=W0231
199199
self._submodules: dict[str, str] = self._find_submodules()
200200
self._stashes: dict = {}
201201

202+
def list_submodules(self) -> list[str]:
203+
raise NotImplementedError
204+
202205
def _find_submodules(self) -> dict[str, str]:
203206
"""Return dict mapping submodule names to submodule paths.
204207
@@ -349,21 +352,6 @@ def add(
349352

350353
def _expand_paths(self, paths: list[str], force: bool = False) -> Iterator[str]:
351354
for path in paths:
352-
if not os.path.isabs(path) and self._submodules:
353-
# NOTE: If path is inside a submodule, Dulwich expects the
354-
# staged paths to be relative to the submodule root (not the
355-
# parent git repo root). We append path to root_dir here so
356-
# that the result of relpath(path, root_dir) is actually the
357-
# path relative to the submodule root.
358-
fs_path = relpath(path, self.root_dir)
359-
for sm_path in self._submodules.values():
360-
assert self.root_dir
361-
if fs_path.startswith(sm_path):
362-
path = os.path.join(
363-
self.root_dir,
364-
relpath(fs_path, sm_path),
365-
)
366-
break
367355
if os.path.isdir(path):
368356
for root, _, fs in os.walk(path):
369357
for fpath in fs:

src/scmrepo/git/backend/gitpython.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,6 @@ def check_attr(
789789
if info == "unset":
790790
return False
791791
return info
792+
793+
def list_submodules(self) -> list[str]:
794+
raise NotImplementedError

src/scmrepo/git/backend/pygit2/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ def release_odb_handles(self):
269269
# can be reacquired later as needed.
270270
self.repo.free()
271271

272+
def list_submodules(self) -> list[str]:
273+
return self.repo.listall_submodules()
274+
272275
@classmethod
273276
def clone(
274277
cls,
@@ -865,6 +868,11 @@ def reset(self, hard: bool = False, paths: Optional[Iterable[str]] = None):
865868
from pygit2 import IndexEntry
866869
from pygit2.enums import ResetMode
867870

871+
if self.list_submodules():
872+
raise NotImplementedError(
873+
"pygit2 seems to remove files from submodules on reset"
874+
)
875+
868876
self.repo.index.read(False) # type: ignore[attr-defined]
869877
if paths is not None:
870878
tree = self.repo.revparse_single("HEAD").tree # type: ignore[attr-defined]

0 commit comments

Comments
 (0)