Skip to content

Commit 914a4ae

Browse files
codexByron
authored andcommitted
Keep submodule checkouts away from the worktree root
Preserve the submodule-specific root-path rejection after resolving checkout paths for symlink containment. Add a regression proving update rejects a checkout path at the parent repository root. Validation: .venv/bin/python -m pytest test/test_submodule.py -q; pre-commit run --files git/objects/submodule/base.py test/test_submodule.py; mypy git/objects/submodule/base.py; git diff --check.
1 parent 0709171 commit 914a4ae

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

git/objects/submodule/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def abspath(self) -> PathLike:
419419
path = super().abspath
420420
root = self.repo.working_tree_dir
421421
assert root is not None
422-
_to_relative_path(osp.realpath(root), osp.realpath(path))
422+
if _to_relative_path(osp.realpath(root), osp.realpath(path)) == ".":
423+
raise ValueError("Submodule checkout path must not be the repository root")
423424
return path
424425

425426
@classmethod

test/test_submodule.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,14 @@ def test_update_rejects_checkout_path_through_outside_symlink(self, rwdir):
14001400
with pytest.raises(ValueError, match="is not in repository"):
14011401
submodule.update(init=True)
14021402

1403+
@with_rw_directory
1404+
def test_update_rejects_checkout_path_at_parent_root(self, rwdir):
1405+
parent = git.Repo.init(osp.join(rwdir, "parent"))
1406+
submodule = Submodule(parent, Submodule.NULL_BIN_SHA, name="module", path=".", url="unused")
1407+
1408+
with pytest.raises(ValueError, match="must not be the repository root"):
1409+
submodule.update(init=True)
1410+
14031411
@skipUnless(sys.platform == "win32", "Specifically for Windows.")
14041412
@with_rw_directory
14051413
def test_to_relative_path_windows_path_kinds(self, rwdir):

0 commit comments

Comments
 (0)